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

This commit is contained in:
inorichi 2016-01-31 18:48:13 +01:00
parent a3ec057384
commit d799ae5d72
3 changed files with 50 additions and 23 deletions

View File

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

View File

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

View File

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