From b1705203701cfe22037239fd8424f2269c0edd58 Mon Sep 17 00:00:00 2001 From: inorichi Date: Mon, 11 Jan 2016 18:21:17 +0100 Subject: [PATCH] Add an option to automatically update last chapter read and another one to ask before updating. Fixes #10 --- .../data/database/models/MangaSync.java | 2 + .../data/preference/PreferencesHelper.java | 8 ++ .../ui/library/LibraryPresenter.java | 2 - .../mangafeed/ui/reader/ReaderActivity.java | 32 ++++++++ .../mangafeed/ui/reader/ReaderPresenter.java | 79 +++++++++++-------- app/src/main/res/values/keys.xml | 2 + app/src/main/res/values/strings.xml | 3 + app/src/main/res/xml/pref_general.xml | 11 +++ 8 files changed, 106 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/eu/kanade/mangafeed/data/database/models/MangaSync.java b/app/src/main/java/eu/kanade/mangafeed/data/database/models/MangaSync.java index deb6e6c33..ef63321ab 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/database/models/MangaSync.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/database/models/MangaSync.java @@ -38,6 +38,8 @@ public class MangaSync implements Serializable { @StorIOSQLiteColumn(name = MangaSyncTable.COLUMN_STATUS) public int status; + public boolean update; + public static MangaSync create(MangaSyncService service) { MangaSync mangasync = new MangaSync(); mangasync.sync_id = service.getId(); diff --git a/app/src/main/java/eu/kanade/mangafeed/data/preference/PreferencesHelper.java b/app/src/main/java/eu/kanade/mangafeed/data/preference/PreferencesHelper.java index b48bf0c7b..44cab3da4 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/preference/PreferencesHelper.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/preference/PreferencesHelper.java @@ -101,6 +101,14 @@ public class PreferencesHelper { return prefs.getBoolean(getKey(R.string.pref_update_only_non_completed_key), false); } + public boolean autoUpdateMangaSync() { + return prefs.getBoolean(getKey(R.string.pref_auto_update_manga_sync_key), true); + } + + public boolean askUpdateMangaSync() { + return prefs.getBoolean(getKey(R.string.pref_ask_update_manga_sync_key), false); + } + public Preference imageDecoder() { return rxPrefs.getInteger(getKey(R.string.pref_image_decoder_key), 0); } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryPresenter.java b/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryPresenter.java index fe75a9ca6..8e3992a34 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryPresenter.java @@ -81,8 +81,6 @@ public class LibraryPresenter extends BasePresenter { .toMap(pair -> pair.first, pair -> pair.second)); } - - public void onOpenManga(Manga manga) { // Avoid further db updates for the library when it's not needed stop(GET_LIBRARY); diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/reader/ReaderActivity.java b/app/src/main/java/eu/kanade/mangafeed/ui/reader/ReaderActivity.java index cc67ea3d6..bb7a31057 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/reader/ReaderActivity.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/reader/ReaderActivity.java @@ -16,6 +16,8 @@ import android.view.View; import android.view.WindowManager; import android.widget.TextView; +import com.afollestad.materialdialogs.MaterialDialog; + import java.util.List; import javax.inject.Inject; @@ -126,6 +128,36 @@ public class ReaderActivity extends BaseRxActivity { super.onSaveInstanceState(outState); } + @Override + public void onBackPressed() { + if (viewer != null) + getPresenter().setCurrentPage(viewer.getCurrentPage()); + getPresenter().onChapterLeft(); + + int chapterToUpdate = getPresenter().getMangaSyncChapterToUpdate(); + + if (chapterToUpdate > 0) { + if (getPresenter().prefs.askUpdateMangaSync()) { + new MaterialDialog.Builder(this) + .content(getString(R.string.confirm_update_manga_sync, chapterToUpdate)) + .positiveText(R.string.button_yes) + .negativeText(R.string.button_no) + .onPositive((dialog, which) -> { + getPresenter().updateMangaSyncLastChapterRead(); + }) + .onAny((dialog1, which1) -> { + finish(); + }) + .show(); + } else { + getPresenter().updateMangaSyncLastChapterRead(); + finish(); + } + } else { + super.onBackPressed(); + } + } + public void onChapterError() { finish(); ToastUtil.showShort(this, R.string.page_list_error); diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/reader/ReaderPresenter.java b/app/src/main/java/eu/kanade/mangafeed/ui/reader/ReaderPresenter.java index 0ac9ebc6c..4be3576d4 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/reader/ReaderPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/reader/ReaderPresenter.java @@ -13,6 +13,7 @@ import de.greenrobot.event.EventBus; import eu.kanade.mangafeed.data.database.DatabaseHelper; import eu.kanade.mangafeed.data.database.models.Chapter; import eu.kanade.mangafeed.data.database.models.Manga; +import eu.kanade.mangafeed.data.database.models.MangaSync; import eu.kanade.mangafeed.data.download.DownloadManager; import eu.kanade.mangafeed.data.mangasync.MangaSyncManager; import eu.kanade.mangafeed.data.mangasync.base.MangaSyncService; @@ -49,6 +50,7 @@ public class ReaderPresenter extends BasePresenter { private Chapter previousChapter; private List pageList; private List nextChapterPageList; + private List mangaSyncList; private PublishSubject retryPageSubject; @@ -57,6 +59,7 @@ public class ReaderPresenter extends BasePresenter { private static final int GET_ADJACENT_CHAPTERS = 3; private static final int RETRY_IMAGES = 4; private static final int PRELOAD_NEXT_CHAPTER = 5; + private static final int GET_MANGA_SYNC = 6; @Override protected void onCreate(Bundle savedState) { @@ -99,22 +102,22 @@ public class ReaderPresenter extends BasePresenter { (view, pages) -> view.onChapterReady(pages, manga, chapter, currentPage), (view, error) -> view.onChapterError()); + restartableFirst(GET_MANGA_SYNC, this::getMangaSyncObservable, + (view, mangaSync) -> {}, + (view, error) -> {}); + registerForStickyEvents(); } @Override protected void onDestroy() { unregisterForEvents(); - onChapterLeft(); - updateMangaSyncLastChapterRead(); super.onDestroy(); } @Override protected void onSave(@NonNull Bundle state) { - if (pageList != null && !isDownloaded) - source.savePageList(chapter.url, pageList); - + onChapterLeft(); super.onSave(state); } @@ -135,6 +138,9 @@ public class ReaderPresenter extends BasePresenter { source = event.getSource(); sourceId = source.getId(); loadChapter(event.getChapter()); + if (prefs.autoUpdateMangaSync()) { + start(GET_MANGA_SYNC); + } } // Returns the page list of a chapter @@ -204,6 +210,11 @@ public class ReaderPresenter extends BasePresenter { .doOnCompleted(this::stopPreloadingNextChapter); } + private Observable> getMangaSyncObservable() { + return db.getMangasSync(manga).createObservable() + .doOnNext(mangaSync -> this.mangaSyncList = mangaSync); + } + // Loads the given chapter private void loadChapter(Chapter chapter) { // Before loading the chapter, stop preloading (if it's working) and save current progress @@ -238,11 +249,11 @@ public class ReaderPresenter extends BasePresenter { // Called before loading another chapter or leaving the reader. It allows to do operations // over the chapter read like saving progress - private void onChapterLeft() { + public void onChapterLeft() { if (pageList == null) return; - // Cache page list for online chapters to allow a faster reopen + // Cache current page list progress for online chapters to allow a faster reopen if (!isDownloaded) source.savePageList(chapter.url, pageList); @@ -259,33 +270,39 @@ public class ReaderPresenter extends BasePresenter { return !chapter.read && currentPage == pageList.size() - 1; } - private void updateMangaSyncLastChapterRead() { - if (pageList == null) - return; + public int getMangaSyncChapterToUpdate() { + if (pageList == null || mangaSyncList == null || mangaSyncList.isEmpty()) + return 0; - db.getMangasSync(manga).createObservable() - .take(1) - .flatMap(Observable::from) - .doOnNext(mangaSync -> { - MangaSyncService service = syncManager.getSyncService(mangaSync.sync_id); - if (!service.isLogged()) - return; + int lastChapterReadLocal = 0; + // If the current chapter has been read, we check with this one + if (chapter.read) + lastChapterReadLocal = (int) Math.floor(chapter.chapter_number); + // If not, we check if the previous chapter has been read + else if (previousChapter != null && previousChapter.read) + lastChapterReadLocal = (int) Math.floor(previousChapter.chapter_number); - int lastChapterReadLocal = 0; - // If the current chapter has been read, we check with this one - if (chapter.read) - lastChapterReadLocal = (int) Math.floor(chapter.chapter_number); - // If not, we check if the previous chapter has been read - else if (previousChapter != null && previousChapter.read) - lastChapterReadLocal = (int) Math.floor(previousChapter.chapter_number); + // We know the chapter we have to check, but we don't know yet if an update is required. + // This boolean is used to return 0 if no update is required + boolean hasToUpdate = false; - if (lastChapterReadLocal > mangaSync.last_chapter_read) { - mangaSync.last_chapter_read = lastChapterReadLocal; - UpdateMangaSyncService.start(getContext(), mangaSync); - } - }) - .subscribe(next -> {}, - error -> Timber.e(error.getCause(), error.getMessage())); + for (MangaSync mangaSync : mangaSyncList) { + if (lastChapterReadLocal > mangaSync.last_chapter_read) { + mangaSync.last_chapter_read = lastChapterReadLocal; + mangaSync.update = true; + hasToUpdate = true; + } + } + return hasToUpdate ? lastChapterReadLocal : 0; + } + + public void updateMangaSyncLastChapterRead() { + for (MangaSync mangaSync : mangaSyncList) { + MangaSyncService service = syncManager.getSyncService(mangaSync.sync_id); + if (service.isLogged() && mangaSync.update) { + UpdateMangaSyncService.start(getContext(), mangaSync); + } + } } public void setCurrentPage(int currentPage) { diff --git a/app/src/main/res/values/keys.xml b/app/src/main/res/values/keys.xml index 71612af6b..2621f1e6e 100644 --- a/app/src/main/res/values/keys.xml +++ b/app/src/main/res/values/keys.xml @@ -12,6 +12,8 @@ pref_library_columns_landscape_key pref_library_update_interval_key pref_update_only_non_completed_key + pref_auto_update_manga_sync_key + pref_ask_update_manga_sync_key pref_default_viewer_key pref_hide_status_bar_key diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c36a19479..ea57c59cf 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -73,6 +73,8 @@ Every 12 hours Daily Every 2 days + Automatically update last chapter read in enabled services + Ask for confirmation before updating Hide status bar @@ -176,6 +178,7 @@ Next chapter not found Previous chapter not found The image could not be loaded.\nTry to change the image decoder + Update last chapter read in enabled services to %1$d? Update progress: %1$d/%2$d diff --git a/app/src/main/res/xml/pref_general.xml b/app/src/main/res/xml/pref_general.xml index 89447cd40..1ed2e3b8e 100644 --- a/app/src/main/res/xml/pref_general.xml +++ b/app/src/main/res/xml/pref_general.xml @@ -19,4 +19,15 @@ android:title="@string/pref_update_only_non_completed" android:defaultValue="false"/> + + + + \ No newline at end of file