2017-01-22 17:13:07 -05:00
|
|
|
package eu.kanade.tachiyomi.widget
|
|
|
|
|
2021-08-19 09:12:52 -04:00
|
|
|
import android.animation.ValueAnimator
|
2017-01-22 17:13:07 -05:00
|
|
|
import android.content.Context
|
2020-01-28 22:47:57 -05:00
|
|
|
import android.util.AttributeSet
|
2021-09-09 21:05:41 -04:00
|
|
|
import android.widget.TextView
|
|
|
|
import androidx.annotation.FloatRange
|
2021-09-18 16:41:23 -04:00
|
|
|
import androidx.coordinatorlayout.widget.CoordinatorLayout
|
2021-09-09 21:05:41 -04:00
|
|
|
import androidx.lifecycle.coroutineScope
|
|
|
|
import androidx.lifecycle.findViewTreeLifecycleOwner
|
2021-08-19 09:12:52 -04:00
|
|
|
import com.google.android.material.animation.AnimationUtils
|
2020-01-05 11:29:27 -05:00
|
|
|
import com.google.android.material.appbar.AppBarLayout
|
2021-09-18 16:41:23 -04:00
|
|
|
import com.google.android.material.appbar.HideToolbarOnScrollBehavior
|
2021-08-19 09:12:52 -04:00
|
|
|
import com.google.android.material.appbar.MaterialToolbar
|
|
|
|
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
|
|
|
|
|
|
|
class ElevationAppBarLayout @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
|
|
|
|
private var transparent = false
|
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-08-19 09:12:52 -04:00
|
|
|
private var elevationAnimator: ValueAnimator? = null
|
|
|
|
private var backgroundAlphaAnimator: ValueAnimator? = null
|
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
|
|
|
|
updateBackgroundAlpha()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-18 16:41:23 -04:00
|
|
|
override fun getBehavior(): CoordinatorLayout.Behavior<AppBarLayout> = HideToolbarOnScrollBehavior()
|
|
|
|
|
2021-08-19 09:12:52 -04:00
|
|
|
/**
|
|
|
|
* Disabled. Lift on scroll is handled manually with [TachiyomiCoordinatorLayout]
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
val from = elevation
|
|
|
|
val to = if (lifted) {
|
|
|
|
resources.getDimension(R.dimen.design_appbar_elevation)
|
|
|
|
} else {
|
|
|
|
0F
|
|
|
|
}
|
|
|
|
|
|
|
|
elevationAnimator?.cancel()
|
|
|
|
elevationAnimator = ValueAnimator.ofFloat(from, to).apply {
|
|
|
|
duration = resources.getInteger(R.integer.app_bar_elevation_anim_duration).toLong()
|
|
|
|
interpolator = AnimationUtils.LINEAR_INTERPOLATOR
|
|
|
|
addUpdateListener {
|
|
|
|
elevation = it.animatedValue as Float
|
|
|
|
}
|
|
|
|
start()
|
|
|
|
}
|
|
|
|
|
|
|
|
updateBackgroundAlpha()
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2017-01-22 17:13:07 -05:00
|
|
|
|
2021-09-09 21:05:41 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-19 09:12:52 -04:00
|
|
|
private fun updateBackgroundAlpha() {
|
|
|
|
val newTransparent = if (lifted) false else isTransparentWhenNotLifted
|
|
|
|
if (transparent != newTransparent) {
|
|
|
|
transparent = newTransparent
|
|
|
|
val fromAlpha = if (transparent) 255 else 0
|
|
|
|
val toAlpha = if (transparent) 0 else 255
|
2017-01-22 17:13:07 -05:00
|
|
|
|
2021-08-19 09:12:52 -04:00
|
|
|
backgroundAlphaAnimator?.cancel()
|
|
|
|
backgroundAlphaAnimator = ValueAnimator.ofInt(fromAlpha, toAlpha).apply {
|
|
|
|
duration = resources.getInteger(R.integer.app_bar_elevation_anim_duration).toLong()
|
|
|
|
interpolator = AnimationUtils.LINEAR_INTERPOLATOR
|
|
|
|
addUpdateListener {
|
|
|
|
val alpha = it.animatedValue as Int
|
|
|
|
background.alpha = alpha
|
|
|
|
toolbar?.background?.alpha = alpha
|
|
|
|
statusBarForeground?.alpha = alpha
|
|
|
|
}
|
|
|
|
start()
|
|
|
|
}
|
2017-01-22 17:13:07 -05:00
|
|
|
}
|
|
|
|
}
|
2020-01-05 11:29:27 -05:00
|
|
|
}
|