1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-15 19:27:31 +00:00

replace DownloadCheckerAsyncTask, close #825

This commit is contained in:
Austin Huang 2021-05-20 15:21:00 -04:00
parent 447ef0d660
commit e726ba3ccf
No known key found for this signature in database
GPG Key ID: 84C23AA04587A91F
2 changed files with 23 additions and 68 deletions

View File

@ -19,11 +19,11 @@ import java.util.List;
import awais.instagrabber.R; import awais.instagrabber.R;
import awais.instagrabber.adapters.FeedAdapterV2; import awais.instagrabber.adapters.FeedAdapterV2;
import awais.instagrabber.asyncs.DownloadedCheckerAsyncTask;
import awais.instagrabber.databinding.ItemFeedGridBinding; import awais.instagrabber.databinding.ItemFeedGridBinding;
import awais.instagrabber.models.PostsLayoutPreferences; import awais.instagrabber.models.PostsLayoutPreferences;
import awais.instagrabber.repositories.responses.Media; import awais.instagrabber.repositories.responses.Media;
import awais.instagrabber.repositories.responses.User; import awais.instagrabber.repositories.responses.User;
import awais.instagrabber.utils.DownloadUtils;
import awais.instagrabber.utils.ResponseBodyUtils; import awais.instagrabber.utils.ResponseBodyUtils;
import awais.instagrabber.utils.TextUtils; import awais.instagrabber.utils.TextUtils;
@ -102,31 +102,28 @@ public class FeedGridItemViewHolder extends RecyclerView.ViewHolder {
binding.typeIcon.setVisibility(View.VISIBLE); binding.typeIcon.setVisibility(View.VISIBLE);
binding.typeIcon.setImageResource(typeIconRes); binding.typeIcon.setImageResource(typeIconRes);
} }
final DownloadedCheckerAsyncTask task = new DownloadedCheckerAsyncTask(result -> { final List<Boolean> checkList = DownloadUtils.checkDownloaded(media);
final List<Boolean> checkList = result.get(media.getPk()); if (checkList == null || checkList.isEmpty()) {
if (checkList == null || checkList.isEmpty()) { return;
return; }
} switch (media.getMediaType()) {
switch (media.getMediaType()) { case MEDIA_TYPE_IMAGE:
case MEDIA_TYPE_IMAGE: case MEDIA_TYPE_VIDEO:
case MEDIA_TYPE_VIDEO: binding.downloaded.setVisibility(checkList.get(0) ? View.VISIBLE : View.GONE);
binding.downloaded.setVisibility(checkList.get(0) ? View.VISIBLE : View.GONE); binding.downloaded.setImageTintList(ColorStateList.valueOf(itemView.getResources().getColor(R.color.green_A400)));
binding.downloaded.setImageTintList(ColorStateList.valueOf(itemView.getResources().getColor(R.color.green_A400))); break;
break; case MEDIA_TYPE_SLIDER:
case MEDIA_TYPE_SLIDER: binding.downloaded.setVisibility(checkList.get(0) ? View.VISIBLE : View.GONE);
binding.downloaded.setVisibility(checkList.get(0) ? View.VISIBLE : View.GONE); final List<Media> carouselMedia = media.getCarouselMedia();
final List<Media> carouselMedia = media.getCarouselMedia(); boolean allDownloaded = checkList.size() == (carouselMedia == null ? 0 : carouselMedia.size());
boolean allDownloaded = checkList.size() == (carouselMedia == null ? 0 : carouselMedia.size()); if (allDownloaded) {
if (allDownloaded) { allDownloaded = checkList.stream().allMatch(downloaded -> downloaded);
allDownloaded = checkList.stream().allMatch(downloaded -> downloaded); }
} binding.downloaded.setImageTintList(ColorStateList.valueOf(itemView.getResources().getColor(
binding.downloaded.setImageTintList(ColorStateList.valueOf(itemView.getResources().getColor( allDownloaded ? R.color.green_A400 : R.color.yellow_400)));
allDownloaded ? R.color.green_A400 : R.color.yellow_400))); break;
break; default:
default: }
}
});
task.execute(media);
} }
private void setThumbImage(final String thumbnailUrl) { private void setThumbImage(final String thumbnailUrl) {

View File

@ -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<Media, Void, Map<String, List<Boolean>>> {
private static final String TAG = "DownloadedCheckerAsyncTask";
private final OnCheckResultListener listener;
public DownloadedCheckerAsyncTask(final OnCheckResultListener listener) {
this.listener = listener;
}
@Override
protected Map<String, List<Boolean>> doInBackground(final Media... feedModels) {
if (feedModels == null) {
return null;
}
final Map<String, List<Boolean>> map = new HashMap<>();
for (final Media media : feedModels) {
map.put(media.getPk(), DownloadUtils.checkDownloaded(media));
}
return map;
}
@Override
protected void onPostExecute(final Map<String, List<Boolean>> result) {
if (listener == null) return;
listener.onResult(result);
}
public interface OnCheckResultListener {
void onResult(final Map<String, List<Boolean>> result);
}
}