11
11
12
12
package com.mifos.feature.activate
13
13
14
- import android.widget.Toast
14
+ import androidclient.feature.activate.generated.resources.Res
15
+ import androidclient.feature.activate.generated.resources.feature_activate
16
+ import androidclient.feature.activate.generated.resources.feature_activate_activation_date
17
+ import androidclient.feature.activate.generated.resources.feature_activate_cancel
18
+ import androidclient.feature.activate.generated.resources.feature_activate_client
19
+ import androidclient.feature.activate.generated.resources.feature_activate_failed_to_activate_client
20
+ import androidclient.feature.activate.generated.resources.feature_activate_select
15
21
import androidx.compose.foundation.layout.Column
16
22
import androidx.compose.foundation.layout.PaddingValues
17
23
import androidx.compose.foundation.layout.Spacer
@@ -23,36 +29,38 @@ import androidx.compose.material3.Button
23
29
import androidx.compose.material3.DatePicker
24
30
import androidx.compose.material3.DatePickerDialog
25
31
import androidx.compose.material3.ExperimentalMaterial3Api
32
+ import androidx.compose.material3.MaterialTheme
26
33
import androidx.compose.material3.SelectableDates
27
34
import androidx.compose.material3.SnackbarHostState
28
35
import androidx.compose.material3.Text
29
36
import androidx.compose.material3.TextButton
30
37
import androidx.compose.material3.rememberDatePickerState
31
38
import androidx.compose.runtime.Composable
39
+ import androidx.compose.runtime.LaunchedEffect
32
40
import androidx.compose.runtime.getValue
33
41
import androidx.compose.runtime.mutableLongStateOf
34
42
import androidx.compose.runtime.mutableStateOf
35
43
import androidx.compose.runtime.remember
36
44
import androidx.compose.runtime.saveable.rememberSaveable
37
45
import androidx.compose.runtime.setValue
38
46
import androidx.compose.ui.Modifier
39
- import androidx.compose.ui.platform.LocalContext
40
- import androidx.compose.ui.res.stringResource
41
- import androidx.compose.ui.tooling.preview.Preview
42
- import androidx.compose.ui.tooling.preview.PreviewParameter
43
- import androidx.compose.ui.tooling.preview.PreviewParameterProvider
44
47
import androidx.compose.ui.unit.dp
45
48
import androidx.compose.ui.unit.sp
46
49
import androidx.lifecycle.compose.collectAsStateWithLifecycle
47
50
import com.mifos.core.common.utils.Constants
51
+ import com.mifos.core.common.utils.formatDate
52
+ import com.mifos.core.designsystem.component.MifosButton
48
53
import com.mifos.core.designsystem.component.MifosCircularProgress
49
54
import com.mifos.core.designsystem.component.MifosDatePickerTextField
50
55
import com.mifos.core.designsystem.component.MifosScaffold
51
56
import com.mifos.core.designsystem.component.MifosSweetError
52
57
import com.mifos.core.model.objects.clients.ActivatePayload
53
- import org.koin.androidx.compose.koinViewModel
54
- import java.text.SimpleDateFormat
55
- import java.util.Locale
58
+ import com.mifos.core.ui.components.MifosAlertDialog
59
+ import com.mifos.core.ui.util.DevicePreview
60
+ import kotlinx.datetime.Clock
61
+ import org.jetbrains.compose.resources.getString
62
+ import org.jetbrains.compose.resources.stringResource
63
+ import org.koin.compose.viewmodel.koinViewModel
56
64
57
65
@Composable
58
66
internal fun ActivateScreen (
@@ -63,6 +71,7 @@ internal fun ActivateScreen(
63
71
val id by viewModel.id.collectAsStateWithLifecycle()
64
72
val activateType by viewModel.activateType.collectAsStateWithLifecycle()
65
73
74
+
66
75
ActivateScreen (
67
76
state = state,
68
77
onActivate = {
@@ -101,25 +110,22 @@ internal fun ActivateScreen(
101
110
onBackPressed : () -> Unit ,
102
111
modifier : Modifier = Modifier ,
103
112
) {
104
- val snackbarHostState = remember { SnackbarHostState () }
105
-
106
113
MifosScaffold (
107
- title = stringResource(id = R .string.feature_activate),
114
+ title = stringResource(Res .string.feature_activate),
108
115
onBackPressed = onBackPressed,
109
- snackbarHostState = snackbarHostState,
110
116
) { paddingValues ->
111
117
Column (modifier = modifier.padding(paddingValues)) {
112
118
when (state) {
113
119
is ActivateUiState .ActivatedSuccessfully -> {
114
- Toast .makeText (
115
- LocalContext .current ,
116
- stringResource(id = state.message),
117
- Toast . LENGTH_SHORT ,
118
- ).show()
119
- onBackPressed( )
120
+ MifosAlertDialog (
121
+ dialogTitle = " Success " ,
122
+ dialogText = stringResource( state.message),
123
+ onConfirmation = onBackPressed ,
124
+ onDismissRequest = onBackPressed,
125
+ )
120
126
}
121
127
122
- is ActivateUiState .Error -> MifosSweetError (message = stringResource(id = state.message)) {}
128
+ is ActivateUiState .Error -> MifosSweetError (message = stringResource(state.message)) {}
123
129
124
130
is ActivateUiState .Loading -> MifosCircularProgress ()
125
131
@@ -136,12 +142,12 @@ private fun ActivateContent(
136
142
) {
137
143
Column (modifier = modifier) {
138
144
var showDatePicker by rememberSaveable { mutableStateOf(false ) }
139
- var activateDate by rememberSaveable { mutableLongStateOf(System .currentTimeMillis ()) }
145
+ var activateDate by rememberSaveable { mutableLongStateOf(Clock . System .now().toEpochMilliseconds ()) }
140
146
val datePickerState = rememberDatePickerState(
141
147
initialSelectedDateMillis = activateDate,
142
148
selectableDates = object : SelectableDates {
143
149
override fun isSelectableDate (utcTimeMillis : Long ): Boolean {
144
- return utcTimeMillis >= System .currentTimeMillis ()
150
+ return utcTimeMillis >= Clock . System .now().toEpochMilliseconds ()
145
151
}
146
152
},
147
153
)
@@ -159,33 +165,31 @@ private fun ActivateContent(
159
165
activateDate = it
160
166
}
161
167
},
162
- ) { Text (stringResource(id = R .string.feature_activate_select)) }
168
+ ) { Text (stringResource(Res .string.feature_activate_select)) }
163
169
},
164
170
dismissButton = {
165
171
TextButton (
166
172
onClick = {
167
173
showDatePicker = false
168
174
},
169
- ) { Text (stringResource(id = R .string.feature_activate_cancel)) }
175
+ ) { Text (stringResource(Res .string.feature_activate_cancel)) }
170
176
},
171
177
) {
172
178
DatePicker (state = datePickerState)
173
179
}
174
180
}
175
181
176
182
MifosDatePickerTextField (
177
- value = SimpleDateFormat (" dd MMMM yyyy" , Locale .getDefault()).format(
178
- activateDate,
179
- ),
180
- label = stringResource(R .string.feature_activate_activation_date),
183
+ value = formatDate(activateDate),
184
+ label = stringResource(Res .string.feature_activate_activation_date),
181
185
openDatePicker = {
182
186
showDatePicker = true
183
187
},
184
188
)
185
189
186
190
Spacer (modifier = Modifier .height(16 .dp))
187
191
188
- Button (
192
+ MifosButton (
189
193
onClick = {
190
194
onActivate(
191
195
ActivatePayload (
@@ -199,30 +203,48 @@ private fun ActivateContent(
199
203
.padding(start = 16 .dp, end = 16 .dp),
200
204
contentPadding = PaddingValues (),
201
205
) {
202
- Text (text = stringResource(id = R .string.feature_activate), fontSize = 16 .sp)
206
+ Text (text = stringResource(Res .string.feature_activate),
207
+ style= MaterialTheme .typography.bodySmall)
203
208
}
204
209
}
205
210
}
206
211
207
- private class ActivateUiStateProvider : PreviewParameterProvider <ActivateUiState > {
212
+ @DevicePreview
213
+ @Composable
214
+ private fun ActivateScreenPreviewInitial () {
215
+ ActivateScreen (
216
+ state = ActivateUiState .Initial ,
217
+ onActivate = {},
218
+ onBackPressed = {},
219
+ )
220
+ }
208
221
209
- override val values: Sequence <ActivateUiState >
210
- get() = sequenceOf(
211
- ActivateUiState .Loading ,
212
- ActivateUiState .Error (R .string.feature_activate_failed_to_activate_client),
213
- ActivateUiState .ActivatedSuccessfully (R .string.feature_activate_client),
214
- ActivateUiState .Initial ,
215
- )
222
+ @DevicePreview
223
+ @Composable
224
+ private fun ActivateScreenPreviewLoading () {
225
+ ActivateScreen (
226
+ state = ActivateUiState .Loading ,
227
+ onActivate = {},
228
+ onBackPressed = {},
229
+ )
216
230
}
217
231
218
- @Preview(showBackground = true )
232
+ @DevicePreview
219
233
@Composable
220
- private fun ActivateScreenPreview (
221
- @PreviewParameter(ActivateUiStateProvider ::class ) state : ActivateUiState ,
222
- ) {
234
+ private fun ActivateScreenPreviewActivatedSuccessfully () {
223
235
ActivateScreen (
224
- state = state ,
236
+ state = ActivateUiState . ActivatedSuccessfully ( Res .string.feature_activate_client) ,
225
237
onActivate = {},
226
238
onBackPressed = {},
227
239
)
228
240
}
241
+
242
+ @DevicePreview
243
+ @Composable
244
+ private fun ActivateScreenPreviewError () {
245
+ ActivateScreen (
246
+ state = ActivateUiState .Error (Res .string.feature_activate_failed_to_activate_client),
247
+ onActivate = {},
248
+ onBackPressed = {},
249
+ )
250
+ }
0 commit comments