From 7f4196e1196a3ac506e46e45e6ce4cf03e992938 Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Thu, 18 Mar 2021 18:09:33 -0400 Subject: [PATCH] close #808 --- .../adapters/FeedStoriesListAdapter.java | 42 +++++++++++++++++-- .../fragments/StoryListViewerFragment.java | 32 +++++++++++++- .../instagrabber/models/FeedStoryModel.java | 9 ++++ app/src/main/res/menu/search.xml | 13 ++++++ 4 files changed, 91 insertions(+), 5 deletions(-) create mode 100755 app/src/main/res/menu/search.xml diff --git a/app/src/main/java/awais/instagrabber/adapters/FeedStoriesListAdapter.java b/app/src/main/java/awais/instagrabber/adapters/FeedStoriesListAdapter.java index 6c54b587..9d7401e0 100755 --- a/app/src/main/java/awais/instagrabber/adapters/FeedStoriesListAdapter.java +++ b/app/src/main/java/awais/instagrabber/adapters/FeedStoriesListAdapter.java @@ -2,23 +2,46 @@ package awais.instagrabber.adapters; import android.view.LayoutInflater; import android.view.ViewGroup; +import android.widget.Filter; +import android.widget.Filterable; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.recyclerview.widget.DiffUtil; import androidx.recyclerview.widget.ListAdapter; -import java.util.ArrayList; -import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; import awais.instagrabber.adapters.viewholder.StoryListViewHolder; import awais.instagrabber.databinding.ItemNotificationBinding; import awais.instagrabber.models.FeedStoryModel; -import awais.instagrabber.utils.Utils; +import awais.instagrabber.utils.TextUtils; -public final class FeedStoriesListAdapter extends ListAdapter { +public final class FeedStoriesListAdapter extends ListAdapter implements Filterable { private final OnFeedStoryClickListener listener; + private List list; + + private final Filter filter = new Filter() { + @Nullable + @Override + protected FilterResults performFiltering(final CharSequence filter) { + final boolean isFilterEmpty = TextUtils.isEmpty(filter); + final String query = isFilterEmpty ? null : filter.toString().toLowerCase(); + + for (FeedStoryModel item : list) { + if (isFilterEmpty) item.setShown(true); + else item.setShown(item.getProfileModel().getUsername().toLowerCase().contains(query)); + } + return null; + } + + @Override + protected void publishResults(final CharSequence constraint, final FilterResults results) { + submitList(list); + notifyDataSetChanged(); + } + }; private static final DiffUtil.ItemCallback diffCallback = new DiffUtil.ItemCallback() { @Override @@ -37,6 +60,17 @@ public final class FeedStoriesListAdapter extends ListAdapter list) { + super.submitList(list.stream().filter(i -> i.isShown()).collect(Collectors.toList())); + this.list = list; + } + @NonNull @Override public StoryListViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, final int viewType) { diff --git a/app/src/main/java/awais/instagrabber/fragments/StoryListViewerFragment.java b/app/src/main/java/awais/instagrabber/fragments/StoryListViewerFragment.java index d71a7cfb..57a9d7be 100644 --- a/app/src/main/java/awais/instagrabber/fragments/StoryListViewerFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/StoryListViewerFragment.java @@ -4,6 +4,9 @@ import android.content.Context; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; @@ -12,6 +15,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.SearchView; import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; import androidx.navigation.NavDirections; @@ -51,8 +55,9 @@ public final class StoryListViewerFragment extends Fragment implements SwipeRefr private ArchivesViewModel archivesViewModel; private StoriesService storiesService; private Context context; - private String type, endCursor = null; + private String type, currentQuery, endCursor = null; private FeedStoriesListAdapter adapter; + private MenuItem menuSearch; private final OnFeedStoryClickListener clickListener = new OnFeedStoryClickListener() { @Override @@ -119,6 +124,7 @@ public final class StoryListViewerFragment extends Fragment implements SwipeRefr fragmentActivity = (AppCompatActivity) requireActivity(); context = getContext(); if (context == null) return; + setHasOptionsMenu(true); storiesService = StoriesService.getInstance(null, 0L, null); } @@ -141,6 +147,30 @@ public final class StoryListViewerFragment extends Fragment implements SwipeRefr shouldRefresh = false; } + @Override + public void onCreateOptionsMenu(@NonNull final Menu menu, final MenuInflater inflater) { + inflater.inflate(R.menu.search, menu); + menuSearch = menu.findItem(R.id.action_search); + final SearchView searchView = (SearchView) menuSearch.getActionView(); + searchView.setQueryHint(getResources().getString(R.string.action_search)); + searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { + + @Override + public boolean onQueryTextSubmit(final String query) { + return false; + } + + @Override + public boolean onQueryTextChange(final String query) { + if (adapter != null) { + currentQuery = query; + adapter.getFilter().filter(query); + } + return true; + } + }); + } + @Override public void onResume() { super.onResume(); diff --git a/app/src/main/java/awais/instagrabber/models/FeedStoryModel.java b/app/src/main/java/awais/instagrabber/models/FeedStoryModel.java index 7aa21239..04595093 100755 --- a/app/src/main/java/awais/instagrabber/models/FeedStoryModel.java +++ b/app/src/main/java/awais/instagrabber/models/FeedStoryModel.java @@ -16,6 +16,7 @@ public final class FeedStoryModel implements Serializable { private final boolean isLive, isBestie; private final long timestamp; private final int mediaCount; + private boolean isShown = true; public FeedStoryModel(final String storyMediaId, final User profileModel, final boolean fullyRead, final long timestamp, final StoryModel firstStoryModel, final int mediaCount, @@ -74,4 +75,12 @@ public final class FeedStoryModel implements Serializable { public boolean isBestie() { return isBestie; } + + public boolean isShown() { + return isShown; + } + + public void setShown(final boolean shown) { + isShown = shown; + } } \ No newline at end of file diff --git a/app/src/main/res/menu/search.xml b/app/src/main/res/menu/search.xml new file mode 100755 index 00000000..040ed720 --- /dev/null +++ b/app/src/main/res/menu/search.xml @@ -0,0 +1,13 @@ + + + + + \ No newline at end of file