Skip to content

Commit

Permalink
NewFeature: TableView columns can be sorted ascendingly or descendingly.
Browse files Browse the repository at this point in the history
  • Loading branch information
evrencoskun committed Nov 25, 2017
1 parent f13c7f7 commit bc7c614
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,24 @@ public int getItemCount() {
return m_jItemList.size();
}

public List<T> getItems() {
return m_jItemList;
}

public void setItems(List<T> p_jItemList) {
m_jItemList = new ArrayList<>(p_jItemList);

this.notifyDataSetChanged();
}

public void setItems(List<T> 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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
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);
}
}
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;
}
}
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());
}
}
}
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();
}
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

}

0 comments on commit bc7c614

Please sign in to comment.