From a2c6685dd82432dc009506d2ec1fcfcec82ee744 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sun, 12 Dec 2021 20:48:23 +0800 Subject: Treat streamable link posts as video posts. --- .../FetchGfycatOrRedgifsVideoLinks.java | 43 ++++++++++------------ .../adapters/PostDetailRecyclerViewAdapter.java | 9 +++++ .../adapters/PostRecyclerViewAdapter.java | 13 +++++-- .../infinityforreddit/post/ParsePost.java | 34 ++++++++++------- .../infinityforreddit/post/Post.java | 22 +++++++++++ 5 files changed, 80 insertions(+), 41 deletions(-) (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatOrRedgifsVideoLinks.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatOrRedgifsVideoLinks.java index 19dc8a9c..16e27ff5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatOrRedgifsVideoLinks.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatOrRedgifsVideoLinks.java @@ -2,17 +2,15 @@ package ml.docilealligator.infinityforreddit; import android.os.Handler; -import androidx.annotation.NonNull; - import org.json.JSONException; import org.json.JSONObject; +import java.io.IOException; import java.util.concurrent.Executor; import ml.docilealligator.infinityforreddit.apis.GfycatAPI; import ml.docilealligator.infinityforreddit.utils.JSONUtils; import retrofit2.Call; -import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; @@ -32,45 +30,42 @@ public class FetchGfycatOrRedgifsVideoLinks { public static void fetchGfycatOrRedgifsVideoLinks(Executor executor, Handler handler, Retrofit gfycatRetrofit, String gfycatId, FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener) { - gfycatRetrofit.create(GfycatAPI.class).getGfycatData(gfycatId).enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull Response response) { + executor.execute(() -> { + try { + Response response = gfycatRetrofit.create(GfycatAPI.class).getGfycatData(gfycatId).execute(); if (response.isSuccessful()) { - parseGfycatVideoLinks(executor, handler, response.body(), fetchGfycatOrRedgifsVideoLinksListener); + parseGfycatVideoLinks(handler, response.body(), fetchGfycatOrRedgifsVideoLinksListener); } else { - fetchGfycatOrRedgifsVideoLinksListener.failed(response.code()); + handler.post(() -> fetchGfycatOrRedgifsVideoLinksListener.failed(response.code())); } - } - - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - fetchGfycatOrRedgifsVideoLinksListener.failed(-1); + } catch (IOException e) { + e.printStackTrace(); + handler.post(() -> fetchGfycatOrRedgifsVideoLinksListener.failed(-1)); } }); + } public void fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(Executor executor, Handler handler, Retrofit gfycatRetrofit, Retrofit redgifsRetrofit, String gfycatId, boolean isGfycatVideo, boolean automaticallyTryRedgifs) { - gfycatCall = (isGfycatVideo ? gfycatRetrofit : redgifsRetrofit).create(GfycatAPI.class).getGfycatData(gfycatId); - gfycatCall.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull Response response) { + executor.execute(() -> { + gfycatCall = (isGfycatVideo ? gfycatRetrofit : redgifsRetrofit).create(GfycatAPI.class).getGfycatData(gfycatId); + try { + Response response = gfycatCall.execute(); if (response.isSuccessful()) { - parseGfycatVideoLinks(executor, handler, response.body(), fetchGfycatOrRedgifsVideoLinksListener); + parseGfycatVideoLinks(handler, response.body(), fetchGfycatOrRedgifsVideoLinksListener); } else { if (response.code() == 404 && isGfycatVideo && automaticallyTryRedgifs) { fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(executor, handler, gfycatRetrofit, redgifsRetrofit, gfycatId, false, false); } else { - fetchGfycatOrRedgifsVideoLinksListener.failed(response.code()); + handler.post(() -> fetchGfycatOrRedgifsVideoLinksListener.failed(response.code())); } } - } - - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { + } catch (IOException e) { + e.printStackTrace(); fetchGfycatOrRedgifsVideoLinksListener.failed(-1); } }); @@ -82,7 +77,7 @@ public class FetchGfycatOrRedgifsVideoLinks { } } - private static void parseGfycatVideoLinks(Executor executor, Handler handler, String response, + private static void parseGfycatVideoLinks(Handler handler, String response, FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener) { try { JSONObject jsonObject = new JSONObject(response); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java index dd1172a8..4157db56 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java @@ -1624,6 +1624,9 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter