Browse Source

Fix downloaded check logic

renovate/org.robolectric-robolectric-4.x
Ammar Githam 4 years ago
parent
commit
8030c7f220
  1. 2
      app/src/main/java/awais/instagrabber/adapters/viewholder/FeedGridItemViewHolder.java
  2. 11
      app/src/main/java/awais/instagrabber/asyncs/DownloadedCheckerAsyncTask.java
  3. 37
      app/src/main/java/awais/instagrabber/utils/DownloadUtils.java

2
app/src/main/java/awais/instagrabber/adapters/viewholder/FeedGridItemViewHolder.java

@ -102,7 +102,7 @@ public class FeedGridItemViewHolder extends RecyclerView.ViewHolder {
binding.typeIcon.setVisibility(View.VISIBLE);
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());
if (checkList == null || checkList.isEmpty()) {
return;

11
app/src/main/java/awais/instagrabber/asyncs/DownloadedCheckerAsyncTask.java

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

37
app/src/main/java/awais/instagrabber/utils/DownloadUtils.java

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

Loading…
Cancel
Save