From 4579f2b37ebd31a27dc06898d6d29fee2ce1a290 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Tue, 3 Nov 2020 19:01:59 +0900 Subject: [PATCH] Add download dialog --- .../instagrabber/adapters/FeedAdapterV2.java | 2 +- .../adapters/FeedItemCallbackAdapter.java | 2 +- .../viewholder/feed/FeedItemViewHolder.java | 5 +- .../viewholder/feed/FeedSliderViewHolder.java | 83 ++--- .../asyncs/DownloadV2AsyncTask.java | 310 ------------------ .../dialogs/ProfilePicDialogFragment.java | 48 +-- .../fragments/HashTagFragment.java | 55 +--- .../fragments/LocationFragment.java | 56 +--- .../fragments/PostViewV2Fragment.java | 46 +-- .../fragments/SavedViewerFragment.java | 56 +--- .../fragments/TopicPostsFragment.java | 56 +--- .../fragments/main/FeedFragment.java | 56 +--- .../fragments/main/ProfileFragment.java | 83 +---- .../instagrabber/utils/DownloadUtils.java | 48 ++- 14 files changed, 145 insertions(+), 761 deletions(-) delete mode 100644 app/src/main/java/awais/instagrabber/asyncs/DownloadV2AsyncTask.java diff --git a/app/src/main/java/awais/instagrabber/adapters/FeedAdapterV2.java b/app/src/main/java/awais/instagrabber/adapters/FeedAdapterV2.java index 0d17f17a..f8e70c04 100644 --- a/app/src/main/java/awais/instagrabber/adapters/FeedAdapterV2.java +++ b/app/src/main/java/awais/instagrabber/adapters/FeedAdapterV2.java @@ -216,7 +216,7 @@ public final class FeedAdapterV2 extends ListAdapter feedItemCallback.onDownloadClick(feedModel)); + if (feedModel.getItemType() != MediaItemType.MEDIA_TYPE_SLIDER) { + bottomBinding.btnDownload.setOnClickListener(v -> feedItemCallback.onDownloadClick(feedModel, -1)); + } bindItem(feedModel); } diff --git a/app/src/main/java/awais/instagrabber/adapters/viewholder/feed/FeedSliderViewHolder.java b/app/src/main/java/awais/instagrabber/adapters/viewholder/feed/FeedSliderViewHolder.java index 2b71cd7c..629d0c67 100644 --- a/app/src/main/java/awais/instagrabber/adapters/viewholder/feed/FeedSliderViewHolder.java +++ b/app/src/main/java/awais/instagrabber/adapters/viewholder/feed/FeedSliderViewHolder.java @@ -27,7 +27,6 @@ import awais.instagrabber.adapters.SliderItemsAdapter; import awais.instagrabber.databinding.ItemFeedSliderBinding; import awais.instagrabber.models.FeedModel; import awais.instagrabber.models.PostChild; -import awais.instagrabber.models.enums.MediaItemType; import awais.instagrabber.utils.Constants; import awais.instagrabber.utils.NumberUtils; import awais.instagrabber.utils.Utils; @@ -86,66 +85,36 @@ public class FeedSliderViewHolder extends FeedItemViewHolder { final String text = (position + 1) + "/" + sliderItemLen; binding.mediaCounter.setText(text); setDimensions(binding.mediaList, sliderItems.get(position)); + binding.itemFeedBottom.btnDownload.setOnClickListener(v -> feedItemCallback.onDownloadClick(feedModel, position)); } }); setDimensions(binding.mediaList, sliderItems.get(0)); - - // binding.mediaList.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { - // private int prevPos = 0; - // - // @Override - // public void onPageSelected(final int position) { - // ViewerPostModel sliderItem = sliderItems.get(prevPos); - // if (sliderItem != null) { - // sliderItem.setSelected(false); - // if (sliderItem.getItemType() == MediaItemType.MEDIA_TYPE_VIDEO) { - // // stop playing prev video - // final ViewSwitcher prevChild = (ViewSwitcher) binding.mediaList.getChildAt(prevPos); - // if (prevChild == null || prevChild.getTag() == null || !(prevChild.getTag() instanceof SimpleExoPlayer)) { - // return; - // } - // ((SimpleExoPlayer) prevChild.getTag()).setPlayWhenReady(false); - // } - // } - // sliderItem = sliderItems.get(position); - // if (sliderItem == null) return; - // sliderItem.setSelected(true); - // final String text = (position + 1) + "/" + sliderItemLen; - // binding.mediaCounter.setText(text); - // prevPos = position; - // if (sliderItem.getItemType() == MediaItemType.MEDIA_TYPE_VIDEO) { - // binding.itemFeedBottom.btnMute.setVisibility(View.VISIBLE); - // if (shouldAutoPlay) { - // autoPlay(position); - // } - // } else binding.itemFeedBottom.btnMute.setVisibility(View.GONE); - // } - // }); + binding.itemFeedBottom.btnDownload.setOnClickListener(v -> feedItemCallback.onDownloadClick(feedModel, 0)); adapter.submitList(sliderItems); - final View.OnClickListener muteClickListener = v -> { - final int currentItem = binding.mediaList.getCurrentItem(); - if (currentItem < 0 || currentItem >= binding.mediaList.getChildCount()) { - return; - } - final PostChild sliderItem = sliderItems.get(currentItem); - if (sliderItem.getItemType() != MediaItemType.MEDIA_TYPE_VIDEO) { - return; - } - final View currentView = binding.mediaList.getChildAt(currentItem); - if (!(currentView instanceof ViewSwitcher)) { - return; - } - final ViewSwitcher viewSwitcher = (ViewSwitcher) currentView; - final Object tag = viewSwitcher.getTag(); - if (!(tag instanceof SimpleExoPlayer)) { - return; - } - final SimpleExoPlayer player = (SimpleExoPlayer) tag; - final float intVol = player.getVolume() == 0f ? 1f : 0f; - player.setVolume(intVol); - // binding.itemFeedBottom.btnMute.setImageResource(intVol == 0f ? R.drawable.ic_volume_up_24 : R.drawable.ic_volume_off_24); - // Utils.sessionVolumeFull = intVol == 1f; - }; + // final View.OnClickListener muteClickListener = v -> { + // final int currentItem = binding.mediaList.getCurrentItem(); + // if (currentItem < 0 || currentItem >= binding.mediaList.getChildCount()) { + // return; + // } + // final PostChild sliderItem = sliderItems.get(currentItem); + // if (sliderItem.getItemType() != MediaItemType.MEDIA_TYPE_VIDEO) { + // return; + // } + // final View currentView = binding.mediaList.getChildAt(currentItem); + // if (!(currentView instanceof ViewSwitcher)) { + // return; + // } + // final ViewSwitcher viewSwitcher = (ViewSwitcher) currentView; + // final Object tag = viewSwitcher.getTag(); + // if (!(tag instanceof SimpleExoPlayer)) { + // return; + // } + // final SimpleExoPlayer player = (SimpleExoPlayer) tag; + // final float intVol = player.getVolume() == 0f ? 1f : 0f; + // player.setVolume(intVol); + // // binding.itemFeedBottom.btnMute.setImageResource(intVol == 0f ? R.drawable.ic_volume_up_24 : R.drawable.ic_volume_off_24); + // // Utils.sessionVolumeFull = intVol == 1f; + // }; // final PostChild firstItem = sliderItems.get(0); // if (firstItem.getItemType() == MediaItemType.MEDIA_TYPE_VIDEO) { // binding.itemFeedBottom.btnMute.setVisibility(View.VISIBLE); diff --git a/app/src/main/java/awais/instagrabber/asyncs/DownloadV2AsyncTask.java b/app/src/main/java/awais/instagrabber/asyncs/DownloadV2AsyncTask.java deleted file mode 100644 index 8f2ac17a..00000000 --- a/app/src/main/java/awais/instagrabber/asyncs/DownloadV2AsyncTask.java +++ /dev/null @@ -1,310 +0,0 @@ -// package awais.instagrabber.asyncs; -// -// import android.content.ContentResolver; -// import android.content.Context; -// import android.content.Intent; -// import android.graphics.Bitmap; -// import android.graphics.BitmapFactory; -// import android.media.MediaMetadataRetriever; -// import android.media.MediaScannerConnection; -// import android.net.Uri; -// import android.os.AsyncTask; -// import android.os.Build; -// import android.util.Log; -// -// import androidx.annotation.NonNull; -// import androidx.annotation.Nullable; -// import androidx.core.content.FileProvider; -// -// import java.io.BufferedInputStream; -// import java.io.File; -// import java.io.FileOutputStream; -// import java.io.InputStream; -// import java.net.URL; -// import java.net.URLConnection; -// import java.util.HashMap; -// import java.util.Map; -// -// import awais.instagrabber.BuildConfig; -// import awais.instagrabber.R; -// import awais.instagrabber.utils.Utils; -// -// import static awais.instagrabber.utils.Utils.logCollector; -// import static awaisomereport.LogCollector.LogFile; -// -// public final class DownloadV2AsyncTask extends AsyncTask { -// private static final String TAG = "DownloadAsync"; -// -// // private final int currentNotificationId; -// // private final int initialNotificationId; -// // private final File outFile; -// // private final String url; -// // private final FetchListener fetchListener; -// // private final NotificationCompat.Builder downloadNotif; -// private String shortCode, username; -// // private final NotificationManagerCompat notificationManager; -// -// public DownloadV2AsyncTask(@NonNull final Context context) { -// // this.shortCode = this.username = resources.getString(R.string.downloader_started); -// -// // @StringRes final int titleRes = R.string.downloader_downloading_post; -// // downloadNotif = new NotificationCompat.Builder(context, Constants.DOWNLOAD_CHANNEL_ID) -// // .setCategory(NotificationCompat.CATEGORY_STATUS) -// // .setSmallIcon(R.mipmap.ic_launcher) -// // .setContentText(shortCode == null ? username : shortCode) -// // .setOngoing(true) -// // .setProgress(100, 0, false) -// // .setAutoCancel(false) -// // .setOnlyAlertOnce(true) -// // .setContentTitle(resources.getString(titleRes)); -// // -// // notificationManager = NotificationManagerCompat.from(context.getApplicationContext()); -// // notificationManager.notify(currentNotificationId, downloadNotif.build()); -// } -// -// // public DownloadV2AsyncTask setItems(final String shortCode, final String username) { -// // this.shortCode = shortCode; -// // this.username = username; -// // if (downloadNotif != null) downloadNotif.setContentText(this.shortCode == null ? this.username : this.shortCode); -// // return this; -// // } -// -// @Nullable -// @Override -// protected File doInBackground(final Void... voids) { -// try { -// final URLConnection urlConnection = new URL(url).openConnection(); -// final long fileSize = Build.VERSION.SDK_INT >= 24 ? urlConnection.getContentLengthLong() : -// urlConnection.getContentLength(); -// float totalRead = 0; -// -// try (final BufferedInputStream bis = new BufferedInputStream(urlConnection.getInputStream()); -// final FileOutputStream fos = new FileOutputStream(outFile)) { -// final byte[] buffer = new byte[0x2000]; -// -// int count; -// boolean deletedIPTC = false; -// while ((count = bis.read(buffer, 0, 0x2000)) != -1) { -// totalRead = totalRead + count; -// -// if (!deletedIPTC) { -// int iptcStart = -1; -// int fbmdStart = -1; -// int fbmdBytesLen = -1; -// -// for (int i = 0; i < buffer.length; ++i) { -// if (buffer[i] == (byte) 0xFF && buffer[i + 1] == (byte) 0xED) -// iptcStart = i; -// else if (buffer[i] == (byte) 'F' && buffer[i + 1] == (byte) 'B' -// && buffer[i + 2] == (byte) 'M' && buffer[i + 3] == (byte) 'D') { -// fbmdStart = i; -// fbmdBytesLen = buffer[i - 10] << 24 | (buffer[i - 9] & 0xFF) << 16 | -// (buffer[i - 8] & 0xFF) << 8 | (buffer[i - 7] & 0xFF) | -// (buffer[i - 6] & 0xFF); -// break; -// } -// } -// -// if (iptcStart != -1 && fbmdStart != -1 && fbmdBytesLen != -1) { -// final int fbmdDataLen = (iptcStart + (fbmdStart - iptcStart) + (fbmdBytesLen - iptcStart)) - 4; -// -// fos.write(buffer, 0, iptcStart); -// fos.write(buffer, fbmdDataLen + iptcStart, count - fbmdDataLen - iptcStart); -// -// publishProgress(totalRead * 100f / fileSize); -// -// deletedIPTC = true; -// continue; -// } -// } -// -// fos.write(buffer, 0, count); -// publishProgress(totalRead * 100f / fileSize); -// } -// fos.flush(); -// } -// -// return outFile; -// } catch (final Exception e) { -// // if (logCollector != null) -// // logCollector.appendException(e, LogFile.ASYNC_DOWNLOADER, "doInBackground", -// // new Pair<>("context", context.get()), -// // new Pair<>("resources", resources), -// // new Pair<>("lastNotifId", initialNotificationId), -// // new Pair<>("downloadNotif", downloadNotif), -// // new Pair<>("currentNotifId", currentNotificationId), -// // new Pair<>("notificationManager", notificationManager)); -// if (BuildConfig.DEBUG) Log.e(TAG, "", e); -// } -// return null; -// } -// -// @Override -// protected void onPreExecute() { -// // if (fetchListener != null) fetchListener.doBefore(); -// } -// -// @Override -// protected void onProgressUpdate(@NonNull final Float... values) { -// // if (downloadNotif != null) { -// // downloadNotif.setProgress(100, values[0].intValue(), false); -// // notificationManager.notify(currentNotificationId, downloadNotif.build()); -// // } -// } -// -// @Override -// protected void onPostExecute(final File result) { -// if (result != null) { -// // final Context context = this.context.get(); -// // context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(result.getAbsoluteFile()))); -// // MediaScannerConnection.scanFile(context, new String[]{result.getAbsolutePath()}, null, null); -// // -// // // if (notificationManager != null) { -// // final Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", result); -// // -// // final ContentResolver contentResolver = context.getContentResolver(); -// // Bitmap bitmap = null; -// // if (Utils.isImage(uri, contentResolver)) { -// // try (final InputStream inputStream = contentResolver.openInputStream(uri)) { -// // bitmap = BitmapFactory.decodeStream(inputStream); -// // } catch (final Exception e) { -// // if (logCollector != null) -// // logCollector.appendException(e, LogFile.ASYNC_DOWNLOADER, "onPostExecute::bitmap_1"); -// // if (BuildConfig.DEBUG) Log.e(TAG, "", e); -// // } -// // } -// // -// // if (bitmap == null) { -// // final MediaMetadataRetriever retriever = new MediaMetadataRetriever(); -// // try { -// // try { -// // retriever.setDataSource(context, uri); -// // } catch (final Exception e) { -// // retriever.setDataSource(result.getAbsolutePath()); -// // } -// // bitmap = retriever.getFrameAtTime(); -// // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) -// // try { -// // retriever.close(); -// // } catch (final Exception e) { -// // if (logCollector != null) -// // logCollector.appendException(e, LogFile.ASYNC_DOWNLOADER, "onPostExecute::bitmap_2"); -// // } -// // } catch (final Exception e) { -// // if (BuildConfig.DEBUG) Log.e("AWAISKING_APP", "", e); -// // if (logCollector != null) -// // logCollector.appendException(e, LogFile.ASYNC_DOWNLOADER, "onPostExecute::bitmap_3"); -// // } -// // } -// -// // final String downloadComplete = resources.getString(R.string.downloader_complete); -// -// // downloadNotif.setContentText(null).setContentTitle(downloadComplete).setProgress(0, 0, false) -// // .setWhen(System.currentTimeMillis()).setOngoing(false).setOnlyAlertOnce(false).setAutoCancel(true) -// // .setGroup(NOTIF_GROUP_NAME).setGroupSummary(true).setContentIntent( -// // PendingIntent.getActivity(context, 2020, new Intent(Intent.ACTION_VIEW, uri) -// // .addFlags( -// // Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_FROM_BACKGROUND | Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION) -// // .putExtra(Intent.EXTRA_STREAM, uri), PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT)); -// // -// // if (bitmap != null) -// // downloadNotif.setStyle(new NotificationCompat.BigPictureStyle().setBigContentTitle(downloadComplete).bigPicture(bitmap)) -// // .setLargeIcon(bitmap).setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL); -// // -// // notificationManager.cancel(currentNotificationId); -// // notificationManager.notify(currentNotificationId + 1, downloadNotif.build()); -// // } -// } -// -// // if (fetchListener != null) fetchListener.onResult(result); -// } -// -// public static class DownloadOptions { -// private final Map urlToFileMap; -// private final Map callbackMap; -// private final boolean showNotification; -// -// public static class Builder { -// private Map urlToFileMap; -// private Map callbackMap; -// private boolean showNotification; -// -// public Builder setUrlToFileMap(@NonNull final Map urlToFileMap) { -// this.urlToFileMap = urlToFileMap; -// return this; -// } -// -// public Builder setCallbackMap(@NonNull final Map callbackMap) { -// this.callbackMap = callbackMap; -// return this; -// } -// -// public Builder addUrl(@NonNull final String url, @NonNull final File file) { -// if (urlToFileMap == null) { -// urlToFileMap = new HashMap<>(); -// } -// urlToFileMap.put(url, file); -// return this; -// } -// -// public Builder addUrl(@NonNull final String url, -// @NonNull final File file, -// @NonNull final DownloadCallback callback) { -// if (urlToFileMap == null) { -// urlToFileMap = new HashMap<>(); -// } -// if (callbackMap == null) { -// callbackMap = new HashMap<>(); -// } -// urlToFileMap.put(url, file); -// callbackMap.put(url, callback); -// return this; -// } -// -// public Builder setShowNotification(final boolean showNotification) { -// this.showNotification = showNotification; -// return this; -// } -// -// public DownloadOptions build() { -// return new DownloadOptions( -// urlToFileMap, -// callbackMap, -// showNotification -// ); -// } -// } -// -// public Builder builder() { -// return new Builder(); -// } -// -// private DownloadOptions(final Map urlToFileMap, -// final Map callbackMap, -// final boolean showNotification) { -// this.urlToFileMap = urlToFileMap; -// this.callbackMap = callbackMap; -// this.showNotification = showNotification; -// } -// -// public Map getUrlToFileMap() { -// return urlToFileMap; -// } -// -// public Map getCallbackMap() { -// return callbackMap; -// } -// -// public boolean isShowNotification() { -// return showNotification; -// } -// } -// -// public interface DownloadCallback { -// void onDownloadStart(); -// -// void onDownloadProgress(); -// -// void onDownloadComplete(); -// } -// } \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/dialogs/ProfilePicDialogFragment.java b/app/src/main/java/awais/instagrabber/dialogs/ProfilePicDialogFragment.java index 4ea3eed4..6a468cf3 100644 --- a/app/src/main/java/awais/instagrabber/dialogs/ProfilePicDialogFragment.java +++ b/app/src/main/java/awais/instagrabber/dialogs/ProfilePicDialogFragment.java @@ -28,7 +28,6 @@ import com.facebook.imagepipeline.image.ImageInfo; import java.io.File; import awais.instagrabber.R; -import awais.instagrabber.asyncs.DownloadAsync; import awais.instagrabber.asyncs.ProfilePictureFetcher; import awais.instagrabber.databinding.DialogProfilepicBinding; import awais.instagrabber.interfaces.FetchListener; @@ -147,54 +146,9 @@ public class ProfilePicDialogFragment extends DialogFragment { if (context == null) return; if (dir.exists() || dir.mkdirs()) { final File saveFile = new File(dir, name + '_' + System.currentTimeMillis() + ".jpg"); - new DownloadAsync(context, - url, - saveFile, - result -> { - final int toastRes = result != null && result.exists() ? - R.string.downloader_downloaded_in_folder : R.string.downloader_error_download_file; - Toast.makeText(context, toastRes, Toast.LENGTH_SHORT).show(); - }) - .setItems(null, name) - .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + DownloadUtils.download(context, url, saveFile.getAbsolutePath()); return; } Toast.makeText(context, R.string.downloader_error_creating_folder, Toast.LENGTH_SHORT).show(); } - - // private void showImageInfo() { - // final Drawable drawable = profileBinding.imageViewer.getDrawable(); - // if (drawable != null) { - // final StringBuilder info = new StringBuilder( - // getString(R.string.profile_viewer_imageinfo, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight())); - // if (drawable instanceof BitmapDrawable) { - // final Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap(); - // if (bitmap != null) { - // final String colorDepthPrefix = getString(R.string.profile_viewer_colordepth_prefix); - // switch (bitmap.getConfig()) { - // case ALPHA_8: - // info.append(colorDepthPrefix).append(" 8-bits(A)"); - // break; - // case RGB_565: - // info.append(colorDepthPrefix).append(" 16-bits-A"); - // break; - // case ARGB_4444: - // info.append(colorDepthPrefix).append(" 16-bits+A"); - // break; - // case ARGB_8888: - // info.append(colorDepthPrefix).append(" 32-bits+A"); - // break; - // case RGBA_F16: - // info.append(colorDepthPrefix).append(" 64-bits+A"); - // break; - // case HARDWARE: - // info.append(colorDepthPrefix).append(" auto"); - // break; - // } - // } - // } - // profileBinding.imageInfo.setText(info); - // profileBinding.imageInfo.setVisibility(View.VISIBLE); - // } - // } } diff --git a/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java b/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java index 05015e3a..721940df 100644 --- a/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java @@ -91,6 +91,7 @@ public class HashTagFragment extends Fragment implements SwipeRefreshLayout.OnRe private boolean storiesFetching; private Set selectedFeedModels; private FeedModel downloadFeedModel; + private int downloadChildPosition = -1; private final OnBackPressedCallback onBackPressedCallback = new OnBackPressedCallback(false) { @Override @@ -145,14 +146,15 @@ public class HashTagFragment extends Fragment implements SwipeRefreshLayout.OnRe } @Override - public void onDownloadClick(final FeedModel feedModel) { + public void onDownloadClick(final FeedModel feedModel, final int childPosition) { final Context context = getContext(); if (context == null) return; if (checkSelfPermission(context, WRITE_PERMISSION) == PermissionChecker.PERMISSION_GRANTED) { - showDownloadDialog(feedModel); + DownloadUtils.showDownloadDialog(context, feedModel, childPosition); return; } downloadFeedModel = feedModel; + downloadChildPosition = childPosition; requestPermissions(DownloadUtils.PERMS, STORAGE_PERM_REQUEST_CODE); } @@ -304,15 +306,16 @@ public class HashTagFragment extends Fragment implements SwipeRefreshLayout.OnRe public void onRequestPermissionsResult(final int requestCode, @NonNull final String[] permissions, @NonNull final int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); final boolean granted = grantResults[0] == PackageManager.PERMISSION_GRANTED; + final Context context = getContext(); + if (context == null) return; if (requestCode == STORAGE_PERM_REQUEST_CODE && granted) { if (downloadFeedModel == null) return; - showDownloadDialog(downloadFeedModel); + DownloadUtils.showDownloadDialog(context, downloadFeedModel, downloadChildPosition); downloadFeedModel = null; + downloadChildPosition = -1; return; } if (requestCode == STORAGE_PERM_REQUEST_CODE_FOR_SELECTION && granted) { - final Context context = getContext(); - if (context == null) return; DownloadUtils.download(context, ImmutableList.copyOf(selectedFeedModels)); binding.posts.endSelection(); } @@ -529,48 +532,6 @@ public class HashTagFragment extends Fragment implements SwipeRefreshLayout.OnRe binding.swipeRefreshLayout.setRefreshing(binding.posts.isFetching() || storiesFetching); } - private void showDownloadDialog(final FeedModel feedModel) { - final Context context = getContext(); - if (context == null) return; - DownloadUtils.download(context, feedModel); - // switch (feedModel.getItemType()) { - // case MEDIA_TYPE_IMAGE: - // case MEDIA_TYPE_VIDEO: - // break; - // case MEDIA_TYPE_SLIDER: - // break; - // } - // final List postModelsToDownload = new ArrayList<>(); - // // if (!session) { - // final DialogInterface.OnClickListener clickListener = (dialog, which) -> { - // if (which == DialogInterface.BUTTON_NEGATIVE) { - // postModelsToDownload.addAll(postModels); - // } else if (which == DialogInterface.BUTTON_POSITIVE) { - // postModelsToDownload.add(postModels.get(childPosition)); - // } else { - // session = true; - // postModelsToDownload.add(postModels.get(childPosition)); - // } - // if (postModelsToDownload.size() > 0) { - // DownloadUtils.batchDownload(context, - // username, - // DownloadMethod.DOWNLOAD_POST_VIEWER, - // postModelsToDownload); - // } - // }; - // new AlertDialog.Builder(context) - // .setTitle(R.string.post_viewer_download_dialog_title) - // .setMessage(R.string.post_viewer_download_message) - // .setNeutralButton(R.string.post_viewer_download_session, clickListener) - // .setPositiveButton(R.string.post_viewer_download_current, clickListener) - // .setNegativeButton(R.string.post_viewer_download_album, clickListener).show(); - // } else { - // DownloadUtils.batchDownload(context, - // username, - // DownloadMethod.DOWNLOAD_POST_VIEWER, - // Collections.singletonList(postModels.get(childPosition))); - } - private void navigateToProfile(final String username) { final NavController navController = NavHostFragment.findNavController(this); final Bundle bundle = new Bundle(); diff --git a/app/src/main/java/awais/instagrabber/fragments/LocationFragment.java b/app/src/main/java/awais/instagrabber/fragments/LocationFragment.java index f0ac0554..2992f9b2 100644 --- a/app/src/main/java/awais/instagrabber/fragments/LocationFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/LocationFragment.java @@ -90,6 +90,7 @@ public class LocationFragment extends Fragment implements SwipeRefreshLayout.OnR private boolean storiesFetching; private Set selectedFeedModels; private FeedModel downloadFeedModel; + private int downloadChildPosition = -1; private final OnBackPressedCallback onBackPressedCallback = new OnBackPressedCallback(false) { @Override @@ -143,13 +144,15 @@ public class LocationFragment extends Fragment implements SwipeRefreshLayout.OnR } @Override - public void onDownloadClick(final FeedModel feedModel) { + public void onDownloadClick(final FeedModel feedModel, final int childPosition) { final Context context = getContext(); if (context == null) return; if (checkSelfPermission(context, WRITE_PERMISSION) == PermissionChecker.PERMISSION_GRANTED) { - showDownloadDialog(feedModel); + DownloadUtils.showDownloadDialog(context, feedModel, childPosition); return; } + downloadFeedModel = feedModel; + downloadChildPosition = childPosition; requestPermissions(DownloadUtils.PERMS, STORAGE_PERM_REQUEST_CODE); } @@ -302,15 +305,16 @@ public class LocationFragment extends Fragment implements SwipeRefreshLayout.OnR public void onRequestPermissionsResult(final int requestCode, @NonNull final String[] permissions, @NonNull final int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); final boolean granted = grantResults[0] == PackageManager.PERMISSION_GRANTED; + final Context context = getContext(); + if (context == null) return; if (requestCode == STORAGE_PERM_REQUEST_CODE && granted) { if (downloadFeedModel == null) return; - showDownloadDialog(downloadFeedModel); + DownloadUtils.showDownloadDialog(context, downloadFeedModel, downloadChildPosition); downloadFeedModel = null; + downloadChildPosition = -1; return; } if (requestCode == STORAGE_PERM_REQUEST_CODE_FOR_SELECTION && granted) { - final Context context = getContext(); - if (context == null) return; DownloadUtils.download(context, ImmutableList.copyOf(selectedFeedModels)); binding.posts.endSelection(); } @@ -505,48 +509,6 @@ public class LocationFragment extends Fragment implements SwipeRefreshLayout.OnR binding.swipeRefreshLayout.setRefreshing(binding.posts.isFetching() || storiesFetching); } - private void showDownloadDialog(final FeedModel feedModel) { - final Context context = getContext(); - if (context == null) return; - DownloadUtils.download(context, feedModel); - // switch (feedModel.getItemType()) { - // case MEDIA_TYPE_IMAGE: - // case MEDIA_TYPE_VIDEO: - // break; - // case MEDIA_TYPE_SLIDER: - // break; - // } - // final List postModelsToDownload = new ArrayList<>(); - // // if (!session) { - // final DialogInterface.OnClickListener clickListener = (dialog, which) -> { - // if (which == DialogInterface.BUTTON_NEGATIVE) { - // postModelsToDownload.addAll(postModels); - // } else if (which == DialogInterface.BUTTON_POSITIVE) { - // postModelsToDownload.add(postModels.get(childPosition)); - // } else { - // session = true; - // postModelsToDownload.add(postModels.get(childPosition)); - // } - // if (postModelsToDownload.size() > 0) { - // DownloadUtils.batchDownload(context, - // username, - // DownloadMethod.DOWNLOAD_POST_VIEWER, - // postModelsToDownload); - // } - // }; - // new AlertDialog.Builder(context) - // .setTitle(R.string.post_viewer_download_dialog_title) - // .setMessage(R.string.post_viewer_download_message) - // .setNeutralButton(R.string.post_viewer_download_session, clickListener) - // .setPositiveButton(R.string.post_viewer_download_current, clickListener) - // .setNegativeButton(R.string.post_viewer_download_album, clickListener).show(); - // } else { - // DownloadUtils.batchDownload(context, - // username, - // DownloadMethod.DOWNLOAD_POST_VIEWER, - // Collections.singletonList(postModels.get(childPosition))); - } - private void navigateToProfile(final String username) { final NavController navController = NavHostFragment.findNavController(this); final Bundle bundle = new Bundle(); diff --git a/app/src/main/java/awais/instagrabber/fragments/PostViewV2Fragment.java b/app/src/main/java/awais/instagrabber/fragments/PostViewV2Fragment.java index f811d1e6..18677bfd 100644 --- a/app/src/main/java/awais/instagrabber/fragments/PostViewV2Fragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/PostViewV2Fragment.java @@ -104,7 +104,7 @@ public class PostViewV2Fragment extends SharedElementTransitionDialogFragment { private boolean wasControlsVisible; private boolean wasPaused; private int captionState = BottomSheetBehavior.STATE_HIDDEN; - private int sliderPosition; + private int sliderPosition = -1; private final VerticalDragHelper.OnVerticalDragListener onVerticalDragListener = new VerticalDragHelper.OnVerticalDragListener() { @@ -522,7 +522,7 @@ public class PostViewV2Fragment extends SharedElementTransitionDialogFragment { public void onRequestPermissionsResult(final int requestCode, @NonNull final String[] permissions, @NonNull final int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == STORAGE_PERM_REQUEST_CODE && grantResults[0] == PackageManager.PERMISSION_GRANTED) { - showDownloadDialog(); + DownloadUtils.showDownloadDialog(context, feedModel, sliderPosition); } } @@ -560,7 +560,7 @@ public class PostViewV2Fragment extends SharedElementTransitionDialogFragment { private void setupDownload() { binding.download.setOnClickListener(v -> { if (checkSelfPermission(context, WRITE_PERMISSION) == PermissionChecker.PERMISSION_GRANTED) { - showDownloadDialog(); + DownloadUtils.showDownloadDialog(context, feedModel, sliderPosition); return; } requestPermissions(DownloadUtils.PERMS, STORAGE_PERM_REQUEST_CODE); @@ -1210,46 +1210,6 @@ public class PostViewV2Fragment extends SharedElementTransitionDialogFragment { }); } - private void showDownloadDialog() { - DownloadUtils.download(context, feedModel); - // switch (feedModel.getItemType()) { - // case MEDIA_TYPE_IMAGE: - // case MEDIA_TYPE_VIDEO: - // break; - // case MEDIA_TYPE_SLIDER: - // break; - // } - // final List postModelsToDownload = new ArrayList<>(); - // // if (!session) { - // final DialogInterface.OnClickListener clickListener = (dialog, which) -> { - // if (which == DialogInterface.BUTTON_NEGATIVE) { - // postModelsToDownload.addAll(postModels); - // } else if (which == DialogInterface.BUTTON_POSITIVE) { - // postModelsToDownload.add(postModels.get(childPosition)); - // } else { - // session = true; - // postModelsToDownload.add(postModels.get(childPosition)); - // } - // if (postModelsToDownload.size() > 0) { - // DownloadUtils.batchDownload(context, - // username, - // DownloadMethod.DOWNLOAD_POST_VIEWER, - // postModelsToDownload); - // } - // }; - // new AlertDialog.Builder(context) - // .setTitle(R.string.post_viewer_download_dialog_title) - // .setMessage(R.string.post_viewer_download_message) - // .setNeutralButton(R.string.post_viewer_download_session, clickListener) - // .setPositiveButton(R.string.post_viewer_download_current, clickListener) - // .setNegativeButton(R.string.post_viewer_download_album, clickListener).show(); - // } else { - // DownloadUtils.batchDownload(context, - // username, - // DownloadMethod.DOWNLOAD_POST_VIEWER, - // Collections.singletonList(postModels.get(childPosition))); - } - private void animateY(final View v, final float finalY, final int duration, diff --git a/app/src/main/java/awais/instagrabber/fragments/SavedViewerFragment.java b/app/src/main/java/awais/instagrabber/fragments/SavedViewerFragment.java index f446cacb..5b964e0e 100644 --- a/app/src/main/java/awais/instagrabber/fragments/SavedViewerFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/SavedViewerFragment.java @@ -61,6 +61,7 @@ public final class SavedViewerFragment extends Fragment implements SwipeRefreshL private String profileId; private Set selectedFeedModels; private FeedModel downloadFeedModel; + private int downloadChildPosition = -1; private final OnBackPressedCallback onBackPressedCallback = new OnBackPressedCallback(false) { @Override @@ -114,13 +115,15 @@ public final class SavedViewerFragment extends Fragment implements SwipeRefreshL } @Override - public void onDownloadClick(final FeedModel feedModel) { + public void onDownloadClick(final FeedModel feedModel, final int childPosition) { final Context context = getContext(); if (context == null) return; if (checkSelfPermission(context, WRITE_PERMISSION) == PermissionChecker.PERMISSION_GRANTED) { - showDownloadDialog(feedModel); + DownloadUtils.showDownloadDialog(context, feedModel, childPosition); return; } + downloadFeedModel = feedModel; + downloadChildPosition = childPosition; requestPermissions(DownloadUtils.PERMS, STORAGE_PERM_REQUEST_CODE); } @@ -342,15 +345,16 @@ public final class SavedViewerFragment extends Fragment implements SwipeRefreshL public void onRequestPermissionsResult(final int requestCode, @NonNull final String[] permissions, @NonNull final int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); final boolean granted = grantResults[0] == PackageManager.PERMISSION_GRANTED; + final Context context = getContext(); + if (context == null) return; if (requestCode == STORAGE_PERM_REQUEST_CODE && granted) { if (downloadFeedModel == null) return; - showDownloadDialog(downloadFeedModel); + DownloadUtils.showDownloadDialog(context, downloadFeedModel, downloadChildPosition); downloadFeedModel = null; + downloadChildPosition = -1; return; } if (requestCode == STORAGE_PERM_REQUEST_CODE_FOR_SELECTION && granted) { - final Context context = getContext(); - if (context == null) return; DownloadUtils.download(context, ImmutableList.copyOf(selectedFeedModels)); binding.posts.endSelection(); } @@ -380,48 +384,6 @@ public final class SavedViewerFragment extends Fragment implements SwipeRefreshL binding.swipeRefreshLayout.setRefreshing(binding.posts.isFetching()); } - private void showDownloadDialog(final FeedModel feedModel) { - final Context context = getContext(); - if (context == null) return; - DownloadUtils.download(context, feedModel); - // switch (feedModel.getItemType()) { - // case MEDIA_TYPE_IMAGE: - // case MEDIA_TYPE_VIDEO: - // break; - // case MEDIA_TYPE_SLIDER: - // break; - // } - // final List postModelsToDownload = new ArrayList<>(); - // // if (!session) { - // final DialogInterface.OnClickListener clickListener = (dialog, which) -> { - // if (which == DialogInterface.BUTTON_NEGATIVE) { - // postModelsToDownload.addAll(postModels); - // } else if (which == DialogInterface.BUTTON_POSITIVE) { - // postModelsToDownload.add(postModels.get(childPosition)); - // } else { - // session = true; - // postModelsToDownload.add(postModels.get(childPosition)); - // } - // if (postModelsToDownload.size() > 0) { - // DownloadUtils.batchDownload(context, - // username, - // DownloadMethod.DOWNLOAD_POST_VIEWER, - // postModelsToDownload); - // } - // }; - // new AlertDialog.Builder(context) - // .setTitle(R.string.post_viewer_download_dialog_title) - // .setMessage(R.string.post_viewer_download_message) - // .setNeutralButton(R.string.post_viewer_download_session, clickListener) - // .setPositiveButton(R.string.post_viewer_download_current, clickListener) - // .setNegativeButton(R.string.post_viewer_download_album, clickListener).show(); - // } else { - // DownloadUtils.batchDownload(context, - // username, - // DownloadMethod.DOWNLOAD_POST_VIEWER, - // Collections.singletonList(postModels.get(childPosition))); - } - private void navigateToProfile(final String username) { final NavController navController = NavHostFragment.findNavController(this); final Bundle bundle = new Bundle(); diff --git a/app/src/main/java/awais/instagrabber/fragments/TopicPostsFragment.java b/app/src/main/java/awais/instagrabber/fragments/TopicPostsFragment.java index 92af3d4d..3f261a6b 100644 --- a/app/src/main/java/awais/instagrabber/fragments/TopicPostsFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/TopicPostsFragment.java @@ -74,6 +74,7 @@ public class TopicPostsFragment extends Fragment implements SwipeRefreshLayout.O private ActionMode actionMode; private Set selectedFeedModels; private FeedModel downloadFeedModel; + private int downloadChildPosition = -1; private final OnBackPressedCallback onBackPressedCallback = new OnBackPressedCallback(false) { @Override @@ -127,13 +128,15 @@ public class TopicPostsFragment extends Fragment implements SwipeRefreshLayout.O } @Override - public void onDownloadClick(final FeedModel feedModel) { + public void onDownloadClick(final FeedModel feedModel, final int childPosition) { final Context context = getContext(); if (context == null) return; if (checkSelfPermission(context, WRITE_PERMISSION) == PermissionChecker.PERMISSION_GRANTED) { - showDownloadDialog(feedModel); + DownloadUtils.showDownloadDialog(context, feedModel, childPosition); return; } + downloadFeedModel = feedModel; + downloadChildPosition = -1; requestPermissions(DownloadUtils.PERMS, STORAGE_PERM_REQUEST_CODE); } @@ -302,15 +305,16 @@ public class TopicPostsFragment extends Fragment implements SwipeRefreshLayout.O public void onRequestPermissionsResult(final int requestCode, @NonNull final String[] permissions, @NonNull final int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); final boolean granted = grantResults[0] == PackageManager.PERMISSION_GRANTED; + final Context context = getContext(); + if (context == null) return; if (requestCode == STORAGE_PERM_REQUEST_CODE && granted) { if (downloadFeedModel == null) return; - showDownloadDialog(downloadFeedModel); + DownloadUtils.showDownloadDialog(context, downloadFeedModel, downloadChildPosition); downloadFeedModel = null; + downloadChildPosition = -1; return; } if (requestCode == STORAGE_PERM_REQUEST_CODE_FOR_SELECTION && granted) { - final Context context = getContext(); - if (context == null) return; DownloadUtils.download(context, ImmutableList.copyOf(selectedFeedModels)); binding.posts.endSelection(); } @@ -408,48 +412,6 @@ public class TopicPostsFragment extends Fragment implements SwipeRefreshLayout.O binding.swipeRefreshLayout.setRefreshing(binding.posts.isFetching()); } - private void showDownloadDialog(final FeedModel feedModel) { - final Context context = getContext(); - if (context == null) return; - DownloadUtils.download(context, feedModel); - // switch (feedModel.getItemType()) { - // case MEDIA_TYPE_IMAGE: - // case MEDIA_TYPE_VIDEO: - // break; - // case MEDIA_TYPE_SLIDER: - // break; - // } - // final List postModelsToDownload = new ArrayList<>(); - // // if (!session) { - // final DialogInterface.OnClickListener clickListener = (dialog, which) -> { - // if (which == DialogInterface.BUTTON_NEGATIVE) { - // postModelsToDownload.addAll(postModels); - // } else if (which == DialogInterface.BUTTON_POSITIVE) { - // postModelsToDownload.add(postModels.get(childPosition)); - // } else { - // session = true; - // postModelsToDownload.add(postModels.get(childPosition)); - // } - // if (postModelsToDownload.size() > 0) { - // DownloadUtils.batchDownload(context, - // username, - // DownloadMethod.DOWNLOAD_POST_VIEWER, - // postModelsToDownload); - // } - // }; - // new AlertDialog.Builder(context) - // .setTitle(R.string.post_viewer_download_dialog_title) - // .setMessage(R.string.post_viewer_download_message) - // .setNeutralButton(R.string.post_viewer_download_session, clickListener) - // .setPositiveButton(R.string.post_viewer_download_current, clickListener) - // .setNegativeButton(R.string.post_viewer_download_album, clickListener).show(); - // } else { - // DownloadUtils.batchDownload(context, - // username, - // DownloadMethod.DOWNLOAD_POST_VIEWER, - // Collections.singletonList(postModels.get(childPosition))); - } - private void navigateToProfile(final String username) { final NavController navController = NavHostFragment.findNavController(this); final Bundle bundle = new Bundle(); diff --git a/app/src/main/java/awais/instagrabber/fragments/main/FeedFragment.java b/app/src/main/java/awais/instagrabber/fragments/main/FeedFragment.java index bb2e693e..ebc93d99 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/FeedFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/main/FeedFragment.java @@ -71,6 +71,7 @@ public class FeedFragment extends Fragment implements SwipeRefreshLayout.OnRefre private ActionMode actionMode; private Set selectedFeedModels; private FeedModel downloadFeedModel; + private int downloadChildPosition = -1; private final FeedAdapterV2.FeedItemCallback feedItemCallback = new FeedAdapterV2.FeedItemCallback() { @Override @@ -94,14 +95,15 @@ public class FeedFragment extends Fragment implements SwipeRefreshLayout.OnRefre } @Override - public void onDownloadClick(final FeedModel feedModel) { + public void onDownloadClick(final FeedModel feedModel, final int childPosition) { final Context context = getContext(); if (context == null) return; if (checkSelfPermission(context, WRITE_PERMISSION) == PermissionChecker.PERMISSION_GRANTED) { - showDownloadDialog(feedModel); + DownloadUtils.showDownloadDialog(context, feedModel, childPosition); return; } downloadFeedModel = feedModel; + downloadChildPosition = childPosition; requestPermissions(DownloadUtils.PERMS, STORAGE_PERM_REQUEST_CODE); } @@ -303,14 +305,16 @@ public class FeedFragment extends Fragment implements SwipeRefreshLayout.OnRefre public void onRequestPermissionsResult(final int requestCode, @NonNull final String[] permissions, @NonNull final int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); final boolean granted = grantResults[0] == PackageManager.PERMISSION_GRANTED; + final Context context = getContext(); + if (context == null) return; if (requestCode == STORAGE_PERM_REQUEST_CODE && granted) { if (downloadFeedModel == null) return; - showDownloadDialog(downloadFeedModel); + DownloadUtils.showDownloadDialog(context, downloadFeedModel, downloadChildPosition); + downloadFeedModel = null; + downloadChildPosition = -1; return; } if (requestCode == STORAGE_PERM_REQUEST_CODE_FOR_SELECTION && granted) { - final Context context = getContext(); - if (context == null) return; DownloadUtils.download(context, ImmutableList.copyOf(selectedFeedModels)); binding.feedRecyclerView.endSelection(); } @@ -370,48 +374,6 @@ public class FeedFragment extends Fragment implements SwipeRefreshLayout.OnRefre }); } - private void showDownloadDialog(@NonNull final FeedModel feedModel) { - final Context context = getContext(); - if (context == null) return; - DownloadUtils.download(context, feedModel); - // switch (feedModel.getItemType()) { - // case MEDIA_TYPE_IMAGE: - // case MEDIA_TYPE_VIDEO: - // break; - // case MEDIA_TYPE_SLIDER: - // break; - // } - // final List postModelsToDownload = new ArrayList<>(); - // // if (!session) { - // final DialogInterface.OnClickListener clickListener = (dialog, which) -> { - // if (which == DialogInterface.BUTTON_NEGATIVE) { - // postModelsToDownload.addAll(postModels); - // } else if (which == DialogInterface.BUTTON_POSITIVE) { - // postModelsToDownload.add(postModels.get(childPosition)); - // } else { - // session = true; - // postModelsToDownload.add(postModels.get(childPosition)); - // } - // if (postModelsToDownload.size() > 0) { - // DownloadUtils.batchDownload(context, - // username, - // DownloadMethod.DOWNLOAD_POST_VIEWER, - // postModelsToDownload); - // } - // }; - // new AlertDialog.Builder(context) - // .setTitle(R.string.post_viewer_download_dialog_title) - // .setMessage(R.string.post_viewer_download_message) - // .setNeutralButton(R.string.post_viewer_download_session, clickListener) - // .setPositiveButton(R.string.post_viewer_download_current, clickListener) - // .setNegativeButton(R.string.post_viewer_download_album, clickListener).show(); - // } else { - // DownloadUtils.batchDownload(context, - // username, - // DownloadMethod.DOWNLOAD_POST_VIEWER, - // Collections.singletonList(postModels.get(childPosition))); - } - private void showPostsLayoutPreferences() { final PostsLayoutPreferencesDialogFragment fragment = new PostsLayoutPreferencesDialogFragment( Constants.PREF_POSTS_LAYOUT, diff --git a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java index 87e917e6..d79e437d 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java @@ -4,7 +4,6 @@ import android.content.Context; import android.content.DialogInterface; import android.content.pm.PackageManager; import android.graphics.Typeface; -import android.graphics.drawable.Animatable; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; @@ -43,9 +42,6 @@ import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; -import com.facebook.drawee.controller.BaseControllerListener; -import com.facebook.drawee.controller.ControllerListener; -import com.facebook.imagepipeline.image.ImageInfo; import com.google.android.material.snackbar.BaseTransientBottomBar; import com.google.android.material.snackbar.Snackbar; import com.google.common.collect.ImmutableList; @@ -123,6 +119,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe private boolean postsSetupDone = false; private Set selectedFeedModels; private FeedModel downloadFeedModel; + private int downloadChildPosition = -1; private final Runnable usernameSettingRunnable = () -> { final ActionBar actionBar = fragmentActivity.getSupportActionBar(); @@ -204,14 +201,15 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe } @Override - public void onDownloadClick(final FeedModel feedModel) { + public void onDownloadClick(final FeedModel feedModel, final int childPosition) { final Context context = getContext(); if (context == null) return; if (checkSelfPermission(context, WRITE_PERMISSION) == PermissionChecker.PERMISSION_GRANTED) { - showDownloadDialog(feedModel); + DownloadUtils.showDownloadDialog(context, feedModel, childPosition); return; } downloadFeedModel = feedModel; + downloadChildPosition = childPosition; requestPermissions(DownloadUtils.PERMS, STORAGE_PERM_REQUEST_CODE); } @@ -457,15 +455,16 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe public void onRequestPermissionsResult(final int requestCode, @NonNull final String[] permissions, @NonNull final int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); final boolean granted = grantResults[0] == PackageManager.PERMISSION_GRANTED; + final Context context = getContext(); + if (context == null) return; if (requestCode == STORAGE_PERM_REQUEST_CODE && granted) { if (downloadFeedModel == null) return; - showDownloadDialog(downloadFeedModel); + DownloadUtils.showDownloadDialog(context, downloadFeedModel, downloadChildPosition); downloadFeedModel = null; + downloadChildPosition = -1; return; } if (requestCode == STORAGE_PERM_REQUEST_CODE_FOR_SELECTION && granted) { - final Context context = getContext(); - if (context == null) return; DownloadUtils.download(context, ImmutableList.copyOf(selectedFeedModels)); binding.postsRecyclerView.endSelection(); } @@ -567,12 +566,12 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe } else { binding.favCb.setVisibility(View.GONE); } - final ControllerListener listener = new BaseControllerListener() { - @Override - public void onFinalImageSet(final String id, final ImageInfo imageInfo, final Animatable animatable) { - startPostponedEnterTransition(); - } - }; + // final ControllerListener listener = new BaseControllerListener() { + // @Override + // public void onFinalImageSet(final String id, final ImageInfo imageInfo, final Animatable animatable) { + // startPostponedEnterTransition(); + // } + // }; binding.mainProfileImage.setImageURI(profileModel.getHdProfilePic()); final long followersCount = profileModel.getFollowersCount(); @@ -751,8 +750,6 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe private void setupCommonListeners() { final String userIdFromCookie = CookieUtils.getUserIdFromCookie(cookie); - // final boolean isSelf = isLoggedIn && profileModel != null && userIdFromCookie != null && userIdFromCookie - // .equals(profileModel.getId()); binding.btnFollow.setOnClickListener(v -> { if (profileModel.getFollowing() || profileModel.getRequested()) { friendshipService.unfollow( @@ -808,12 +805,10 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe PostItemType.TAGGED); NavHostFragment.findNavController(this).navigate(action); }); - binding.btnDM.setOnClickListener(v -> { - new CreateThreadAction(cookie, profileModel.getId(), threadId -> { - final NavDirections action = ProfileFragmentDirections.actionProfileFragmentToDMThreadFragment(threadId, profileModel.getUsername()); - NavHostFragment.findNavController(this).navigate(action); - }).execute(); - }); + binding.btnDM.setOnClickListener(v -> new CreateThreadAction(cookie, profileModel.getId(), threadId -> { + final NavDirections action = ProfileFragmentDirections.actionProfileFragmentToDMThreadFragment(threadId, profileModel.getUsername()); + NavHostFragment.findNavController(this).navigate(action); + }).execute()); binding.mainProfileImage.setOnClickListener(v -> { if (!hasStories) { // show profile pic @@ -986,48 +981,6 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe navController.navigate(R.id.action_global_profileFragment, bundle); } - private void showDownloadDialog(final FeedModel feedModel) { - final Context context = getContext(); - if (context == null) return; - DownloadUtils.download(context, feedModel); - // switch (feedModel.getItemType()) { - // case MEDIA_TYPE_IMAGE: - // case MEDIA_TYPE_VIDEO: - // break; - // case MEDIA_TYPE_SLIDER: - // break; - // } - // final List postModelsToDownload = new ArrayList<>(); - // // if (!session) { - // final DialogInterface.OnClickListener clickListener = (dialog, which) -> { - // if (which == DialogInterface.BUTTON_NEGATIVE) { - // postModelsToDownload.addAll(postModels); - // } else if (which == DialogInterface.BUTTON_POSITIVE) { - // postModelsToDownload.add(postModels.get(childPosition)); - // } else { - // session = true; - // postModelsToDownload.add(postModels.get(childPosition)); - // } - // if (postModelsToDownload.size() > 0) { - // DownloadUtils.batchDownload(context, - // username, - // DownloadMethod.DOWNLOAD_POST_VIEWER, - // postModelsToDownload); - // } - // }; - // new AlertDialog.Builder(context) - // .setTitle(R.string.post_viewer_download_dialog_title) - // .setMessage(R.string.post_viewer_download_message) - // .setNeutralButton(R.string.post_viewer_download_session, clickListener) - // .setPositiveButton(R.string.post_viewer_download_current, clickListener) - // .setNegativeButton(R.string.post_viewer_download_album, clickListener).show(); - // } else { - // DownloadUtils.batchDownload(context, - // username, - // DownloadMethod.DOWNLOAD_POST_VIEWER, - // Collections.singletonList(postModels.get(childPosition))); - } - private void showPostsLayoutPreferences() { final PostsLayoutPreferencesDialogFragment fragment = new PostsLayoutPreferencesDialogFragment( Constants.PREF_PROFILE_POSTS_LAYOUT, diff --git a/app/src/main/java/awais/instagrabber/utils/DownloadUtils.java b/app/src/main/java/awais/instagrabber/utils/DownloadUtils.java index f9c37f80..295a7476 100644 --- a/app/src/main/java/awais/instagrabber/utils/DownloadUtils.java +++ b/app/src/main/java/awais/instagrabber/utils/DownloadUtils.java @@ -3,6 +3,7 @@ package awais.instagrabber.utils; import android.Manifest; import android.app.Activity; import android.content.Context; +import android.content.DialogInterface; import android.content.pm.PackageManager; import android.os.AsyncTask; import android.os.Environment; @@ -13,6 +14,7 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.appcompat.app.AlertDialog; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import androidx.work.Constraints; @@ -275,7 +277,9 @@ public final class DownloadUtils { return ""; } - public static void checkExistence(final File downloadDir, final File customDir, final boolean isSlider, + public static void checkExistence(final File downloadDir, + final File customDir, + final boolean isSlider, @NonNull final BasePostModel model) { boolean exists = false; @@ -311,6 +315,38 @@ public final class DownloadUtils { model.setDownloaded(exists); } + public static void showDownloadDialog(@NonNull Context context, + @NonNull final FeedModel feedModel, + final int childPosition) { + if (childPosition >= 0) { + final DialogInterface.OnClickListener clickListener = (dialog, which) -> { + switch (which) { + case 0: + DownloadUtils.download(context, feedModel, childPosition); + break; + case 1: + DownloadUtils.download(context, feedModel); + break; + case DialogInterface.BUTTON_NEGATIVE: + default: + dialog.dismiss(); + break; + } + }; + final String[] items = new String[]{ + context.getString(R.string.post_viewer_download_current), + context.getString(R.string.post_viewer_download_album), + }; + new AlertDialog.Builder(context) + .setTitle(R.string.post_viewer_download_dialog_title) + .setItems(items, clickListener) + .setNegativeButton(R.string.cancel, null) + .show(); + return; + } + DownloadUtils.download(context, feedModel); + } + public static void download(@NonNull final Context context, @NonNull final FeedModel feedModel) { download(context, feedModel, -1); @@ -345,6 +381,9 @@ public final class DownloadUtils { case MEDIA_TYPE_SLIDER: final List sliderItems = feedModel.getSliderItems(); for (int i = 0; i < sliderItems.size(); i++) { + if (childPositionIfSingle >= 0 && feedModels.size() == 1 && i != childPositionIfSingle) { + continue; + } final PostChild child = sliderItems.get(i); final String url = child.getDisplayUrl(); final File file = getDownloadChildSaveFile(downloadDir, feedModel.getPostId(), i + 1, url); @@ -357,6 +396,13 @@ public final class DownloadUtils { download(context, map); } + public static void download(final Context context, + final String url, + final String filePath) { + if (context == null || url == null || filePath == null) return; + download(context, Collections.singletonMap(url, filePath)); + } + private static void download(final Context context, final Map urlFilePathMap) { final Constraints constraints = new Constraints.Builder() .setRequiredNetworkType(NetworkType.CONNECTED)