|
@ -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) { |
|
|
|
|
|
liveData.postValue(Resource.success(Collections.emptyList())); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
showRecentSearchesAndFavorites(); |
|
|
|
|
|
|
|
|
showRecentSearchesAndFavorites(type, liveData); |
|
|
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())); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|