Skip to content

Commit e203eab

Browse files
PratyushSingh07therajanmaurya
authored andcommitted
refactor #1507: migrated settings screen to compose
1 parent 251c82d commit e203eab

File tree

7 files changed

+78
-42
lines changed

7 files changed

+78
-42
lines changed

core/designsystem/src/main/java/org/mifos/mobilewallet/mifospay/designsystem/component/MifosDialogBox.kt

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,25 @@ import androidx.compose.material3.AlertDialog
22
import androidx.compose.material3.Text
33
import androidx.compose.material3.TextButton
44
import androidx.compose.runtime.Composable
5+
import androidx.compose.runtime.MutableState
56
import androidx.compose.ui.res.stringResource
67

8+
/**
9+
* @author pratyush
10+
* @since 12/02/2024
11+
*/
12+
713
@Composable
814
fun MifosDialogBox(
9-
showDialog: Boolean,
15+
showDialogState: MutableState<Boolean>,
1016
onDismiss: () -> Unit,
1117
title: Int,
1218
message: Int? = null,
1319
confirmButtonText: Int,
1420
onConfirm: () -> Unit,
1521
dismissButtonText: Int
1622
) {
17-
if (showDialog) {
23+
if (showDialogState.value) {
1824
AlertDialog(
1925
onDismissRequest = onDismiss,
2026
title = { Text(text = stringResource(id = title)) },

core/ui/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ android {
99
compileSdk = 34
1010

1111
defaultConfig {
12-
minSdk = 24
12+
minSdk = 21
1313

1414
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
1515
consumerProguardFiles("consumer-rules.pro")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.mifos.mobilewallet.mifospay.ui.utility
2+
3+
data class DialogState(
4+
val type: DialogType = DialogType.NONE,
5+
val onConfirm: () -> Unit = {}
6+
)

mifospay/src/main/java/org/mifos/mobilewallet/mifospay/settings/ui/DialogType.kt core/ui/src/main/kotlin/org/mifos/mobilewallet/mifospay/ui/utility/DialogType.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.mifos.mobilewallet.mifospay.settings.ui
1+
package org.mifos.mobilewallet.mifospay.ui.utility
22

33
enum class DialogType {
44
NONE,

mifospay/build.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ dependencies {
4747
implementation(projects.core.data)
4848
implementation(libs.androidx.constraintlayout)
4949

50+
implementation(projects.core.ui)
5051
implementation(projects.core.designsystem)
5152
implementation(libs.androidx.appcompat)
5253
implementation(libs.androidx.core.ktx)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.mifos.mobilewallet.mifospay.settings.ui
2+
3+
import MifosDialogBox
4+
import androidx.compose.runtime.Composable
5+
import androidx.compose.runtime.mutableStateOf
6+
import androidx.compose.runtime.remember
7+
import org.mifos.mobilewallet.mifospay.R
8+
import org.mifos.mobilewallet.mifospay.ui.utility.DialogState
9+
import org.mifos.mobilewallet.mifospay.ui.utility.DialogType
10+
11+
@Composable
12+
fun DialogManager(dialogState: DialogState, onDismiss: () -> Unit) {
13+
when (dialogState.type) {
14+
DialogType.DISABLE_ACCOUNT -> MifosDialogBox(
15+
showDialogState = remember { mutableStateOf(true) },
16+
title = R.string.alert_disable_account,
17+
message = R.string.alert_disable_account_desc,
18+
confirmButtonText = R.string.ok,
19+
dismissButtonText = R.string.cancel,
20+
onConfirm = dialogState.onConfirm,
21+
onDismiss = onDismiss
22+
)
23+
24+
DialogType.LOGOUT -> MifosDialogBox(
25+
showDialogState = remember { mutableStateOf(true) },
26+
title = R.string.log_out_title,
27+
message = R.string.empty,
28+
confirmButtonText = R.string.yes,
29+
dismissButtonText = R.string.no,
30+
onConfirm = dialogState.onConfirm,
31+
onDismiss = onDismiss
32+
)
33+
34+
DialogType.NONE -> {}
35+
}
36+
}

mifospay/src/main/java/org/mifos/mobilewallet/mifospay/settings/ui/SettingsScreen.kt

+25-38
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.mifos.mobilewallet.mifospay.settings.ui
22

3-
import MifosDialogBox
43
import androidx.compose.foundation.background
54
import androidx.compose.foundation.layout.Column
65
import androidx.compose.foundation.layout.Row
@@ -27,14 +26,26 @@ import org.mifos.mobilewallet.mifospay.designsystem.component.MifosItemCard
2726
import org.mifos.mobilewallet.mifospay.designsystem.component.MifosTopBar
2827
import org.mifos.mobilewallet.mifospay.designsystem.theme.mifosText
2928
import org.mifos.mobilewallet.mifospay.designsystem.theme.styleSettingsButton
29+
import org.mifos.mobilewallet.mifospay.ui.utility.DialogState
30+
import org.mifos.mobilewallet.mifospay.ui.utility.DialogType
31+
32+
/**
33+
* @author pratyush
34+
* @since 12/02/2024
35+
*/
3036

3137
@Composable
3238
fun SettingsScreen(
3339
backPress: () -> Unit,
3440
disable: () -> Unit,
3541
logout: () -> Unit
3642
) {
37-
var currentDialog by remember { mutableStateOf(DialogType.NONE) }
43+
var dialogState by remember { mutableStateOf(DialogState()) }
44+
45+
DialogManager(
46+
dialogState = dialogState,
47+
onDismiss = { dialogState = DialogState(type = DialogType.NONE) }
48+
)
3849

3950
Scaffold(
4051
topBar = {
@@ -69,7 +80,12 @@ fun SettingsScreen(
6980

7081
Row(modifier = Modifier.padding(8.dp)) {
7182
MifosItemCard(
72-
onClick = { currentDialog = DialogType.DISABLE_ACCOUNT },
83+
onClick = {
84+
dialogState = DialogState(
85+
type = DialogType.DISABLE_ACCOUNT,
86+
onConfirm = { disable.invoke() }
87+
)
88+
},
7389
colors = CardDefaults.cardColors(Color.Black)
7490
) {
7591
Text(
@@ -84,7 +100,12 @@ fun SettingsScreen(
84100

85101
Row(modifier = Modifier.padding(8.dp)) {
86102
MifosItemCard(
87-
onClick = { currentDialog = DialogType.LOGOUT },
103+
onClick = {
104+
dialogState = DialogState(
105+
type = DialogType.LOGOUT,
106+
onConfirm = { logout() }
107+
)
108+
},
88109
colors = CardDefaults.cardColors(Color.Black)
89110
) {
90111
Text(
@@ -99,40 +120,6 @@ fun SettingsScreen(
99120
}
100121
}
101122

102-
if (currentDialog != DialogType.NONE) {
103-
MifosDialogBox(
104-
showDialog = true,
105-
onDismiss = { currentDialog = DialogType.NONE },
106-
title = when (currentDialog) {
107-
DialogType.DISABLE_ACCOUNT -> R.string.alert_disable_account
108-
DialogType.LOGOUT -> R.string.log_out_title
109-
else -> R.string.empty
110-
},
111-
message = when (currentDialog) {
112-
DialogType.DISABLE_ACCOUNT -> R.string.alert_disable_account_desc
113-
else -> R.string.empty
114-
},
115-
confirmButtonText = when (currentDialog) {
116-
DialogType.DISABLE_ACCOUNT -> R.string.ok
117-
DialogType.LOGOUT -> R.string.yes
118-
else -> R.string.empty
119-
},
120-
dismissButtonText = when (currentDialog) {
121-
DialogType.DISABLE_ACCOUNT -> R.string.cancel
122-
DialogType.LOGOUT -> R.string.no
123-
else -> R.string.empty
124-
},
125-
onConfirm = {
126-
when (currentDialog) {
127-
DialogType.DISABLE_ACCOUNT -> disable()
128-
DialogType.LOGOUT -> logout()
129-
else -> {}
130-
}
131-
currentDialog = DialogType.NONE
132-
}
133-
)
134-
}
135-
136123
}
137124

138125
@Preview(showSystemUi = true)

0 commit comments

Comments
 (0)