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

Webtoon reader "restores" position on rotation. Fixes #93

inorichi 9 éve
szülő
commit
d799ae5d72

+ 31 - 21
app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.java

@@ -7,6 +7,7 @@ import android.graphics.Color;
 import android.os.Build;
 import android.os.Bundle;
 import android.support.annotation.NonNull;
+import android.support.v4.app.FragmentManager;
 import android.support.v4.content.ContextCompat;
 import android.support.v7.widget.Toolbar;
 import android.view.Menu;
@@ -20,11 +21,8 @@ import com.afollestad.materialdialogs.MaterialDialog;
 
 import java.util.List;
 
-import javax.inject.Inject;
-
 import butterknife.Bind;
 import butterknife.ButterKnife;
-import eu.kanade.tachiyomi.App;
 import eu.kanade.tachiyomi.R;
 import eu.kanade.tachiyomi.data.database.models.Chapter;
 import eu.kanade.tachiyomi.data.database.models.Manga;
@@ -49,8 +47,6 @@ public class ReaderActivity extends BaseRxActivity<ReaderPresenter> {
     @Bind(R.id.page_number) TextView pageNumber;
     @Bind(R.id.toolbar) Toolbar toolbar;
 
-    @Inject PreferencesHelper preferences;
-
     private BaseReader viewer;
     private ReaderMenu readerMenu;
 
@@ -75,7 +71,6 @@ public class ReaderActivity extends BaseRxActivity<ReaderPresenter> {
     @Override
     public void onCreate(Bundle savedState) {
         super.onCreate(savedState);
-        App.get(this).getComponent().inject(this);
         setContentView(R.layout.activity_reader);
         ButterKnife.bind(this);
 
@@ -169,8 +164,7 @@ public class ReaderActivity extends BaseRxActivity<ReaderPresenter> {
         }
 
         if (viewer == null) {
-            viewer = createViewer(manga);
-            getSupportFragmentManager().beginTransaction().replace(R.id.reader, viewer).commit();
+            viewer = getOrCreateViewer(manga);
         }
         viewer.onPageListReady(pages, currentPage);
         readerMenu.onChapterReady(pages.size(), manga, chapter, currentPage);
@@ -180,19 +174,33 @@ public class ReaderActivity extends BaseRxActivity<ReaderPresenter> {
         readerMenu.onAdjacentChapters(previous, next);
     }
 
-    private BaseReader createViewer(Manga manga) {
-        int mangaViewer = manga.viewer == 0 ? preferences.getDefaultViewer() : manga.viewer;
+    private BaseReader getOrCreateViewer(Manga manga) {
+        int mangaViewer = manga.viewer == 0 ? getPreferences().getDefaultViewer() : manga.viewer;
+
+        FragmentManager fm = getSupportFragmentManager();
+
+        // Try to reuse the viewer using its tag
+        BaseReader fragment = (BaseReader) fm.findFragmentByTag(manga.viewer + "");
+        if (fragment == null) {
+            // Create a new viewer
+            switch (mangaViewer) {
+                case LEFT_TO_RIGHT: default:
+                    fragment = new LeftToRightReader();
+                    break;
+                case RIGHT_TO_LEFT:
+                    fragment = new RightToLeftReader();
+                    break;
+                case VERTICAL:
+                    fragment = new VerticalReader();
+                    break;
+                case WEBTOON:
+                    fragment = new WebtoonReader();
+                    break;
+            }
 
-        switch (mangaViewer) {
-            case LEFT_TO_RIGHT: default:
-                return new LeftToRightReader();
-            case RIGHT_TO_LEFT:
-                return new RightToLeftReader();
-            case VERTICAL:
-                return new VerticalReader();
-            case WEBTOON:
-                return new WebtoonReader();
+            fm.beginTransaction().replace(R.id.reader, fragment, manga.viewer + "").commit();
         }
+        return fragment;
     }
 
     public void onPageChanged(int currentPageIndex, int totalPages) {
@@ -225,6 +233,8 @@ public class ReaderActivity extends BaseRxActivity<ReaderPresenter> {
     }
 
     private void initializeSettings() {
+        PreferencesHelper preferences = getPreferences();
+
         subscriptions.add(preferences.showPageNumber()
                 .asObservable()
                 .subscribe(this::setPageNumberVisibility));
@@ -290,7 +300,7 @@ public class ReaderActivity extends BaseRxActivity<ReaderPresenter> {
 
     private void setCustomBrightness(boolean enabled) {
         if (enabled) {
-            subscriptions.add(customBrightnessSubscription = preferences.customBrightnessValue()
+            subscriptions.add(customBrightnessSubscription = getPreferences().customBrightnessValue()
                     .asObservable()
                     .subscribe(this::setCustomBrightnessValue));
         } else {
@@ -348,7 +358,7 @@ public class ReaderActivity extends BaseRxActivity<ReaderPresenter> {
     }
 
     public PreferencesHelper getPreferences() {
-        return preferences;
+        return getPresenter().prefs;
     }
 
     public BaseReader getViewer() {

+ 3 - 1
app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerReader.java

@@ -19,6 +19,7 @@ public abstract class PagerReader extends BaseReader {
     protected PagerReaderAdapter adapter;
     protected Pager pager;
 
+    private boolean isReady;
     protected boolean transitions;
     protected CompositeSubscription subscriptions;
 
@@ -80,6 +81,7 @@ public abstract class PagerReader extends BaseReader {
                 .subscribe(value -> transitions = value));
 
         setPages();
+        isReady = true;
     }
 
     @Override
@@ -93,7 +95,7 @@ public abstract class PagerReader extends BaseReader {
         if (this.pages != pages) {
             this.pages = pages;
             this.currentPage = currentPage;
-            if (isResumed()) {
+            if (isReady) {
                 setPages();
             }
         }

+ 16 - 1
app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonReader.java

@@ -29,8 +29,11 @@ public class WebtoonReader extends BaseReader {
     private Subscription decoderSubscription;
     private GestureDetector gestureDetector;
 
+    private boolean isReady;
     private int scrollDistance;
 
+    private static final String SCROLL_STATE = "scroll_state";
+
     private static final float LEFT_REGION = 0.33f;
     private static final float RIGHT_REGION = 0.66f;
 
@@ -43,6 +46,9 @@ public class WebtoonReader extends BaseReader {
 
         layoutManager = new PreCachingLayoutManager(getActivity());
         layoutManager.setExtraLayoutSpace(screenHeight / 2);
+        if (savedState != null) {
+            layoutManager.onRestoreInstanceState(savedState.getParcelable(SCROLL_STATE));
+        }
 
         recycler = new RecyclerView(getActivity());
         recycler.setLayoutParams(new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
@@ -74,6 +80,7 @@ public class WebtoonReader extends BaseReader {
         });
 
         setPages();
+        isReady = true;
 
         return recycler;
     }
@@ -90,6 +97,12 @@ public class WebtoonReader extends BaseReader {
         super.onPause();
     }
 
+    @Override
+    public void onSaveInstanceState(Bundle outState) {
+        super.onSaveInstanceState(outState);
+        outState.putParcelable(SCROLL_STATE, layoutManager.onSaveInstanceState());
+    }
+
     private void unsubscribeStatus() {
         if (subscription != null && !subscription.isUnsubscribed())
             subscription.unsubscribe();
@@ -104,7 +117,9 @@ public class WebtoonReader extends BaseReader {
     public void onPageListReady(List<Page> pages, int currentPage) {
         if (this.pages != pages) {
             this.pages = pages;
-            if (isResumed()) {
+            // Restoring current page is not supported. It's getting weird scrolling jumps
+            // this.currentPage = currentPage;
+            if (isReady) {
                 setPages();
             }
         }