@@ -7,13 +7,16 @@ import androidx.compose.foundation.interaction.MutableInteractionSource
77import androidx.compose.foundation.layout.Box
88import androidx.compose.foundation.layout.fillMaxSize
99import androidx.compose.runtime.Composable
10+ import androidx.compose.runtime.State
1011import androidx.compose.runtime.collectAsState
1112import androidx.compose.runtime.derivedStateOf
1213import androidx.compose.runtime.getValue
1314import androidx.compose.runtime.remember
1415import androidx.compose.ui.Alignment.Companion.BottomCenter
1516import androidx.compose.ui.Modifier
1617import androidx.compose.ui.draw.alpha
18+ import com.getcode.models.BillState
19+ import com.getcode.models.Confirmation
1720import com.getcode.theme.Black40
1821import com.getcode.ui.modals.TipConfirmation
1922import com.getcode.ui.utils.AnimationUtils
@@ -28,36 +31,24 @@ fun PaymentScaffold(content: @Composable () -> Unit) {
2831 val state by payments.state.collectAsState()
2932 Box (modifier = Modifier .fillMaxSize()) {
3033 content()
31- val showScrim by remember(state.billState) {
32- derivedStateOf {
33- val loginConfirmation = state.billState.loginConfirmation
34- val paymentConfirmation = state.billState.privatePaymentConfirmation
35- val socialPaymentConfirmation = state.billState.socialUserPaymentConfirmation
36- val publicPaymentConfirmation = state.billState.publicPaymentConfirmation
37- val messageTipPaymentConfirmation = state.billState.messageTipPaymentConfirmation
34+ val scrimDetails by rememberConfirmationDetails(state.billState)
3835
39- listOf (
40- loginConfirmation,
41- paymentConfirmation,
42- socialPaymentConfirmation,
43- publicPaymentConfirmation,
44- messageTipPaymentConfirmation
45- ).any {
46- it?.showScrim == true
47- }
48- }
49- }
36+ val scrimAlpha by animateFloatAsState(if (scrimDetails.show) 1f else 0f , label = " scrim visibility" )
5037
51- val scrimAlpha by animateFloatAsState(if (showScrim) 1f else 0f , label = " scrim visibility" )
52-
53- if (showScrim) {
38+ if (scrimDetails.show) {
5439 Box (
5540 modifier = Modifier
5641 .fillMaxSize()
5742 .alpha(scrimAlpha)
5843 .background(Black40 )
59- .rememberedClickable(indication = null ,
60- interactionSource = remember { MutableInteractionSource () }) {}
44+ .rememberedClickable(
45+ indication = null ,
46+ interactionSource = remember { MutableInteractionSource () }
47+ ) {
48+ if (scrimDetails.cancellable) {
49+ payments.cancelPayment()
50+ }
51+ }
6152 )
6253 }
6354
@@ -122,4 +113,29 @@ fun PaymentScaffold(content: @Composable () -> Unit) {
122113 }
123114 }
124115 }
116+ }
117+
118+ data class ScrimDetails (val show : Boolean , val cancellable : Boolean )
119+
120+ @Composable
121+ private fun rememberConfirmationDetails (billState : BillState ): State <ScrimDetails > {
122+ return remember(billState) {
123+ derivedStateOf {
124+ val loginConfirmation = billState.loginConfirmation
125+ val paymentConfirmation = billState.privatePaymentConfirmation
126+ val socialPaymentConfirmation = billState.socialUserPaymentConfirmation
127+ val publicPaymentConfirmation = billState.publicPaymentConfirmation
128+ val messageTipPaymentConfirmation = billState.messageTipPaymentConfirmation
129+
130+ listOf (
131+ loginConfirmation,
132+ paymentConfirmation,
133+ socialPaymentConfirmation,
134+ publicPaymentConfirmation,
135+ messageTipPaymentConfirmation
136+ ).firstNotNullOfOrNull { it }?.let { conf ->
137+ ScrimDetails (conf.showScrim, conf.cancellable)
138+ } ? : ScrimDetails (show = false , cancellable = false )
139+ }
140+ }
125141}
0 commit comments