From dbbe2955596016f2b307c28115fb69a7f4cd0bba Mon Sep 17 00:00:00 2001 From: Jays2Kings Date: Fri, 10 Mar 2023 15:39:55 -0500 Subject: [PATCH] Automatically scroll manga/chapter name in reader activity --- .../tachiyomi/ui/reader/ReaderActivity.kt | 43 +++++++++++++++++++ 1 file changed, 43 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 2681931369..22f55bc607 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 @@ -19,6 +19,7 @@ import android.graphics.drawable.LayerDrawable import android.net.Uri import android.os.Build import android.os.Bundle +import android.text.TextUtils import android.text.style.DynamicDrawableSpan import android.text.style.ImageSpan import android.view.Gravity @@ -32,6 +33,7 @@ import android.view.ViewGroup import android.view.Window import android.view.WindowManager import android.view.animation.AnimationUtils +import android.widget.TextView import androidx.activity.OnBackPressedCallback import androidx.activity.addCallback import androidx.activity.viewModels @@ -49,6 +51,7 @@ import androidx.core.view.WindowInsetsCompat import androidx.core.view.WindowInsetsCompat.Type.statusBars import androidx.core.view.WindowInsetsCompat.Type.systemBars import androidx.core.view.WindowInsetsControllerCompat +import androidx.core.view.children import androidx.core.view.isInvisible import androidx.core.view.isVisible import androidx.core.view.updateLayoutParams @@ -148,6 +151,7 @@ import timber.log.Timber import java.io.File import java.text.DecimalFormat import java.text.DecimalFormatSymbols +import java.util.Collections import java.util.Locale import kotlin.math.abs import kotlin.math.max @@ -1117,6 +1121,7 @@ class ReaderActivity : BaseActivity() { toolbarAnimation.doOnStart { window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) } + toolbarAnimation.doOnEnd { delayTitleScroll() } binding.appBar.startAnimation(toolbarAnimation) } binding.chaptersSheet.chaptersBottomSheet.sheetBehavior?.collapse() @@ -1136,6 +1141,9 @@ class ReaderActivity : BaseActivity() { } else if (!animate) { binding.appBar.isVisible = false } + listOfNotNull(getTitleTextView(), getSubtitleTextView()).forEach { textView -> + textView.isSelected = false + } } menuStickyVisible = false } @@ -1310,6 +1318,17 @@ class ReaderActivity : BaseActivity() { binding.toolbar.subtitle = chapter.preferredChapterName(this, viewModel.manga!!, preferences) + listOfNotNull(getTitleTextView(), getSubtitleTextView()).forEach { textView -> + textView.ellipsize = TextUtils.TruncateAt.MARQUEE + textView.marqueeRepeatLimit = -1 + textView.isSingleLine = true + textView.isFocusable = true + textView.isFocusableInTouchMode = true + textView.isHorizontalFadingEdgeEnabled = true + textView.setFadingEdgeLength(16.dpToPx) + textView.setHorizontallyScrolling(true) + } + if (viewerChapters.nextChapter == null && viewerChapters.prevChapter == null) { binding.readerNav.leftChapter.isVisible = false binding.readerNav.rightChapter.isVisible = false @@ -1325,6 +1344,27 @@ class ReaderActivity : BaseActivity() { } } + private fun getTitleTextView(): TextView? = getTextViewsWithText(binding.toolbar.title) + private fun getSubtitleTextView(): TextView? = getTextViewsWithText(binding.toolbar.subtitle) + private fun delayTitleScroll() { + val list = listOfNotNull(getTitleTextView(), getSubtitleTextView()) + if (list.isNotEmpty()) { + scope.launchUI { + delay(1000) + list.forEach { it.isSelected = true } + } + } + } + + + private fun getTextViewsWithText(text: CharSequence?): TextView? { + if (text.isNullOrBlank()) return null + val viewTopComparator = Comparator { view1, view2 -> view1.top - view2.top } + val textViews = binding.toolbar.children.filterIsInstance() + .filter { TextUtils.equals(it.text, text) }.toList() + return if (textViews.isEmpty()) null else Collections.max(textViews, viewTopComparator) + } + /** * Called from the view model if the initial load couldn't load the pages of the chapter. In * this case the activity is closed and a toast is shown to the user. @@ -1723,6 +1763,9 @@ class ReaderActivity : BaseActivity() { toolbarAnimation.doOnStart { window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) } + toolbarAnimation.doOnEnd { + delayTitleScroll() + } binding.appBar.startAnimation(toolbarAnimation) } else if (!visible && (menuStickyVisible || menuVisible)) { if (menuStickyVisible && !menuVisible) {