From b3cd83ad31ce174f84258889abf4a28392e0a570 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Sat, 15 May 2021 13:21:58 +0900 Subject: [PATCH] Fix clicking story from story list after searching opens wrong story. Fixes austinhuang0131/barinsta#1189 --- .../adapters/FeedStoriesListAdapter.java | 40 ++++++++++++------- .../viewholder/StoryListViewHolder.java | 3 +- .../fragments/StoryListViewerFragment.java | 14 ++++--- .../instagrabber/models/FeedStoryModel.java | 24 ++++------- 4 files changed, 44 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/adapters/FeedStoriesListAdapter.java b/app/src/main/java/awais/instagrabber/adapters/FeedStoriesListAdapter.java index 9d7401e0..146904d0 100755 --- a/app/src/main/java/awais/instagrabber/adapters/FeedStoriesListAdapter.java +++ b/app/src/main/java/awais/instagrabber/adapters/FeedStoriesListAdapter.java @@ -23,23 +23,29 @@ public final class FeedStoriesListAdapter extends ListAdapter list; private final Filter filter = new Filter() { - @Nullable + @NonNull @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)); + final String query = TextUtils.isEmpty(filter) ? null : filter.toString().toLowerCase(); + List filteredList = list; + if (list != null && query != null) { + filteredList = list.stream() + .filter(feedStoryModel -> feedStoryModel.getProfileModel() + .getUsername() + .toLowerCase() + .contains(query)) + .collect(Collectors.toList()); } - return null; + final FilterResults filterResults = new FilterResults(); + filterResults.count = filteredList != null ? filteredList.size() : 0; + filterResults.values = filteredList; + return filterResults; } @Override protected void publishResults(final CharSequence constraint, final FilterResults results) { - submitList(list); - notifyDataSetChanged(); + //noinspection unchecked + submitList((List) results.values, true); } }; @@ -65,10 +71,16 @@ public final class FeedStoriesListAdapter extends ListAdapter list, final boolean isFiltered) { + if (!isFiltered) { + this.list = list; + } + super.submitList(list); + } + @Override public void submitList(final List list) { - super.submitList(list.stream().filter(i -> i.isShown()).collect(Collectors.toList())); - this.list = list; + submitList(list, false); } @NonNull @@ -82,11 +94,11 @@ public final class FeedStoriesListAdapter extends ListAdapter { if (notificationClickListener == null) return; - notificationClickListener.onFeedStoryClick(model, position); + notificationClickListener.onFeedStoryClick(model); }); } diff --git a/app/src/main/java/awais/instagrabber/fragments/StoryListViewerFragment.java b/app/src/main/java/awais/instagrabber/fragments/StoryListViewerFragment.java index f4bf129a..f8bdfb81 100644 --- a/app/src/main/java/awais/instagrabber/fragments/StoryListViewerFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/StoryListViewerFragment.java @@ -23,9 +23,12 @@ import androidx.navigation.fragment.NavHostFragment; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; +import com.google.common.collect.Iterables; + import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Objects; import awais.instagrabber.R; import awais.instagrabber.adapters.FeedStoriesListAdapter; @@ -58,15 +61,17 @@ public final class StoryListViewerFragment extends Fragment implements SwipeRefr private StoriesService storiesService; private Context context; private String type; - private String currentQuery; private String endCursor = null; private FeedStoriesListAdapter adapter; - private MenuItem menuSearch; private final OnFeedStoryClickListener clickListener = new OnFeedStoryClickListener() { @Override - public void onFeedStoryClick(final FeedStoryModel model, final int position) { + public void onFeedStoryClick(final FeedStoryModel model) { if (model == null) return; + final List feedStoryModels = feedStoriesViewModel.getList().getValue(); + if (feedStoryModels == null) return; + final int position = Iterables.indexOf(feedStoryModels, feedStoryModel -> feedStoryModel != null + && Objects.equals(feedStoryModel.getStoryMediaId(), model.getStoryMediaId())); final NavDirections action = StoryListViewerFragmentDirections .actionStoryListFragmentToStoryViewerFragment(StoryViewerOptions.forFeedStoryPosition(position)); NavHostFragment.findNavController(StoryListViewerFragment.this).navigate(action); @@ -153,7 +158,7 @@ public final class StoryListViewerFragment extends Fragment implements SwipeRefr @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 MenuItem 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() { @@ -166,7 +171,6 @@ public final class StoryListViewerFragment extends Fragment implements SwipeRefr @Override public boolean onQueryTextChange(final String query) { if (adapter != null) { - currentQuery = query; adapter.getFilter().filter(query); } return true; diff --git a/app/src/main/java/awais/instagrabber/models/FeedStoryModel.java b/app/src/main/java/awais/instagrabber/models/FeedStoryModel.java index 04595093..b87091e5 100755 --- a/app/src/main/java/awais/instagrabber/models/FeedStoryModel.java +++ b/app/src/main/java/awais/instagrabber/models/FeedStoryModel.java @@ -16,11 +16,15 @@ 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, - final boolean isLive, final boolean isBestie) { + public FeedStoryModel(final String storyMediaId, + final User profileModel, + final boolean fullyRead, + final long timestamp, + final StoryModel firstStoryModel, + final int mediaCount, + final boolean isLive, + final boolean isBestie) { this.storyMediaId = storyMediaId; this.profileModel = profileModel; this.fullyRead = fullyRead; @@ -52,10 +56,6 @@ public final class FeedStoryModel implements Serializable { return profileModel; } - // public void setFirstStoryModel(final StoryModel firstStoryModel) { - // this.firstStoryModel = firstStoryModel; - // } - public StoryModel getFirstStoryModel() { return firstStoryModel; } @@ -75,12 +75,4 @@ 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