From ade1097f302b1d07fd3d0456517d79a83c30d576 Mon Sep 17 00:00:00 2001
From: Alex Ning <chineseperson5@gmail.com>
Date: Wed, 23 Jun 2021 20:22:29 +0800
Subject: Replace AsyncTask with Executor in FetchGfycatOrRedgifsVideoLinks.

---
 .../FetchGfycatOrRedgifsVideoLinks.java            | 89 ++++++++--------------
 .../activities/ViewVideoActivity.java              |  2 +-
 .../adapters/PostDetailRecyclerViewAdapter.java    |  5 +-
 .../adapters/PostRecyclerViewAdapter.java          | 11 +--
 4 files changed, 42 insertions(+), 65 deletions(-)

(limited to 'app/src')

diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatOrRedgifsVideoLinks.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatOrRedgifsVideoLinks.java
index 1fc8960c..19dc8a9c 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatOrRedgifsVideoLinks.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatOrRedgifsVideoLinks.java
@@ -1,12 +1,14 @@
 package ml.docilealligator.infinityforreddit;
 
-import android.os.AsyncTask;
+import android.os.Handler;
 
 import androidx.annotation.NonNull;
 
 import org.json.JSONException;
 import org.json.JSONObject;
 
+import java.util.concurrent.Executor;
+
 import ml.docilealligator.infinityforreddit.apis.GfycatAPI;
 import ml.docilealligator.infinityforreddit.utils.JSONUtils;
 import retrofit2.Call;
@@ -16,8 +18,6 @@ import retrofit2.Retrofit;
 
 public class FetchGfycatOrRedgifsVideoLinks {
     private FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener;
-    private ParseGfycatVideoLinksAsyncTask parseGfycatVideoLinksAsyncTask;
-    Retrofit gfycatRetrofit;
     Call<String> gfycatCall;
 
     public interface FetchGfycatOrRedgifsVideoLinksListener {
@@ -29,13 +29,14 @@ public class FetchGfycatOrRedgifsVideoLinks {
         this.fetchGfycatOrRedgifsVideoLinksListener = fetchGfycatOrRedgifsVideoLinksListener;
     }
 
-    public static void fetchGfycatOrRedgifsVideoLinks(Retrofit gfycatRetrofit, String gfycatId,
+    public static void fetchGfycatOrRedgifsVideoLinks(Executor executor, Handler handler, Retrofit gfycatRetrofit,
+                                                      String gfycatId,
                                                       FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener) {
         gfycatRetrofit.create(GfycatAPI.class).getGfycatData(gfycatId).enqueue(new Callback<String>() {
             @Override
             public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
                 if (response.isSuccessful()) {
-                    new ParseGfycatVideoLinksAsyncTask(response.body(), fetchGfycatOrRedgifsVideoLinksListener).execute();
+                    parseGfycatVideoLinks(executor, handler, response.body(), fetchGfycatOrRedgifsVideoLinksListener);
                 } else {
                     fetchGfycatOrRedgifsVideoLinksListener.failed(response.code());
                 }
@@ -48,7 +49,8 @@ public class FetchGfycatOrRedgifsVideoLinks {
         });
     }
 
-    public void fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(Retrofit gfycatRetrofit, Retrofit redgifsRetrofit,
+    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);
@@ -56,11 +58,11 @@ public class FetchGfycatOrRedgifsVideoLinks {
             @Override
             public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
                 if (response.isSuccessful()) {
-                    parseGfycatVideoLinksAsyncTask = new ParseGfycatVideoLinksAsyncTask(response.body(), fetchGfycatOrRedgifsVideoLinksListener);
-                    parseGfycatVideoLinksAsyncTask.execute();
+                    parseGfycatVideoLinks(executor, handler, response.body(), fetchGfycatOrRedgifsVideoLinksListener);
                 } else {
                     if (response.code() == 404 && isGfycatVideo && automaticallyTryRedgifs) {
-                        fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(gfycatRetrofit, redgifsRetrofit, gfycatId, false, false);
+                        fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(executor, handler, gfycatRetrofit,
+                                redgifsRetrofit, gfycatId, false, false);
                     } else {
                         fetchGfycatOrRedgifsVideoLinksListener.failed(response.code());
                     }
@@ -78,60 +80,33 @@ public class FetchGfycatOrRedgifsVideoLinks {
         if (gfycatCall != null && !gfycatCall.isCanceled()) {
             gfycatCall.cancel();
         }
-        if (parseGfycatVideoLinksAsyncTask != null && !parseGfycatVideoLinksAsyncTask.isCancelled()) {
-            parseGfycatVideoLinksAsyncTask.cancel(true);
-        }
     }
 
-    private static class ParseGfycatVideoLinksAsyncTask extends AsyncTask<Void, Void, Void> {
-
-        private String response;
-        private String webm;
-        private String mp4;
-        private boolean parseFailed = false;
-        private FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener;
-
-        ParseGfycatVideoLinksAsyncTask(String response, FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener) {
-            this.response = response;
-            this.fetchGfycatOrRedgifsVideoLinksListener = fetchGfycatOrRedgifsVideoLinksListener;
-        }
-
-        @Override
-        protected Void doInBackground(Void... voids) {
-            try {
-                JSONObject jsonObject = new JSONObject(response);
-                mp4 = jsonObject.getJSONObject(JSONUtils.GFY_ITEM_KEY).has(JSONUtils.MP4_URL_KEY) ?
-                        jsonObject.getJSONObject(JSONUtils.GFY_ITEM_KEY).getString(JSONUtils.MP4_URL_KEY)
-                        : jsonObject.getJSONObject(JSONUtils.GFY_ITEM_KEY)
+    private static void parseGfycatVideoLinks(Executor executor, Handler handler, String response,
+                                              FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener) {
+        try {
+            JSONObject jsonObject = new JSONObject(response);
+            String mp4 = jsonObject.getJSONObject(JSONUtils.GFY_ITEM_KEY).has(JSONUtils.MP4_URL_KEY) ?
+                    jsonObject.getJSONObject(JSONUtils.GFY_ITEM_KEY).getString(JSONUtils.MP4_URL_KEY)
+                    : jsonObject.getJSONObject(JSONUtils.GFY_ITEM_KEY)
+                    .getJSONObject(JSONUtils.CONTENT_URLS_KEY)
+                    .getJSONObject(JSONUtils.MP4_KEY)
+                    .getString(JSONUtils.URL_KEY);
+            String webm;
+            if (jsonObject.getJSONObject(JSONUtils.GFY_ITEM_KEY).has(JSONUtils.WEBM_URL_KEY)) {
+                webm = jsonObject.getJSONObject(JSONUtils.GFY_ITEM_KEY).getString(JSONUtils.WEBM_URL_KEY);
+            } else if (jsonObject.getJSONObject(JSONUtils.GFY_ITEM_KEY).getJSONObject(JSONUtils.CONTENT_URLS_KEY).has(JSONUtils.WEBM_KEY)) {
+                webm = jsonObject.getJSONObject(JSONUtils.GFY_ITEM_KEY)
                         .getJSONObject(JSONUtils.CONTENT_URLS_KEY)
-                        .getJSONObject(JSONUtils.MP4_KEY)
+                        .getJSONObject(JSONUtils.WEBM_KEY)
                         .getString(JSONUtils.URL_KEY);
-                if (jsonObject.getJSONObject(JSONUtils.GFY_ITEM_KEY).has(JSONUtils.WEBM_URL_KEY)) {
-                    webm = jsonObject.getJSONObject(JSONUtils.GFY_ITEM_KEY).getString(JSONUtils.WEBM_URL_KEY);
-                } else if (jsonObject.getJSONObject(JSONUtils.GFY_ITEM_KEY).getJSONObject(JSONUtils.CONTENT_URLS_KEY).has(JSONUtils.WEBM_KEY)) {
-                    webm = jsonObject.getJSONObject(JSONUtils.GFY_ITEM_KEY)
-                            .getJSONObject(JSONUtils.CONTENT_URLS_KEY)
-                            .getJSONObject(JSONUtils.WEBM_KEY)
-                            .getString(JSONUtils.URL_KEY);
-                } else {
-                    webm = mp4;
-                }
-            } catch (JSONException e) {
-                e.printStackTrace();
-                parseFailed = true;
-            }
-
-            return null;
-        }
-
-        @Override
-        protected void onPostExecute(Void aVoid) {
-            super.onPostExecute(aVoid);
-            if (parseFailed) {
-                fetchGfycatOrRedgifsVideoLinksListener.failed(-1);
             } else {
-                fetchGfycatOrRedgifsVideoLinksListener.success(webm, mp4);
+                webm = mp4;
             }
+            handler.post(() -> fetchGfycatOrRedgifsVideoLinksListener.success(webm, mp4));
+        } catch (JSONException e) {
+            e.printStackTrace();
+            handler.post(() -> fetchGfycatOrRedgifsVideoLinksListener.failed(-1));
         }
     }
 }
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java
index 428a94da..c18fcc89 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java
@@ -390,7 +390,7 @@ public class ViewVideoActivity extends AppCompatActivity {
 
     private void loadGfycatOrRedgifsVideo(Retrofit retrofit, String gfycatId, Bundle savedInstanceState, boolean needErrorHandling) {
         progressBar.setVisibility(View.VISIBLE);
-        FetchGfycatOrRedgifsVideoLinks.fetchGfycatOrRedgifsVideoLinks(retrofit, gfycatId,
+        FetchGfycatOrRedgifsVideoLinks.fetchGfycatOrRedgifsVideoLinks(mExecutor, new Handler(), retrofit, gfycatId,
                 new FetchGfycatOrRedgifsVideoLinks.FetchGfycatOrRedgifsVideoLinksListener() {
                     @Override
                     public void success(String webm, String mp4) {
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 4334c42a..3cc27dc1 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java
@@ -696,8 +696,9 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler
                         }
                     });
                     ((PostDetailVideoAutoplayViewHolder) holder).fetchGfycatOrRedgifsVideoLinks
-                            .fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(mGfycatRetrofit, mRedgifsRetrofit,
-                                    mPost.getGfycatId(), mPost.isGfycat(), mAutomaticallyTryRedgifs);
+                            .fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(mExecutor, new Handler(),
+                                    mGfycatRetrofit, mRedgifsRetrofit, mPost.getGfycatId(),
+                                    mPost.isGfycat(), mAutomaticallyTryRedgifs);
                 } else {
                     ((PostDetailVideoAutoplayViewHolder) holder).bindVideoUri(Uri.parse(mPost.getVideoUrl()));
                 }
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java
index 408a6617..68c1d155 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java
@@ -31,7 +31,6 @@ import androidx.constraintlayout.widget.ConstraintLayout;
 import androidx.constraintlayout.widget.ConstraintSet;
 import androidx.core.graphics.drawable.DrawableCompat;
 import androidx.paging.PagedListAdapter;
-import androidx.paging.PagingDataAdapter;
 import androidx.recyclerview.widget.DiffUtil;
 import androidx.recyclerview.widget.ItemTouchHelper;
 import androidx.recyclerview.widget.RecyclerView;
@@ -732,8 +731,9 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
                             }
                         });
                         ((PostVideoAutoplayViewHolder) holder).fetchGfycatOrRedgifsVideoLinks
-                                .fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(mGfycatRetrofit, mRedgifsRetrofit,
-                                        post.getGfycatId(), post.isGfycat(), mAutomaticallyTryRedgifs);
+                                .fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(mExecutor, new Handler(),
+                                        mGfycatRetrofit, mRedgifsRetrofit, post.getGfycatId(),
+                                        post.isGfycat(), mAutomaticallyTryRedgifs);
                     } else {
                         ((PostVideoAutoplayViewHolder) holder).bindVideoUri(Uri.parse(post.getVideoUrl()));
                     }
@@ -855,8 +855,9 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
                             }
                         });
                         ((PostCard2VideoAutoplayViewHolder) holder).fetchGfycatOrRedgifsVideoLinks
-                                .fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(mGfycatRetrofit, mRedgifsRetrofit,
-                                        post.getGfycatId(), post.isGfycat(), mAutomaticallyTryRedgifs);
+                                .fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(mExecutor, new Handler(),
+                                        mGfycatRetrofit, mRedgifsRetrofit, post.getGfycatId(), post.isGfycat(),
+                                        mAutomaticallyTryRedgifs);
                     } else {
                         ((PostCard2VideoAutoplayViewHolder) holder).bindVideoUri(Uri.parse(post.getVideoUrl()));
                     }
-- 
cgit v1.2.3