Use fallback images for Giphy results.

This commit is contained in:
Greyson Parrelli 2019-10-20 00:52:14 -04:00
parent 1f85b1f3d2
commit b9057a1c11
3 changed files with 53 additions and 9 deletions

View file

@ -21,6 +21,7 @@ public class ChunkedImageUrl implements Key {
}
public ChunkedImageUrl(@NonNull String url, long size) {
if (url == null) throw new RuntimeException();
this.url = url;
this.size = size;
}

View file

@ -1,27 +1,37 @@
package org.thoughtcrime.securesms.giph.model;
import android.text.TextUtils;
import androidx.annotation.Nullable;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.thoughtcrime.securesms.util.Util;
public class GiphyImage {
@JsonProperty
private ImageTypes images;
public String getGifUrl() {
return images.downsized.url;
ImageData data = getGifData();
return data != null ? data.url : null;
}
public long getGifSize() {
return images.downsized.size;
ImageData data = getGifData();
return data != null ? data.size : 0;
}
public String getGifMmsUrl() {
return images.fixed_height_downsampled.url;
ImageData data = getGifMmsData();
return data != null ? data.url : null;
}
public long getMmsGifSize() {
return images.fixed_height_downsampled.size;
ImageData data = getGifMmsData();
return data != null ? data.size : 0;
}
public float getGifAspectRatio() {
@ -29,19 +39,45 @@ public class GiphyImage {
}
public int getGifWidth() {
return images.downsized.width;
ImageData data = getGifData();
return data != null ? data.width : 0;
}
public int getGifHeight() {
return images.downsized.height;
ImageData data = getGifData();
return data != null ? data.height : 0;
}
public String getStillUrl() {
return images.downsized_still.url;
ImageData data = getStillData();
return data != null ? data.url : null;
}
public long getStillSize() {
return images.downsized_still.size;
ImageData data = getStillData();
return data != null ? data.size : 0;
}
private @Nullable ImageData getGifData() {
return getFirstNonEmpty(images.downsized, images.downsized_medium, images.fixed_height, images.fixed_width);
}
private @Nullable ImageData getGifMmsData() {
return getFirstNonEmpty(images.fixed_height_downsampled, images.fixed_width_downsampled);
}
private @Nullable ImageData getStillData() {
return getFirstNonEmpty(images.downsized_still, images.fixed_height_still, images.fixed_width_still);
}
private static @Nullable ImageData getFirstNonEmpty(ImageData... data) {
for (ImageData image : data) {
if (!TextUtils.isEmpty(image.url)) {
return image;
}
}
return null;
}
public static class ImageTypes {

View file

@ -6,6 +6,9 @@ import android.net.Uri;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import com.annimon.stream.Stream;
import org.thoughtcrime.securesms.logging.Log;
@ -59,7 +62,11 @@ public abstract class GiphyLoader extends AsyncLoader<List<GiphyImage>> {
}
GiphyResponse giphyResponse = JsonUtils.fromJson(response.body().byteStream(), GiphyResponse.class);
List<GiphyImage> results = giphyResponse.getData();
List<GiphyImage> results = Stream.of(giphyResponse.getData())
.filterNot(g -> TextUtils.isEmpty(g.getGifUrl()))
.filterNot(g -> TextUtils.isEmpty(g.getGifMmsUrl()))
.filterNot(g -> TextUtils.isEmpty(g.getStillUrl()))
.toList();
if (results == null) return new LinkedList<>();
else return results;