From 39d3033d44db115a0048cfca0bce2d2fc87e4f57 Mon Sep 17 00:00:00 2001 From: inorichi Date: Wed, 9 Dec 2015 14:55:32 +0100 Subject: [PATCH] Recover reader from process restart --- .../data/database/models/Chapter.java | 4 +- .../mangafeed/data/database/models/Manga.java | 4 +- .../mangafeed/event/RetryPageEvent.java | 17 ------ .../mangafeed/ui/reader/ReaderPresenter.java | 52 ++++++++++--------- .../common/ViewPagerReaderFragment.java | 4 +- 5 files changed, 35 insertions(+), 46 deletions(-) delete mode 100644 app/src/main/java/eu/kanade/mangafeed/event/RetryPageEvent.java diff --git a/app/src/main/java/eu/kanade/mangafeed/data/database/models/Chapter.java b/app/src/main/java/eu/kanade/mangafeed/data/database/models/Chapter.java index 146e918709..1ecf9a38f0 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/database/models/Chapter.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/database/models/Chapter.java @@ -3,11 +3,13 @@ package eu.kanade.mangafeed.data.database.models; import com.pushtorefresh.storio.sqlite.annotations.StorIOSQLiteColumn; import com.pushtorefresh.storio.sqlite.annotations.StorIOSQLiteType; +import java.io.Serializable; + import eu.kanade.mangafeed.data.database.tables.ChapterTable; import eu.kanade.mangafeed.util.UrlUtil; @StorIOSQLiteType(table = ChapterTable.TABLE) -public class Chapter { +public class Chapter implements Serializable { @StorIOSQLiteColumn(name = ChapterTable.COLUMN_ID, key = true) public Long id; diff --git a/app/src/main/java/eu/kanade/mangafeed/data/database/models/Manga.java b/app/src/main/java/eu/kanade/mangafeed/data/database/models/Manga.java index 047a21fc88..a08fcf7a4c 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/database/models/Manga.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/database/models/Manga.java @@ -3,11 +3,13 @@ package eu.kanade.mangafeed.data.database.models; import com.pushtorefresh.storio.sqlite.annotations.StorIOSQLiteColumn; import com.pushtorefresh.storio.sqlite.annotations.StorIOSQLiteType; +import java.io.Serializable; + import eu.kanade.mangafeed.data.database.tables.MangaTable; import eu.kanade.mangafeed.util.UrlUtil; @StorIOSQLiteType(table = MangaTable.TABLE) -public class Manga { +public class Manga implements Serializable { @StorIOSQLiteColumn(name = MangaTable.COLUMN_ID, key = true) public Long id; diff --git a/app/src/main/java/eu/kanade/mangafeed/event/RetryPageEvent.java b/app/src/main/java/eu/kanade/mangafeed/event/RetryPageEvent.java deleted file mode 100644 index 7ff8c7ad0c..0000000000 --- a/app/src/main/java/eu/kanade/mangafeed/event/RetryPageEvent.java +++ /dev/null @@ -1,17 +0,0 @@ -package eu.kanade.mangafeed.event; - -import eu.kanade.mangafeed.data.source.model.Page; - -public class RetryPageEvent { - - private Page page; - - public RetryPageEvent(Page page) { - this.page = page; - } - - public Page getPage() { - return page; - } - -} 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 b367b028dc..b1acc15bf0 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 @@ -17,9 +17,9 @@ import eu.kanade.mangafeed.data.database.models.ChapterSync; import eu.kanade.mangafeed.data.database.models.Manga; import eu.kanade.mangafeed.data.download.DownloadManager; import eu.kanade.mangafeed.data.preference.PreferencesHelper; +import eu.kanade.mangafeed.data.source.SourceManager; import eu.kanade.mangafeed.data.source.base.Source; import eu.kanade.mangafeed.data.source.model.Page; -import eu.kanade.mangafeed.event.RetryPageEvent; import eu.kanade.mangafeed.event.ReaderEvent; import eu.kanade.mangafeed.event.UpdateChapterSyncEvent; import eu.kanade.mangafeed.ui.base.presenter.BasePresenter; @@ -38,16 +38,18 @@ public class ReaderPresenter extends BasePresenter { @Inject DatabaseHelper db; @Inject DownloadManager downloadManager; @Inject ChapterSyncManager syncManager; + @Inject SourceManager sourceManager; + @State Manga manga; + @State Chapter chapter; + @State int sourceId; + @State boolean isDownloaded; + @State int currentPage; private Source source; - private Manga manga; - private Chapter chapter; private Chapter nextChapter; private Chapter previousChapter; private List pageList; private List nextChapterPageList; - private boolean isDownloaded; - @State int currentPage; private PublishSubject retryPageSubject; @@ -63,6 +65,10 @@ public class ReaderPresenter extends BasePresenter { protected void onCreate(Bundle savedState) { super.onCreate(savedState); + if (savedState != null) { + onProcessRestart(); + } + retryPageSubject = PublishSubject.create(); restartableLatestCache(GET_PAGE_LIST, @@ -97,42 +103,35 @@ public class ReaderPresenter extends BasePresenter { this::getPreloadNextChapterObservable, (view, pages) -> {}, (view, error) -> Timber.e("An error occurred while preloading a chapter")); - } - @Override - protected void onTakeView(ReaderActivity view) { - super.onTakeView(view); registerForStickyEvents(); } - @Override - protected void onDropView() { - unregisterForEvents(); - super.onDropView(); - } - @Override protected void onDestroy() { + unregisterForEvents(); onChapterLeft(); super.onDestroy(); } + private void onProcessRestart() { + source = sourceManager.get(sourceId); + + // These are started by GET_PAGE_LIST, so we don't let them restart itselves + stop(GET_PAGE_IMAGES); + stop(RETRY_IMAGES); + stop(PRELOAD_NEXT_CHAPTER); + } + @EventBusHook public void onEventMainThread(ReaderEvent event) { EventBus.getDefault().removeStickyEvent(event); - source = event.getSource(); manga = event.getManga(); + source = event.getSource(); + sourceId = source.getSourceId(); loadChapter(event.getChapter()); } - @EventBusHook - public void onEventMainThread(RetryPageEvent event) { - EventBus.getDefault().removeStickyEvent(event); - Page page = event.getPage(); - page.setStatus(Page.QUEUE); - retryPageSubject.onNext(page); - } - // Returns the page list of a chapter private Observable> getPageListObservable() { return isDownloaded ? @@ -216,6 +215,11 @@ public class ReaderPresenter extends BasePresenter { return downloadManager.isChapterDownloaded(source, manga, chapter); } + public void retryPage(Page page) { + page.setStatus(Page.QUEUE); + retryPageSubject.onNext(page); + } + // Called before loading another chapter or leaving the reader. It allows to do operations // over the chapter read like saving progress private void onChapterLeft() { diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/reader/viewer/common/ViewPagerReaderFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/reader/viewer/common/ViewPagerReaderFragment.java index b8f98c4d5d..9632dd406b 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/reader/viewer/common/ViewPagerReaderFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/reader/viewer/common/ViewPagerReaderFragment.java @@ -19,10 +19,8 @@ import java.util.concurrent.atomic.AtomicInteger; import butterknife.Bind; import butterknife.ButterKnife; -import de.greenrobot.event.EventBus; import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.data.source.model.Page; -import eu.kanade.mangafeed.event.RetryPageEvent; import eu.kanade.mangafeed.ui.base.fragment.BaseFragment; import eu.kanade.mangafeed.ui.reader.ReaderActivity; import rx.Observable; @@ -63,7 +61,7 @@ public class ViewPagerReaderFragment extends BaseFragment { retryButton.setOnTouchListener((v, event) -> { if (event.getAction() == MotionEvent.ACTION_UP) { if (page != null) - EventBus.getDefault().postSticky(new RetryPageEvent(page)); + ((ReaderActivity) getActivity()).getPresenter().retryPage(page); return true; } return true;