From 879b8fa50aad031d8ca9de86f4a39466a0dfcd5c Mon Sep 17 00:00:00 2001 From: seven332 Date: Thu, 17 Jan 2019 18:41:49 +0800 Subject: [PATCH] Support downloading galleries from FavoritesScene --- .../hippo/ehviewer/ui/CommonOperations.java | 59 +++++++++++++++---- .../ehviewer/ui/scene/FavoritesScene.java | 16 ++++- .../main/res/drawable/v_download_dark_x24.xml | 29 +++++++++ app/src/main/res/layout/scene_favorites.xml | 7 +++ 4 files changed, 96 insertions(+), 15 deletions(-) create mode 100644 app/src/main/res/drawable/v_download_dark_x24.xml diff --git a/app/src/main/java/com/hippo/ehviewer/ui/CommonOperations.java b/app/src/main/java/com/hippo/ehviewer/ui/CommonOperations.java index 7ed62840a..478be560a 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/CommonOperations.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/CommonOperations.java @@ -45,6 +45,9 @@ import com.hippo.util.ExceptionUtils; import com.hippo.yorozuya.FileUtils; import com.hippo.yorozuya.IOUtils; +import com.hippo.yorozuya.collect.LongList; +import java.util.ArrayList; +import java.util.Collections; import org.json.JSONException; import org.json.JSONObject; @@ -281,11 +284,37 @@ public final class CommonOperations { } } - // TODO Add context if activity and context are different style public static void startDownload(final MainActivity activity, final GalleryInfo galleryInfo, boolean forceDefault) { + startDownload(activity, Collections.singletonList(galleryInfo), forceDefault); + } + + // TODO Add context if activity and context are different style + public static void startDownload(final MainActivity activity, final List galleryInfos, boolean forceDefault) { final DownloadManager dm = EhApplication.getDownloadManager(activity); - boolean justStart = forceDefault || dm.containDownloadInfo(galleryInfo.gid); + LongList toStart = new LongList(); + List toAdd = new ArrayList<>(); + for (GalleryInfo gi : galleryInfos) { + if (dm.containDownloadInfo(gi.gid)) { + toStart.add(gi.gid); + } else { + toAdd.add(gi); + } + } + + if (!toStart.isEmpty()) { + Intent intent = new Intent(activity, DownloadService.class); + intent.setAction(DownloadService.ACTION_START_RANGE); + intent.putExtra(DownloadService.KEY_GID_LIST, toStart); + activity.startService(intent); + } + + if (toAdd.isEmpty()) { + activity.showTip(R.string.added_to_download_list, BaseScene.LENGTH_SHORT); + return; + } + + boolean justStart = forceDefault; String label = null; // Get default download label if (!justStart && Settings.getHasDefaultDownloadLabel()) { @@ -299,12 +328,14 @@ public final class CommonOperations { } if (justStart) { - // Already in download list or get default label - Intent intent = new Intent(activity, DownloadService.class); - intent.setAction(DownloadService.ACTION_START); - intent.putExtra(DownloadService.KEY_LABEL, label); - intent.putExtra(DownloadService.KEY_GALLERY_INFO, galleryInfo); - activity.startService(intent); + // Got default label + for (GalleryInfo gi : toAdd) { + Intent intent = new Intent(activity, DownloadService.class); + intent.setAction(DownloadService.ACTION_START); + intent.putExtra(DownloadService.KEY_LABEL, label); + intent.putExtra(DownloadService.KEY_GALLERY_INFO, gi); + activity.startService(intent); + } // Notify activity.showTip(R.string.added_to_download_list, BaseScene.LENGTH_SHORT); } else { @@ -330,11 +361,13 @@ public final class CommonOperations { } } // Start download - Intent intent = new Intent(activity, DownloadService.class); - intent.setAction(DownloadService.ACTION_START); - intent.putExtra(DownloadService.KEY_LABEL, label); - intent.putExtra(DownloadService.KEY_GALLERY_INFO, galleryInfo); - activity.startService(intent); + for (GalleryInfo gi : toAdd) { + Intent intent = new Intent(activity, DownloadService.class); + intent.setAction(DownloadService.ACTION_START); + intent.putExtra(DownloadService.KEY_LABEL, label); + intent.putExtra(DownloadService.KEY_GALLERY_INFO, gi); + activity.startService(intent); + } // Save settings if (builder.isChecked()) { Settings.putHasDefaultDownloadLabel(true); diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.java index cc726acac..7f68f88b2 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.java @@ -63,6 +63,7 @@ import com.hippo.ehviewer.client.EhUrl; import com.hippo.ehviewer.client.data.FavListUrlBuilder; import com.hippo.ehviewer.client.data.GalleryInfo; import com.hippo.ehviewer.client.parser.FavoritesParser; +import com.hippo.ehviewer.ui.CommonOperations; import com.hippo.ehviewer.ui.MainActivity; import com.hippo.ehviewer.ui.annotation.DrawerLifeCircle; import com.hippo.ehviewer.ui.annotation.ViewLifeCircle; @@ -762,7 +763,18 @@ public class FavoritesScene extends BaseScene implements } switch (position) { - case 0: { // Delete + case 0: { // Download + Activity activity = getActivity2(); + if (activity != null) { + CommonOperations.startDownload(getActivity2(), mModifyGiList, false); + } + mModifyGiList.clear(); + if (mRecyclerView != null && mRecyclerView.isInCustomChoice()) { + mRecyclerView.outOfCustomChoiceMode(); + } + break; + } + case 1: { // Delete DeleteDialogHelper helper = new DeleteDialogHelper(); new AlertDialog.Builder(context) .setTitle(R.string.delete_favorites_dialog_title) @@ -772,7 +784,7 @@ public class FavoritesScene extends BaseScene implements .show(); break; } - case 1: { // Move + case 2: { // Move MoveDialogHelper helper = new MoveDialogHelper(); // First is local favorite, the other 10 is cloud favorite String[] array = new String[11]; diff --git a/app/src/main/res/drawable/v_download_dark_x24.xml b/app/src/main/res/drawable/v_download_dark_x24.xml new file mode 100644 index 000000000..d56e0c549 --- /dev/null +++ b/app/src/main/res/drawable/v_download_dark_x24.xml @@ -0,0 +1,29 @@ + + + + + + + + diff --git a/app/src/main/res/layout/scene_favorites.xml b/app/src/main/res/layout/scene_favorites.xml index 4fa32d8f7..cfbe9bc55 100644 --- a/app/src/main/res/layout/scene_favorites.xml +++ b/app/src/main/res/layout/scene_favorites.xml @@ -44,6 +44,13 @@ android:paddingRight="@dimen/corner_fab_margin" android:paddingBottom="@dimen/corner_fab_margin"> + +