1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-22 06:37:30 +00:00
This commit is contained in:
Austin Huang 2021-06-19 12:10:56 -04:00
parent 7e45716e61
commit c2ac61986f
No known key found for this signature in database
GPG Key ID: 84C23AA04587A91F
4 changed files with 26 additions and 13 deletions

View File

@ -68,7 +68,7 @@ public class SearchItemViewHolder extends RecyclerView.ViewHolder {
binding.delete.setOnClickListener(v -> { binding.delete.setOnClickListener(v -> {
if (onSearchItemClickListener != null) { if (onSearchItemClickListener != null) {
binding.delete.setEnabled(false); binding.delete.setEnabled(false);
onSearchItemClickListener.onSearchItemDelete(searchItem); onSearchItemClickListener.onSearchItemDelete(searchItem, type);
} }
}); });
} }

View File

@ -192,6 +192,6 @@ public class SearchCategoryFragment extends Fragment {
public interface OnSearchItemClickListener { public interface OnSearchItemClickListener {
void onSearchItemClick(SearchItem searchItem); void onSearchItemClick(SearchItem searchItem);
void onSearchItemDelete(SearchItem searchItem); void onSearchItemDelete(SearchItem searchItem, FavoriteType type);
} }
} }

View File

@ -229,7 +229,7 @@ public class SearchFragment extends Fragment implements SearchCategoryFragment.O
} }
@Override @Override
public void onSearchItemDelete(final SearchItem searchItem) { public void onSearchItemDelete(final SearchItem searchItem, final FavoriteType type) {
final LiveData<Resource<Object>> liveData = viewModel.deleteRecentSearch(searchItem); final LiveData<Resource<Object>> liveData = viewModel.deleteRecentSearch(searchItem);
if (liveData == null) return; if (liveData == null) return;
liveData.observe(getViewLifecycleOwner(), new Observer<Resource<Object>>() { liveData.observe(getViewLifecycleOwner(), new Observer<Resource<Object>>() {
@ -238,7 +238,7 @@ public class SearchFragment extends Fragment implements SearchCategoryFragment.O
if (resource == null) return; if (resource == null) return;
switch (resource.status) { switch (resource.status) {
case SUCCESS: case SUCCESS:
viewModel.search("", FavoriteType.TOP); viewModel.search("", type);
liveData.removeObserver(this); liveData.removeObserver(this);
break; break;
case ERROR: case ERROR:

View File

@ -126,11 +126,7 @@ public class SearchFragmentViewModel extends AppStateViewModel {
final MutableLiveData<Resource<List<SearchItem>>> liveData = getLiveDataByType(type); final MutableLiveData<Resource<List<SearchItem>>> liveData = getLiveDataByType(type);
if (liveData == null) return; if (liveData == null) return;
if (TextUtils.isEmpty(query)) { if (TextUtils.isEmpty(query)) {
if (type != FavoriteType.TOP) { showRecentSearchesAndFavorites(type, liveData);
liveData.postValue(Resource.success(Collections.emptyList()));
return;
}
showRecentSearchesAndFavorites();
return; return;
} }
if (query.equals("@") || query.equals("#")) 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<Resource<List<SearchItem>>> liveData) {
final SettableFuture<List<RecentSearch>> recentResultsFuture = SettableFuture.create(); final SettableFuture<List<RecentSearch>> recentResultsFuture = SettableFuture.create();
final SettableFuture<List<Favorite>> favoritesFuture = SettableFuture.create(); final SettableFuture<List<Favorite>> favoritesFuture = SettableFuture.create();
recentSearchRepository.getAllRecentSearches( recentSearchRepository.getAllRecentSearches(
@ -187,6 +184,14 @@ public class SearchFragmentViewModel extends AppStateViewModel {
recentResultsFuture.set(Collections.emptyList()); recentResultsFuture.set(Collections.emptyList());
return; return;
} }
if (type != FavoriteType.TOP) {
recentResultsFuture.set((List<RecentSearch>) recentSearches
.stream()
.filter(rs -> rs.getType() == type)
.collect(Collectors.toList())
);
return;
}
//noinspection unchecked //noinspection unchecked
recentResultsFuture.set((List<RecentSearch>) recentSearches); recentResultsFuture.set((List<RecentSearch>) recentSearches);
}), Dispatchers.getIO()) }), Dispatchers.getIO())
@ -198,6 +203,14 @@ public class SearchFragmentViewModel extends AppStateViewModel {
Log.e(TAG, "showRecentSearchesAndFavorites: ", throwable); Log.e(TAG, "showRecentSearchesAndFavorites: ", throwable);
return; return;
} }
if (type != FavoriteType.TOP) {
favoritesFuture.set((List<Favorite>) favorites
.stream()
.filter(f -> f.getType() == type)
.collect(Collectors.toList())
);
return;
}
//noinspection unchecked //noinspection unchecked
favoritesFuture.set((List<Favorite>) favorites); favoritesFuture.set((List<Favorite>) favorites);
}), Dispatchers.getIO()) }), Dispatchers.getIO())
@ -209,12 +222,12 @@ public class SearchFragmentViewModel extends AppStateViewModel {
public void onSuccess(@Nullable final List<List<?>> result) { public void onSuccess(@Nullable final List<List<?>> result) {
if (!TextUtils.isEmpty(tempQuery)) return; // Make sure user has not entered anything before updating results if (!TextUtils.isEmpty(tempQuery)) return; // Make sure user has not entered anything before updating results
if (result == null) { if (result == null) {
topResults.postValue(Resource.success(Collections.emptyList())); liveData.postValue(Resource.success(Collections.emptyList()));
return; return;
} }
try { try {
//noinspection unchecked //noinspection unchecked
topResults.postValue(Resource.success( liveData.postValue(Resource.success(
ImmutableList.<SearchItem>builder() ImmutableList.<SearchItem>builder()
.addAll(SearchItem.fromRecentSearch((List<RecentSearch>) result.get(0))) .addAll(SearchItem.fromRecentSearch((List<RecentSearch>) result.get(0)))
.addAll(SearchItem.fromFavorite((List<Favorite>) result.get(1))) .addAll(SearchItem.fromFavorite((List<Favorite>) result.get(1)))
@ -222,7 +235,7 @@ public class SearchFragmentViewModel extends AppStateViewModel {
)); ));
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG, "onSuccess: ", e); Log.e(TAG, "onSuccess: ", e);
topResults.postValue(Resource.success(Collections.emptyList())); liveData.postValue(Resource.success(Collections.emptyList()));
} }
} }