From 1fdd9077d5b776a121d78ea498ca000801345db6 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Fri, 9 Apr 2021 19:36:11 +0900 Subject: [PATCH] Remove invalid gifs before submitting to adapter. Fixes https://github.com/austinhuang0131/barinsta/issues/1013 --- .../viewmodels/GifPickerViewModel.java | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/viewmodels/GifPickerViewModel.java b/app/src/main/java/awais/instagrabber/viewmodels/GifPickerViewModel.java index dd014204..69450281 100644 --- a/app/src/main/java/awais/instagrabber/viewmodels/GifPickerViewModel.java +++ b/app/src/main/java/awais/instagrabber/viewmodels/GifPickerViewModel.java @@ -13,12 +13,17 @@ import java.io.IOException; import java.util.Collections; import java.util.List; import java.util.Locale; +import java.util.Objects; +import java.util.stream.Collectors; import awais.instagrabber.R; import awais.instagrabber.models.Resource; +import awais.instagrabber.repositories.responses.AnimatedMediaFixedHeight; import awais.instagrabber.repositories.responses.giphy.GiphyGif; +import awais.instagrabber.repositories.responses.giphy.GiphyGifImages; import awais.instagrabber.repositories.responses.giphy.GiphyGifResponse; import awais.instagrabber.repositories.responses.giphy.GiphyGifResults; +import awais.instagrabber.utils.TextUtils; import awais.instagrabber.webservices.GifService; import retrofit2.Call; import retrofit2.Callback; @@ -92,12 +97,25 @@ public class GifPickerViewModel extends ViewModel { final GiphyGifResults results = giphyGifResponse.getResults(); images.postValue(Resource.success( ImmutableList.builder() - .addAll(results.getGiphy() == null ? Collections.emptyList() : results.getGiphy()) - .addAll(results.getGiphyGifs() == null ? Collections.emptyList() : results.getGiphyGifs()) + .addAll(results.getGiphy() == null ? Collections.emptyList() : filterInvalid(results.getGiphy())) + .addAll(results.getGiphyGifs() == null ? Collections.emptyList() : filterInvalid(results.getGiphyGifs())) .build() )); } + private List filterInvalid(@NonNull final List giphyGifs) { + return giphyGifs.stream() + .filter(Objects::nonNull) + .filter(giphyGif -> { + final GiphyGifImages images = giphyGif.getImages(); + if (images == null) return false; + final AnimatedMediaFixedHeight fixedHeight = images.getFixedHeight(); + if (fixedHeight == null) return false; + return !TextUtils.isEmpty(fixedHeight.getWebp()); + }) + .collect(Collectors.toList()); + } + // @NonNull // private List getGiphyGifImages(@NonNull final List giphy) { // return giphy.stream()