From 29682537e0878671bb75ae19dee0a3a71f72e3b2 Mon Sep 17 00:00:00 2001 From: Brandon McAnsh Date: Tue, 9 Jan 2024 16:22:22 -0500 Subject: [PATCH] fix(home/toast): revert animation changes and remember toast to prevent recomp additionally set toast to null _after_ animation runs Signed-off-by: Brandon McAnsh --- .../com/getcode/view/main/home/DecorView.kt | 36 ++++++++++--------- .../com/getcode/view/main/home/HomeScan.kt | 4 ++- .../getcode/view/main/home/HomeViewModel.kt | 15 ++++++-- 3 files changed, 36 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/com/getcode/view/main/home/DecorView.kt b/app/src/main/java/com/getcode/view/main/home/DecorView.kt index 14e76f1d4b..351f417471 100644 --- a/app/src/main/java/com/getcode/view/main/home/DecorView.kt +++ b/app/src/main/java/com/getcode/view/main/home/DecorView.kt @@ -1,6 +1,7 @@ package com.getcode.view.main.home import androidx.compose.animation.AnimatedContent +import androidx.compose.animation.AnimatedContentTransitionScope import androidx.compose.animation.AnimatedVisibility import androidx.compose.animation.core.tween import androidx.compose.animation.fadeIn @@ -26,8 +27,12 @@ import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.MaterialTheme import androidx.compose.material.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.derivedStateOf +import androidx.compose.runtime.getValue +import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.alpha import androidx.compose.ui.draw.clip import androidx.compose.ui.res.painterResource import androidx.compose.ui.text.font.FontWeight @@ -81,31 +86,30 @@ internal fun DecorView( ) Column(modifier = Modifier.align(Alignment.BottomCenter)) { - //Balance Changed Toast - AnimatedContent( + AnimatedVisibility( modifier = Modifier .align(Alignment.End) .padding(end = CodeTheme.dimens.grid.x5, bottom = CodeTheme.dimens.grid.x3), - targetState = dataState.billState.toast, - transitionSpec = { - slideInVertically(animationSpec = tween(600), initialOffsetY = { it }) + - fadeIn(animationSpec = tween(500, 100)) togetherWith if (!isPaused) - slideOutVertically(animationSpec = tween(600), targetOffsetY = { it }) + - fadeOut(animationSpec = tween(500, 100)) - else fadeOut(animationSpec = tween(0)) - }, - label = "toast", - ) { toast -> - if (toast != null) { - Timber.d("toast=$toast") - } + visible = dataState.billState.showToast, + enter = slideInVertically(animationSpec = tween(600), initialOffsetY = { it }) + + fadeIn(animationSpec = tween(500, 100)), + exit = if (!isPaused) + slideOutVertically(animationSpec = tween(600), targetOffsetY = { it }) + + fadeOut(animationSpec = tween(500, 100)) + else fadeOut(animationSpec = tween(0)), + ) { Row( modifier = Modifier .wrapContentSize() .clip(CodeTheme.shapes.xxl) .background(Black50) - .padding(horizontal = CodeTheme.dimens.grid.x2, vertical = CodeTheme.dimens.grid.x1), + .padding( + horizontal = CodeTheme.dimens.grid.x2, + vertical = CodeTheme.dimens.grid.x1 + ), ) { + val toast by + remember(dataState.billState.toast) { derivedStateOf { dataState.billState.toast } } Text( text = toast?.formattedAmount.orEmpty(), style = CodeTheme.typography.body2.copy( diff --git a/app/src/main/java/com/getcode/view/main/home/HomeScan.kt b/app/src/main/java/com/getcode/view/main/home/HomeScan.kt index 9947ff3b2e..1625b045df 100644 --- a/app/src/main/java/com/getcode/view/main/home/HomeScan.kt +++ b/app/src/main/java/com/getcode/view/main/home/HomeScan.kt @@ -211,7 +211,9 @@ private fun HomeScan( Lifecycle.Event.ON_PAUSE -> { isPaused = true - homeViewModel.startSheetDismissTimer { navigator.hide() } + homeViewModel.startSheetDismissTimer { + Timber.d("hiding from timeout") + navigator.hide() } } Lifecycle.Event.ON_RESUME -> { diff --git a/app/src/main/java/com/getcode/view/main/home/HomeViewModel.kt b/app/src/main/java/com/getcode/view/main/home/HomeViewModel.kt index d694f4555d..5d198103f4 100644 --- a/app/src/main/java/com/getcode/view/main/home/HomeViewModel.kt +++ b/app/src/main/java/com/getcode/view/main/home/HomeViewModel.kt @@ -57,6 +57,7 @@ import java.util.concurrent.TimeUnit import javax.inject.Inject import kotlin.concurrent.schedule import kotlin.concurrent.timerTask +import kotlin.time.Duration.Companion.milliseconds import kotlin.time.Duration.Companion.seconds sealed interface PresentationStyle { @@ -213,7 +214,6 @@ class HomeViewModel @Inject constructor( } if (bill.didReceive) { - delay(300) withContext(Dispatchers.Main) { uiFlow.update { val billState = it.billState @@ -349,11 +349,22 @@ class HomeViewModel @Inject constructor( val billState = uiModel.billState uiModel.copy( billState = billState.copy( - toast = null, showToast = false ) ) } + // wait for animation to run + Timer().schedule(500.milliseconds.inWholeMilliseconds) { + uiFlow.update { + uiModel -> + val billState = uiModel.billState + uiModel.copy( + billState = billState.copy( + toast = null + ) + ) + } + } } }