2022-01-06 22:54:51 -05:00
|
|
|
@file:Suppress("PackageDirectoryMismatch")
|
|
|
|
|
2021-11-18 10:47:24 -05:00
|
|
|
package com.google.android.material.appbar
|
2017-01-22 17:13:07 -05:00
|
|
|
|
2021-10-15 22:09:19 -04:00
|
|
|
import android.animation.AnimatorSet
|
2021-08-19 09:12:52 -04:00
|
|
|
import android.animation.ValueAnimator
|
2021-10-15 22:09:19 -04:00
|
|
|
import android.annotation.SuppressLint
|
2017-01-22 17:13:07 -05:00
|
|
|
import android.content.Context
|
2022-01-28 10:14:13 -05:00
|
|
|
import android.graphics.Canvas
|
|
|
|
import android.graphics.drawable.Drawable
|
2020-01-28 22:47:57 -05:00
|
|
|
import android.util.AttributeSet
|
2022-01-28 10:14:13 -05:00
|
|
|
import android.view.animation.LinearInterpolator
|
2021-09-09 21:05:41 -04:00
|
|
|
import android.widget.TextView
|
|
|
|
import androidx.annotation.FloatRange
|
|
|
|
import androidx.lifecycle.coroutineScope
|
|
|
|
import androidx.lifecycle.findViewTreeLifecycleOwner
|
2021-10-15 22:09:19 -04:00
|
|
|
import com.google.android.material.shape.MaterialShapeDrawable
|
2022-01-28 10:14:13 -05:00
|
|
|
import dev.chrisbanes.insetter.applyInsetter
|
2021-08-19 09:12:52 -04:00
|
|
|
import eu.kanade.tachiyomi.R
|
2021-09-09 21:05:41 -04:00
|
|
|
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
|
2017-01-22 17:13:07 -05:00
|
|
|
|
2021-11-18 10:47:24 -05:00
|
|
|
/**
|
|
|
|
* [AppBarLayout] with our own lift state handler and custom title alpha.
|
|
|
|
*
|
|
|
|
* Inside this package to access some package-private methods.
|
|
|
|
*/
|
|
|
|
class TachiyomiAppBarLayout @JvmOverloads constructor(
|
2020-02-26 18:03:34 -05:00
|
|
|
context: Context,
|
|
|
|
attrs: AttributeSet? = null
|
2017-01-22 17:13:07 -05:00
|
|
|
) : AppBarLayout(context, attrs) {
|
|
|
|
|
2021-08-19 09:12:52 -04:00
|
|
|
private var lifted = true
|
2017-01-22 17:13:07 -05:00
|
|
|
|
2021-08-19 09:12:52 -04:00
|
|
|
private val toolbar by lazy { findViewById<MaterialToolbar>(R.id.toolbar) }
|
2017-01-22 17:13:07 -05:00
|
|
|
|
2021-09-09 21:05:41 -04:00
|
|
|
@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
|
|
|
|
}
|
|
|
|
|
2021-10-15 22:09:19 -04:00
|
|
|
private var animatorSet: AnimatorSet? = null
|
|
|
|
|
|
|
|
private var statusBarForegroundAnimator: ValueAnimator? = null
|
2022-01-28 10:14:13 -05:00
|
|
|
private var currentOffset = 0
|
2021-01-18 17:24:23 -05:00
|
|
|
|
2021-08-19 09:12:52 -04:00
|
|
|
var isTransparentWhenNotLifted = false
|
|
|
|
set(value) {
|
|
|
|
if (field != value) {
|
|
|
|
field = value
|
2021-10-15 22:09:19 -04:00
|
|
|
updateStates()
|
2021-08-19 09:12:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-11-18 10:47:24 -05:00
|
|
|
* Disabled. Lift on scroll is handled manually with [eu.kanade.tachiyomi.widget.TachiyomiCoordinatorLayout]
|
2021-08-19 09:12:52 -04:00
|
|
|
*/
|
|
|
|
override fun isLiftOnScroll(): Boolean = false
|
2017-01-22 17:13:07 -05:00
|
|
|
|
2021-08-19 09:12:52 -04:00
|
|
|
override fun isLifted(): Boolean = lifted
|
2017-01-22 17:13:07 -05:00
|
|
|
|
2021-08-19 09:12:52 -04:00
|
|
|
override fun setLifted(lifted: Boolean): Boolean {
|
|
|
|
return if (this.lifted != lifted) {
|
|
|
|
this.lifted = lifted
|
2021-10-15 22:09:19 -04:00
|
|
|
updateStates()
|
2021-08-19 09:12:52 -04:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2017-01-22 17:13:07 -05:00
|
|
|
|
2021-11-18 10:47:24 -05:00
|
|
|
override fun setLiftedState(lifted: Boolean, force: Boolean): Boolean = false
|
|
|
|
|
2022-01-28 10:14:13 -05:00
|
|
|
override fun draw(canvas: Canvas) {
|
|
|
|
super.draw(canvas)
|
|
|
|
val saveCount = canvas.save()
|
|
|
|
canvas.translate(0f, -currentOffset.toFloat())
|
|
|
|
statusBarForeground?.draw(canvas)
|
|
|
|
canvas.restoreToCount(saveCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
|
|
|
|
super.onLayout(changed, l, t, r, b)
|
|
|
|
statusBarForeground?.setBounds(0, 0, width, paddingTop)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onOffsetChanged(offset: Int) {
|
|
|
|
currentOffset = offset
|
|
|
|
super.onOffsetChanged(offset)
|
|
|
|
|
|
|
|
// Show status bar foreground when offset
|
|
|
|
val foreground = (statusBarForeground as? MaterialShapeDrawable) ?: return
|
|
|
|
val start = foreground.alpha
|
|
|
|
val end = if (offset != 0) 255 else 0
|
|
|
|
|
|
|
|
statusBarForegroundAnimator?.cancel()
|
|
|
|
if (animatorSet?.isRunning == true) {
|
|
|
|
foreground.alpha = end
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (start != end) {
|
|
|
|
statusBarForegroundAnimator = ValueAnimator.ofInt(start, end).apply {
|
|
|
|
duration = resources.getInteger(R.integer.app_bar_elevation_anim_duration).toLong()
|
|
|
|
interpolator = LINEAR_INTERPOLATOR
|
|
|
|
addUpdateListener {
|
|
|
|
foreground.alpha = it.animatedValue as Int
|
|
|
|
}
|
|
|
|
start()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-09 21:05:41 -04:00
|
|
|
override fun onAttachedToWindow() {
|
|
|
|
super.onAttachedToWindow()
|
2021-10-15 22:09:19 -04:00
|
|
|
toolbar.background.alpha = 0 // Use app bar background
|
|
|
|
|
2021-09-09 21:05:41 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-28 10:14:13 -05:00
|
|
|
override fun setStatusBarForeground(drawable: Drawable?) {
|
|
|
|
super.setStatusBarForeground(drawable)
|
|
|
|
setWillNotDraw(statusBarForeground == null)
|
2021-10-15 22:09:19 -04:00
|
|
|
}
|
2017-01-22 17:13:07 -05:00
|
|
|
|
2021-10-15 22:09:19 -04:00
|
|
|
@SuppressLint("Recycle")
|
|
|
|
private fun updateStates() {
|
|
|
|
val animators = mutableListOf<ValueAnimator>()
|
|
|
|
|
|
|
|
val fromElevation = elevation
|
|
|
|
val toElevation = if (lifted) {
|
|
|
|
resources.getDimension(R.dimen.design_appbar_elevation)
|
|
|
|
} else {
|
|
|
|
0F
|
|
|
|
}
|
|
|
|
if (fromElevation != toElevation) {
|
|
|
|
ValueAnimator.ofFloat(fromElevation, toElevation).apply {
|
2021-08-19 09:12:52 -04:00
|
|
|
addUpdateListener {
|
2021-10-15 22:09:19 -04:00
|
|
|
elevation = it.animatedValue as Float
|
2021-11-18 10:47:24 -05:00
|
|
|
(statusBarForeground as? MaterialShapeDrawable)?.elevation = it.animatedValue as Float
|
2021-08-19 09:12:52 -04:00
|
|
|
}
|
2021-10-15 22:09:19 -04:00
|
|
|
animators.add(this)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
val transparent = if (lifted) false else isTransparentWhenNotLifted
|
2021-12-17 09:32:42 -05:00
|
|
|
val fromAlpha = (background as? MaterialShapeDrawable)?.alpha ?: background.alpha
|
2021-10-15 22:09:19 -04:00
|
|
|
val toAlpha = if (transparent) 0 else 255
|
|
|
|
if (fromAlpha != toAlpha) {
|
|
|
|
ValueAnimator.ofInt(fromAlpha, toAlpha).apply {
|
|
|
|
addUpdateListener {
|
|
|
|
val value = it.animatedValue as Int
|
|
|
|
background.alpha = value
|
|
|
|
}
|
|
|
|
animators.add(this)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (animators.isNotEmpty()) {
|
|
|
|
animatorSet?.cancel()
|
|
|
|
animatorSet = AnimatorSet().apply {
|
|
|
|
duration = resources.getInteger(R.integer.app_bar_elevation_anim_duration).toLong()
|
2022-01-28 10:14:13 -05:00
|
|
|
interpolator = LINEAR_INTERPOLATOR
|
2021-10-15 22:09:19 -04:00
|
|
|
playTogether(*animators.toTypedArray())
|
2021-08-19 09:12:52 -04:00
|
|
|
start()
|
|
|
|
}
|
2017-01-22 17:13:07 -05:00
|
|
|
}
|
|
|
|
}
|
2021-11-18 10:47:24 -05:00
|
|
|
|
|
|
|
init {
|
|
|
|
statusBarForeground = MaterialShapeDrawable.createWithElevationOverlay(context)
|
2022-01-28 10:14:13 -05:00
|
|
|
applyInsetter {
|
|
|
|
type(navigationBars = true) {
|
|
|
|
margin(horizontal = true)
|
|
|
|
}
|
|
|
|
type(statusBars = true) {
|
|
|
|
padding(top = true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
private val LINEAR_INTERPOLATOR = LinearInterpolator()
|
2021-11-18 10:47:24 -05:00
|
|
|
}
|
2020-01-05 11:29:27 -05:00
|
|
|
}
|