From d2553b39a67d641f37b5c9b178d0738ba6795a11 Mon Sep 17 00:00:00 2001 From: seven332 Date: Sat, 5 Mar 2016 00:47:25 +0800 Subject: [PATCH] Add select download label --- .../java/com/hippo/ehviewer/Settings.java | 22 ++++++ .../ehviewer/download/DownloadManager.java | 4 ++ .../hippo/ehviewer/ui/CommonOperations.java | 71 ++++++++++++++++++- .../ehviewer/ui/scene/DownloadScene.java | 29 ++++++++ .../ehviewer/ui/scene/FavoritesScene.java | 2 +- .../ehviewer/ui/scene/GalleryDetailScene.java | 6 +- .../ehviewer/ui/scene/GalleryListScene.java | 26 +++++-- .../res/drawable/v_download_box_dark_x24.xml | 29 ++++++++ app/src/main/res/menu/drawer_download.xml | 6 ++ app/src/main/res/values/pathdata.xml | 1 + app/src/main/res/values/strings.xml | 4 +- 11 files changed, 183 insertions(+), 17 deletions(-) create mode 100644 app/src/main/res/drawable/v_download_box_dark_x24.xml diff --git a/app/src/main/java/com/hippo/ehviewer/Settings.java b/app/src/main/java/com/hippo/ehviewer/Settings.java index da0501139..3a26eeb07 100644 --- a/app/src/main/java/com/hippo/ehviewer/Settings.java +++ b/app/src/main/java/com/hippo/ehviewer/Settings.java @@ -273,6 +273,28 @@ public class Settings { putString(KEY_RECENT_DOWNLOAD_LABEL, value); } + private static final String KEY_HAS_DEFAULT_DOWNLOAD_LABEL = "has_default_download_label"; + private static final boolean DEFAULT_HAS_DOWNLOAD_LABEL = false; + + public static boolean getHasDefaultDownloadLabel() { + return getBoolean(KEY_HAS_DEFAULT_DOWNLOAD_LABEL, DEFAULT_HAS_DOWNLOAD_LABEL); + } + + public static void putHasDefaultDownloadLabel(boolean hasDefaultDownloadLabel) { + putBoolean(KEY_HAS_DEFAULT_DOWNLOAD_LABEL, hasDefaultDownloadLabel); + } + + private static final String KEY_DEFAULT_DOWNLOAD_LABEL = "default_download_label"; + private static final String DEFAULT_DOWNLOAD_LABEL = null; + + public static String getDefaultDownloadLabel() { + return getString(KEY_DEFAULT_DOWNLOAD_LABEL, DEFAULT_DOWNLOAD_LABEL); + } + + public static void putDefaultDownloadLabel(String value) { + putString(KEY_DEFAULT_DOWNLOAD_LABEL, value); + } + /******************** ****** Favorites ********************/ diff --git a/app/src/main/java/com/hippo/ehviewer/download/DownloadManager.java b/app/src/main/java/com/hippo/ehviewer/download/DownloadManager.java index 9ebec4cd9..12fa8773e 100644 --- a/app/src/main/java/com/hippo/ehviewer/download/DownloadManager.java +++ b/app/src/main/java/com/hippo/ehviewer/download/DownloadManager.java @@ -174,6 +174,10 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener { return false; } + public boolean containDownloadInfo(int gid) { + return mAllInfoMap.indexOfKey(gid) >= 0; + } + @NonNull public List getLabelList() { return mLabelList; 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 a5c82746e..d0095d985 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/CommonOperations.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/CommonOperations.java @@ -17,6 +17,7 @@ package com.hippo.ehviewer.ui; import android.app.Activity; +import android.content.Intent; import android.support.v7.app.AlertDialog; import com.hippo.app.ListCheckBoxDialogBuilder; @@ -27,6 +28,11 @@ import com.hippo.ehviewer.Settings; import com.hippo.ehviewer.client.EhClient; import com.hippo.ehviewer.client.EhRequest; import com.hippo.ehviewer.client.data.GalleryInfo; +import com.hippo.ehviewer.dao.DownloadLabelRaw; +import com.hippo.ehviewer.download.DownloadManager; +import com.hippo.ehviewer.download.DownloadService; + +import java.util.List; public final class CommonOperations { @@ -63,10 +69,10 @@ public final class CommonOperations { public void onItemClick(ListCheckBoxDialogBuilder builder, AlertDialog dialog, int position) { int slot = position - 1; doAddToFavorites(activity, galleryInfo, slot, listener); - if (!builder.isChecked()) { - Settings.putDefaultFavSlot(Settings.INVALID_DEFAULT_FAV_SLOT); - } else { + if (builder.isChecked()) { Settings.putDefaultFavSlot(slot); + } else { + Settings.putDefaultFavSlot(Settings.INVALID_DEFAULT_FAV_SLOT); } } }, activity.getString(R.string.remember_favorite_slot), false) @@ -84,4 +90,63 @@ public final class CommonOperations { request.setCallback(listener); client.execute(request); } + + public static void startDownload(final Activity activity, final GalleryInfo galleryInfo) { + final DownloadManager dm = EhApplication.getDownloadManager(activity); + + boolean justStart = dm.containDownloadInfo(galleryInfo.gid); + String label = null; + if (!justStart && Settings.getHasDefaultDownloadLabel()) { + label = Settings.getDefaultDownloadLabel(); + justStart = label == null || dm.containLabel(label); + } + + 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); + } else { + // Let use chose label + List list = dm.getLabelList(); + final String[] items = new String[list.size() + 1]; + items[0] = activity.getString(R.string.default_download_label_name); + for (int i = 0, n = list.size(); i < n; i++) { + items[i + 1] = list.get(i).getLabel(); + } + + new ListCheckBoxDialogBuilder(activity, items, + new ListCheckBoxDialogBuilder.OnItemClickListener() { + @Override + public void onItemClick(ListCheckBoxDialogBuilder builder, AlertDialog dialog, int position) { + String label; + if (position == 0) { + label = null; + } else { + label = items[position]; + if (!dm.containLabel(label)) { + label = null; + } + } + // 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); + // Save settings + if (builder.isChecked()) { + Settings.putHasDefaultDownloadLabel(true); + Settings.putDefaultDownloadLabel(label); + } else { + Settings.putHasDefaultDownloadLabel(false); + } + } + }, activity.getString(R.string.remember_download_label), false) + .setTitle(R.string.download) + .show(); + } + } } diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadScene.java index c51e6a64a..6d21e59b3 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadScene.java @@ -308,6 +308,35 @@ public class DownloadScene extends ToolbarScene case R.id.action_settings: startScene(new Announcer(DownloadLabelScene.class)); return true; + case R.id.action_default_download_label: + DownloadManager dm = EhApplication.getDownloadManager(getContext()); + List list = dm.getLabelList(); + final String[] items = new String[list.size() + 2]; + items[0] = getString(R.string.let_me_select); + items[1] = getString(R.string.default_download_label_name); + for (int i = 0, n = list.size(); i < n; i++) { + items[i + 2] = list.get(i).getLabel(); + } + new AlertDialog.Builder(getContext()) + .setTitle(R.string.default_download_label) + .setItems(items, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + if (which == 0) { + Settings.putHasDefaultDownloadLabel(false); + } else { + Settings.putHasDefaultDownloadLabel(true); + String label; + if (which == 1) { + label = null; + } else { + label = items[which]; + } + Settings.putDefaultDownloadLabel(label); + } + } + }).show(); + return true; } return false; } 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 c1a814098..eb1e6ba7a 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 @@ -347,7 +347,7 @@ public class FavoritesScene extends BaseScene implements switch (id) { case R.id.action_default_favorites_slot: String[] items = new String[12]; - items[0] = getString(R.string.let_me_select_favorites_slot); + items[0] = getString(R.string.let_me_select); items[1] = getString(R.string.local_favorites); String[] favCat = Settings.getFavCat(); System.arraycopy(favCat, 0, items, 2, 10); diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.java index 9fd6b169a..93d55dfcb 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.java @@ -71,7 +71,6 @@ import com.hippo.ehviewer.client.data.GalleryTagGroup; import com.hippo.ehviewer.client.data.LargePreviewSet; import com.hippo.ehviewer.client.data.ListUrlBuilder; import com.hippo.ehviewer.client.parser.RateGalleryParser; -import com.hippo.ehviewer.download.DownloadService; import com.hippo.ehviewer.ui.CommonOperations; import com.hippo.ehviewer.ui.GalleryActivity; import com.hippo.ehviewer.ui.annotation.ViewLifeCircle; @@ -992,10 +991,7 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene galleryInfo = mGalleryDetail; } if (galleryInfo != null) { - Intent intent = new Intent(getActivity(), DownloadService.class); - intent.setAction(DownloadService.ACTION_START); - intent.putExtra(DownloadService.KEY_GALLERY_INFO, galleryInfo); - getActivity().startService(intent); + CommonOperations.startDownload(getActivity(), galleryInfo); } } else if (mRead == v) { GalleryInfo galleryInfo = null; diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryListScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryListScene.java index 556eafd55..f4a49d9c1 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryListScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryListScene.java @@ -20,7 +20,6 @@ import android.animation.Animator; import android.annotation.TargetApi; import android.content.Context; import android.content.DialogInterface; -import android.content.Intent; import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.os.Build; @@ -60,7 +59,7 @@ import com.hippo.ehviewer.client.EhUtils; import com.hippo.ehviewer.client.data.GalleryInfo; import com.hippo.ehviewer.client.data.ListUrlBuilder; import com.hippo.ehviewer.client.parser.GalleryListParser; -import com.hippo.ehviewer.download.DownloadService; +import com.hippo.ehviewer.ui.CommonOperations; import com.hippo.ehviewer.widget.FitPaddingLayout; import com.hippo.ehviewer.widget.SearchBar; import com.hippo.ehviewer.widget.SearchLayout; @@ -460,13 +459,10 @@ public final class GalleryListScene extends BaseScene public void onClick(DialogInterface dialog, int which) { switch (which) { case 0: // Download - Intent intent = new Intent(getActivity(), DownloadService.class); - intent.setAction(DownloadService.ACTION_START); - intent.putExtra(DownloadService.KEY_GALLERY_INFO, gi); - getActivity().startService(intent); + CommonOperations.startDownload(getActivity(), gi); break; case 1: // Favorites - // TODO + CommonOperations.addToFavorites(getActivity(), gi, new addToFavoriteListener()); break; } } @@ -892,4 +888,20 @@ public final class GalleryListScene extends BaseScene return scene instanceof GalleryListScene; } } + + private class addToFavoriteListener implements EhClient.Callback { + + @Override + public void onSuccess(Void result) { + Toast.makeText(getContext(), R.string.add_to_favorite_success, Toast.LENGTH_SHORT).show(); + } + + @Override + public void onFailure(Exception e) { + Toast.makeText(getContext(), R.string.add_to_favorite_failure, Toast.LENGTH_SHORT).show(); + } + + @Override + public void onCancel() {} + } } diff --git a/app/src/main/res/drawable/v_download_box_dark_x24.xml b/app/src/main/res/drawable/v_download_box_dark_x24.xml new file mode 100644 index 000000000..4fa82bd21 --- /dev/null +++ b/app/src/main/res/drawable/v_download_box_dark_x24.xml @@ -0,0 +1,29 @@ + + + + + + + + diff --git a/app/src/main/res/menu/drawer_download.xml b/app/src/main/res/menu/drawer_download.xml index 00520c547..1880f4de5 100644 --- a/app/src/main/res/menu/drawer_download.xml +++ b/app/src/main/res/menu/drawer_download.xml @@ -25,4 +25,10 @@ android:title="@string/settings" app:showAsAction="always"/> + + diff --git a/app/src/main/res/values/pathdata.xml b/app/src/main/res/values/pathdata.xml index d365968c9..8b3a1f90c 100644 --- a/app/src/main/res/values/pathdata.xml +++ b/app/src/main/res/values/pathdata.xml @@ -50,6 +50,7 @@ M12,15.39L8.24,17.66L9.23,13.38L5.91,10.5L10.29,10.13L12,6.09L13.71,10.13L18.09,10.5L14.77,13.38L15.76,17.66M22,9.24L14.81,8.63L12,2L9.19,8.63L2,9.24L7.45,13.97L5.82,21L12,17.27L18.18,21L16.54,13.97L22,9.24Z M13,9H11V7H13M13,17H11V11H13M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2Z M5,3H19A2,2 0 0,1 21,5V19A2,2 0 0,1 19,21H5A2,2 0 0,1 3,19V5A2,2 0 0,1 5,3M12,17L12.72,16.34C15.3,14 17,12.46 17,10.57C17,9.03 15.79,7.82 14.25,7.82C13.38,7.82 12.55,8.23 12,8.87C11.45,8.23 10.62,7.82 9.75,7.82C8.21,7.82 7,9.03 7,10.57C7,12.46 8.7,14 11.28,16.34L12,17Z + M19,3H5C3.9,3,3,3.9,3,5V19C3,20.1,3.9,21,5,21H19C20.1,21,21,20.1,21,19V5C21,3.9,20.1,3,19,3ZM12,17L7,12H10V8H14V12H17L12,17Z M13,0C20.2,0,26,5.9,26,13.2,26,16.9,24,20.8,18.7,26.1L13,32,7.2,26.2C2,20.8,0,16.9,0,13.3,0,5.9,5.8,0,13,0Z diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1ffd86209..7876777e7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -254,6 +254,8 @@ EhViewer Download Service %s %1$s, %2$s + Remember download label + Default download label No labels @@ -325,7 +327,7 @@ Delete %d items from favorites Move favorites Default favorites slot - Let me select when necessary + Let me select Something wrong happened