1+ package com.getcode.navigation.screens
2+
3+ import androidx.activity.compose.BackHandler
4+ import androidx.compose.foundation.Image
5+ import androidx.compose.foundation.background
6+ import androidx.compose.foundation.clickable
7+ import androidx.compose.foundation.interaction.MutableInteractionSource
8+ import androidx.compose.foundation.layout.Arrangement
9+ import androidx.compose.foundation.layout.Box
10+ import androidx.compose.foundation.layout.Column
11+ import androidx.compose.foundation.layout.fillMaxSize
12+ import androidx.compose.foundation.layout.fillMaxWidth
13+ import androidx.compose.foundation.layout.padding
14+ import androidx.compose.foundation.shape.CircleShape
15+ import androidx.compose.material.Text
16+ import androidx.compose.runtime.Composable
17+ import androidx.compose.runtime.remember
18+ import androidx.compose.ui.Alignment
19+ import androidx.compose.ui.Modifier
20+ import androidx.compose.ui.res.painterResource
21+ import cafe.adriel.voyager.core.screen.Screen
22+ import com.getcode.manager.ModalManager
23+ import com.getcode.theme.BrandLight
24+ import com.getcode.theme.CodeTheme
25+ import com.getcode.ui.components.ButtonState
26+ import com.getcode.ui.components.CodeButton
27+ import com.getcode.ui.utils.addIf
28+
29+ fun buildMessageContent (
30+ message : ModalManager .Message ,
31+ onClose : (ModalManager .ActionType ? ) -> Unit
32+ ): Screen {
33+ return ModalContainerMessage (message, onClose)
34+ }
35+
36+ private data class ModalContainerMessage (
37+ val message : ModalManager .Message ,
38+ val onClose : (ModalManager .ActionType ? ) -> Unit ,
39+ ) : Screen, NamedScreen, ModalRoot {
40+
41+ @Composable
42+ override fun Content () {
43+ ModalContainer (
44+ modalHeightMetric = ModalHeightMetric .WrapContent ,
45+ closeButtonEnabled = { it is ModalContainerMessage },
46+ onCloseClicked = {
47+ onClose(null )
48+ }
49+ ) {
50+ Column (
51+ modifier = Modifier .padding(horizontal = CodeTheme .dimens.inset),
52+ horizontalAlignment = Alignment .CenterHorizontally ,
53+ verticalArrangement = Arrangement .spacedBy(CodeTheme .dimens.grid.x2)
54+ ) {
55+ message.icon?.let { imageResId ->
56+ Box (
57+ modifier = Modifier
58+ .background(BrandLight , CircleShape ),
59+ contentAlignment = Alignment .Center
60+ ) {
61+ Image (
62+ modifier = Modifier .padding(CodeTheme .dimens.grid.x3),
63+ painter = painterResource(imageResId),
64+ contentDescription = null ,
65+ )
66+ }
67+ }
68+ Text (
69+ modifier = Modifier
70+ .fillMaxWidth()
71+ .addIf(message.icon != null ) {
72+ Modifier .padding(top = CodeTheme .dimens.grid.x2)
73+ },
74+ text = message.title,
75+ style = CodeTheme .typography.displaySmall,
76+ color = CodeTheme .colors.onBackground,
77+ )
78+
79+ if (message.subtitle.isNotEmpty()) {
80+ Text (
81+ modifier = Modifier .fillMaxWidth(),
82+ text = message.subtitle,
83+ style = CodeTheme .typography.textSmall,
84+ color = CodeTheme .colors.onBackground,
85+ )
86+ }
87+
88+
89+ CodeButton (
90+ modifier = Modifier
91+ .fillMaxWidth()
92+ .padding(top = CodeTheme .dimens.grid.x2),
93+ buttonState = ButtonState .Filled ,
94+ text = message.positiveText,
95+ onClick = {
96+ message.onPositive()
97+ onClose(ModalManager .ActionType .Positive )
98+ }
99+ )
100+
101+ message.negativeText?.let { negativeText ->
102+ if (negativeText.isNotEmpty()) {
103+ CodeButton (
104+ modifier = Modifier .fillMaxWidth(),
105+ buttonState = ButtonState .Filled10 ,
106+ text = negativeText,
107+ onClick = {
108+ message.onNegative()
109+ onClose(ModalManager .ActionType .Negative )
110+ }
111+ )
112+ }
113+ }
114+
115+ message.tertiaryText?.let { tertiaryText ->
116+ if (tertiaryText.isNotEmpty()) {
117+ CodeButton (
118+ modifier = Modifier .fillMaxWidth(),
119+ buttonState = ButtonState .Bordered ,
120+ text = tertiaryText,
121+ onClick = {
122+ message.onTertiary()
123+ onClose(ModalManager .ActionType .Tertiary )
124+ }
125+ )
126+ }
127+ }
128+ }
129+ }
130+
131+ BackHandler (message.isDismissibleByBackButton) {
132+ onClose(null )
133+ }
134+ }
135+ }
0 commit comments