Browse Source

Fix clicking story from story list after searching opens wrong story. Fixes austinhuang0131/barinsta#1189

renovate/org.robolectric-robolectric-4.x
Ammar Githam 4 years ago
parent
commit
b3cd83ad31
  1. 40
      app/src/main/java/awais/instagrabber/adapters/FeedStoriesListAdapter.java
  2. 3
      app/src/main/java/awais/instagrabber/adapters/viewholder/StoryListViewHolder.java
  3. 14
      app/src/main/java/awais/instagrabber/fragments/StoryListViewerFragment.java
  4. 24
      app/src/main/java/awais/instagrabber/models/FeedStoryModel.java

40
app/src/main/java/awais/instagrabber/adapters/FeedStoriesListAdapter.java

@ -23,23 +23,29 @@ public final class FeedStoriesListAdapter extends ListAdapter<FeedStoryModel, St
private List<FeedStoryModel> list; private List<FeedStoryModel> list;
private final Filter filter = new Filter() { private final Filter filter = new Filter() {
@Nullable
@NonNull
@Override @Override
protected FilterResults performFiltering(final CharSequence filter) { 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<FeedStoryModel> 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 @Override
protected void publishResults(final CharSequence constraint, final FilterResults results) { protected void publishResults(final CharSequence constraint, final FilterResults results) {
submitList(list);
notifyDataSetChanged();
//noinspection unchecked
submitList((List<FeedStoryModel>) results.values, true);
} }
}; };
@ -65,10 +71,16 @@ public final class FeedStoriesListAdapter extends ListAdapter<FeedStoryModel, St
return filter; return filter;
} }
private void submitList(@Nullable final List<FeedStoryModel> list, final boolean isFiltered) {
if (!isFiltered) {
this.list = list;
}
super.submitList(list);
}
@Override @Override
public void submitList(final List<FeedStoryModel> list) { public void submitList(final List<FeedStoryModel> list) {
super.submitList(list.stream().filter(i -> i.isShown()).collect(Collectors.toList()));
this.list = list;
submitList(list, false);
} }
@NonNull @NonNull
@ -82,11 +94,11 @@ public final class FeedStoriesListAdapter extends ListAdapter<FeedStoryModel, St
@Override @Override
public void onBindViewHolder(@NonNull final StoryListViewHolder holder, final int position) { public void onBindViewHolder(@NonNull final StoryListViewHolder holder, final int position) {
final FeedStoryModel model = getItem(position); final FeedStoryModel model = getItem(position);
holder.bind(model, position, listener);
holder.bind(model, listener);
} }
public interface OnFeedStoryClickListener { public interface OnFeedStoryClickListener {
void onFeedStoryClick(final FeedStoryModel model, final int position);
void onFeedStoryClick(final FeedStoryModel model);
void onProfileClick(final String username); void onProfileClick(final String username);
} }

3
app/src/main/java/awais/instagrabber/adapters/viewholder/StoryListViewHolder.java

@ -20,7 +20,6 @@ public final class StoryListViewHolder extends RecyclerView.ViewHolder {
} }
public void bind(final FeedStoryModel model, public void bind(final FeedStoryModel model,
final int position,
final OnFeedStoryClickListener notificationClickListener) { final OnFeedStoryClickListener notificationClickListener) {
if (model == null) return; if (model == null) return;
@ -53,7 +52,7 @@ public final class StoryListViewHolder extends RecyclerView.ViewHolder {
itemView.setOnClickListener(v -> { itemView.setOnClickListener(v -> {
if (notificationClickListener == null) return; if (notificationClickListener == null) return;
notificationClickListener.onFeedStoryClick(model, position);
notificationClickListener.onFeedStoryClick(model);
}); });
} }

14
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.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.google.common.collect.Iterables;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Objects;
import awais.instagrabber.R; import awais.instagrabber.R;
import awais.instagrabber.adapters.FeedStoriesListAdapter; import awais.instagrabber.adapters.FeedStoriesListAdapter;
@ -58,15 +61,17 @@ public final class StoryListViewerFragment extends Fragment implements SwipeRefr
private StoriesService storiesService; private StoriesService storiesService;
private Context context; private Context context;
private String type; private String type;
private String currentQuery;
private String endCursor = null; private String endCursor = null;
private FeedStoriesListAdapter adapter; private FeedStoriesListAdapter adapter;
private MenuItem menuSearch;
private final OnFeedStoryClickListener clickListener = new OnFeedStoryClickListener() { private final OnFeedStoryClickListener clickListener = new OnFeedStoryClickListener() {
@Override @Override
public void onFeedStoryClick(final FeedStoryModel model, final int position) {
public void onFeedStoryClick(final FeedStoryModel model) {
if (model == null) return; if (model == null) return;
final List<FeedStoryModel> 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 final NavDirections action = StoryListViewerFragmentDirections
.actionStoryListFragmentToStoryViewerFragment(StoryViewerOptions.forFeedStoryPosition(position)); .actionStoryListFragmentToStoryViewerFragment(StoryViewerOptions.forFeedStoryPosition(position));
NavHostFragment.findNavController(StoryListViewerFragment.this).navigate(action); NavHostFragment.findNavController(StoryListViewerFragment.this).navigate(action);
@ -153,7 +158,7 @@ public final class StoryListViewerFragment extends Fragment implements SwipeRefr
@Override @Override
public void onCreateOptionsMenu(@NonNull final Menu menu, final MenuInflater inflater) { public void onCreateOptionsMenu(@NonNull final Menu menu, final MenuInflater inflater) {
inflater.inflate(R.menu.search, menu); 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(); final SearchView searchView = (SearchView) menuSearch.getActionView();
searchView.setQueryHint(getResources().getString(R.string.action_search)); searchView.setQueryHint(getResources().getString(R.string.action_search));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@ -166,7 +171,6 @@ public final class StoryListViewerFragment extends Fragment implements SwipeRefr
@Override @Override
public boolean onQueryTextChange(final String query) { public boolean onQueryTextChange(final String query) {
if (adapter != null) { if (adapter != null) {
currentQuery = query;
adapter.getFilter().filter(query); adapter.getFilter().filter(query);
} }
return true; return true;

24
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 boolean isLive, isBestie;
private final long timestamp; private final long timestamp;
private final int mediaCount; 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.storyMediaId = storyMediaId;
this.profileModel = profileModel; this.profileModel = profileModel;
this.fullyRead = fullyRead; this.fullyRead = fullyRead;
@ -52,10 +56,6 @@ public final class FeedStoryModel implements Serializable {
return profileModel; return profileModel;
} }
// public void setFirstStoryModel(final StoryModel firstStoryModel) {
// this.firstStoryModel = firstStoryModel;
// }
public StoryModel getFirstStoryModel() { public StoryModel getFirstStoryModel() {
return firstStoryModel; return firstStoryModel;
} }
@ -75,12 +75,4 @@ public final class FeedStoryModel implements Serializable {
public boolean isBestie() { public boolean isBestie() {
return isBestie; return isBestie;
} }
public boolean isShown() {
return isShown;
}
public void setShown(final boolean shown) {
isShown = shown;
}
} }
Loading…
Cancel
Save