Add an option to automatically update last chapter read and another one to ask before updating. Fixes #10

This commit is contained in:
inorichi 2016-01-11 18:21:17 +01:00
parent 226cc6990d
commit b170520370
8 changed files with 106 additions and 33 deletions

View file

@ -38,6 +38,8 @@ public class MangaSync implements Serializable {
@StorIOSQLiteColumn(name = MangaSyncTable.COLUMN_STATUS) @StorIOSQLiteColumn(name = MangaSyncTable.COLUMN_STATUS)
public int status; public int status;
public boolean update;
public static MangaSync create(MangaSyncService service) { public static MangaSync create(MangaSyncService service) {
MangaSync mangasync = new MangaSync(); MangaSync mangasync = new MangaSync();
mangasync.sync_id = service.getId(); mangasync.sync_id = service.getId();

View file

@ -101,6 +101,14 @@ public class PreferencesHelper {
return prefs.getBoolean(getKey(R.string.pref_update_only_non_completed_key), false); 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<Integer> imageDecoder() { public Preference<Integer> imageDecoder() {
return rxPrefs.getInteger(getKey(R.string.pref_image_decoder_key), 0); return rxPrefs.getInteger(getKey(R.string.pref_image_decoder_key), 0);
} }

View file

@ -81,8 +81,6 @@ public class LibraryPresenter extends BasePresenter<LibraryFragment> {
.toMap(pair -> pair.first, pair -> pair.second)); .toMap(pair -> pair.first, pair -> pair.second));
} }
public void onOpenManga(Manga manga) { public void onOpenManga(Manga manga) {
// Avoid further db updates for the library when it's not needed // Avoid further db updates for the library when it's not needed
stop(GET_LIBRARY); stop(GET_LIBRARY);

View file

@ -16,6 +16,8 @@ import android.view.View;
import android.view.WindowManager; import android.view.WindowManager;
import android.widget.TextView; import android.widget.TextView;
import com.afollestad.materialdialogs.MaterialDialog;
import java.util.List; import java.util.List;
import javax.inject.Inject; import javax.inject.Inject;
@ -126,6 +128,36 @@ public class ReaderActivity extends BaseRxActivity<ReaderPresenter> {
super.onSaveInstanceState(outState); 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() { public void onChapterError() {
finish(); finish();
ToastUtil.showShort(this, R.string.page_list_error); ToastUtil.showShort(this, R.string.page_list_error);

View file

@ -13,6 +13,7 @@ import de.greenrobot.event.EventBus;
import eu.kanade.mangafeed.data.database.DatabaseHelper; import eu.kanade.mangafeed.data.database.DatabaseHelper;
import eu.kanade.mangafeed.data.database.models.Chapter; import eu.kanade.mangafeed.data.database.models.Chapter;
import eu.kanade.mangafeed.data.database.models.Manga; 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.download.DownloadManager;
import eu.kanade.mangafeed.data.mangasync.MangaSyncManager; import eu.kanade.mangafeed.data.mangasync.MangaSyncManager;
import eu.kanade.mangafeed.data.mangasync.base.MangaSyncService; import eu.kanade.mangafeed.data.mangasync.base.MangaSyncService;
@ -49,6 +50,7 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
private Chapter previousChapter; private Chapter previousChapter;
private List<Page> pageList; private List<Page> pageList;
private List<Page> nextChapterPageList; private List<Page> nextChapterPageList;
private List<MangaSync> mangaSyncList;
private PublishSubject<Page> retryPageSubject; private PublishSubject<Page> retryPageSubject;
@ -57,6 +59,7 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
private static final int GET_ADJACENT_CHAPTERS = 3; private static final int GET_ADJACENT_CHAPTERS = 3;
private static final int RETRY_IMAGES = 4; private static final int RETRY_IMAGES = 4;
private static final int PRELOAD_NEXT_CHAPTER = 5; private static final int PRELOAD_NEXT_CHAPTER = 5;
private static final int GET_MANGA_SYNC = 6;
@Override @Override
protected void onCreate(Bundle savedState) { protected void onCreate(Bundle savedState) {
@ -99,22 +102,22 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
(view, pages) -> view.onChapterReady(pages, manga, chapter, currentPage), (view, pages) -> view.onChapterReady(pages, manga, chapter, currentPage),
(view, error) -> view.onChapterError()); (view, error) -> view.onChapterError());
restartableFirst(GET_MANGA_SYNC, this::getMangaSyncObservable,
(view, mangaSync) -> {},
(view, error) -> {});
registerForStickyEvents(); registerForStickyEvents();
} }
@Override @Override
protected void onDestroy() { protected void onDestroy() {
unregisterForEvents(); unregisterForEvents();
onChapterLeft();
updateMangaSyncLastChapterRead();
super.onDestroy(); super.onDestroy();
} }
@Override @Override
protected void onSave(@NonNull Bundle state) { protected void onSave(@NonNull Bundle state) {
if (pageList != null && !isDownloaded) onChapterLeft();
source.savePageList(chapter.url, pageList);
super.onSave(state); super.onSave(state);
} }
@ -135,6 +138,9 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
source = event.getSource(); source = event.getSource();
sourceId = source.getId(); sourceId = source.getId();
loadChapter(event.getChapter()); loadChapter(event.getChapter());
if (prefs.autoUpdateMangaSync()) {
start(GET_MANGA_SYNC);
}
} }
// Returns the page list of a chapter // Returns the page list of a chapter
@ -204,6 +210,11 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
.doOnCompleted(this::stopPreloadingNextChapter); .doOnCompleted(this::stopPreloadingNextChapter);
} }
private Observable<List<MangaSync>> getMangaSyncObservable() {
return db.getMangasSync(manga).createObservable()
.doOnNext(mangaSync -> this.mangaSyncList = mangaSync);
}
// Loads the given chapter // Loads the given chapter
private void loadChapter(Chapter chapter) { private void loadChapter(Chapter chapter) {
// Before loading the chapter, stop preloading (if it's working) and save current progress // Before loading the chapter, stop preloading (if it's working) and save current progress
@ -238,11 +249,11 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
// Called before loading another chapter or leaving the reader. It allows to do operations // Called before loading another chapter or leaving the reader. It allows to do operations
// over the chapter read like saving progress // over the chapter read like saving progress
private void onChapterLeft() { public void onChapterLeft() {
if (pageList == null) if (pageList == null)
return; 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) if (!isDownloaded)
source.savePageList(chapter.url, pageList); source.savePageList(chapter.url, pageList);
@ -259,33 +270,39 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
return !chapter.read && currentPage == pageList.size() - 1; return !chapter.read && currentPage == pageList.size() - 1;
} }
private void updateMangaSyncLastChapterRead() { public int getMangaSyncChapterToUpdate() {
if (pageList == null) if (pageList == null || mangaSyncList == null || mangaSyncList.isEmpty())
return; return 0;
db.getMangasSync(manga).createObservable() int lastChapterReadLocal = 0;
.take(1) // If the current chapter has been read, we check with this one
.flatMap(Observable::from) if (chapter.read)
.doOnNext(mangaSync -> { lastChapterReadLocal = (int) Math.floor(chapter.chapter_number);
MangaSyncService service = syncManager.getSyncService(mangaSync.sync_id); // If not, we check if the previous chapter has been read
if (!service.isLogged()) else if (previousChapter != null && previousChapter.read)
return; lastChapterReadLocal = (int) Math.floor(previousChapter.chapter_number);
int lastChapterReadLocal = 0; // We know the chapter we have to check, but we don't know yet if an update is required.
// If the current chapter has been read, we check with this one // This boolean is used to return 0 if no update is required
if (chapter.read) boolean hasToUpdate = false;
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);
if (lastChapterReadLocal > mangaSync.last_chapter_read) { for (MangaSync mangaSync : mangaSyncList) {
mangaSync.last_chapter_read = lastChapterReadLocal; if (lastChapterReadLocal > mangaSync.last_chapter_read) {
UpdateMangaSyncService.start(getContext(), mangaSync); mangaSync.last_chapter_read = lastChapterReadLocal;
} mangaSync.update = true;
}) hasToUpdate = true;
.subscribe(next -> {}, }
error -> Timber.e(error.getCause(), error.getMessage())); }
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) { public void setCurrentPage(int currentPage) {

View file

@ -12,6 +12,8 @@
<string name="pref_library_columns_landscape_key">pref_library_columns_landscape_key</string> <string name="pref_library_columns_landscape_key">pref_library_columns_landscape_key</string>
<string name="pref_library_update_interval_key">pref_library_update_interval_key</string> <string name="pref_library_update_interval_key">pref_library_update_interval_key</string>
<string name="pref_update_only_non_completed_key">pref_update_only_non_completed_key</string> <string name="pref_update_only_non_completed_key">pref_update_only_non_completed_key</string>
<string name="pref_auto_update_manga_sync_key">pref_auto_update_manga_sync_key</string>
<string name="pref_ask_update_manga_sync_key">pref_ask_update_manga_sync_key</string>
<string name="pref_default_viewer_key">pref_default_viewer_key</string> <string name="pref_default_viewer_key">pref_default_viewer_key</string>
<string name="pref_hide_status_bar_key">pref_hide_status_bar_key</string> <string name="pref_hide_status_bar_key">pref_hide_status_bar_key</string>

View file

@ -73,6 +73,8 @@
<string name="update_12hour">Every 12 hours</string> <string name="update_12hour">Every 12 hours</string>
<string name="update_24hour">Daily</string> <string name="update_24hour">Daily</string>
<string name="update_48hour">Every 2 days</string> <string name="update_48hour">Every 2 days</string>
<string name="pref_auto_update_manga_sync">Automatically update last chapter read in enabled services</string>
<string name="pref_ask_update_manga_sync">Ask for confirmation before updating</string>
<!-- Reader section --> <!-- Reader section -->
<string name="pref_hide_status_bar">Hide status bar</string> <string name="pref_hide_status_bar">Hide status bar</string>
@ -176,6 +178,7 @@
<string name="no_next_chapter">Next chapter not found</string> <string name="no_next_chapter">Next chapter not found</string>
<string name="no_previous_chapter">Previous chapter not found</string> <string name="no_previous_chapter">Previous chapter not found</string>
<string name="decode_image_error">The image could not be loaded.\nTry to change the image decoder</string> <string name="decode_image_error">The image could not be loaded.\nTry to change the image decoder</string>
<string name="confirm_update_manga_sync">Update last chapter read in enabled services to %1$d?</string>
<!-- Library update service notifications --> <!-- Library update service notifications -->
<string name="notification_update_progress">Update progress: %1$d/%2$d</string> <string name="notification_update_progress">Update progress: %1$d/%2$d</string>

View file

@ -19,4 +19,15 @@
android:title="@string/pref_update_only_non_completed" android:title="@string/pref_update_only_non_completed"
android:defaultValue="false"/> android:defaultValue="false"/>
<CheckBoxPreference
android:key="@string/pref_auto_update_manga_sync_key"
android:title="@string/pref_auto_update_manga_sync"
android:defaultValue="true"/>
<CheckBoxPreference
android:key="@string/pref_ask_update_manga_sync_key"
android:title="@string/pref_ask_update_manga_sync"
android:defaultValue="false"
android:dependency="@string/pref_auto_update_manga_sync_key"/>
</PreferenceScreen> </PreferenceScreen>