Implement animated color lerp for material toolbars.

fork-5.53.8
Alex Hart 2022-06-10 16:20:02 -03:00 zatwierdzone przez Greyson Parrelli
rodzic ef3c776b4b
commit 2eb933c2d4
11 zmienionych plików z 88 dodań i 73 usunięć

Wyświetl plik

@ -52,7 +52,7 @@ abstract class DSLSettingsFragment(
adapter = settingsAdapter
getMaterial3OnScrollHelper(toolbar)?.let {
addOnScrollListener(it)
it.attach(this)
}
}

Wyświetl plik

@ -70,6 +70,7 @@ import android.widget.Toast;
import androidx.activity.OnBackPressedCallback;
import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@ -325,8 +326,6 @@ import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import kotlin.Unit;
import static org.thoughtcrime.securesms.database.GroupDatabase.GroupRecord;
/**
@ -718,6 +717,7 @@ public class ConversationParentFragment extends Fragment
fragment.setLastSeen(System.currentTimeMillis());
markLastSeen();
EventBus.getDefault().unregister(this);
material3OnScrollHelper.setColorImmediate();
}
@Override
@ -2221,7 +2221,17 @@ public class ConversationParentFragment extends Fragment
voiceNoteMediaController.getVoiceNotePlaybackState().observe(getViewLifecycleOwner(), inputPanel.getPlaybackStateObserver());
material3OnScrollHelper = new Material3OnScrollHelper(Collections.singletonList(toolbarBackground), Collections.emptyList(), this::updateStatusBarColor);
material3OnScrollHelper = new Material3OnScrollHelper(requireActivity(), Collections.singletonList(toolbarBackground), Collections.emptyList()) {
@Override
public int getActiveColorRes() {
return getActiveToolbarColor(wallpaper.getDrawable() != null);
}
@Override
public int getInactiveColorRes() {
return getInactiveToolbarColor(wallpaper.getDrawable() != null);
}
};
}
private @ColorInt int getButtonToggleBackgroundColor(MessageSendType newTransport) {
@ -2253,8 +2263,7 @@ public class ConversationParentFragment extends Fragment
attachmentKeyboardStub.get().setWallpaperEnabled(true);
}
toolbarBackground.setBackgroundResource(R.color.material3_toolbar_background_wallpaper);
updateStatusBarColor(toolbarBackground.isActivated());
material3OnScrollHelper.setColorImmediate();
int toolbarTextAndIconColor = getResources().getColor(R.color.signal_colorNeutralInverse);
toolbar.setTitleTextColor(toolbarTextAndIconColor);
setToolbarActionItemTint(toolbar, toolbarTextAndIconColor);
@ -2267,8 +2276,7 @@ public class ConversationParentFragment extends Fragment
attachmentKeyboardStub.get().setWallpaperEnabled(false);
}
toolbarBackground.setBackgroundResource(R.color.material3_toolbar_background);
updateStatusBarColor(toolbarBackground.isActivated());
material3OnScrollHelper.setColorImmediate();
int toolbarTextAndIconColor = getResources().getColor(R.color.signal_colorOnSurface);
toolbar.setTitleTextColor(toolbarTextAndIconColor);
setToolbarActionItemTint(toolbar, toolbarTextAndIconColor);
@ -2277,31 +2285,14 @@ public class ConversationParentFragment extends Fragment
messageRequestBottomView.setWallpaperEnabled(chatWallpaper != null);
}
private Unit updateStatusBarColor(boolean isActive) {
// TODO [alex] LargeScreenSupport -- statusBarBox
if (Build.VERSION.SDK_INT > 23) {
boolean hasWallpaper = wallpaper.getDrawable() != null;
int toolbarColor = isActive ? getActiveToolbarColor(requireContext(), hasWallpaper)
: getInactiveToolbarColor(requireContext(), hasWallpaper);
WindowUtil.setStatusBarColor(requireActivity().getWindow(), toolbarColor);
}
return Unit.INSTANCE;
private static @ColorRes int getActiveToolbarColor(boolean hasWallpaper) {
return hasWallpaper ? R.color.conversation_toolbar_color_wallpaper_scrolled
: R.color.signal_colorSurface2;
}
private static @ColorInt int getActiveToolbarColor(@NonNull Context context, boolean hasWallpaper) {
int colorRes = hasWallpaper ? R.color.conversation_toolbar_color_wallpaper_scrolled
: R.color.signal_colorSurface2;
return ContextCompat.getColor(context, colorRes);
}
private static @ColorInt int getInactiveToolbarColor(@NonNull Context context, boolean hasWallpaper) {
int colorRes = hasWallpaper ? R.color.conversation_toolbar_color_wallpaper
: R.color.signal_colorBackground;
return ContextCompat.getColor(context, colorRes);
private static @ColorRes int getInactiveToolbarColor(boolean hasWallpaper) {
return hasWallpaper ? R.color.conversation_toolbar_color_wallpaper
: R.color.signal_colorBackground;
}
private void setToolbarActionItemTint(@NonNull Toolbar toolbar, @ColorInt int tint) {
@ -3623,12 +3614,12 @@ public class ConversationParentFragment extends Fragment
@Override
public void bindScrollHelper(@NonNull RecyclerView recyclerView) {
recyclerView.addOnScrollListener(material3OnScrollHelper);
material3OnScrollHelper.attach(recyclerView);
}
@Override
public void onMessageDetailsFragmentDismissed() {
updateStatusBarColor(toolbarBackground.isActivated());
material3OnScrollHelper.setColorImmediate();
}
// Listeners

Wyświetl plik

@ -336,12 +336,10 @@ class MainActivityListHostFragment : Fragment(R.layout.main_activity_list_host_f
}
override fun bindScrollHelper(recyclerView: RecyclerView) {
recyclerView.addOnScrollListener(
Material3OnScrollHelper(
requireActivity(),
listOf(_toolbarBackground),
listOf(_searchToolbar)
)
)
Material3OnScrollHelper(
requireActivity(),
listOf(_toolbarBackground),
listOf(_searchToolbar)
).attach(recyclerView)
}
}

Wyświetl plik

@ -109,7 +109,7 @@ public final class MessageDetailsFragment extends FullScreenDialogFragment {
list.setAdapter(adapter);
list.setItemAnimator(null);
list.addOnScrollListener(new Material3OnScrollHelper(requireActivity(), toolbarShadow));
new Material3OnScrollHelper(requireActivity(), toolbarShadow).attach(list);
}
private void initializeViewModel() {

Wyświetl plik

@ -1,10 +1,12 @@
package org.thoughtcrime.securesms.util
import android.animation.ValueAnimator
import android.app.Activity
import android.os.Build
import android.view.View
import androidx.annotation.ColorInt
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.animation.ArgbEvaluatorCompat
import org.thoughtcrime.securesms.R
import org.thoughtcrime.securesms.util.views.Stub
@ -13,20 +15,36 @@ import org.thoughtcrime.securesms.util.views.Stub
* This can be used to appropriately tint toolbar backgrounds. Also can emit the state change
* for other purposes.
*/
class Material3OnScrollHelper(
open class Material3OnScrollHelper(
private val activity: Activity,
private val views: List<View>,
private val viewStubs: List<Stub<out View>> = emptyList(),
private val onActiveStateChanged: (Boolean) -> Unit
) : RecyclerView.OnScrollListener() {
private val viewStubs: List<Stub<out View>> = emptyList()
) {
constructor(activity: Activity, views: List<View>, viewStubs: List<Stub<out View>>) : this(views, viewStubs, { updateStatusBarColor(activity, it) })
open val activeColorRes: Int = R.color.signal_colorSurface2
open val inactiveColorRes: Int = R.color.signal_colorBackground
constructor(activity: Activity, view: View) : this(listOf(view), emptyList(), { updateStatusBarColor(activity, it) })
constructor(activity: Activity, view: View) : this(activity, listOf(view), emptyList())
private var animator: ValueAnimator? = null
private var active: Boolean? = null
private val scrollListener = OnScrollListener()
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
updateActiveState(recyclerView.canScrollVertically(-1))
fun attach(recyclerView: RecyclerView) {
recyclerView.addOnScrollListener(scrollListener)
scrollListener.onScrolled(recyclerView, 0, 0)
}
/**
* Cancels any currently running scroll animation and sets the color immediately.
*/
fun setColorImmediate() {
if (active == null) {
return
}
animator?.cancel()
setColor(ContextCompat.getColor(activity, if (active == true) activeColorRes else inactiveColorRes))
}
private fun updateActiveState(isActive: Boolean) {
@ -34,23 +52,41 @@ class Material3OnScrollHelper(
return
}
val hadActiveState = active != null
active = isActive
views.forEach { it.isActivated = isActive }
viewStubs.filter { it.resolved() }.forEach { it.get().isActivated = isActive }
onActiveStateChanged(isActive)
}
if (animator?.isRunning == true) {
animator?.reverse()
} else {
val startColor = ContextCompat.getColor(activity, if (isActive) inactiveColorRes else activeColorRes)
val endColor = ContextCompat.getColor(activity, if (isActive) activeColorRes else inactiveColorRes)
companion object {
fun updateStatusBarColor(activity: Activity, isActive: Boolean) {
if (Build.VERSION.SDK_INT > 21) {
if (isActive) {
WindowUtil.setStatusBarColor(activity.window, ContextCompat.getColor(activity, R.color.signal_colorSurface2))
} else {
WindowUtil.setStatusBarColor(activity.window, ContextCompat.getColor(activity, R.color.signal_colorBackground))
if (hadActiveState) {
animator = ValueAnimator.ofObject(ArgbEvaluatorCompat(), startColor, endColor).apply {
duration = 200
addUpdateListener { animator ->
setColor(animator.animatedValue as Int)
}
start()
}
} else {
setColorImmediate()
}
}
}
private fun setColor(@ColorInt color: Int) {
WindowUtil.setStatusBarColor(activity.window, color)
views.forEach { it.setBackgroundColor(color) }
viewStubs.filter { it.resolved() }.forEach { it.get().setBackgroundColor(color) }
}
private inner class OnScrollListener : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
updateActiveState(recyclerView.canScrollVertically(-1))
}
}
}

Wyświetl plik

@ -1,5 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/signal_colorSurface2" android:state_activated="true" />
<item android:color="@color/signal_colorBackground" />
</selector>

Wyświetl plik

@ -1,5 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/conversation_toolbar_color_wallpaper_scrolled" android:state_activated="true" />
<item android:color="@color/conversation_toolbar_color_wallpaper" />
</selector>

Wyświetl plik

@ -164,7 +164,7 @@
android:id="@+id/toolbar_background"
android:layout_width="match_parent"
android:layout_height="@dimen/signal_m3_toolbar_height"
android:background="@color/material3_toolbar_background"
android:background="@color/signal_colorBackground"
app:layout_constraintEnd_toEndOf="@id/parent_end_guideline"
app:layout_constraintStart_toStartOf="@id/parent_start_guideline"
app:layout_constraintTop_toTopOf="@id/status_bar_guideline" />

Wyświetl plik

@ -3,7 +3,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="@dimen/signal_m3_toolbar_height"
android:background="@color/material3_toolbar_background"
android:background="@color/signal_colorBackground"
android:minHeight="@dimen/signal_m3_toolbar_height"
android:theme="?attr/actionBarStyle"
android:visibility="gone"

Wyświetl plik

@ -8,7 +8,7 @@
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/signal_m3_toolbar_height"
android:background="@color/material3_toolbar_background"
android:background="@color/signal_colorBackground"
android:minHeight="@dimen/signal_m3_toolbar_height"
android:theme="?attr/settingsToolbarStyle"
app:layout_constraintEnd_toEndOf="parent"

Wyświetl plik

@ -9,7 +9,7 @@
android:id="@+id/toolbar_background"
android:layout_width="match_parent"
android:layout_height="@dimen/signal_m3_toolbar_height"
android:background="@color/material3_toolbar_background"
android:background="@color/signal_colorBackground"
app:layout_constraintTop_toTopOf="parent" />
<org.thoughtcrime.securesms.util.views.DarkOverflowToolbar