Просмотр исходного кода

Recover reader from process restart

inorichi 9 лет назад
Родитель
Сommit
39d3033d44

+ 3 - 1
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;

+ 3 - 1
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;

+ 0 - 17
app/src/main/java/eu/kanade/mangafeed/event/RetryPageEvent.java

@@ -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;
-    }
-
-}

+ 28 - 24
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<ReaderActivity> {
     @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<Page> pageList;
     private List<Page> nextChapterPageList;
-    private boolean isDownloaded;
-    @State int currentPage;
 
     private PublishSubject<Page> retryPageSubject;
 
@@ -63,6 +65,10 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
     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<ReaderActivity> {
                 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<List<Page>> getPageListObservable() {
         return isDownloaded ?
@@ -216,6 +215,11 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
         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() {

+ 1 - 3
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;