diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt index fe4ef1308c..fd20c9bef7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt @@ -14,13 +14,11 @@ import android.view.MenuInflater import android.view.MenuItem import android.view.View import android.view.ViewGroup -import android.widget.TextView import androidx.annotation.FloatRange import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.view.ActionMode import androidx.core.os.bundleOf import androidx.core.view.WindowInsetsCompat -import androidx.core.view.children import androidx.core.view.doOnLayout import androidx.core.view.isVisible import androidx.core.view.updateLayoutParams @@ -94,6 +92,7 @@ import eu.kanade.tachiyomi.util.view.getCoordinates import eu.kanade.tachiyomi.util.view.shrinkOnScroll import eu.kanade.tachiyomi.util.view.snack import eu.kanade.tachiyomi.widget.materialdialogs.QuadStateTextView +import kotlinx.coroutines.cancel import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach import reactivecircus.flowbinding.recyclerview.scrollEvents @@ -148,8 +147,6 @@ class MangaController : private val preferences: PreferencesHelper by injectLazy() private val coverCache: CoverCache by injectLazy() - private var toolbarTextView: TextView? = null - private var mangaInfoAdapter: MangaInfoHeaderAdapter? = null private var chaptersHeaderAdapter: MangaChaptersHeaderAdapter? = null private var chaptersAdapter: ChaptersAdapter? = null @@ -198,6 +195,10 @@ class MangaController : // Hide toolbar title on enter if (type.isEnter) { updateToolbarTitleAlpha() + } else { + // Cancel listeners early + viewScope.cancel() + updateToolbarTitleAlpha(1F) } } @@ -344,11 +345,7 @@ class MangaController : private fun updateToolbarTitleAlpha(@FloatRange(from = 0.0, to = 1.0) alpha: Float? = null) { val scrolledList = binding.fullRecycler ?: binding.infoRecycler!! - if (toolbarTextView == null) { - toolbarTextView = (activity as? MainActivity)?.binding?.toolbar?.children - ?.find { it is TextView } as? TextView - } - toolbarTextView?.alpha = when { + (activity as? MainActivity)?.binding?.appbar?.titleTextAlpha = when { // Specific alpha provided alpha != null -> alpha @@ -412,8 +409,6 @@ class MangaController : chaptersAdapter = null settingsSheet = null addSnackbar?.dismiss() - updateToolbarTitleAlpha(1F) - toolbarTextView = null super.onDestroyView(view) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/ElevationAppBarLayout.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/ElevationAppBarLayout.kt index 5ef1fdff8f..3f68bb1c27 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/ElevationAppBarLayout.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/ElevationAppBarLayout.kt @@ -3,10 +3,19 @@ package eu.kanade.tachiyomi.widget import android.animation.ValueAnimator import android.content.Context import android.util.AttributeSet +import android.widget.TextView +import androidx.annotation.FloatRange +import androidx.lifecycle.coroutineScope +import androidx.lifecycle.findViewTreeLifecycleOwner import com.google.android.material.animation.AnimationUtils import com.google.android.material.appbar.AppBarLayout import com.google.android.material.appbar.MaterialToolbar import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.util.view.findChild +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.onEach +import reactivecircus.flowbinding.android.view.HierarchyChangeEvent +import reactivecircus.flowbinding.android.view.hierarchyChangeEvents class ElevationAppBarLayout @JvmOverloads constructor( context: Context, @@ -18,6 +27,19 @@ class ElevationAppBarLayout @JvmOverloads constructor( private val toolbar by lazy { findViewById(R.id.toolbar) } + @FloatRange(from = 0.0, to = 1.0) + var titleTextAlpha = 1F + set(value) { + field = value + titleTextView?.alpha = field + } + + private var titleTextView: TextView? = null + set(value) { + field = value + field?.alpha = titleTextAlpha + } + private var elevationAnimator: ValueAnimator? = null private var backgroundAlphaAnimator: ValueAnimator? = null @@ -63,6 +85,29 @@ class ElevationAppBarLayout @JvmOverloads constructor( } } + override fun onAttachedToWindow() { + super.onAttachedToWindow() + titleTextView = toolbar.findChild() + findViewTreeLifecycleOwner()?.lifecycle?.coroutineScope?.let { scope -> + toolbar.hierarchyChangeEvents() + .onEach { + when (it) { + is HierarchyChangeEvent.ChildAdded -> { + if (it.child is TextView) { + titleTextView = it.child as TextView + } + } + is HierarchyChangeEvent.ChildRemoved -> { + if (it.child == titleTextView) { + titleTextView = null + } + } + } + } + .launchIn(scope) + } + } + private fun updateBackgroundAlpha() { val newTransparent = if (lifted) false else isTransparentWhenNotLifted if (transparent != newTransparent) {