From d799ae5d728067de0858930d68026b77b8e676a5 Mon Sep 17 00:00:00 2001 From: inorichi Date: Sun, 31 Jan 2016 18:48:13 +0100 Subject: [PATCH] Webtoon reader "restores" position on rotation. Fixes #93 --- .../tachiyomi/ui/reader/ReaderActivity.java | 52 +++++++++++-------- .../ui/reader/viewer/pager/PagerReader.java | 4 +- .../reader/viewer/webtoon/WebtoonReader.java | 17 +++++- 3 files changed, 50 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.java b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.java index aa27e2e523..27d1151a74 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.java +++ b/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 { @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 { @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 { } 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 { 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; - 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(); + 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; + } + + 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 { } private void initializeSettings() { + PreferencesHelper preferences = getPreferences(); + subscriptions.add(preferences.showPageNumber() .asObservable() .subscribe(this::setPageNumberVisibility)); @@ -290,7 +300,7 @@ public class ReaderActivity extends BaseRxActivity { 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 { } public PreferencesHelper getPreferences() { - return preferences; + return getPresenter().prefs; } public BaseReader getViewer() { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerReader.java b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerReader.java index d3d49c2bc3..7f6bd146f7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerReader.java +++ b/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(); } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonReader.java b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonReader.java index 1879d2fca3..2c18030895 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonReader.java +++ b/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 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(); } }