From d9a2255be90b014951276a8fedcfcf944b78b0e1 Mon Sep 17 00:00:00 2001 From: lifeweaver Date: Sun, 9 Apr 2017 11:01:07 -0300 Subject: [PATCH] Retain last read page when using the webtoon mode (#738) * Retain last read page when using the webtoon mode, see issue #453 * #738 inorichi's request change to webtoonreader pull request * #738 per inorichi recycler could be null at the point scrollToLastPageRead was called, moved to below the check in the view had been initialized. --- .../ui/reader/viewer/webtoon/WebtoonReader.kt | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonReader.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonReader.kt index f160f0ce6a..edb46e8d72 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonReader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonReader.kt @@ -72,9 +72,6 @@ class WebtoonReader : BaseReader() { layoutManager = PreCachingLayoutManager(activity) layoutManager.extraLayoutSpace = screenHeight / 2 - if (savedState != null) { - layoutManager.scrollToPositionWithOffset(savedState.getInt(SAVED_POSITION), 0) - } recycler = RecyclerView(activity).apply { layoutParams = ViewGroup.LayoutParams(MATCH_PARENT, WRAP_CONTENT) @@ -107,6 +104,26 @@ class WebtoonReader : BaseReader() { return recycler } + + /** + * Uses two ways to scroll to the last page read. + */ + private fun scrollToLastPageRead(last_page_read: Int) { + // Scrolls to the correct page initially, but isn't reliable beyond that. + recycler.addOnLayoutChangeListener(object: View.OnLayoutChangeListener { + override fun onLayoutChange(p0: View?, p1: Int, p2: Int, p3: Int, p4: Int, p5: Int, p6: Int, p7: Int, p8: Int) { + if(pages.isEmpty()) { + setActivePage(last_page_read) + } else { + recycler.removeOnLayoutChangeListener(this) + } + } + }) + + // Scrolls to the correct page after app has been in use, but can't do it the very first time. + recycler.post { setActivePage(last_page_read) } + } + override fun onDestroyView() { subscriptions.unsubscribe() super.onDestroyView() @@ -150,6 +167,7 @@ class WebtoonReader : BaseReader() { // Make sure the view is already initialized. if (view != null) { setPagesOnAdapter() + scrollToLastPageRead(this.currentPage) } }