MangaController: Title fixes (#5879)

* MangaController: Move toolbar's TextView reference to ElevationAppBarLayout

* MangaController: Update title alpha earlier when exiting

(cherry picked from commit b45c3227291228e9b9a7de0b24f33e97f6c66981)

# Conflicts:
#	app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt
This commit is contained in:
Ivan Iskandar 2021-09-10 08:05:41 +07:00 committed by Jobobby04
parent 0492d501f3
commit 3d71e643e1
2 changed files with 51 additions and 11 deletions

View File

@ -14,13 +14,11 @@ import android.view.MenuInflater
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.TextView
import androidx.annotation.FloatRange import androidx.annotation.FloatRange
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.view.ActionMode import androidx.appcompat.view.ActionMode
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
import androidx.core.view.WindowInsetsCompat import androidx.core.view.WindowInsetsCompat
import androidx.core.view.children
import androidx.core.view.doOnLayout import androidx.core.view.doOnLayout
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.core.view.updateLayoutParams import androidx.core.view.updateLayoutParams
@ -114,6 +112,7 @@ import exh.source.isMdBasedSource
import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.NonCancellable import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@ -187,8 +186,6 @@ class MangaController :
private val preferences: PreferencesHelper by injectLazy() private val preferences: PreferencesHelper by injectLazy()
private val coverCache: CoverCache by injectLazy() private val coverCache: CoverCache by injectLazy()
private var toolbarTextView: TextView? = null
private var mangaInfoAdapter: MangaInfoHeaderAdapter? = null private var mangaInfoAdapter: MangaInfoHeaderAdapter? = null
private var chaptersHeaderAdapter: MangaChaptersHeaderAdapter? = null private var chaptersHeaderAdapter: MangaChaptersHeaderAdapter? = null
@ -252,6 +249,10 @@ class MangaController :
// Hide toolbar title on enter // Hide toolbar title on enter
if (type.isEnter) { if (type.isEnter) {
updateToolbarTitleAlpha() updateToolbarTitleAlpha()
} else {
// Cancel listeners early
viewScope.cancel()
updateToolbarTitleAlpha(1F)
} }
} }
@ -423,11 +424,7 @@ class MangaController :
private fun updateToolbarTitleAlpha(@FloatRange(from = 0.0, to = 1.0) alpha: Float? = null) { private fun updateToolbarTitleAlpha(@FloatRange(from = 0.0, to = 1.0) alpha: Float? = null) {
val scrolledList = binding.fullRecycler ?: binding.infoRecycler!! val scrolledList = binding.fullRecycler ?: binding.infoRecycler!!
if (toolbarTextView == null) { (activity as? MainActivity)?.binding?.appbar?.titleTextAlpha = when {
toolbarTextView = (activity as? MainActivity)?.binding?.toolbar?.children
?.find { it is TextView } as? TextView
}
toolbarTextView?.alpha = when {
// Specific alpha provided // Specific alpha provided
alpha != null -> alpha alpha != null -> alpha
@ -492,8 +489,6 @@ class MangaController :
chaptersAdapter = null chaptersAdapter = null
settingsSheet = null settingsSheet = null
addSnackbar?.dismiss() addSnackbar?.dismiss()
updateToolbarTitleAlpha(1F)
toolbarTextView = null
super.onDestroyView(view) super.onDestroyView(view)
} }

View File

@ -3,10 +3,19 @@ package eu.kanade.tachiyomi.widget
import android.animation.ValueAnimator import android.animation.ValueAnimator
import android.content.Context import android.content.Context
import android.util.AttributeSet 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.animation.AnimationUtils
import com.google.android.material.appbar.AppBarLayout import com.google.android.material.appbar.AppBarLayout
import com.google.android.material.appbar.MaterialToolbar import com.google.android.material.appbar.MaterialToolbar
import eu.kanade.tachiyomi.R 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( class ElevationAppBarLayout @JvmOverloads constructor(
context: Context, context: Context,
@ -18,6 +27,19 @@ class ElevationAppBarLayout @JvmOverloads constructor(
private val toolbar by lazy { findViewById<MaterialToolbar>(R.id.toolbar) } private val toolbar by lazy { findViewById<MaterialToolbar>(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 elevationAnimator: ValueAnimator? = null
private var backgroundAlphaAnimator: ValueAnimator? = null private var backgroundAlphaAnimator: ValueAnimator? = null
@ -63,6 +85,29 @@ class ElevationAppBarLayout @JvmOverloads constructor(
} }
} }
override fun onAttachedToWindow() {
super.onAttachedToWindow()
titleTextView = toolbar.findChild<TextView>()
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() { private fun updateBackgroundAlpha() {
val newTransparent = if (lifted) false else isTransparentWhenNotLifted val newTransparent = if (lifted) false else isTransparentWhenNotLifted
if (transparent != newTransparent) { if (transparent != newTransparent) {