Reimplement main activity toolbars in compose.

This commit is contained in:
Alex Hart
2025-03-28 14:34:04 -03:00
committed by Greyson Parrelli
parent 5f7ce0d96d
commit f1985cf506
24 changed files with 1405 additions and 680 deletions

View File

@@ -1,13 +1,12 @@
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.core.widget.NestedScrollView
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.RecyclerView
@@ -22,40 +21,25 @@ import org.thoughtcrime.securesms.util.views.Stub
* for other purposes.
*/
open class Material3OnScrollHelper(
private val context: Context,
private val setStatusBarColor: (Int) -> Unit,
private val getStatusBarColor: () -> Int,
private val activity: FragmentActivity,
private val setStatusBarColor: (Int) -> Unit = { WindowUtil.setStatusBarColor(activity.window, it) },
getStatusBarColor: () -> Int = { WindowUtil.getStatusBarColor(activity.window) },
private val setChatFolderColor: (Int) -> Unit = {},
private val views: List<View>,
private val onSetToolbarColor: (Int) -> Unit = {},
private val views: List<View> = emptyList(),
private val viewStubs: List<Stub<out View>> = emptyList(),
lifecycleOwner: LifecycleOwner
) {
constructor(activity: Activity, view: View, lifecycleOwner: LifecycleOwner) : this(activity = activity, views = listOf(view), lifecycleOwner = lifecycleOwner)
constructor(activity: Activity, views: List<View>, viewStubs: List<Stub<out View>> = emptyList(), lifecycleOwner: LifecycleOwner) : this(
activity = activity,
views = views,
viewStubs = viewStubs,
lifecycleOwner = lifecycleOwner,
setChatFolderColor = {}
)
constructor(
activity: Activity,
views: List<View>,
viewStubs: List<Stub<out View>> = emptyList(),
lifecycleOwner: LifecycleOwner,
setChatFolderColor: (Int) -> Unit = {}
) : this(
context = activity,
setStatusBarColor = { WindowUtil.setStatusBarColor(activity.window, it) },
getStatusBarColor = { WindowUtil.getStatusBarColor(activity.window) },
setChatFolderColor = setChatFolderColor,
views = views,
viewStubs = viewStubs,
lifecycleOwner = lifecycleOwner
)
companion object {
/**
* Override for our single java usage.
*/
@JvmStatic
fun create(activity: FragmentActivity, toolbar: View): Material3OnScrollHelper {
return Material3OnScrollHelper(activity = activity, views = listOf(toolbar), lifecycleOwner = activity)
}
}
open val activeColorSet: ColorSet = ColorSet(
toolbarColorRes = R.color.signal_colorSurface2,
@@ -116,9 +100,9 @@ open class Material3OnScrollHelper(
animator?.cancel()
val colorSet = if (active == true) activeColorSet else inactiveColorSet
setToolbarColor(ContextCompat.getColor(context, colorSet.toolbarColorRes))
setStatusBarColor(ContextCompat.getColor(context, colorSet.statusBarColorRes))
setChatFolderColor(ContextCompat.getColor(context, colorSet.chatFolderColorRes))
setToolbarColor(ContextCompat.getColor(activity, colorSet.toolbarColorRes))
setStatusBarColor(ContextCompat.getColor(activity, colorSet.statusBarColorRes))
setChatFolderColor(ContextCompat.getColor(activity, colorSet.chatFolderColorRes))
}
private fun updateActiveState(isActive: Boolean) {
@@ -139,12 +123,12 @@ open class Material3OnScrollHelper(
val endColorSet = if (isActive) activeColorSet else inactiveColorSet
if (hadActiveState) {
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)
val startChatFolderColor = ContextCompat.getColor(context, startColorSet.chatFolderColorRes)
val endChatFolderColor = ContextCompat.getColor(context, endColorSet.chatFolderColorRes)
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 startChatFolderColor = ContextCompat.getColor(activity, startColorSet.chatFolderColorRes)
val endChatFolderColor = ContextCompat.getColor(activity, endColorSet.chatFolderColorRes)
animator = ValueAnimator.ofFloat(0f, 1f).apply {
duration = 200
@@ -164,6 +148,7 @@ open class Material3OnScrollHelper(
private fun setToolbarColor(@ColorInt color: Int) {
views.forEach { it.setBackgroundColor(color) }
viewStubs.filter { it.resolved() }.forEach { it.get().setBackgroundColor(color) }
onSetToolbarColor(color)
}
private inner class OnScrollListener : RecyclerView.OnScrollListener(), AppBarLayout.OnOffsetChangedListener, NestedScrollView.OnScrollChangeListener {