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.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
@ -114,6 +112,7 @@ import exh.source.isMdBasedSource
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.withContext
@ -187,8 +186,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
@ -252,6 +249,10 @@ class MangaController :
// Hide toolbar title on enter
if (type.isEnter) {
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) {
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
@ -492,8 +489,6 @@ class MangaController :
chaptersAdapter = null
settingsSheet = null
addSnackbar?.dismiss()
updateToolbarTitleAlpha(1F)
toolbarTextView = null
super.onDestroyView(view)
}

View File

@ -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<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 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() {
val newTransparent = if (lifted) false else isTransparentWhenNotLifted
if (transparent != newTransparent) {