-
-
Notifications
You must be signed in to change notification settings - Fork 458
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NewFeature: TableView columns can be sorted ascendingly or descendingly.
- Loading branch information
1 parent
f13c7f7
commit bc7c614
Showing
8 changed files
with
222 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
tableview/src/main/java/com/evrencoskun/tableview/handler/ColumnSortHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<List<ITableViewComparator>> m_jOriginalList = m_iCellRecyclerViewAdapter.getItems(); | ||
List<List<ITableViewComparator>> 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<List<ITableViewComparator>> m_jNewItems, int p_nXPosition) { | ||
|
||
// Find the differences between old cell items and new items. | ||
final ColumnSortCallback diffCallback = new ColumnSortCallback( | ||
(List<List<ITableViewComparator>>) 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); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
tableview/src/main/java/com/evrencoskun/tableview/sort/ColumnSortCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<List<ITableViewComparator>> m_jOldCellItems; | ||
private List<List<ITableViewComparator>> m_jNewCellItems; | ||
private int m_nColumnPosition; | ||
|
||
public ColumnSortCallback(List<List<ITableViewComparator>> p_jOldCellItems, | ||
List<List<ITableViewComparator>> 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; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
tableview/src/main/java/com/evrencoskun/tableview/sort/ColumnSortComparator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<List<ITableViewComparator>> { | ||
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<ITableViewComparator> t1, List<ITableViewComparator> 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()); | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
tableview/src/main/java/com/evrencoskun/tableview/sort/ITableViewComparator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} |
30 changes: 30 additions & 0 deletions
30
tableview/src/main/java/com/evrencoskun/tableview/sort/SortOrder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <code>1, 4, 0</code> sorted in | ||
* <code>ASCENDING</code> order is <code>0, 1, 4</code>. | ||
*/ | ||
ASCENDING, | ||
|
||
/** | ||
* Enumeration value indicating the items are sorted in decreasing order. | ||
* For example, the set <code>1, 4, 0</code> sorted in | ||
* <code>DESCENDING</code> order is <code>4, 1, 0</code>. | ||
*/ | ||
DESCENDING, | ||
|
||
/** | ||
* Enumeration value indicating the items are unordered. | ||
* For example, the set <code>1, 4, 0</code> in | ||
* <code>UNSORTED</code> order is <code>1, 4, 0</code>. | ||
*/ | ||
UNSORTED | ||
|
||
} |