mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-22 09:49:30 +01:00
Allow autofocus of name field.
This commit is contained in:
committed by
Cody Henthorne
parent
b05f4430f6
commit
0312dfcfcd
@@ -2,12 +2,14 @@ package org.thoughtcrime.securesms.util
|
||||
|
||||
import android.animation.ValueAnimator
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.view.View
|
||||
import androidx.annotation.ColorInt
|
||||
import androidx.annotation.ColorRes
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.google.android.material.animation.ArgbEvaluatorCompat
|
||||
import com.google.android.material.appbar.AppBarLayout
|
||||
import org.thoughtcrime.securesms.R
|
||||
import org.thoughtcrime.securesms.util.views.Stub
|
||||
|
||||
@@ -17,11 +19,18 @@ import org.thoughtcrime.securesms.util.views.Stub
|
||||
* for other purposes.
|
||||
*/
|
||||
open class Material3OnScrollHelper(
|
||||
private val activity: Activity,
|
||||
private val context: Context,
|
||||
private val setStatusBarColor: (Int) -> Unit,
|
||||
private val views: List<View>,
|
||||
private val viewStubs: List<Stub<out View>> = emptyList()
|
||||
) {
|
||||
|
||||
constructor(activity: Activity, views: List<View>, viewStubs: List<Stub<out View>>) : this(activity, { WindowUtil.setStatusBarColor(activity.window, it) }, views, viewStubs)
|
||||
|
||||
constructor(activity: Activity, views: List<View>) : this(activity, { WindowUtil.setStatusBarColor(activity.window, it) }, views, emptyList())
|
||||
|
||||
constructor(activity: Activity, view: View) : this(activity, { WindowUtil.setStatusBarColor(activity.window, it) }, listOf(view), emptyList())
|
||||
|
||||
/**
|
||||
* A pair of colors tied to a specific state.
|
||||
*/
|
||||
@@ -35,15 +44,23 @@ open class Material3OnScrollHelper(
|
||||
open val activeColorSet: ColorSet = ColorSet(R.color.signal_colorSurface2)
|
||||
open val inactiveColorSet: ColorSet = ColorSet(R.color.signal_colorBackground)
|
||||
|
||||
constructor(activity: Activity, view: View) : this(activity, listOf(view), emptyList())
|
||||
|
||||
private var animator: ValueAnimator? = null
|
||||
private var active: Boolean? = null
|
||||
private val scrollListener = OnScrollListener()
|
||||
|
||||
fun attach(recyclerView: RecyclerView) {
|
||||
recyclerView.addOnScrollListener(scrollListener)
|
||||
scrollListener.onScrolled(recyclerView, 0, 0)
|
||||
recyclerView.addOnScrollListener(
|
||||
OnScrollListener().apply {
|
||||
onScrolled(recyclerView, 0, 0)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
fun attach(appBarLayout: AppBarLayout) {
|
||||
appBarLayout.addOnOffsetChangedListener(
|
||||
OnOffsetChangedListener().apply {
|
||||
onOffsetChanged(appBarLayout, 0)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,8 +73,8 @@ open class Material3OnScrollHelper(
|
||||
|
||||
animator?.cancel()
|
||||
val colorSet = if (active == true) activeColorSet else inactiveColorSet
|
||||
setToolbarColor(ContextCompat.getColor(activity, colorSet.toolbarColorRes))
|
||||
setStatusBarColor(ContextCompat.getColor(activity, colorSet.statusBarColorRes))
|
||||
setToolbarColor(ContextCompat.getColor(context, colorSet.toolbarColorRes))
|
||||
setStatusBarColor(ContextCompat.getColor(context, colorSet.statusBarColorRes))
|
||||
}
|
||||
|
||||
private fun updateActiveState(isActive: Boolean) {
|
||||
@@ -78,10 +95,10 @@ open class Material3OnScrollHelper(
|
||||
val endColorSet = if (isActive) activeColorSet else inactiveColorSet
|
||||
|
||||
if (hadActiveState) {
|
||||
val startToolbarColor = ContextCompat.getColor(activity, startColorSet.toolbarColorRes)
|
||||
val endToolbarColor = ContextCompat.getColor(activity, endColorSet.toolbarColorRes)
|
||||
val startStatusBarColor = ContextCompat.getColor(activity, startColorSet.statusBarColorRes)
|
||||
val endStatusBarColor = ContextCompat.getColor(activity, endColorSet.statusBarColorRes)
|
||||
val startToolbarColor = ContextCompat.getColor(context, startColorSet.toolbarColorRes)
|
||||
val endToolbarColor = ContextCompat.getColor(context, endColorSet.toolbarColorRes)
|
||||
val startStatusBarColor = ContextCompat.getColor(context, startColorSet.statusBarColorRes)
|
||||
val endStatusBarColor = ContextCompat.getColor(context, endColorSet.statusBarColorRes)
|
||||
|
||||
animator = ValueAnimator.ofFloat(0f, 1f).apply {
|
||||
duration = 200
|
||||
@@ -102,13 +119,15 @@ open class Material3OnScrollHelper(
|
||||
viewStubs.filter { it.resolved() }.forEach { it.get().setBackgroundColor(color) }
|
||||
}
|
||||
|
||||
private fun setStatusBarColor(@ColorInt color: Int) {
|
||||
WindowUtil.setStatusBarColor(activity.window, color)
|
||||
}
|
||||
|
||||
private inner class OnScrollListener : RecyclerView.OnScrollListener() {
|
||||
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
|
||||
updateActiveState(recyclerView.canScrollVertically(-1))
|
||||
}
|
||||
}
|
||||
|
||||
private inner class OnOffsetChangedListener : AppBarLayout.OnOffsetChangedListener {
|
||||
override fun onOffsetChanged(appBarLayout: AppBarLayout, verticalOffset: Int) {
|
||||
updateActiveState(verticalOffset != 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user