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)
public int status;
public boolean update;
public static MangaSync create(MangaSyncService service) {
MangaSync mangasync = new MangaSync();
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);
}
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() {
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));
}
public void onOpenManga(Manga manga) {
// Avoid further db updates for the library when it's not needed
stop(GET_LIBRARY);

View file

@ -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<ReaderPresenter> {
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);

View file

@ -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<ReaderActivity> {
private Chapter previousChapter;
private List<Page> pageList;
private List<Page> nextChapterPageList;
private List<MangaSync> mangaSyncList;
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 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<ReaderActivity> {
(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<ReaderActivity> {
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<ReaderActivity> {
.doOnCompleted(this::stopPreloadingNextChapter);
}
private Observable<List<MangaSync>> 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<ReaderActivity> {
// 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<ReaderActivity> {
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) {

View file

@ -12,6 +12,8 @@
<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_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_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_24hour">Daily</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 -->
<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_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="confirm_update_manga_sync">Update last chapter read in enabled services to %1$d?</string>
<!-- Library update service notifications -->
<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: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>