From c2ac61986fa94990fffd26340ac3e4ce33d5965d Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Sat, 19 Jun 2021 12:10:56 -0400 Subject: [PATCH] close #1362 --- .../viewholder/SearchItemViewHolder.java | 2 +- .../search/SearchCategoryFragment.java | 2 +- .../fragments/search/SearchFragment.java | 4 +-- .../viewmodels/SearchFragmentViewModel.java | 31 +++++++++++++------ 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/adapters/viewholder/SearchItemViewHolder.java b/app/src/main/java/awais/instagrabber/adapters/viewholder/SearchItemViewHolder.java index 4eb93fed..a72e17c3 100644 --- a/app/src/main/java/awais/instagrabber/adapters/viewholder/SearchItemViewHolder.java +++ b/app/src/main/java/awais/instagrabber/adapters/viewholder/SearchItemViewHolder.java @@ -68,7 +68,7 @@ public class SearchItemViewHolder extends RecyclerView.ViewHolder { binding.delete.setOnClickListener(v -> { if (onSearchItemClickListener != null) { binding.delete.setEnabled(false); - onSearchItemClickListener.onSearchItemDelete(searchItem); + onSearchItemClickListener.onSearchItemDelete(searchItem, type); } }); } diff --git a/app/src/main/java/awais/instagrabber/fragments/search/SearchCategoryFragment.java b/app/src/main/java/awais/instagrabber/fragments/search/SearchCategoryFragment.java index 4eb1ed8d..d4703dbd 100644 --- a/app/src/main/java/awais/instagrabber/fragments/search/SearchCategoryFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/search/SearchCategoryFragment.java @@ -192,6 +192,6 @@ public class SearchCategoryFragment extends Fragment { public interface OnSearchItemClickListener { void onSearchItemClick(SearchItem searchItem); - void onSearchItemDelete(SearchItem searchItem); + void onSearchItemDelete(SearchItem searchItem, FavoriteType type); } } diff --git a/app/src/main/java/awais/instagrabber/fragments/search/SearchFragment.java b/app/src/main/java/awais/instagrabber/fragments/search/SearchFragment.java index ab879a77..371793cc 100644 --- a/app/src/main/java/awais/instagrabber/fragments/search/SearchFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/search/SearchFragment.java @@ -229,7 +229,7 @@ public class SearchFragment extends Fragment implements SearchCategoryFragment.O } @Override - public void onSearchItemDelete(final SearchItem searchItem) { + public void onSearchItemDelete(final SearchItem searchItem, final FavoriteType type) { final LiveData> liveData = viewModel.deleteRecentSearch(searchItem); if (liveData == null) return; liveData.observe(getViewLifecycleOwner(), new Observer>() { @@ -238,7 +238,7 @@ public class SearchFragment extends Fragment implements SearchCategoryFragment.O if (resource == null) return; switch (resource.status) { case SUCCESS: - viewModel.search("", FavoriteType.TOP); + viewModel.search("", type); liveData.removeObserver(this); break; case ERROR: diff --git a/app/src/main/java/awais/instagrabber/viewmodels/SearchFragmentViewModel.java b/app/src/main/java/awais/instagrabber/viewmodels/SearchFragmentViewModel.java index 8bdcfb08..da2d0618 100644 --- a/app/src/main/java/awais/instagrabber/viewmodels/SearchFragmentViewModel.java +++ b/app/src/main/java/awais/instagrabber/viewmodels/SearchFragmentViewModel.java @@ -126,11 +126,7 @@ public class SearchFragmentViewModel extends AppStateViewModel { final MutableLiveData>> liveData = getLiveDataByType(type); if (liveData == null) return; if (TextUtils.isEmpty(query)) { - if (type != FavoriteType.TOP) { - liveData.postValue(Resource.success(Collections.emptyList())); - return; - } - showRecentSearchesAndFavorites(); + showRecentSearchesAndFavorites(type, liveData); return; } if (query.equals("@") || query.equals("#")) return; @@ -177,7 +173,8 @@ public class SearchFragmentViewModel extends AppStateViewModel { }); } - private void showRecentSearchesAndFavorites() { + private void showRecentSearchesAndFavorites(@NonNull final FavoriteType type, + @NonNull final MutableLiveData>> liveData) { final SettableFuture> recentResultsFuture = SettableFuture.create(); final SettableFuture> favoritesFuture = SettableFuture.create(); recentSearchRepository.getAllRecentSearches( @@ -187,6 +184,14 @@ public class SearchFragmentViewModel extends AppStateViewModel { recentResultsFuture.set(Collections.emptyList()); return; } + if (type != FavoriteType.TOP) { + recentResultsFuture.set((List) recentSearches + .stream() + .filter(rs -> rs.getType() == type) + .collect(Collectors.toList()) + ); + return; + } //noinspection unchecked recentResultsFuture.set((List) recentSearches); }), Dispatchers.getIO()) @@ -198,6 +203,14 @@ public class SearchFragmentViewModel extends AppStateViewModel { Log.e(TAG, "showRecentSearchesAndFavorites: ", throwable); return; } + if (type != FavoriteType.TOP) { + favoritesFuture.set((List) favorites + .stream() + .filter(f -> f.getType() == type) + .collect(Collectors.toList()) + ); + return; + } //noinspection unchecked favoritesFuture.set((List) favorites); }), Dispatchers.getIO()) @@ -209,12 +222,12 @@ public class SearchFragmentViewModel extends AppStateViewModel { public void onSuccess(@Nullable final List> result) { if (!TextUtils.isEmpty(tempQuery)) return; // Make sure user has not entered anything before updating results if (result == null) { - topResults.postValue(Resource.success(Collections.emptyList())); + liveData.postValue(Resource.success(Collections.emptyList())); return; } try { //noinspection unchecked - topResults.postValue(Resource.success( + liveData.postValue(Resource.success( ImmutableList.builder() .addAll(SearchItem.fromRecentSearch((List) result.get(0))) .addAll(SearchItem.fromFavorite((List) result.get(1))) @@ -222,7 +235,7 @@ public class SearchFragmentViewModel extends AppStateViewModel { )); } catch (Exception e) { Log.e(TAG, "onSuccess: ", e); - topResults.postValue(Resource.success(Collections.emptyList())); + liveData.postValue(Resource.success(Collections.emptyList())); } }