From 8b9b4423c2ce724033b091b17635c35f0aeaef7b Mon Sep 17 00:00:00 2001 From: hiroshi Date: Tue, 6 Dec 2016 15:50:39 +0800 Subject: [PATCH] =?UTF-8?q?#25=20=E5=AE=8C=E6=88=905.0=E4=BB=A5=E4=B8=8B?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=AD=98=E5=82=A8=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 +- .idea/compiler.xml | 22 --- .idea/copyright/profiles_settings.xml | 3 - .idea/encodings.xml | 6 - .idea/gradle.xml | 19 --- .idea/markdown-navigator.xml | 67 --------- .../markdown-navigator/profiles_settings.xml | 3 - .idea/misc.xml | 46 ------ .idea/modules.xml | 9 -- .idea/runConfigurations.xml | 12 -- .idea/vcs.xml | 6 - app/src/main/AndroidManifest.xml | 3 + .../com/hiroshi/cimoc/CimocApplication.java | 3 +- .../java/com/hiroshi/cimoc/core/Download.java | 34 +++-- .../cimoc/core/manager/TaskManager.java | 5 +- .../com/hiroshi/cimoc/global/FastClick.java | 2 +- .../cimoc/presenter/ChapterPresenter.java | 89 ------------ .../cimoc/presenter/DetailPresenter.java | 127 ++++++++++------- .../cimoc/presenter/ReaderPresenter.java | 2 +- .../hiroshi/cimoc/presenter/TagPresenter.java | 2 - .../cimoc/presenter/TaskPresenter.java | 27 ++-- .../java/com/hiroshi/cimoc/rx/RxEvent.java | 18 ++- .../cimoc/ui/activity/ChapterActivity.java | 69 ++------- .../cimoc/ui/activity/DetailActivity.java | 134 +++++++++++------- .../cimoc/ui/activity/DirPickerActivity.java | 100 +++++++++++++ .../cimoc/ui/activity/MainActivity.java | 2 + .../cimoc/ui/activity/SettingsActivity.java | 26 +++- .../cimoc/ui/activity/TaskActivity.java | 7 +- .../hiroshi/cimoc/ui/adapter/DirAdapter.java | 56 ++++++++ .../cimoc/ui/fragment/ComicFragment.java | 19 ++- .../cimoc/ui/fragment/SearchFragment.java | 27 ++-- .../fragment/dialog/MultiDialogFragment.java | 24 ++-- .../dialog/StorageEditorDialogFragment.java | 6 +- .../hiroshi/cimoc/ui/view/ChapterView.java | 17 --- .../com/hiroshi/cimoc/ui/view/DetailView.java | 16 +-- .../com/hiroshi/cimoc/ui/view/TaskView.java | 2 +- app/src/main/res/layout/activity_settings.xml | 106 +++++++------- app/src/main/res/layout/item_dir.xml | 12 ++ app/src/main/res/values/strings.xml | 10 +- 39 files changed, 526 insertions(+), 614 deletions(-) delete mode 100644 .idea/compiler.xml delete mode 100644 .idea/copyright/profiles_settings.xml delete mode 100644 .idea/encodings.xml delete mode 100644 .idea/gradle.xml delete mode 100644 .idea/markdown-navigator.xml delete mode 100644 .idea/markdown-navigator/profiles_settings.xml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/runConfigurations.xml delete mode 100644 .idea/vcs.xml delete mode 100644 app/src/main/java/com/hiroshi/cimoc/presenter/ChapterPresenter.java create mode 100644 app/src/main/java/com/hiroshi/cimoc/ui/activity/DirPickerActivity.java create mode 100644 app/src/main/java/com/hiroshi/cimoc/ui/adapter/DirAdapter.java delete mode 100644 app/src/main/java/com/hiroshi/cimoc/ui/view/ChapterView.java create mode 100644 app/src/main/res/layout/item_dir.xml diff --git a/.gitignore b/.gitignore index 4b0cd006..ef108e39 100644 --- a/.gitignore +++ b/.gitignore @@ -8,7 +8,7 @@ build/ # Intellij *.iml -.idea +.idea/ # Local configuration file (sdk path, etc) local.properties diff --git a/.idea/compiler.xml b/.idea/compiler.xml deleted file mode 100644 index 96cc43ef..00000000 --- a/.idea/compiler.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml deleted file mode 100644 index e7bedf33..00000000 --- a/.idea/copyright/profiles_settings.xml +++ /dev/null @@ -1,3 +0,0 @@ - - - \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml deleted file mode 100644 index 97626ba4..00000000 --- a/.idea/encodings.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml deleted file mode 100644 index 9c8f7004..00000000 --- a/.idea/gradle.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/markdown-navigator.xml b/.idea/markdown-navigator.xml deleted file mode 100644 index d327c939..00000000 --- a/.idea/markdown-navigator.xml +++ /dev/null @@ -1,67 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/markdown-navigator/profiles_settings.xml b/.idea/markdown-navigator/profiles_settings.xml deleted file mode 100644 index 674a5916..00000000 --- a/.idea/markdown-navigator/profiles_settings.xml +++ /dev/null @@ -1,3 +0,0 @@ - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 5d199810..00000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,46 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 4e2e6b90..00000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml deleted file mode 100644 index 7f68460d..00000000 --- a/.idea/runConfigurations.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7f..00000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ae8a585a..5815edde 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -63,6 +63,9 @@ android:name=".ui.activity.PartFavoriteActivity" android:screenOrientation="portrait"/> + + diff --git a/app/src/main/java/com/hiroshi/cimoc/CimocApplication.java b/app/src/main/java/com/hiroshi/cimoc/CimocApplication.java index 8ab12ceb..4f21d771 100644 --- a/app/src/main/java/com/hiroshi/cimoc/CimocApplication.java +++ b/app/src/main/java/com/hiroshi/cimoc/CimocApplication.java @@ -56,8 +56,9 @@ public void initRootDocumentFile() { File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "Cimoc"); if (file.exists() || file.mkdirs()) { mDocumentFile = DocumentFile.fromFile(file); + } else { + mDocumentFile = null; } - mDocumentFile = DocumentFile.fromFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath())); } else if (uri.startsWith("content")) { mDocumentFile = DocumentFile.fromTreeUri(this, Uri.parse(uri)); } else { diff --git a/app/src/main/java/com/hiroshi/cimoc/core/Download.java b/app/src/main/java/com/hiroshi/cimoc/core/Download.java index af38265c..9bb22a0b 100644 --- a/app/src/main/java/com/hiroshi/cimoc/core/Download.java +++ b/app/src/main/java/com/hiroshi/cimoc/core/Download.java @@ -48,24 +48,22 @@ private static DocumentFile createComicIndex(DocumentFile root, Comic comic) { return null; } - public static Observable updateComicIndex(final List list, final Comic comic) { - return Observable.create(new Observable.OnSubscribe() { - @Override - public void call(Subscriber subscriber) { - ContentResolver resolver = CimocApplication.getResolver(); - DocumentFile root = CimocApplication.getDocumentFile(); - try { - createNoMedia(root); - String jsonString = getJsonFromComic(list, comic); - DocumentFile file = createComicIndex(root, comic); - DocumentUtils.writeStringToFile(resolver, file, "cimoc".concat(jsonString)); - subscriber.onNext(null); - subscriber.onCompleted(); - } catch (Exception e) { - subscriber.onError(e); - } - } - }).observeOn(Schedulers.io()); + /** + * 写漫画索引,不关心是否成功,若没有索引文件,则不能排序章节及扫描恢复漫画,但不影响下载及观看 + * @param list + * @param comic + */ + public static void updateComicIndex(List list, Comic comic) { + ContentResolver resolver = CimocApplication.getResolver(); + DocumentFile root = CimocApplication.getDocumentFile(); + try { + createNoMedia(root); + String jsonString = getJsonFromComic(list, comic); + DocumentFile file = createComicIndex(root, comic); + DocumentUtils.writeStringToFile(resolver, file, "cimoc".concat(jsonString)); + } catch (Exception e) { + e.printStackTrace(); + } } private static String getJsonFromComic(List list, Comic comic) throws JSONException { diff --git a/app/src/main/java/com/hiroshi/cimoc/core/manager/TaskManager.java b/app/src/main/java/com/hiroshi/cimoc/core/manager/TaskManager.java index a01f6437..39964e13 100644 --- a/app/src/main/java/com/hiroshi/cimoc/core/manager/TaskManager.java +++ b/app/src/main/java/com/hiroshi/cimoc/core/manager/TaskManager.java @@ -39,8 +39,9 @@ public Observable> listInRx() { .list(); } - public long insert(Task task) { - return mTaskDao.insert(task); + public void insert(Task task) { + long id = mTaskDao.insert(task); + task.setId(id); } public void update(Task task) { diff --git a/app/src/main/java/com/hiroshi/cimoc/global/FastClick.java b/app/src/main/java/com/hiroshi/cimoc/global/FastClick.java index 6acff0ad..7deec81a 100644 --- a/app/src/main/java/com/hiroshi/cimoc/global/FastClick.java +++ b/app/src/main/java/com/hiroshi/cimoc/global/FastClick.java @@ -10,7 +10,7 @@ public class FastClick { public static boolean isClickValid() { long cur = System.currentTimeMillis(); - boolean valid = cur - last > 800; + boolean valid = cur - last > 600; if (valid) { last = cur; } diff --git a/app/src/main/java/com/hiroshi/cimoc/presenter/ChapterPresenter.java b/app/src/main/java/com/hiroshi/cimoc/presenter/ChapterPresenter.java deleted file mode 100644 index 66e8b174..00000000 --- a/app/src/main/java/com/hiroshi/cimoc/presenter/ChapterPresenter.java +++ /dev/null @@ -1,89 +0,0 @@ -package com.hiroshi.cimoc.presenter; - -import com.hiroshi.cimoc.core.Download; -import com.hiroshi.cimoc.core.manager.ComicManager; -import com.hiroshi.cimoc.core.manager.TaskManager; -import com.hiroshi.cimoc.model.Chapter; -import com.hiroshi.cimoc.model.Comic; -import com.hiroshi.cimoc.model.MiniComic; -import com.hiroshi.cimoc.model.Task; -import com.hiroshi.cimoc.rx.RxBus; -import com.hiroshi.cimoc.rx.RxEvent; -import com.hiroshi.cimoc.ui.view.ChapterView; - -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.Callable; - -import rx.Observable; -import rx.android.schedulers.AndroidSchedulers; -import rx.functions.Action1; -import rx.functions.Func1; - -/** - * Created by Hiroshi on 2016/11/14. - */ - -public class ChapterPresenter extends BasePresenter { - - private ComicManager mComicManager; - private TaskManager mTaskManager; - private Comic mComic; - - public ChapterPresenter() { - mComicManager = ComicManager.getInstance(); - mTaskManager = TaskManager.getInstance(); - } - - public void load(long id) { - mComic = mComicManager.load(id); - } - - /** - * 添加任务到数据库 - * @param cList 所有章节列表,用于写索引文件 - * @param dList 下载章节列表 - */ - public void addTask(List cList, final List dList) { - mCompositeSubscription.add(Download.updateComicIndex(cList, mComic) - .flatMap(new Func1>>() { - @Override - public Observable> call(Void v) { - return mComicManager.callInRx(new Callable>() { - @Override - public ArrayList call() throws Exception { - Long key = mComic.getId(); - mComic.setDownload(System.currentTimeMillis()); - mComicManager.updateOrInsert(mComic); - ArrayList result = new ArrayList<>(); - for (Chapter chapter : dList) { - Task task = new Task(null, key, chapter.getPath(), chapter.getTitle(), 0, 0); - long id = mTaskManager.insert(task); - task.setId(id); - task.setSource(mComic.getSource()); - task.setCid(mComic.getCid()); - task.setState(Task.STATE_WAIT); - result.add(task); - } - return result; - } - }); - } - }) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe(new Action1>() { - @Override - public void call(ArrayList list) { - RxBus.getInstance().post(new RxEvent(RxEvent.EVENT_TASK_INSERT, new MiniComic(mComic), list)); - mBaseView.onTaskAddSuccess(list); - } - }, new Action1() { - @Override - public void call(Throwable throwable) { - throwable.printStackTrace(); - mBaseView.onTaskAddFail(); - } - })); - } - -} diff --git a/app/src/main/java/com/hiroshi/cimoc/presenter/DetailPresenter.java b/app/src/main/java/com/hiroshi/cimoc/presenter/DetailPresenter.java index c343c593..34a4d334 100644 --- a/app/src/main/java/com/hiroshi/cimoc/presenter/DetailPresenter.java +++ b/app/src/main/java/com/hiroshi/cimoc/presenter/DetailPresenter.java @@ -1,5 +1,6 @@ package com.hiroshi.cimoc.presenter; +import com.hiroshi.cimoc.core.Download; import com.hiroshi.cimoc.core.Manga; import com.hiroshi.cimoc.core.manager.ComicManager; import com.hiroshi.cimoc.core.manager.TagManager; @@ -12,10 +13,13 @@ import com.hiroshi.cimoc.rx.RxEvent; import com.hiroshi.cimoc.ui.view.DetailView; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import rx.Observable; +import rx.Subscriber; import rx.android.schedulers.AndroidSchedulers; import rx.functions.Action1; @@ -35,33 +39,27 @@ public DetailPresenter() { mTagManager = TagManager.getInstance(); } + @SuppressWarnings("unchecked") @Override protected void initSubscription() { addSubscription(RxEvent.EVENT_COMIC_CHAPTER_CHANGE, new Action1() { @Override public void call(RxEvent rxEvent) { - mComic = mComicManager.load(mComic.getId()); - mBaseView.onChapterChange(mComic.getLast()); + String path = (String) rxEvent.getData(); + mComic.setLast(path); + mBaseView.onLastChange(mComic.getLast()); } }); - addSubscription(RxEvent.EVENT_TASK_INSERT, new Action1() { - @Override - public void call(RxEvent rxEvent) { - mComic = mComicManager.load(((MiniComic) rxEvent.getData()).getId()); - } - }); - } - - public void loadDetail(int source, String cid) { - mComic = mComicManager.loadOrCreate(source, cid); - cancelHighlight(); - loadDetail(); } - public void loadDetail(long id) { - mComic = mComicManager.load(id); - cancelHighlight(); - loadDetail(); + public void load(long id, int source, String cid) { + if (id == -1) { + mComic = mComicManager.loadOrCreate(source, cid); + } else { + mComic = mComicManager.load(id); + cancelHighlight(); + } + load(); } private void updateChapterList(List list) { @@ -80,7 +78,7 @@ private void updateChapterList(List list) { } } - private void loadDetail() { + private void load() { mCompositeSubscription.add(Manga.info(mComic) .doOnNext(new Action1>() { @Override @@ -122,8 +120,9 @@ private void cancelHighlight() { * 更新最后阅读 * @param path 最后阅读 * @param favorite 是否从收藏界面进入 + * @return 漫画ID */ - public void updateLast(String path, boolean favorite) { + public long updateLast(String path, boolean favorite) { if (favorite && mComic.getFavorite() != null) { // 从收藏界面进入且没有取消收藏 mComic.setFavorite(System.currentTimeMillis()); } @@ -134,50 +133,78 @@ public void updateLast(String path, boolean favorite) { } mComicManager.updateOrInsert(mComic); RxBus.getInstance().post(new RxEvent(RxEvent.EVENT_COMIC_READ, new MiniComic(mComic), favorite)); + return mComic.getId(); } - public long getId() { - return mComic.getId() == null ? -1 : mComic.getId(); - } - - public void onFavoriteClick() { - if (mComic.getFavorite() != null) { - unfavoriteComic(); - } else { - favoriteComic(); - } - } - - public void onTagClick() { - if (mComic.getFavorite() != null) { - mBaseView.onTagOpenSuccess(); - } else { - mBaseView.onTagOpenFail(); - } + public Comic getComic() { + return mComic; } - public void onTitleClick(String path, boolean favorite) { - if (mComic.getLast() != null) { - path = mComic.getLast(); - } - updateLast(path, favorite); - mBaseView.onLastOpenSuccess(path); - } - - private void favoriteComic() { + public void favoriteComic() { mComic.setFavorite(System.currentTimeMillis()); mComicManager.updateOrInsert(mComic); RxBus.getInstance().post(new RxEvent(RxEvent.EVENT_COMIC_FAVORITE, new MiniComic(mComic))); - mBaseView.onFavoriteSuccess(); } - private void unfavoriteComic() { + public void unfavoriteComic() { long id = mComic.getId(); mComic.setFavorite(null); mTagManager.deleteByComic(id); mComicManager.updateOrDelete(mComic); RxBus.getInstance().post(new RxEvent(RxEvent.EVENT_COMIC_UNFAVORITE, id)); - mBaseView.onUnfavoriteSuccess(); + } + + private ArrayList getTaskList(List list) { + ArrayList result = new ArrayList<>(list.size()); + for (Chapter chapter : list) { + Task task = new Task(null, -1, chapter.getPath(), chapter.getTitle(), 0, 0); + task.setSource(mComic.getSource()); + task.setCid(mComic.getCid()); + task.setState(Task.STATE_WAIT); + result.add(task); + } + return result; + } + + /** + * 添加任务到数据库 + * @param cList 所有章节列表,用于写索引文件 + * @param dList 下载章节列表 + */ + public void addTask(final List cList, final List dList) { + mCompositeSubscription.add(Observable.create(new Observable.OnSubscribe>() { + @Override + public void call(Subscriber> subscriber) { + final ArrayList result = getTaskList(dList); + mComic.setDownload(System.currentTimeMillis()); + mComicManager.runInTx(new Runnable() { + @Override + public void run() { + mComicManager.updateOrInsert(mComic); + for (Task task : result) { + task.setKey(mComic.getId()); + mTaskManager.insert(task); + } + } + }); + Download.updateComicIndex(cList, mComic); + subscriber.onNext(result); + subscriber.onCompleted(); + } + }).observeOn(AndroidSchedulers.mainThread()) + .subscribe(new Action1>() { + @Override + public void call(ArrayList list) { + RxBus.getInstance().post(new RxEvent(RxEvent.EVENT_TASK_INSERT, new MiniComic(mComic), list)); + mBaseView.onTaskAddSuccess(list); + } + }, new Action1() { + @Override + public void call(Throwable throwable) { + + mBaseView.onTaskAddFail(); + } + })); } } diff --git a/app/src/main/java/com/hiroshi/cimoc/presenter/ReaderPresenter.java b/app/src/main/java/com/hiroshi/cimoc/presenter/ReaderPresenter.java index 01670d89..a4c74b85 100644 --- a/app/src/main/java/com/hiroshi/cimoc/presenter/ReaderPresenter.java +++ b/app/src/main/java/com/hiroshi/cimoc/presenter/ReaderPresenter.java @@ -124,7 +124,7 @@ private void updateChapter(Chapter chapter, boolean isNext) { mComic.setLast(chapter.getPath()); mComic.setPage(isNext ? 1 : chapter.getCount()); mComicManager.update(mComic); - RxBus.getInstance().post(new RxEvent(RxEvent.EVENT_COMIC_CHAPTER_CHANGE)); + RxBus.getInstance().post(new RxEvent(RxEvent.EVENT_COMIC_CHAPTER_CHANGE, chapter.getPath())); } public void savePicture(InputStream inputStream, String url) { diff --git a/app/src/main/java/com/hiroshi/cimoc/presenter/TagPresenter.java b/app/src/main/java/com/hiroshi/cimoc/presenter/TagPresenter.java index b5226c7e..75b2428a 100644 --- a/app/src/main/java/com/hiroshi/cimoc/presenter/TagPresenter.java +++ b/app/src/main/java/com/hiroshi/cimoc/presenter/TagPresenter.java @@ -2,7 +2,6 @@ import com.hiroshi.cimoc.core.manager.TagManager; import com.hiroshi.cimoc.model.Tag; -import com.hiroshi.cimoc.rx.RxBus; import com.hiroshi.cimoc.rx.RxEvent; import com.hiroshi.cimoc.ui.view.TagView; @@ -64,7 +63,6 @@ public void run() { .subscribe(new Action1() { @Override public void call(Void aVoid) { - RxBus.getInstance().post(new RxEvent(RxEvent.EVENT_TAG_DELETE, tag)); mBaseView.onTagDeleteSuccess(tag); } }, new Action1() { diff --git a/app/src/main/java/com/hiroshi/cimoc/presenter/TaskPresenter.java b/app/src/main/java/com/hiroshi/cimoc/presenter/TaskPresenter.java index 4139d484..0edf7c52 100644 --- a/app/src/main/java/com/hiroshi/cimoc/presenter/TaskPresenter.java +++ b/app/src/main/java/com/hiroshi/cimoc/presenter/TaskPresenter.java @@ -36,6 +36,7 @@ public TaskPresenter() { mComicManager = ComicManager.getInstance(); } + @SuppressWarnings("unchecked") @Override protected void initSubscription() { addSubscription(RxEvent.EVENT_TASK_STATE_CHANGE, new Action1() { @@ -72,20 +73,9 @@ public void call(RxEvent rxEvent) { addSubscription(RxEvent.EVENT_COMIC_CHAPTER_CHANGE, new Action1() { @Override public void call(RxEvent rxEvent) { - String last = (String) rxEvent.getData(); - int page = (int) rxEvent.getData(1); - mComic.setHistory(System.currentTimeMillis()); - mComic.setLast(last); - mComic.setPage(page); - mComicManager.update(mComic); - mBaseView.onChapterChange(last); - } - }); - addSubscription(RxEvent.EVENT_COMIC_PAGE_CHANGE, new Action1() { - @Override - public void call(RxEvent rxEvent) { - mComic.setPage((Integer) rxEvent.getData()); - mComicManager.update(mComic); + String path = (String) rxEvent.getData(); + mComic.setLast(path); + mBaseView.onLastChange(path); } }); } @@ -103,7 +93,7 @@ private void updateTaskList(List list) { } } - public void load(long id) { + public void load(long id, final boolean asc) { mComic = mComicManager.load(id); mCompositeSubscription.add(mTaskManager.listInRx(id) .doOnNext(new Action1>() { @@ -115,7 +105,11 @@ public void call(List list) { Collections.sort(list, new Comparator() { @Override public int compare(Task lhs, Task rhs) { - return sList.indexOf(lhs.getPath()) - sList.indexOf(rhs.getPath()); + if (asc) { + return sList.indexOf(rhs.getPath()) - sList.indexOf(lhs.getPath()); + } else { + return sList.indexOf(lhs.getPath()) - sList.indexOf(rhs.getPath()); + } } }); } @@ -130,6 +124,7 @@ public void call(List list) { }, new Action1() { @Override public void call(Throwable throwable) { + throwable.printStackTrace(); mBaseView.onTaskLoadFail(); } })); diff --git a/app/src/main/java/com/hiroshi/cimoc/rx/RxEvent.java b/app/src/main/java/com/hiroshi/cimoc/rx/RxEvent.java index 396924bb..b22bcfd0 100644 --- a/app/src/main/java/com/hiroshi/cimoc/rx/RxEvent.java +++ b/app/src/main/java/com/hiroshi/cimoc/rx/RxEvent.java @@ -13,9 +13,8 @@ public class RxEvent { public static final int EVENT_COMIC_FAVORITE = 1; public static final int EVENT_COMIC_UNFAVORITE = 2; public static final int EVENT_COMIC_READ = 3; - public static final int EVENT_COMIC_PAGE_CHANGE = 4; - public static final int EVENT_COMIC_CHAPTER_CHANGE = 5; - public static final int EVENT_COMIC_FAVORITE_RESTORE = 6; + public static final int EVENT_COMIC_CHAPTER_CHANGE = 4; + public static final int EVENT_COMIC_FAVORITE_RESTORE = 5; public static final int EVENT_TASK_STATE_CHANGE = 21; public static final int EVENT_TASK_PROCESS = 22; @@ -28,18 +27,17 @@ public class RxEvent { public static final int EVENT_SOURCE_ENABLE = 61; public static final int EVENT_SOURCE_DISABLE = 62; - public static final int EVENT_TAG_DELETE = 81; - public static final int EVENT_TAG_UPDATE = 82; - public static final int EVENT_TAG_RESTORE = 83; + public static final int EVENT_TAG_UPDATE = 81; + public static final int EVENT_TAG_RESTORE = 82; public static final int EVENT_THEME_CHANGE = 101; public static final int EVENT_DIALOG_PROGRESS = 121; - @IntDef({EVENT_COMIC_FAVORITE, EVENT_COMIC_UNFAVORITE, EVENT_COMIC_READ, EVENT_COMIC_PAGE_CHANGE, EVENT_COMIC_CHAPTER_CHANGE, - EVENT_COMIC_FAVORITE_RESTORE, EVENT_TASK_STATE_CHANGE, EVENT_TASK_PROCESS, EVENT_TASK_INSERT, EVENT_DOWNLOAD_REMOVE, - EVENT_DOWNLOAD_START, EVENT_DOWNLOAD_STOP, EVENT_SOURCE_ENABLE, EVENT_SOURCE_DISABLE, EVENT_TAG_DELETE, - EVENT_THEME_CHANGE, EVENT_TAG_UPDATE, EVENT_TAG_RESTORE, EVENT_DIALOG_PROGRESS}) + @IntDef({EVENT_COMIC_FAVORITE, EVENT_COMIC_UNFAVORITE, EVENT_COMIC_READ, EVENT_COMIC_CHAPTER_CHANGE, EVENT_COMIC_FAVORITE_RESTORE, + EVENT_TASK_STATE_CHANGE, EVENT_TASK_PROCESS, EVENT_TASK_INSERT, EVENT_DOWNLOAD_REMOVE, EVENT_DOWNLOAD_START, + EVENT_DOWNLOAD_STOP, EVENT_SOURCE_ENABLE, EVENT_SOURCE_DISABLE, EVENT_THEME_CHANGE, EVENT_TAG_UPDATE, EVENT_TAG_RESTORE, + EVENT_DIALOG_PROGRESS}) @Retention(RetentionPolicy.SOURCE) public @interface EventType {} diff --git a/app/src/main/java/com/hiroshi/cimoc/ui/activity/ChapterActivity.java b/app/src/main/java/com/hiroshi/cimoc/ui/activity/ChapterActivity.java index bfc7f7eb..173c56d9 100644 --- a/app/src/main/java/com/hiroshi/cimoc/ui/activity/ChapterActivity.java +++ b/app/src/main/java/com/hiroshi/cimoc/ui/activity/ChapterActivity.java @@ -1,5 +1,6 @@ package com.hiroshi.cimoc.ui.activity; +import android.app.Activity; import android.content.Context; import android.content.Intent; import android.support.v7.widget.LinearLayoutManager; @@ -10,11 +11,7 @@ import com.hiroshi.cimoc.R; import com.hiroshi.cimoc.model.Chapter; import com.hiroshi.cimoc.model.Pair; -import com.hiroshi.cimoc.model.Task; -import com.hiroshi.cimoc.presenter.ChapterPresenter; -import com.hiroshi.cimoc.service.DownloadService; import com.hiroshi.cimoc.ui.adapter.ChapterAdapter; -import com.hiroshi.cimoc.ui.view.ChapterView; import com.hiroshi.cimoc.utils.PermissionUtils; import java.util.ArrayList; @@ -26,17 +23,10 @@ * Created by Hiroshi on 2016/11/14. */ -public class ChapterActivity extends CoordinatorActivity implements ChapterView { +public class ChapterActivity extends CoordinatorActivity { - private ChapterPresenter mPresenter; private ChapterAdapter mChapterAdapter; - @Override - protected void initPresenter() { - mPresenter = new ChapterPresenter(); - mPresenter.attachView(this); - } - @Override protected void initView() { super.initView(); @@ -50,12 +40,6 @@ protected void initView() { hideProgressBar(); } - @Override - protected void initData() { - long id = getIntent().getLongExtra(EXTRA_ID, -1); - mPresenter.load(id); - } - private List> getAdapterList() { List list = getIntent().getParcelableArrayListExtra(EXTRA_CHAPTER); List> result = new ArrayList<>(list.size()); @@ -65,13 +49,6 @@ private List> getAdapterList() { return result; } - @Override - protected void onDestroy() { - mPresenter.detachView(); - mPresenter = null; - super.onDestroy(); - } - @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.chapter_menu, menu); @@ -103,56 +80,34 @@ public void onItemClick(View view, int position) { } @OnClick(R.id.coordinator_action_button) void onActionButtonClick() { - List cList = new ArrayList<>(); - List dList = new ArrayList<>(); + ArrayList list = new ArrayList<>(); for (Pair pair : mChapterAdapter.getDateSet()) { - cList.add(pair.first); if (!pair.first.isDownload() && pair.second) { - dList.add(pair.first); + list.add(pair.first); } } - if (dList.isEmpty()) { + if (list.isEmpty()) { showSnackbar(R.string.chapter_download_empty); } else if (PermissionUtils.hasStoragePermission(this)) { - showProgressDialog(); - mPresenter.addTask(cList, dList); + Intent intent = new Intent(); + intent.putParcelableArrayListExtra(EXTRA_CHAPTER, list); + setResult(Activity.RESULT_OK, intent); + finish(); } else { - onTaskAddFail(); + showSnackbar(R.string.chapter_download_perm_fail); } } - @Override - public void onTaskAddSuccess(ArrayList list) { - Intent intent = DownloadService.createIntent(this, list); - startService(intent); - for (Pair pair : mChapterAdapter.getDateSet()) { - if (pair.second && !pair.first.isDownload()) { - pair.first.setDownload(true); - } - } - mChapterAdapter.notifyDataSetChanged(); - showSnackbar(R.string.chapter_download_queue_success); - hideProgressDialog(); - } - - @Override - public void onTaskAddFail() { - hideProgressDialog(); - showSnackbar(R.string.chapter_download_queue_fail); - } - @Override protected String getDefaultTitle() { return getString(R.string.chapter); } - private static String EXTRA_ID = "a"; - private static String EXTRA_CHAPTER = "b"; + public static final String EXTRA_CHAPTER = "cimoc.intent.extra.EXTRA_CHAPTER"; - public static Intent createIntent(Context context, long id, ArrayList list) { + public static Intent createIntent(Context context, ArrayList list) { Intent intent = new Intent(context, ChapterActivity.class); - intent.putExtra(EXTRA_ID, id); intent.putExtra(EXTRA_CHAPTER, list); return intent; } diff --git a/app/src/main/java/com/hiroshi/cimoc/ui/activity/DetailActivity.java b/app/src/main/java/com/hiroshi/cimoc/ui/activity/DetailActivity.java index 68e94acf..93a7266a 100644 --- a/app/src/main/java/com/hiroshi/cimoc/ui/activity/DetailActivity.java +++ b/app/src/main/java/com/hiroshi/cimoc/ui/activity/DetailActivity.java @@ -1,5 +1,6 @@ package com.hiroshi.cimoc.ui.activity; +import android.app.Activity; import android.content.Context; import android.content.Intent; import android.support.v7.widget.GridLayoutManager; @@ -11,13 +12,16 @@ import com.hiroshi.cimoc.core.manager.PreferenceManager; import com.hiroshi.cimoc.model.Chapter; import com.hiroshi.cimoc.model.Comic; +import com.hiroshi.cimoc.model.Task; import com.hiroshi.cimoc.presenter.DetailPresenter; +import com.hiroshi.cimoc.service.DownloadService; import com.hiroshi.cimoc.ui.adapter.DetailAdapter; import com.hiroshi.cimoc.ui.view.DetailView; import java.util.ArrayList; -import java.util.LinkedList; +import java.util.HashSet; import java.util.List; +import java.util.Set; import butterknife.OnClick; @@ -26,6 +30,8 @@ */ public class DetailActivity extends CoordinatorActivity implements DetailView, DetailAdapter.OnTitleClickListener { + public static final int REQUEST_CODE_DOWNLOAD = 0; + private DetailAdapter mDetailAdapter; private DetailPresenter mPresenter; @@ -38,7 +44,7 @@ protected void initPresenter() { @Override protected void initView() { super.initView(); - mDetailAdapter = new DetailAdapter(this, new LinkedList()); + mDetailAdapter = new DetailAdapter(this, new ArrayList()); mRecyclerView.setItemAnimator(null); mRecyclerView.setLayoutManager(new GridLayoutManager(this, 4)); mRecyclerView.setAdapter(mDetailAdapter); @@ -49,13 +55,9 @@ protected void initView() { @Override protected void initData() { long id = getIntent().getLongExtra(EXTRA_ID, -1); - if (id == -1) { - int source = getIntent().getIntExtra(EXTRA_SOURCE, -1); - String cid = getIntent().getStringExtra(EXTRA_CID); - mPresenter.loadDetail(source, cid); - } else { - mPresenter.loadDetail(id); - } + int source = getIntent().getIntExtra(EXTRA_SOURCE, -1); + String cid = getIntent().getStringExtra(EXTRA_CID); + mPresenter.load(id, source, cid); } @Override @@ -73,80 +75,110 @@ public boolean onCreateOptionsMenu(Menu menu) { @Override public boolean onOptionsItemSelected(MenuItem item) { + Intent intent; if (!isProgressBarShown()) { switch (item.getItemId()) { case R.id.detail_download: - Intent intent = ChapterActivity.createIntent(this, mPresenter.getId(), new ArrayList<>(mDetailAdapter.getDateSet())); - startActivity(intent); + intent = ChapterActivity.createIntent(this, new ArrayList<>(mDetailAdapter.getDateSet())); + startActivityForResult(intent, REQUEST_CODE_DOWNLOAD); break; case R.id.detail_tag: - mPresenter.onTagClick(); + if (mPresenter.getComic().getFavorite() != null) { + intent = TagEditorActivity.createIntent(this, mPresenter.getComic().getId()); + startActivity(intent); + } else { + showSnackbar(R.string.detail_tag_favorite); + } break; } } return super.onOptionsItemSelected(item); } - @OnClick(R.id.coordinator_action_button) void onActionButtonClick() { - mPresenter.onFavoriteClick(); - } - - @Override - public void onTagOpenFail() { - showSnackbar(R.string.detail_tag_favorite); - } - - @Override - public void onTagOpenSuccess() { - Intent intent = TagEditorActivity.createIntent(this, mPresenter.getId()); - startActivity(intent); - } - @Override - public void onFavoriteSuccess() { - mActionButton.setImageResource(R.drawable.ic_favorite_white_24dp); - showSnackbar(R.string.detail_favorite); + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + super.onActivityResult(requestCode, resultCode, data); + if (resultCode == Activity.RESULT_OK) { + switch (requestCode) { + case REQUEST_CODE_DOWNLOAD: + showProgressDialog(); + List list = data.getParcelableArrayListExtra(ChapterActivity.EXTRA_CHAPTER); + mPresenter.addTask(mDetailAdapter.getDateSet(), list); + break; + } + } } - @Override - public void onUnfavoriteSuccess() { - mActionButton.setImageResource(R.drawable.ic_favorite_border_white_24dp); - showSnackbar(R.string.detail_unfavorite); + @OnClick(R.id.coordinator_action_button) void onActionButtonClick() { + if (mPresenter.getComic().getFavorite() != null) { + mPresenter.unfavoriteComic(); + mActionButton.setImageResource(R.drawable.ic_favorite_border_white_24dp); + showSnackbar(R.string.detail_unfavorite); + } else { + mPresenter.favoriteComic(); + mActionButton.setImageResource(R.drawable.ic_favorite_white_24dp); + showSnackbar(R.string.detail_favorite); + } } @Override public void onItemClick(View view, int position) { if (position != 0) { - String last = mDetailAdapter.getItem(position - 1).getPath(); - mPresenter.updateLast(last, getIntent().getBooleanExtra(EXTRA_FAVORITE, false)); - mDetailAdapter.setLast(last); - int mode = mPreference.getInt(PreferenceManager.PREF_READER_MODE, PreferenceManager.READER_MODE_PAGE); - Intent intent = ReaderActivity.createIntent(this, mPresenter.getId(), mDetailAdapter.getDateSet(), mode); - startActivity(intent); + String path = mDetailAdapter.getItem(position - 1).getPath(); + startReader(path); } } @Override public void onTitleClick() { - String path = mDetailAdapter.getItem(mDetailAdapter.getDateSet().size() - 1).getPath(); - boolean favorite = getIntent().getBooleanExtra(EXTRA_FAVORITE, false); - mPresenter.onTitleClick(path, favorite); + String path = mPresenter.getComic().getLast(); + if (path != null) { + path = mDetailAdapter.getItem(mDetailAdapter.getDateSet().size() - 1).getPath(); + } + startReader(path); } - @Override - public void onLastOpenSuccess(String path) { + private void startReader(String path) { + boolean favorite = getIntent().getBooleanExtra(EXTRA_FAVORITE, false); + long id = mPresenter.updateLast(path, favorite); mDetailAdapter.setLast(path); - List list = mDetailAdapter.getDateSet(); int mode = mPreference.getInt(PreferenceManager.PREF_READER_MODE, PreferenceManager.READER_MODE_PAGE); - Intent intent = ReaderActivity.createIntent(DetailActivity.this, mPresenter.getId(), list, mode); + Intent intent = ReaderActivity.createIntent(DetailActivity.this, id, mDetailAdapter.getDateSet(), mode); startActivity(intent); } @Override - public void onChapterChange(String last) { + public void onLastChange(String last) { mDetailAdapter.setLast(last); } + @Override + public void onTaskAddSuccess(ArrayList list) { + Intent intent = DownloadService.createIntent(this, list); + startService(intent); + updateChapterList(list); + showSnackbar(R.string.detail_download_queue_success); + hideProgressDialog(); + } + + private void updateChapterList(List list) { + Set set = new HashSet<>(); + for (Task task : list) { + set.add(task.getPath()); + } + for (Chapter chapter : mDetailAdapter.getDateSet()) { + if (set.contains(chapter.getPath())) { + chapter.setDownload(true); + } + } + } + + @Override + public void onTaskAddFail() { + hideProgressDialog(); + showSnackbar(R.string.detail_download_queue_fail); + } + @Override public void onComicLoadSuccess(Comic comic) { mDetailAdapter.setInfo(comic.getSource(), comic.getCover(), comic.getTitle(), comic.getAuthor(), @@ -167,12 +199,6 @@ public void onChapterLoadSuccess(List list) { mDetailAdapter.addAll(list); } - @Override - public void onDownloadLoadFail() { - hideProgressBar(); - showSnackbar(R.string.detail_download_load_fail); - } - @Override public void onNetworkError() { hideProgressBar(); diff --git a/app/src/main/java/com/hiroshi/cimoc/ui/activity/DirPickerActivity.java b/app/src/main/java/com/hiroshi/cimoc/ui/activity/DirPickerActivity.java new file mode 100644 index 00000000..c667832b --- /dev/null +++ b/app/src/main/java/com/hiroshi/cimoc/ui/activity/DirPickerActivity.java @@ -0,0 +1,100 @@ +package com.hiroshi.cimoc.ui.activity; + +import android.app.Activity; +import android.content.Intent; +import android.os.Environment; +import android.support.v4.provider.DocumentFile; +import android.support.v7.widget.LinearLayoutManager; +import android.util.Log; +import android.view.View; + +import com.hiroshi.cimoc.R; +import com.hiroshi.cimoc.ui.adapter.DirAdapter; + +import java.io.File; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import butterknife.OnClick; + +/** + * Created by Hiroshi on 2016/12/6. + */ + +public class DirPickerActivity extends CoordinatorActivity { + + public static final String EXTRA_PICKER_PATH = "cimoc.intent.extra.EXTRA_PICKER_PATH"; + + private DirAdapter mDirAdapter; + private File mFile; + + @Override + protected void initView() { + super.initView(); + mDirAdapter = new DirAdapter(this, new ArrayList()); + mDirAdapter.setOnItemClickListener(this); + mRecyclerView.setHasFixedSize(true); + mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); + mRecyclerView.setAdapter(mDirAdapter); + mActionButton.setImageResource(R.drawable.ic_done_white_24dp); + mActionButton.show(); + } + + @Override + protected void initData() { + mFile = Environment.getExternalStorageDirectory(); + updateData(); + hideProgressBar(); + } + + @OnClick(R.id.coordinator_action_button) void onActionButtonClick() { + Intent intent = new Intent(); + intent.putExtra(EXTRA_PICKER_PATH, mFile.getAbsolutePath()); + setResult(Activity.RESULT_OK, intent); + finish(); + } + + @Override + public void onItemClick(View view, int position) { + if (position == 0) { + if (mFile.getParentFile() == null) { + return; + } + mFile = mFile.getParentFile(); + } else { + String title = mDirAdapter.getItem(position); + mFile = new File(mFile.getAbsolutePath(), title); + } + updateData(); + mActionButton.show(); + } + + private void updateData() { + mDirAdapter.setData(listDir(mFile)); + if (mToolbar != null) { + mToolbar.setTitle(mFile.getAbsolutePath()); + } + } + + private List listDir(File parent) { + List list = new ArrayList<>(); + File[] files = parent.listFiles(); + if (files != null) { + for (File dir : parent.listFiles()) { + if (dir.isDirectory()) { + list.add(dir.getName()); + } + } + Collections.sort(list); + } + list.add(0, getString(R.string.dir_picker_parent)); + return list; + } + + @Override + protected String getDefaultTitle() { + return getString(R.string.dir_picker); + } + +} diff --git a/app/src/main/java/com/hiroshi/cimoc/ui/activity/MainActivity.java b/app/src/main/java/com/hiroshi/cimoc/ui/activity/MainActivity.java index 1fa8d0a6..66b4b3f2 100644 --- a/app/src/main/java/com/hiroshi/cimoc/ui/activity/MainActivity.java +++ b/app/src/main/java/com/hiroshi/cimoc/ui/activity/MainActivity.java @@ -10,6 +10,7 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.annotation.StyleRes; +import android.support.design.widget.CheckableImageButton; import android.support.design.widget.NavigationView; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; @@ -255,6 +256,7 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis switch (requestCode) { case 0: if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { + ((CimocApplication) getApplication()).initRootDocumentFile(); showSnackbar(R.string.main_permission_success); } else { showSnackbar(R.string.main_permission_fail); diff --git a/app/src/main/java/com/hiroshi/cimoc/ui/activity/SettingsActivity.java b/app/src/main/java/com/hiroshi/cimoc/ui/activity/SettingsActivity.java index f350271b..fa239714 100644 --- a/app/src/main/java/com/hiroshi/cimoc/ui/activity/SettingsActivity.java +++ b/app/src/main/java/com/hiroshi/cimoc/ui/activity/SettingsActivity.java @@ -10,6 +10,7 @@ import android.support.v4.content.ContextCompat; import android.support.v4.provider.DocumentFile; import android.support.v7.widget.AppCompatCheckBox; +import android.util.Log; import android.view.View; import android.widget.CheckBox; import android.widget.TextView; @@ -27,6 +28,7 @@ import com.hiroshi.cimoc.utils.ServiceUtils; import com.hiroshi.cimoc.utils.ThemeUtils; +import java.io.File; import java.util.List; import butterknife.BindView; @@ -50,6 +52,7 @@ public class SettingsActivity extends BackActivity implements SettingsView { @BindView(R.id.settings_layout) View mSettingsLayout; @BindView(R.id.settings_reader_bright_checkbox) AppCompatCheckBox mBrightBox; @BindView(R.id.settings_reader_hide_checkbox) AppCompatCheckBox mHideBox; + @BindView(R.id.settings_download_order_checkbox) AppCompatCheckBox mOrderBox; private SettingsPresenter mPresenter; @@ -76,6 +79,7 @@ protected void initView() { mConnectionValue = mPreference.getInt(PreferenceManager.PREF_DOWNLOAD_CONNECTION, 0); mBrightBox.setChecked(mPreference.getBoolean(PreferenceManager.PREF_READER_KEEP_ON, false)); mHideBox.setChecked(mPreference.getBoolean(PreferenceManager.PREF_READER_HIDE_INFO, false)); + mOrderBox.setChecked(mPreference.getBoolean(PreferenceManager.PREF_DOWNLOAD_ORDER, false)); } @Override @@ -85,7 +89,7 @@ public void onDestroy() { super.onDestroy(); } - @OnClick({R.id.settings_reader_bright_btn, R.id.settings_reader_hide_btn}) + @OnClick({R.id.settings_reader_bright_btn, R.id.settings_reader_hide_btn, R.id.settings_download_order_btn}) void onCheckBoxClick(View view) { switch (view.getId()) { case R.id.settings_reader_bright_btn: @@ -94,6 +98,9 @@ void onCheckBoxClick(View view) { case R.id.settings_reader_hide_btn: checkedAndSave(mHideBox, PreferenceManager.PREF_READER_HIDE_INFO); break; + case R.id.settings_download_order_btn: + checkedAndSave(mOrderBox, PreferenceManager.PREF_DOWNLOAD_ORDER); + break; } } @@ -128,18 +135,24 @@ private void checkedAndSave(CheckBox box, String key) { @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { - super.onActivityResult(requestCode, resultCode, data); + //super.onActivityResult(requestCode, resultCode, data); if (resultCode == Activity.RESULT_OK) { switch (requestCode) { case DIALOG_REQUEST_OTHER_STORAGE: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // Todo release permission ? + showProgressDialog(); Uri uri = data.getData(); int flags = data.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); getContentResolver().takePersistableUriPermission(uri, flags); - showProgressDialog(); - mPresenter.moveFiles(DocumentFile.fromTreeUri(this, uri)); mTempStorage = uri.toString(); + mPresenter.moveFiles(DocumentFile.fromTreeUri(this, uri)); + } else { + showProgressDialog(); + String path = data.getStringExtra(DirPickerActivity.EXTRA_PICKER_PATH); + DocumentFile file = DocumentFile.fromFile(new File(path)); + mTempStorage = file.getUri().toString(); + mPresenter.moveFiles(file); } break; } @@ -192,6 +205,7 @@ private void changeTheme(int primary, int accent) { new int[]{0x8A000000, ContextCompat.getColor(this, accent)}); mBrightBox.setSupportButtonTintList(stateList); mHideBox.setSupportButtonTintList(stateList); + mOrderBox.setSupportButtonTintList(stateList); } @OnClick(R.id.settings_other_storage_btn) void onOtherStorageClick() { @@ -219,17 +233,17 @@ private void changeTheme(int primary, int accent) { @Override public void onFileMoveSuccess() { + hideProgressDialog(); mPreference.putString(PreferenceManager.PREF_OTHER_STORAGE, mTempStorage); mStoragePath = mTempStorage; ((CimocApplication) getApplication()).initRootDocumentFile(); showSnackbar(R.string.settings_other_storage_move_success); - hideProgressDialog(); } @Override public void onFileMoveFail() { - showSnackbar(R.string.settings_other_storage_move_fail); hideProgressDialog(); + showSnackbar(R.string.settings_other_storage_move_fail); } @Override diff --git a/app/src/main/java/com/hiroshi/cimoc/ui/activity/TaskActivity.java b/app/src/main/java/com/hiroshi/cimoc/ui/activity/TaskActivity.java index b0d6df78..847fa872 100644 --- a/app/src/main/java/com/hiroshi/cimoc/ui/activity/TaskActivity.java +++ b/app/src/main/java/com/hiroshi/cimoc/ui/activity/TaskActivity.java @@ -68,7 +68,8 @@ protected void initView() { @Override protected void initData() { long key = getIntent().getLongExtra(EXTRA_KEY, -1); - mPresenter.load(key); + boolean asc = mPreference.getBoolean(PreferenceManager.PREF_DOWNLOAD_ORDER, false); + mPresenter.load(key, asc); } @Override @@ -96,8 +97,8 @@ public boolean onCreateOptionsMenu(Menu menu) { } @Override - public void onChapterChange(String last) { - mTaskAdapter.setLast(mPresenter.getComic().getLast()); + public void onLastChange(String path) { + mTaskAdapter.setLast(path); } @Override diff --git a/app/src/main/java/com/hiroshi/cimoc/ui/adapter/DirAdapter.java b/app/src/main/java/com/hiroshi/cimoc/ui/adapter/DirAdapter.java new file mode 100644 index 00000000..d514fa86 --- /dev/null +++ b/app/src/main/java/com/hiroshi/cimoc/ui/adapter/DirAdapter.java @@ -0,0 +1,56 @@ +package com.hiroshi.cimoc.ui.adapter; + +import android.content.Context; +import android.support.v7.widget.RecyclerView; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import com.hiroshi.cimoc.R; + +import java.util.List; + +import butterknife.BindView; + +/** + * Created by Hiroshi on 2016/12/6. + */ + +public class DirAdapter extends BaseAdapter { + + static class DirHolder extends BaseAdapter.BaseViewHolder { + @BindView(R.id.item_dir_title) TextView mDirTitle; + + DirHolder(View view) { + super(view); + } + } + + public DirAdapter(Context context, List list) { + super(context, list); + } + + @Override + public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + View view = mInflater.inflate(R.layout.item_dir, parent, false); + return new DirHolder(view); + } + + @Override + public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { + super.onBindViewHolder(holder, position); + DirHolder viewHolder = (DirHolder) holder; + viewHolder.mDirTitle.setText(mDataSet.get(position)); + } + + @Override + public RecyclerView.ItemDecoration getItemDecoration() { + return null; + } + + @Override + protected boolean isClickValid() { + return true; + } + +} diff --git a/app/src/main/java/com/hiroshi/cimoc/ui/fragment/ComicFragment.java b/app/src/main/java/com/hiroshi/cimoc/ui/fragment/ComicFragment.java index ba934aee..3235d05d 100644 --- a/app/src/main/java/com/hiroshi/cimoc/ui/fragment/ComicFragment.java +++ b/app/src/main/java/com/hiroshi/cimoc/ui/fragment/ComicFragment.java @@ -96,6 +96,7 @@ public void onDialogResult(int requestCode, Bundle bundle) { int index = bundle.getInt(EXTRA_DIALOG_RESULT_INDEX); Intent intent = PartFavoriteActivity.createIntent(getActivity(), mTagList.get(index).getId(), mTagList.get(index).getTitle()); startActivity(intent); + break; } } @@ -108,14 +109,18 @@ public void showSnackbar(String msg) { public void onTagLoadSuccess(List list) { hideProgressDialog(); mTagList = list; - int size = list.size(); - String[] item = new String[size]; - for (int i = 0; i < size; ++i) { - item[i] = list.get(i).getTitle(); + if (!list.isEmpty()) { + int size = list.size(); + String[] item = new String[size]; + for (int i = 0; i < size; ++i) { + item[i] = list.get(i).getTitle(); + } + ItemDialogFragment fragment = ItemDialogFragment.newInstance(R.string.comic_tag_select, item, DIALOG_REQUEST_FILTER); + fragment.setTargetFragment(this, 0); + fragment.show(getFragmentManager(), null); + } else { + showSnackbar(R.string.comic_load_tag_empty); } - ItemDialogFragment fragment = ItemDialogFragment.newInstance(R.string.comic_tag_select, item, DIALOG_REQUEST_FILTER); - fragment.setTargetFragment(this, 0); - fragment.show(getFragmentManager(), null); } @Override diff --git a/app/src/main/java/com/hiroshi/cimoc/ui/fragment/SearchFragment.java b/app/src/main/java/com/hiroshi/cimoc/ui/fragment/SearchFragment.java index 1674c607..c0222c39 100644 --- a/app/src/main/java/com/hiroshi/cimoc/ui/fragment/SearchFragment.java +++ b/app/src/main/java/com/hiroshi/cimoc/ui/fragment/SearchFragment.java @@ -77,6 +77,7 @@ public void afterTextChanged(Editable s) {} @Override protected void initData() { + mSourceList = new ArrayList<>(); mPresenter.load(); } @@ -106,18 +107,20 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.search_menu_source: - int size = mSourceList.size(); - String[] arr1 = new String[size]; - boolean[] arr2 = new boolean[size]; - for (int i = 0; i < size; ++i) { - arr1[i] = mSourceList.get(i).getTitle(); - arr2[i] = mSourceList.get(i).getCheck(); + if (!mSourceList.isEmpty()) { + int size = mSourceList.size(); + String[] arr1 = new String[size]; + boolean[] arr2 = new boolean[size]; + for (int i = 0; i < size; ++i) { + arr1[i] = mSourceList.get(i).getTitle(); + arr2[i] = mSourceList.get(i).getCheck(); + } + MultiDialogFragment fragment = + MultiDialogFragment.newInstance(R.string.search_source_select, arr1, arr2, DIALOG_REQUEST_SOURCE); + fragment.setTargetFragment(this, 0); + fragment.show(getFragmentManager(), null); + break; } - MultiDialogFragment fragment = - MultiDialogFragment.newInstance(R.string.search_source_select, arr1, arr2, -1); - fragment.setTargetFragment(this, 0); - fragment.show(getFragmentManager(), null); - break; } return super.onOptionsItemSelected(item); } @@ -165,8 +168,8 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { @Override public void onSourceLoadSuccess(List list) { - mSourceList = new ArrayList<>(list); hideProgressBar(); + mSourceList.addAll(list); } @Override diff --git a/app/src/main/java/com/hiroshi/cimoc/ui/fragment/dialog/MultiDialogFragment.java b/app/src/main/java/com/hiroshi/cimoc/ui/fragment/dialog/MultiDialogFragment.java index 1f7b9951..4e4baffa 100644 --- a/app/src/main/java/com/hiroshi/cimoc/ui/fragment/dialog/MultiDialogFragment.java +++ b/app/src/main/java/com/hiroshi/cimoc/ui/fragment/dialog/MultiDialogFragment.java @@ -5,6 +5,7 @@ import android.content.DialogInterface; import android.os.Bundle; import android.support.v7.app.AlertDialog; +import android.util.SparseBooleanArray; import com.hiroshi.cimoc.R; import com.hiroshi.cimoc.ui.view.DialogView; @@ -15,29 +16,32 @@ public class MultiDialogFragment extends DialogFragment implements DialogInterface.OnClickListener { - private boolean[] mCheck; - @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); String[] item = getArguments().getStringArray(DialogView.EXTRA_DIALOG_ITEMS); - mCheck = getArguments().getBooleanArray(DialogView.EXTRA_DIALOG_CHOICE_ITEMS); + boolean[] check = getArguments().getBooleanArray(DialogView.EXTRA_DIALOG_CHOICE_ITEMS); builder.setTitle(getArguments().getInt(DialogView.EXTRA_DIALOG_TITLE)) - .setMultiChoiceItems(item, mCheck, new DialogInterface.OnMultiChoiceClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int index, boolean value) { - mCheck[index] = value; - } - }) + .setMultiChoiceItems(item, check, null) .setPositiveButton(R.string.dialog_positive, this); return builder.create(); } + private boolean[] getCheckedArray(DialogInterface dialog) { + SparseBooleanArray array = ((AlertDialog) dialog).getListView().getCheckedItemPositions(); + int size = array.size(); + boolean[] result = new boolean[size]; + for (int i = 0; i != size; ++i) { + result[i] = array.valueAt(i); + } + return result; + } + @Override public void onClick(DialogInterface dialogInterface, int which) { int requestCode = getArguments().getInt(DialogView.EXTRA_DIALOG_REQUEST_CODE); Bundle bundle = new Bundle(); - bundle.putBooleanArray(DialogView.EXTRA_DIALOG_RESULT_VALUE, mCheck); + bundle.putBooleanArray(DialogView.EXTRA_DIALOG_RESULT_VALUE, getCheckedArray(dialogInterface)); DialogView target = (DialogView) (getTargetFragment() != null ? getTargetFragment() : getActivity()); target.onDialogResult(requestCode, bundle); } diff --git a/app/src/main/java/com/hiroshi/cimoc/ui/fragment/dialog/StorageEditorDialogFragment.java b/app/src/main/java/com/hiroshi/cimoc/ui/fragment/dialog/StorageEditorDialogFragment.java index e4dc0443..201c3d94 100644 --- a/app/src/main/java/com/hiroshi/cimoc/ui/fragment/dialog/StorageEditorDialogFragment.java +++ b/app/src/main/java/com/hiroshi/cimoc/ui/fragment/dialog/StorageEditorDialogFragment.java @@ -8,6 +8,7 @@ import android.support.v7.app.AlertDialog; import com.hiroshi.cimoc.R; +import com.hiroshi.cimoc.ui.activity.DirPickerActivity; import com.hiroshi.cimoc.ui.view.DialogView; /** @@ -24,10 +25,13 @@ public Dialog onCreateDialog(Bundle savedInstanceState) { dialog.setButton(DialogInterface.BUTTON_POSITIVE, title, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int which) { + int requestCode = getArguments().getInt(DialogView.EXTRA_DIALOG_REQUEST_CODE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - int requestCode = getArguments().getInt(DialogView.EXTRA_DIALOG_REQUEST_CODE); Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); getActivity().startActivityForResult(intent, requestCode); + } else { + Intent intent = new Intent(getActivity(), DirPickerActivity.class); + getActivity().startActivityForResult(intent, requestCode); } } }); diff --git a/app/src/main/java/com/hiroshi/cimoc/ui/view/ChapterView.java b/app/src/main/java/com/hiroshi/cimoc/ui/view/ChapterView.java deleted file mode 100644 index 8e3f2e27..00000000 --- a/app/src/main/java/com/hiroshi/cimoc/ui/view/ChapterView.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.hiroshi.cimoc.ui.view; - -import com.hiroshi.cimoc.model.Task; - -import java.util.ArrayList; - -/** - * Created by Hiroshi on 2016/11/14. - */ - -public interface ChapterView extends BaseView { - - void onTaskAddSuccess(ArrayList list); - - void onTaskAddFail(); - -} diff --git a/app/src/main/java/com/hiroshi/cimoc/ui/view/DetailView.java b/app/src/main/java/com/hiroshi/cimoc/ui/view/DetailView.java index 5981ab75..e217a1a8 100644 --- a/app/src/main/java/com/hiroshi/cimoc/ui/view/DetailView.java +++ b/app/src/main/java/com/hiroshi/cimoc/ui/view/DetailView.java @@ -2,7 +2,9 @@ import com.hiroshi.cimoc.model.Chapter; import com.hiroshi.cimoc.model.Comic; +import com.hiroshi.cimoc.model.Task; +import java.util.ArrayList; import java.util.List; /** @@ -14,22 +16,14 @@ public interface DetailView extends BaseView { void onChapterLoadSuccess(List list); - void onChapterChange(String chapter); - - void onDownloadLoadFail(); + void onLastChange(String chapter); void onParseError(); void onNetworkError(); - void onTagOpenSuccess(); - - void onTagOpenFail(); - - void onLastOpenSuccess(String path); - - void onFavoriteSuccess(); + void onTaskAddSuccess(ArrayList list); - void onUnfavoriteSuccess(); + void onTaskAddFail(); } diff --git a/app/src/main/java/com/hiroshi/cimoc/ui/view/TaskView.java b/app/src/main/java/com/hiroshi/cimoc/ui/view/TaskView.java index ca3ea6fa..045de340 100644 --- a/app/src/main/java/com/hiroshi/cimoc/ui/view/TaskView.java +++ b/app/src/main/java/com/hiroshi/cimoc/ui/view/TaskView.java @@ -13,7 +13,7 @@ public interface TaskView extends BaseView, DialogView { void onTaskLoadFail(); - void onChapterChange(String last); + void onLastChange(String path); void onTaskAdd(List list); diff --git a/app/src/main/res/layout/activity_settings.xml b/app/src/main/res/layout/activity_settings.xml index 08a0169e..456ac432 100644 --- a/app/src/main/res/layout/activity_settings.xml +++ b/app/src/main/res/layout/activity_settings.xml @@ -141,66 +141,76 @@ android:text="@string/settings_reader_hide_summary"/> - - - + android:layout_height="wrap_content"> - - - + - + + + + + android:gravity="center_vertical" + android:padding="16dp" + android:clickable="true" + android:background="?android:attr/selectableItemBackground"> + + + + - + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d3fda117..4cef2e41 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -38,7 +38,8 @@ 标签 请先收藏漫画 下载 - 读取下载列表失败 + 已加入下载队列 + 加入下载队列失败 标签编辑 没有找到标签 @@ -49,15 +50,15 @@ 章节列表 全选 + 获取权限失败 未选择章节 - 已加入下载队列 - 加入下载队列失败 收藏 历史 下载 过滤 读取标签失败 + 没有找到标签 标签选择 检查更新 @@ -203,6 +204,9 @@ 批量删除 删除所有 + 目录选择 + < 返回上层目录 > + 事件选择 上一页