1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-22 22:57:29 +00:00

Fix downloaded check logic

This commit is contained in:
Ammar Githam 2021-04-05 21:01:33 +09:00
parent 9b691a453e
commit 8030c7f220
3 changed files with 24 additions and 26 deletions

View File

@ -102,7 +102,7 @@ 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(itemView.getContext(), result -> { final DownloadedCheckerAsyncTask task = new DownloadedCheckerAsyncTask(result -> {
final List<Boolean> checkList = result.get(media.getPk()); final List<Boolean> checkList = result.get(media.getPk());
if (checkList == null || checkList.isEmpty()) { if (checkList == null || checkList.isEmpty()) {
return; return;

View File

@ -1,9 +1,7 @@
package awais.instagrabber.asyncs; package awais.instagrabber.asyncs;
import android.content.Context;
import android.os.AsyncTask; import android.os.AsyncTask;
import java.lang.ref.WeakReference;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -14,12 +12,9 @@ import awais.instagrabber.utils.DownloadUtils;
public final class DownloadedCheckerAsyncTask extends AsyncTask<Media, Void, Map<String, List<Boolean>>> { public final class DownloadedCheckerAsyncTask extends AsyncTask<Media, Void, Map<String, List<Boolean>>> {
private static final String TAG = "DownloadedCheckerAsyncTask"; private static final String TAG = "DownloadedCheckerAsyncTask";
private final WeakReference<Context> context;
private final OnCheckResultListener listener; private final OnCheckResultListener listener;
public DownloadedCheckerAsyncTask(final Context context, public DownloadedCheckerAsyncTask(final OnCheckResultListener listener) {
final OnCheckResultListener listener) {
this.context = new WeakReference<>(context);
this.listener = listener; this.listener = listener;
} }
@ -30,9 +25,7 @@ public final class DownloadedCheckerAsyncTask extends AsyncTask<Media, Void, Map
} }
final Map<String, List<Boolean>> map = new HashMap<>(); final Map<String, List<Boolean>> map = new HashMap<>();
for (final Media media : feedModels) { for (final Media media : feedModels) {
final Context context = this.context.get(); map.put(media.getPk(), DownloadUtils.checkDownloaded(media));
if (context == null) return map;
map.put(media.getPk(), DownloadUtils.checkDownloaded(context, media));
} }
return map; return map;
} }

View File

@ -5,7 +5,6 @@ import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.UriPermission; import android.content.UriPermission;
import android.net.Uri; import android.net.Uri;
import android.provider.DocumentsContract;
import android.util.Log; import android.util.Log;
import android.webkit.MimeTypeMap; import android.webkit.MimeTypeMap;
import android.widget.Toast; import android.widget.Toast;
@ -338,8 +337,7 @@ public final class DownloadUtils {
return ""; return "";
} }
public static List<Boolean> checkDownloaded(@NonNull final Context context, public static List<Boolean> checkDownloaded(@NonNull final Media media) {
@NonNull final Media media) {
final List<Boolean> checkList = new LinkedList<>(); final List<Boolean> checkList = new LinkedList<>();
final User user = media.getUser(); final User user = media.getUser();
String username = "username"; String username = "username";
@ -351,9 +349,10 @@ public final class DownloadUtils {
case MEDIA_TYPE_IMAGE: case MEDIA_TYPE_IMAGE:
case MEDIA_TYPE_VIDEO: { case MEDIA_TYPE_VIDEO: {
final String url = ResponseBodyUtils.getImageUrl(media); final String url = ResponseBodyUtils.getImageUrl(media);
final Pair<List<String>, String> file = getDownloadSavePaths(userFolderPaths, media.getCode(), url, ""); final Pair<List<String>, String> file = getDownloadSavePaths(new ArrayList<>(userFolderPaths), media.getCode(), url, "");
final Pair<List<String>, String> usernameFile = getDownloadSavePaths(userFolderPaths, media.getCode(), url, username); final Pair<List<String>, String> usernameFile = getDownloadSavePaths(new ArrayList<>(userFolderPaths), media.getCode(), url,
checkList.add(checkPathExists(context, file.first) || checkPathExists(context, usernameFile.first)); username);
checkList.add(checkPathExists(file.first) || checkPathExists(usernameFile.first));
break; break;
} }
case MEDIA_TYPE_SLIDER: case MEDIA_TYPE_SLIDER:
@ -362,9 +361,11 @@ public final class DownloadUtils {
final Media child = sliderItems.get(i); final Media child = sliderItems.get(i);
if (child == null) continue; if (child == null) continue;
final String url = ResponseBodyUtils.getImageUrl(child); final String url = ResponseBodyUtils.getImageUrl(child);
final Pair<List<String>, String> file = getDownloadChildSavePaths(userFolderPaths, media.getCode(), i + 1, url, ""); final Pair<List<String>, String> file = getDownloadChildSavePaths(new ArrayList<>(userFolderPaths), media.getCode(), i + 1, url,
final Pair<List<String>, String> usernameFile = getDownloadChildSavePaths(userFolderPaths, media.getCode(), i + 1, url, username); "");
checkList.add(checkPathExists(context, file.first) || checkPathExists(context, usernameFile.first)); final Pair<List<String>, String> usernameFile = getDownloadChildSavePaths(new ArrayList<>(userFolderPaths), media.getCode(),
i + 1, url, username);
checkList.add(checkPathExists(file.first) || checkPathExists(usernameFile.first));
} }
break; break;
default: default:
@ -372,13 +373,16 @@ public final class DownloadUtils {
return checkList; return checkList;
} }
private static boolean checkPathExists(@NonNull final Context context, private static boolean checkPathExists(@NonNull final List<String> paths) {
@NonNull final List<String> paths) {
if (root == null) return false; if (root == null) return false;
final String joined = android.text.TextUtils.join("/", paths); DocumentFile dir = root;
final Uri userFolderUri = DocumentsContract.buildDocumentUriUsingTree(root.getUri(), joined); for (final String path : paths) {
final DocumentFile userFolder = DocumentFile.fromSingleUri(context, userFolderUri); dir = dir.findFile(path);
return userFolder != null && userFolder.exists(); if (dir == null || !dir.exists()) {
return false;
}
}
return true;
} }
public static void showDownloadDialog(@NonNull Context context, public static void showDownloadDialog(@NonNull Context context,
@ -501,7 +505,8 @@ public final class DownloadUtils {
final String usernamePrepend = Utils.settingsHelper.getBoolean(Constants.DOWNLOAD_PREPEND_USER_NAME) && mediaUser != null final String usernamePrepend = Utils.settingsHelper.getBoolean(Constants.DOWNLOAD_PREPEND_USER_NAME) && mediaUser != null
? mediaUser.getUsername() ? mediaUser.getUsername()
: ""; : "";
final Pair<List<String>, String> pair = getDownloadChildSavePaths(userFolderPaths, media.getCode(), i + 1, url, final Pair<List<String>, String> pair = getDownloadChildSavePaths(new ArrayList<>(userFolderPaths), media.getCode(), i + 1,
url,
usernamePrepend); usernamePrepend);
final DocumentFile file = createFile(pair); final DocumentFile file = createFile(pair);
if (file == null) continue; if (file == null) continue;