From 356cd4ef522bec405253954ec4b2dea90a4e69c7 Mon Sep 17 00:00:00 2001 From: Andreas Date: Wed, 21 Jul 2021 23:58:32 +0200 Subject: [PATCH] Auto hide reader menu when user starts reading again (#5578) * Hide reader menu when user starts reading again * Hide menu on zoom and scrolling around during zoom Didn't work for webtoon * Only listen when menu is visible --- .../tachiyomi/ui/reader/ReaderActivity.kt | 22 +++++++++++++++++++ .../ui/reader/viewer/pager/PagerPageHolder.kt | 22 +++++++++++++++++++ .../ui/reader/viewer/pager/PagerViewer.kt | 1 + .../ui/reader/viewer/webtoon/WebtoonViewer.kt | 1 + 4 files changed, 46 insertions(+) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt index 57fb6ce96e..c0a8843187 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt @@ -2,6 +2,7 @@ package eu.kanade.tachiyomi.ui.reader import android.annotation.SuppressLint import android.annotation.TargetApi +import android.app.ActionBar import android.app.ProgressDialog import android.content.ClipData import android.content.Context @@ -189,6 +190,7 @@ class ReaderActivity : BaseRxActivity() readingModeToast?.cancel() progressDialog?.dismiss() progressDialog = null + listeners = mutableListOf() } /** @@ -486,12 +488,23 @@ class ReaderActivity : BaseRxActivity() ) } + private var listeners: MutableList = mutableListOf() + + fun addOnMenuVisibilityListener(listener: ActionBar.OnMenuVisibilityListener) { + listeners.add(listener) + } + + fun removeOnMenuVisibilityListener(listener: ActionBar.OnMenuVisibilityListener) { + listeners.remove(listener) + } + /** * Sets the visibility of the menu according to [visible] and with an optional parameter to * [animate] the views. */ fun setMenuVisibility(visible: Boolean, animate: Boolean = true) { menuVisible = visible + listeners.forEach { listener -> listener.onMenuVisibilityChanged(visible) } if (visible) { windowInsetsController.show(WindowInsetsCompat.Type.systemBars()) binding.readerMenu.isVisible = true @@ -737,6 +750,15 @@ class ReaderActivity : BaseRxActivity() } } + /** + * Called from the viewer to hide the menu. + */ + fun hideMenu() { + if (menuVisible) { + setMenuVisibility(false) + } + } + /** * Called from the page sheet. It delegates the call to the presenter to do some IO, which * will call [onShareImageResult] with the path the image was saved on when it's ready. diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerPageHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerPageHolder.kt index 455c4308f6..814b16d334 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerPageHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerPageHolder.kt @@ -1,6 +1,7 @@ package eu.kanade.tachiyomi.ui.reader.viewer.pager import android.annotation.SuppressLint +import android.app.ActionBar import android.graphics.PointF import android.graphics.drawable.Animatable import android.view.GestureDetector @@ -99,9 +100,28 @@ class PagerPageHolder( */ private var readImageHeaderSubscription: Subscription? = null + private var visibilityListener = ActionBar.OnMenuVisibilityListener { isVisible -> + if (isVisible.not()) { + subsamplingImageView?.setOnStateChangedListener(null) + return@OnMenuVisibilityListener + } + subsamplingImageView?.setOnStateChangedListener( + object : SubsamplingScaleImageView.OnStateChangedListener { + override fun onScaleChanged(newScale: Float, origin: Int) { + viewer.activity.hideMenu() + } + + override fun onCenterChanged(newCenter: PointF?, origin: Int) { + viewer.activity.hideMenu() + } + } + ) + } + init { addView(progressIndicator) observeStatus() + viewer.activity.addOnMenuVisibilityListener(visibilityListener) } /** @@ -114,6 +134,8 @@ class PagerPageHolder( unsubscribeStatus() unsubscribeReadImageHeader() subsamplingImageView?.setOnImageEventListener(null) + subsamplingImageView?.setOnStateChangedListener(null) + viewer.activity.removeOnMenuVisibilityListener(visibilityListener) } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerViewer.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerViewer.kt index fc37c3de09..a956b61b5c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerViewer.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerViewer.kt @@ -153,6 +153,7 @@ abstract class PagerViewer(val activity: ReaderActivity) : BaseViewer { * Called when a new page (either a [ReaderPage] or [ChapterTransition]) is marked as active */ private fun onPageChange(position: Int) { + activity.hideMenu() val page = adapter.items.getOrNull(position) if (page != null && currentPage != page) { val allowPreload = checkAllowPreload(page as? ReaderPage) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonViewer.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonViewer.kt index eae4936eeb..700d5a7319 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonViewer.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonViewer.kt @@ -243,6 +243,7 @@ class WebtoonViewer(val activity: ReaderActivity, val isContinuous: Boolean = tr } fun onScrolled(pos: Int? = null) { + activity.hideMenu() val position = pos ?: layoutManager.findLastEndVisibleItemPosition() val item = adapter.items.getOrNull(position) val allowPreload = checkAllowPreload(item as? ReaderPage)