From e726ba3ccfbc7d410ccc6301dcc1ec03a928c359 Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Thu, 20 May 2021 15:21:00 -0400 Subject: [PATCH] replace DownloadCheckerAsyncTask, close #825 --- .../viewholder/FeedGridItemViewHolder.java | 49 +++++++++---------- .../asyncs/DownloadedCheckerAsyncTask.java | 42 ---------------- 2 files changed, 23 insertions(+), 68 deletions(-) delete mode 100644 app/src/main/java/awais/instagrabber/asyncs/DownloadedCheckerAsyncTask.java diff --git a/app/src/main/java/awais/instagrabber/adapters/viewholder/FeedGridItemViewHolder.java b/app/src/main/java/awais/instagrabber/adapters/viewholder/FeedGridItemViewHolder.java index c6e1d571..d31abf8a 100644 --- a/app/src/main/java/awais/instagrabber/adapters/viewholder/FeedGridItemViewHolder.java +++ b/app/src/main/java/awais/instagrabber/adapters/viewholder/FeedGridItemViewHolder.java @@ -19,11 +19,11 @@ import java.util.List; import awais.instagrabber.R; import awais.instagrabber.adapters.FeedAdapterV2; -import awais.instagrabber.asyncs.DownloadedCheckerAsyncTask; import awais.instagrabber.databinding.ItemFeedGridBinding; import awais.instagrabber.models.PostsLayoutPreferences; import awais.instagrabber.repositories.responses.Media; import awais.instagrabber.repositories.responses.User; +import awais.instagrabber.utils.DownloadUtils; import awais.instagrabber.utils.ResponseBodyUtils; import awais.instagrabber.utils.TextUtils; @@ -102,31 +102,28 @@ public class FeedGridItemViewHolder extends RecyclerView.ViewHolder { binding.typeIcon.setVisibility(View.VISIBLE); binding.typeIcon.setImageResource(typeIconRes); } - final DownloadedCheckerAsyncTask task = new DownloadedCheckerAsyncTask(result -> { - final List checkList = result.get(media.getPk()); - if (checkList == null || checkList.isEmpty()) { - return; - } - switch (media.getMediaType()) { - case MEDIA_TYPE_IMAGE: - case MEDIA_TYPE_VIDEO: - binding.downloaded.setVisibility(checkList.get(0) ? View.VISIBLE : View.GONE); - binding.downloaded.setImageTintList(ColorStateList.valueOf(itemView.getResources().getColor(R.color.green_A400))); - break; - case MEDIA_TYPE_SLIDER: - binding.downloaded.setVisibility(checkList.get(0) ? View.VISIBLE : View.GONE); - final List carouselMedia = media.getCarouselMedia(); - boolean allDownloaded = checkList.size() == (carouselMedia == null ? 0 : carouselMedia.size()); - if (allDownloaded) { - allDownloaded = checkList.stream().allMatch(downloaded -> downloaded); - } - binding.downloaded.setImageTintList(ColorStateList.valueOf(itemView.getResources().getColor( - allDownloaded ? R.color.green_A400 : R.color.yellow_400))); - break; - default: - } - }); - task.execute(media); + final List checkList = DownloadUtils.checkDownloaded(media); + if (checkList == null || checkList.isEmpty()) { + return; + } + switch (media.getMediaType()) { + case MEDIA_TYPE_IMAGE: + case MEDIA_TYPE_VIDEO: + binding.downloaded.setVisibility(checkList.get(0) ? View.VISIBLE : View.GONE); + binding.downloaded.setImageTintList(ColorStateList.valueOf(itemView.getResources().getColor(R.color.green_A400))); + break; + case MEDIA_TYPE_SLIDER: + binding.downloaded.setVisibility(checkList.get(0) ? View.VISIBLE : View.GONE); + final List carouselMedia = media.getCarouselMedia(); + boolean allDownloaded = checkList.size() == (carouselMedia == null ? 0 : carouselMedia.size()); + if (allDownloaded) { + allDownloaded = checkList.stream().allMatch(downloaded -> downloaded); + } + binding.downloaded.setImageTintList(ColorStateList.valueOf(itemView.getResources().getColor( + allDownloaded ? R.color.green_A400 : R.color.yellow_400))); + break; + default: + } } private void setThumbImage(final String thumbnailUrl) { diff --git a/app/src/main/java/awais/instagrabber/asyncs/DownloadedCheckerAsyncTask.java b/app/src/main/java/awais/instagrabber/asyncs/DownloadedCheckerAsyncTask.java deleted file mode 100644 index 605bed3f..00000000 --- a/app/src/main/java/awais/instagrabber/asyncs/DownloadedCheckerAsyncTask.java +++ /dev/null @@ -1,42 +0,0 @@ -package awais.instagrabber.asyncs; - -import android.os.AsyncTask; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import awais.instagrabber.repositories.responses.Media; -import awais.instagrabber.utils.DownloadUtils; - -public final class DownloadedCheckerAsyncTask extends AsyncTask>> { - private static final String TAG = "DownloadedCheckerAsyncTask"; - - private final OnCheckResultListener listener; - - public DownloadedCheckerAsyncTask(final OnCheckResultListener listener) { - this.listener = listener; - } - - @Override - protected Map> doInBackground(final Media... feedModels) { - if (feedModels == null) { - return null; - } - final Map> map = new HashMap<>(); - for (final Media media : feedModels) { - map.put(media.getPk(), DownloadUtils.checkDownloaded(media)); - } - return map; - } - - @Override - protected void onPostExecute(final Map> result) { - if (listener == null) return; - listener.onResult(result); - } - - public interface OnCheckResultListener { - void onResult(final Map> result); - } -}