Skip to content

Commit

Permalink
Resolved #362 - Improved filter engine
Browse files Browse the repository at this point in the history
  • Loading branch information
davideas committed May 11, 2017
1 parent 6f0cf2b commit d7c945a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,11 @@ private void cancelExistingAnimation(final int hashCode) {
}

/**
* Checks if at the provided position, the item is a Header or Footer
* Checks if at the provided position, the item is a Header or Footer.
*
* @param position the position to check
* @return true if is a scrollable item
* @return true if it's a scrollable item
* @since 5.0.0-rc1
*/
public abstract boolean isScrollableHeaderOrFooter(int position);

Expand All @@ -353,6 +354,7 @@ private void cancelExistingAnimation(final int hashCode) {
*
* @param holder the ViewHolder just bound
* @param position the current item position
* @since 5.0.0-b1
*/
@SuppressWarnings("ConstantConditions")
protected final void animateView(final RecyclerView.ViewHolder holder, final int position) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,17 @@ public final List<T> getScrollableFooters() {
@Override
public final boolean isScrollableHeaderOrFooter(int position) {
T item = getItem(position);
return isScrollableHeaderOrFooter(item);
}

/**
* Checks if at the provided item is a Header or Footer.
*
* @param item the item to check
* @return true if it's a scrollable item
* @since 5.0.0-rc2
*/
public final boolean isScrollableHeaderOrFooter(T item) {
return item != null && mScrollableHeaders.contains(item) || mScrollableFooters.contains(item);
}

Expand Down Expand Up @@ -4069,6 +4080,7 @@ public void filterItems(@NonNull List<T> unfilteredItems, @IntRange(from = 0) lo
* <li>Any pending deleted items are always deleted before filter is performed.</li>
* <li>Expandable items are picked up and displayed if at least a child is collected by
* the current filter.</li>
* <li>Filter is skipped while endless loading.</li>
* <li>Items are animated thanks to {@link #animateTo(List, Payload)} BUT a limit of
* {@value ANIMATE_TO_LIMIT} (default) items is set. <b>Note:</b> Above this limit,
* {@link #notifyDataSetChanged()} will be called to improve performance. you can change
Expand Down Expand Up @@ -4145,13 +4157,15 @@ private boolean filterObject(T item, List<T> values) {
// Stop filter task if cancelled
if (mFilterAsyncTask != null && mFilterAsyncTask.isCancelled()) return false;
// Skip already filtered items (it happens when internal originalList)
if (values.contains(item)) return false;
if (mOriginalList != null && (isScrollableHeaderOrFooter(item) || values.contains(item))) {
return false;
}
// Start to compose the filteredItems to maintain the order of addition
// It will be discarded if no subItem will be filtered
List<T> filteredItems = new ArrayList<>();
filteredItems.add(item);
// Filter subItems
boolean filtered = filterSubObjects(item, filteredItems);
boolean filtered = filterExpandableObject(item, filteredItems);
// If no subItem was filtered, fallback to Normal filter
if (!filtered) {
filtered = filterObject(item, getSearchText());
Expand All @@ -4169,8 +4183,7 @@ private boolean filterObject(T item, List<T> values) {
return filtered;
}

private boolean filterSubObjects(T item, List<T> filteredItems) {
// Reset expansion flag
private boolean filterExpandableObject(T item, List<T> filteredItems) {
boolean filtered = false;
// Is item an expandable?
if (isExpandable(item)) {
Expand All @@ -4183,10 +4196,11 @@ private boolean filterSubObjects(T item, List<T> filteredItems) {
}
// SubItems scan filter
for (T subItem : getCurrentChildren(expandable)) {
// Recursive filter for subExpandable
if (subItem instanceof IExpandable && filterObject(subItem, filteredItems)) {
filtered = true;
} else {
// Use normal filter for subItems
// Use normal filter for normal subItem
subItem.setHidden(!filterObject(subItem, getSearchText()));
if (!subItem.isHidden()) {
filtered = true;
Expand Down Expand Up @@ -4224,6 +4238,7 @@ protected boolean filterObject(T item, String constraint) {

/**
* Clears flags after searchText is cleared out for Expandable items and sub items.
* Also restore headers visibility.
*/
private void resetFilterFlags(List<T> items) {
IHeader sameHeader = null;
Expand Down Expand Up @@ -5457,6 +5472,10 @@ private class FilterAsyncTask extends AsyncTask<Void, Void, Void> {

@Override
protected void onPreExecute() {
if (endlessLoading) {
Log.w(TAG, "Cannot filter while endlessLoading");
this.cancel(true);
}
// Note: In case some items are in pending deletion (Undo started),
// we commit the deletion before starting or resetting the filter.
if (isRestoreInTime() && mDeleteCompleteListener != null) {
Expand Down Expand Up @@ -5590,7 +5609,6 @@ protected void onPostFilter() {
* @since 5.0.0-rc1
*/
public class HandlerCallback implements Handler.Callback {

@CallSuper
@Override
public boolean handleMessage(Message message) {
Expand Down

0 comments on commit d7c945a

Please sign in to comment.