diff --git a/tableview/src/main/java/com/evrencoskun/tableview/TableView.java b/tableview/src/main/java/com/evrencoskun/tableview/TableView.java index 1e8198bb..595b14d3 100644 --- a/tableview/src/main/java/com/evrencoskun/tableview/TableView.java +++ b/tableview/src/main/java/com/evrencoskun/tableview/TableView.java @@ -16,6 +16,7 @@ import com.evrencoskun.tableview.adapter.AbstractTableAdapter; import com.evrencoskun.tableview.adapter.recyclerview.CellRecyclerView; import com.evrencoskun.tableview.adapter.recyclerview.holder.AbstractViewHolder; +import com.evrencoskun.tableview.handler.ColumnSortHandler; import com.evrencoskun.tableview.handler.SelectionHandler; import com.evrencoskun.tableview.layoutmanager.CellLayoutManager; import com.evrencoskun.tableview.layoutmanager.ColumnHeaderLayoutManager; @@ -24,6 +25,7 @@ import com.evrencoskun.tableview.listener.itemclick.RowHeaderRecyclerViewItemClickListener; import com.evrencoskun.tableview.listener.scroll.HorizontalRecyclerViewListener; import com.evrencoskun.tableview.listener.scroll.VerticalRecyclerViewListener; +import com.evrencoskun.tableview.sort.SortOrder; /** * Created by evrencoskun on 11/06/2017. @@ -49,6 +51,7 @@ public class TableView extends FrameLayout implements ITableView { private CellLayoutManager m_iCellLayoutManager; private SelectionHandler m_iSelectionHandler; + private ColumnSortHandler m_iColumnSortHandler; private int m_nRowHeaderWidth; private int m_nColumnHeaderHeight; @@ -129,8 +132,9 @@ private void initialize() { addView(m_jRowHeaderRecyclerView); addView(m_jCellRecyclerView); - // Create Selection Handler + // Create Handlers m_iSelectionHandler = new SelectionHandler(this); + m_iColumnSortHandler = new ColumnSortHandler(this); initializeListeners(); } @@ -341,6 +345,10 @@ public void setTableViewListener(ITableViewListener p_jTableViewListener) { m_jRowHeaderRecyclerView.addOnItemTouchListener(m_jRowHeaderRecyclerViewItemClickListener); } + public void sortColumn(int p_nColumnPosition, SortOrder p_eSortOrder) { + m_iColumnSortHandler.sort(p_nColumnPosition, p_eSortOrder); + } + /** * Returns the index of the selected row, -1 if no row is selected. diff --git a/tableview/src/main/java/com/evrencoskun/tableview/adapter/recyclerview/AbstractRecyclerViewAdapter.java b/tableview/src/main/java/com/evrencoskun/tableview/adapter/recyclerview/AbstractRecyclerViewAdapter.java index 55b29c5f..ecb9ffdf 100644 --- a/tableview/src/main/java/com/evrencoskun/tableview/adapter/recyclerview/AbstractRecyclerViewAdapter.java +++ b/tableview/src/main/java/com/evrencoskun/tableview/adapter/recyclerview/AbstractRecyclerViewAdapter.java @@ -37,12 +37,24 @@ public int getItemCount() { return m_jItemList.size(); } + public List getItems() { + return m_jItemList; + } + public void setItems(List p_jItemList) { m_jItemList = new ArrayList<>(p_jItemList); this.notifyDataSetChanged(); } + public void setItems(List p_jItemList, boolean p_bNotifyDataSet) { + m_jItemList = new ArrayList<>(p_jItemList); + + if (p_bNotifyDataSet) { + this.notifyDataSetChanged(); + } + } + public T getItem(int position) { if (m_jItemList == null || m_jItemList.isEmpty() || position < 0 || position >= m_jItemList.size()) { diff --git a/tableview/src/main/java/com/evrencoskun/tableview/adapter/recyclerview/CellRecyclerViewAdapter.java b/tableview/src/main/java/com/evrencoskun/tableview/adapter/recyclerview/CellRecyclerViewAdapter.java index 23913bde..3df515d8 100644 --- a/tableview/src/main/java/com/evrencoskun/tableview/adapter/recyclerview/CellRecyclerViewAdapter.java +++ b/tableview/src/main/java/com/evrencoskun/tableview/adapter/recyclerview/CellRecyclerViewAdapter.java @@ -135,8 +135,8 @@ public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) { if (cellViewHolder != null) { // Control to ignore selection color if (!m_iTableAdapter.getTableView().IsIgnoreSelectionColors()) { - cellViewHolder.setBackgroundColor(m_iTableAdapter.getTableView().getSelectedColor - ()); + cellViewHolder.setBackgroundColor(m_iTableAdapter.getTableView() + .getSelectedColor()); } cellViewHolder.setSelected(true); diff --git a/tableview/src/main/java/com/evrencoskun/tableview/handler/ColumnSortHandler.java b/tableview/src/main/java/com/evrencoskun/tableview/handler/ColumnSortHandler.java new file mode 100644 index 00000000..0d09d130 --- /dev/null +++ b/tableview/src/main/java/com/evrencoskun/tableview/handler/ColumnSortHandler.java @@ -0,0 +1,64 @@ +package com.evrencoskun.tableview.handler; + +import android.support.v7.util.DiffUtil; + +import com.evrencoskun.tableview.ITableView; +import com.evrencoskun.tableview.adapter.recyclerview.CellRecyclerViewAdapter; +import com.evrencoskun.tableview.adapter.recyclerview.RowHeaderRecyclerViewAdapter; +import com.evrencoskun.tableview.sort.ColumnSortCallback; +import com.evrencoskun.tableview.sort.ColumnSortComparator; +import com.evrencoskun.tableview.sort.ITableViewComparator; +import com.evrencoskun.tableview.sort.SortOrder; + +import java.util.Collections; +import java.util.List; + +/** + * Created by evrencoskun on 24.11.2017. + */ + +public class ColumnSortHandler { + + private CellRecyclerViewAdapter m_iCellRecyclerViewAdapter; + private RowHeaderRecyclerViewAdapter m_iRowHeaderRecyclerViewAdapter; + + public ColumnSortHandler(ITableView p_iTableView) { + this.m_iCellRecyclerViewAdapter = (CellRecyclerViewAdapter) p_iTableView + .getCellRecyclerView().getAdapter(); + + this.m_iRowHeaderRecyclerViewAdapter = (RowHeaderRecyclerViewAdapter) p_iTableView + .getRowHeaderRecyclerView().getAdapter(); + } + + public void sort(int p_nXPosition, SortOrder p_nSortOrder) { + List> m_jOriginalList = m_iCellRecyclerViewAdapter.getItems(); + List> m_jSortedList = m_jOriginalList; + + + if (p_nSortOrder != SortOrder.UNSORTED) { + // Do descending / ascending sort + Collections.sort(m_jSortedList, new ColumnSortComparator(p_nXPosition, p_nSortOrder)); + } + + // Set sorted data list + swapItems(m_jSortedList, p_nXPosition); + } + + public void swapItems(List> m_jNewItems, int p_nXPosition) { + + // Find the differences between old cell items and new items. + final ColumnSortCallback diffCallback = new ColumnSortCallback( + (List>) m_iCellRecyclerViewAdapter.getItems(), + m_jNewItems, p_nXPosition); + + final DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallback); + + // Set new items without calling notifyCellDataSetChanged method of CellRecyclerViewAdapter + m_iCellRecyclerViewAdapter.setItems(m_jNewItems, false); + + + // Update both cellRecyclerViewAdapter and RowHeaderRecyclerViewAdapter + diffResult.dispatchUpdatesTo(m_iCellRecyclerViewAdapter); + diffResult.dispatchUpdatesTo(m_iRowHeaderRecyclerViewAdapter); + } +} diff --git a/tableview/src/main/java/com/evrencoskun/tableview/sort/ColumnSortCallback.java b/tableview/src/main/java/com/evrencoskun/tableview/sort/ColumnSortCallback.java new file mode 100644 index 00000000..ffef128f --- /dev/null +++ b/tableview/src/main/java/com/evrencoskun/tableview/sort/ColumnSortCallback.java @@ -0,0 +1,63 @@ +package com.evrencoskun.tableview.sort; + +import android.support.v7.util.DiffUtil; + +import java.util.List; + +/** + * Created by evrencoskun on 23.11.2017. + */ + +public class ColumnSortCallback extends DiffUtil.Callback { + + private List> m_jOldCellItems; + private List> m_jNewCellItems; + private int m_nColumnPosition; + + public ColumnSortCallback(List> p_jOldCellItems, + List> p_jNewCellItems, int + p_nColumnPosition) { + this.m_jOldCellItems = p_jOldCellItems; + this.m_jNewCellItems = p_jNewCellItems; + this.m_nColumnPosition = p_nColumnPosition; + } + + @Override + public int getOldListSize() { + return m_jOldCellItems.size(); + } + + @Override + public int getNewListSize() { + return m_jNewCellItems.size(); + } + + @Override + public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) { + // Control for precaution from IndexOutOfBoundsException + if (m_jOldCellItems.size() > oldItemPosition && m_jNewCellItems.size() > newItemPosition) { + if (m_jOldCellItems.get(oldItemPosition).size() > m_nColumnPosition && + m_jNewCellItems.get(newItemPosition).size() > m_nColumnPosition) { + // Compare ids + return m_jOldCellItems.get(oldItemPosition).get(m_nColumnPosition).getId().equals + (m_jNewCellItems.get(newItemPosition).get(m_nColumnPosition).getId()); + } + } + return false; + } + + @Override + public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) { + // Control for precaution from IndexOutOfBoundsException + if (m_jOldCellItems.size() > oldItemPosition && m_jNewCellItems.size() > newItemPosition) { + if (m_jOldCellItems.get(oldItemPosition).size() > m_nColumnPosition && + m_jNewCellItems.get(newItemPosition).size() > m_nColumnPosition) { + // Compare contents + return m_jOldCellItems.get(oldItemPosition).get(m_nColumnPosition).getContent() + .equals(m_jNewCellItems.get(newItemPosition).get(m_nColumnPosition) + .getContent()); + } + } + return false; + } +} diff --git a/tableview/src/main/java/com/evrencoskun/tableview/sort/ColumnSortComparator.java b/tableview/src/main/java/com/evrencoskun/tableview/sort/ColumnSortComparator.java new file mode 100644 index 00000000..a0edb56b --- /dev/null +++ b/tableview/src/main/java/com/evrencoskun/tableview/sort/ColumnSortComparator.java @@ -0,0 +1,28 @@ +package com.evrencoskun.tableview.sort; + +import java.util.Comparator; +import java.util.List; + +/** + * Created by evrencoskun on 25.11.2017. + */ + +public class ColumnSortComparator implements Comparator> { + private int m_nXPosition; + private SortOrder m_nSortOrder; + + public ColumnSortComparator(int p_nXPosition, SortOrder p_nSortOrder) { + this.m_nXPosition = p_nXPosition; + this.m_nSortOrder = p_nSortOrder; + } + + @Override + public int compare(List t1, List t2) { + if (m_nSortOrder == SortOrder.DESCENDING) { + return t2.get(m_nXPosition).getContent().compareTo(t1.get(m_nXPosition).getContent()); + } else { + // Default sorting process is ASCENDING + return t1.get(m_nXPosition).getContent().compareTo(t2.get(m_nXPosition).getContent()); + } + } +} \ No newline at end of file diff --git a/tableview/src/main/java/com/evrencoskun/tableview/sort/ITableViewComparator.java b/tableview/src/main/java/com/evrencoskun/tableview/sort/ITableViewComparator.java new file mode 100644 index 00000000..a0b06a6f --- /dev/null +++ b/tableview/src/main/java/com/evrencoskun/tableview/sort/ITableViewComparator.java @@ -0,0 +1,14 @@ +package com.evrencoskun.tableview.sort; + +import java.util.Comparator; + +/** + * Created by evrencoskun on 24.11.2017. + */ + +public interface ITableViewComparator extends Comparator { + + String getId(); + + String getContent(); +} diff --git a/tableview/src/main/java/com/evrencoskun/tableview/sort/SortOrder.java b/tableview/src/main/java/com/evrencoskun/tableview/sort/SortOrder.java new file mode 100644 index 00000000..7daa0d37 --- /dev/null +++ b/tableview/src/main/java/com/evrencoskun/tableview/sort/SortOrder.java @@ -0,0 +1,30 @@ +package com.evrencoskun.tableview.sort; + +/** + * Created by evrencoskun on 25.11.2017. + */ + +public enum SortOrder { + + /** + * Enumeration value indicating the items are sorted in increasing order. + * For example, the set 1, 4, 0 sorted in + * ASCENDING order is 0, 1, 4. + */ + ASCENDING, + + /** + * Enumeration value indicating the items are sorted in decreasing order. + * For example, the set 1, 4, 0 sorted in + * DESCENDING order is 4, 1, 0. + */ + DESCENDING, + + /** + * Enumeration value indicating the items are unordered. + * For example, the set 1, 4, 0 in + * UNSORTED order is 1, 4, 0. + */ + UNSORTED + +}