Skip to content

Commit

Permalink
Use fallback images for Giphy results.
Browse files Browse the repository at this point in the history
  • Loading branch information
greyson-signal committed Oct 20, 2019
1 parent 1f85b1f commit b9057a1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public ChunkedImageUrl(@NonNull String url) {
}

public ChunkedImageUrl(@NonNull String url, long size) {
if (url == null) throw new RuntimeException();
this.url = url;
this.size = size;
}
Expand Down
52 changes: 44 additions & 8 deletions src/org/thoughtcrime/securesms/giph/model/GiphyImage.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,83 @@
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() {
return (float)images.downsized.width / (float)images.downsized.height;
}

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 {
Expand Down
9 changes: 8 additions & 1 deletion src/org/thoughtcrime/securesms/giph/net/GiphyLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.text.TextUtils;

import com.annimon.stream.Stream;

import org.thoughtcrime.securesms.logging.Log;


Expand Down Expand Up @@ -59,7 +62,11 @@ public List<GiphyImage> loadInBackground() {
}

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;
Expand Down

0 comments on commit b9057a1

Please sign in to comment.