Skip to content

Commit

Permalink
Resolved #362 - Filter items with optional originalList
Browse files Browse the repository at this point in the history
  • Loading branch information
davideas committed May 10, 2017
1 parent 8641a5b commit 0164675
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -483,9 +483,13 @@ public boolean onQueryTextChange(String newText) {
if (mAdapter.hasNewSearchText(newText)) {
Log.d(TAG, "onQueryTextChange newText: " + newText);
mAdapter.setSearchText(newText);

// Fill and Filter mItems with your custom list and automatically animate the changes
// Watch out! The original list must be a copy
mAdapter.filterItems(DatabaseService.getInstance().getDatabaseList(), DatabaseConfiguration.delay);
// - Option A: Use the internal list as original list
mAdapter.filterItems(DatabaseConfiguration.delay);

// - Option B: Provide any new list to filter
//mAdapter.filterItems(DatabaseService.getInstance().getDatabaseList(), DatabaseConfiguration.delay);
}
// Disable SwipeRefresh if search is active!!
mSwipeRefreshLayout.setEnabled(!mAdapter.hasSearchText());
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public class FlexibleAdapter<T extends IFlexible>
private static final String EXTRA_SEARCH = TAG + "_searchText";

/* The main container for ALL items */
private List<T> mItems, mTempItems;
private List<T> mItems, mTempItems, mOriginalList;

/* HashSet, AsyncTask and DiffUtil objects, will increase performance in big list */
private Set<T> mHashItems;
Expand Down Expand Up @@ -534,6 +534,7 @@ public void updateDataSet(List<T> items) {
*/
@CallSuper
public void updateDataSet(@Nullable List<T> items, boolean animate) {
mOriginalList = null; // Reset original list from filter
if (items == null) items = new ArrayList<>();
if (animate) {
mHandler.removeMessages(UPDATE);
Expand Down Expand Up @@ -3965,11 +3966,11 @@ public void setSearchText(String searchText) {
* <ul><li>During {@link #filterItems(List)}: If the items have highlighted text, those items
* must be refreshed in order to change the displayed text back to normal. This happens
* systematically when searchText is reduced in length by the user.
* <br>The notification is always triggered when filter is active!</li>
* <br>However, the notification is always triggered when filter is invoked!</li>
* <li>During {@link #updateDataSet(List, boolean)}: If the most recent content has to be
* displayed, we can optimize what to bind thanks to the {@link Payload#CHANGE}.
* displayed, we can optimize what to bind thanks to the {@link Payload}.
* <br>The notification is triggered when the method of the implemented items
* {@link IFlexible#shouldNotifyChange(IFlexible)} also return true.</li></ul>
* {@link IFlexible#shouldNotifyChange(IFlexible)} also returns true.</li></ul>
* Default value is {@code false}.
*
* @param notifyChange true to trigger {@link #notifyItemChanged(int)},
Expand Down Expand Up @@ -4002,6 +4003,37 @@ public final FlexibleAdapter setNotifyMoveOfFilteredItems(boolean notifyMove) {
return this;
}

/**
* Filters the current list with the searchText previously set with
* {@link #setSearchText(String)}.
*
* @see #filterItems(long)
* @see #filterItems(List)
* @see #onPostFilter()
* @see #setAnimateToLimit(int)
* @since 5.0.0-rc2
*/
public void filterItems() {
if (mOriginalList == null) mOriginalList = mItems;
filterItems(mOriginalList);
}

/**
* Same as {@link #filterItems()} but with a delay in the execution, useful to grab
* more characters from user before starting the search.
*
* @param delay any non-negative delay
* @see #filterItems()
* @see #filterItems(List, long)
* @see #onPostFilter()
* @see #setAnimateToLimit(int)
* @since 5.0.0-rc2
*/
public void filterItems(@IntRange(from = 0) long delay) {
if (mOriginalList == null) mOriginalList = mItems;
filterItems(mOriginalList, delay);
}

/**
* <b>WATCH OUT! ADAPTER ALREADY CREATES A <u>COPY</u> OF THE PROVIDED LIST</b>: due to internal
* mechanism, items are removed and/or added in order to animate items in the final list.
Expand All @@ -4010,7 +4042,8 @@ public final FlexibleAdapter setNotifyMoveOfFilteredItems(boolean notifyMove) {
*
* @param unfilteredItems the list to filter
* @param delay any non-negative delay
* @see #filterObject(IFlexible, String)
* @see #filterItems(long)
* @see #filterItems(List)
* @see #onPostFilter()
* @see #setAnimateToLimit(int)
* @since 5.0.0-b1
Expand Down Expand Up @@ -4043,7 +4076,8 @@ public void filterItems(@NonNull List<T> unfilteredItems, @IntRange(from = 0) lo
* </ol>
*
* @param unfilteredItems the list to filter
* @see #filterObject(IFlexible, String)
* @see #filterItems()
* @see #filterItems(List, long)
* @see #onPostFilter()
* @see #setAnimateToLimit(int)
* @since 4.1.0 Created
Expand Down Expand Up @@ -4072,7 +4106,7 @@ private synchronized void filterItemsAsync(@NonNull List<T> unfilteredItems) {
header.setHidden(false);
filteredItems.add(header);
}
addFilteredSubItems(filteredItems, item); //recursive add
addFilteredSubItems(filteredItems, item); //Adds filtered item + recursive add
} else {
item.setHidden(true);
}
Expand All @@ -4081,7 +4115,9 @@ private synchronized void filterItemsAsync(@NonNull List<T> unfilteredItems) {
filteredItems = unfilteredItems; //original items with no filter
resetFilterFlags(filteredItems); //recursive reset
mExpandedFilterFlags = null;
restoreScrollableHeadersAndFooters(filteredItems);
if (mOriginalList == null)
restoreScrollableHeadersAndFooters(filteredItems);
mOriginalList = null;
}

// Animate search results only in case of new SearchText
Expand Down Expand Up @@ -4171,6 +4207,7 @@ private void addFilteredSubItems(List<T> values, T item) {
if (isExpandable(item)) {
IExpandable expandable = (IExpandable) item;
if (hasSubItems(expandable)) {
if (mOriginalList != null && expandable.isExpanded()) return;
// Add subItems if not hidden by filterObject()
List<T> filteredSubItems = new ArrayList<>();
List<T> subItems = expandable.getSubItems();
Expand Down Expand Up @@ -4210,15 +4247,15 @@ private void resetFilterFlags(List<T> items) {
}
}
// Show subItems for expanded items
if (expandable.isExpanded()) {
if (expandable.isExpanded() && mOriginalList == null) {
if (i < items.size()) items.addAll(i + 1, subItems);
else items.addAll(subItems);
i += subItems.size();
}
}
}
// Restore headers visibility
if (headersShown) {
if (headersShown && mOriginalList == null) {
IHeader header = getHeaderOf(item);
if (header != null && !header.equals(sameHeader) && !isExpandable((T) header)) {
header.setHidden(false);
Expand Down Expand Up @@ -5424,6 +5461,7 @@ protected void onPreExecute() {
if (isRestoreInTime() && mDeleteCompleteListener != null) {
if (DEBUG) Log.d(TAG, "Hiding all deleted items before filtering/updating");
newItems.removeAll(getDeletedItems());
if (mOriginalList != null) mOriginalList.removeAll(getDeletedItems());
mDeleteCompleteListener.onDeleteConfirmed();
}
}
Expand Down

0 comments on commit 0164675

Please sign in to comment.