Forráskód Böngészése

Recover page position when configuration changes

inorichi 9 éve
szülő
commit
5e01937cce

+ 7 - 2
app/src/main/java/eu/kanade/mangafeed/presenter/ReaderPresenter.java

@@ -27,7 +27,7 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
     private Source source;
     private Chapter chapter;
     private List<Page> pageList;
-    @State int savedSelectedPage = -1;
+    @State int savedSelectedPage = 0;
 
     private static final int GET_PAGE_LIST = 1;
     private static final int GET_PAGE_IMAGES = 2;
@@ -40,7 +40,12 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
                 () -> getPageListObservable()
                         .doOnNext(pages -> pageList = pages)
                         .doOnCompleted(() -> start(GET_PAGE_IMAGES)),
-                (view, pages) -> view.onPageListReady(pages),
+                (view, pages) -> {
+                    view.onPageListReady(pages);
+                    if (savedSelectedPage != 0) {
+                        view.setSelectedPage(savedSelectedPage);
+                    }
+                },
                 (view, error) -> Timber.e("An error occurred while downloading page list")
         );
 

+ 6 - 6
app/src/main/java/eu/kanade/mangafeed/ui/activity/ReaderActivity.java

@@ -71,13 +71,14 @@ public class ReaderActivity extends BaseRxActivity<ReaderPresenter> {
         viewer.onPageListReady(pages);
     }
 
-    public void onPageChanged(int currentPage, int totalPages) {
-        String page = currentPage + "/" + totalPages;
+    public void onPageChanged(int currentPageIndex, int totalPages) {
+        currentPage = currentPageIndex;
+        String page = (currentPageIndex + 1) + "/" + totalPages;
         pageNumber.setText(page);
     }
 
-    public void setCurrentPage(int page) {
-        currentPage = page;
+    public void setSelectedPage(int pageIndex) {
+        viewer.setSelectedPage(pageIndex);
     }
 
     public void hideStatusBar() {
@@ -98,7 +99,7 @@ public class ReaderActivity extends BaseRxActivity<ReaderPresenter> {
 
     private BaseViewer getViewer() {
         switch (prefs.getDefaultViewer()) {
-            case LEFT_TO_RIGHT:
+            case LEFT_TO_RIGHT: default:
                 return new LeftToRightViewer(this, container);
             case RIGHT_TO_LEFT:
                 return new RightToLeftViewer(this, container);
@@ -107,7 +108,6 @@ public class ReaderActivity extends BaseRxActivity<ReaderPresenter> {
             case WEBTOON:
                 return new WebtoonViewer(this, container);
         }
-        return null;
     }
 
 }

+ 2 - 7
app/src/main/java/eu/kanade/mangafeed/ui/viewer/RightToLeftViewer.java

@@ -25,13 +25,8 @@ public class RightToLeftViewer extends ViewPagerViewer {
     }
 
     @Override
-    public int getCurrentPageFromPos(int position) {
-        return getTotalPages() - position;
-    }
-
-    @Override
-    public int getPosFromPage(Page page) {
-        return getTotalPages() - (page.getPageNumber() + 1);
+    public int getCurrentPageIndex(int viewerPosition) {
+        return getTotalPages() - viewerPosition - 1;
     }
 
     @Override

+ 6 - 0
app/src/main/java/eu/kanade/mangafeed/ui/viewer/VerticalViewer.java

@@ -52,6 +52,12 @@ public class VerticalViewer extends BaseViewer {
         return adapter.getCount();
     }
 
+    @Override
+    public void setSelectedPage(int pageNumber) {
+        // TODO
+        return;
+    }
+
     @Override
     public void onPageListReady(List<Page> pages) {
         adapter.setPages(pages);

+ 6 - 0
app/src/main/java/eu/kanade/mangafeed/ui/viewer/WebtoonViewer.java

@@ -33,6 +33,12 @@ public class WebtoonViewer extends BaseViewer {
         return adapter.getItemCount();
     }
 
+    @Override
+    public void setSelectedPage(int pageNumber) {
+        // TODO
+        return;
+    }
+
     @Override
     public void onPageListReady(List<Page> pages) {
         adapter.setPages(pages);

+ 6 - 7
app/src/main/java/eu/kanade/mangafeed/ui/viewer/base/BaseViewer.java

@@ -20,18 +20,17 @@ public abstract class BaseViewer {
     }
 
     public void updatePageNumber() {
-        activity.onPageChanged(getCurrentPageFromPos(currentPosition), getTotalPages());
+        activity.onPageChanged(getCurrentPageIndex(currentPosition), getTotalPages());
     }
 
-    public int getCurrentPageFromPos(int position) {
-        return position + 1;
-    }
-
-    public int getPosFromPage(Page page) {
-        return page.getPageNumber();
+    // Returns the page index given a position in the viewer. Useful por a right to left viewer,
+    // where the current page is the inverse of the position
+    public int getCurrentPageIndex(int viewerPosition) {
+        return viewerPosition;
     }
 
     public abstract int getTotalPages();
+    public abstract void setSelectedPage(int pageNumber);
     public abstract void onPageListReady(List<Page> pages);
     public abstract boolean onImageTouch(MotionEvent motionEvent);
 }

+ 5 - 0
app/src/main/java/eu/kanade/mangafeed/ui/viewer/base/ViewPagerViewer.java

@@ -65,6 +65,11 @@ public abstract class ViewPagerViewer extends BaseViewer {
         return adapter.getCount();
     }
 
+    @Override
+    public void setSelectedPage(int pageNumber) {
+        viewPager.setCurrentItem(getCurrentPageIndex(pageNumber));
+    }
+
     @Override
     public void onPageListReady(List<Page> pages) {
         adapter.setPages(pages);