Browse Source

close #1362

renovate/org.robolectric-robolectric-4.x
Austin Huang 3 years ago
parent
commit
c2ac61986f
No known key found for this signature in database GPG Key ID: 84C23AA04587A91F
  1. 2
      app/src/main/java/awais/instagrabber/adapters/viewholder/SearchItemViewHolder.java
  2. 2
      app/src/main/java/awais/instagrabber/fragments/search/SearchCategoryFragment.java
  3. 4
      app/src/main/java/awais/instagrabber/fragments/search/SearchFragment.java
  4. 31
      app/src/main/java/awais/instagrabber/viewmodels/SearchFragmentViewModel.java

2
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);
}
});
}

2
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);
}
}

4
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<Resource<Object>> liveData = viewModel.deleteRecentSearch(searchItem);
if (liveData == null) return;
liveData.observe(getViewLifecycleOwner(), new Observer<Resource<Object>>() {
@ -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:

31
app/src/main/java/awais/instagrabber/viewmodels/SearchFragmentViewModel.java

@ -126,11 +126,7 @@ public class SearchFragmentViewModel extends AppStateViewModel {
final MutableLiveData<Resource<List<SearchItem>>> 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<Resource<List<SearchItem>>> liveData) {
final SettableFuture<List<RecentSearch>> recentResultsFuture = SettableFuture.create();
final SettableFuture<List<Favorite>> 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<RecentSearch>) recentSearches
.stream()
.filter(rs -> rs.getType() == type)
.collect(Collectors.toList())
);
return;
}
//noinspection unchecked
recentResultsFuture.set((List<RecentSearch>) 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<Favorite>) favorites
.stream()
.filter(f -> f.getType() == type)
.collect(Collectors.toList())
);
return;
}
//noinspection unchecked
favoritesFuture.set((List<Favorite>) favorites);
}), Dispatchers.getIO())
@ -209,12 +222,12 @@ public class SearchFragmentViewModel extends AppStateViewModel {
public void onSuccess(@Nullable final List<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.<SearchItem>builder()
.addAll(SearchItem.fromRecentSearch((List<RecentSearch>) result.get(0)))
.addAll(SearchItem.fromFavorite((List<Favorite>) 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()));
}
}

Loading…
Cancel
Save