Skip to content

Commit

Permalink
refactor #1507: migrated settings screen to compose
Browse files Browse the repository at this point in the history
  • Loading branch information
PratyushSingh07 authored and therajanmaurya committed Feb 17, 2024
1 parent 251c82d commit e203eab
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@ import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.ui.res.stringResource

/**
* @author pratyush
* @since 12/02/2024
*/

@Composable
fun MifosDialogBox(
showDialog: Boolean,
showDialogState: MutableState<Boolean>,
onDismiss: () -> Unit,
title: Int,
message: Int? = null,
confirmButtonText: Int,
onConfirm: () -> Unit,
dismissButtonText: Int
) {
if (showDialog) {
if (showDialogState.value) {
AlertDialog(
onDismissRequest = onDismiss,
title = { Text(text = stringResource(id = title)) },
Expand Down
2 changes: 1 addition & 1 deletion core/ui/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {
compileSdk = 34

defaultConfig {
minSdk = 24
minSdk = 21

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.mifos.mobilewallet.mifospay.ui.utility

data class DialogState(
val type: DialogType = DialogType.NONE,
val onConfirm: () -> Unit = {}
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.mifos.mobilewallet.mifospay.settings.ui
package org.mifos.mobilewallet.mifospay.ui.utility

enum class DialogType {
NONE,
Expand Down
1 change: 1 addition & 0 deletions mifospay/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ dependencies {
implementation(projects.core.data)
implementation(libs.androidx.constraintlayout)

implementation(projects.core.ui)
implementation(projects.core.designsystem)
implementation(libs.androidx.appcompat)
implementation(libs.androidx.core.ktx)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.mifos.mobilewallet.mifospay.settings.ui

import MifosDialogBox
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import org.mifos.mobilewallet.mifospay.R
import org.mifos.mobilewallet.mifospay.ui.utility.DialogState
import org.mifos.mobilewallet.mifospay.ui.utility.DialogType

@Composable
fun DialogManager(dialogState: DialogState, onDismiss: () -> Unit) {
when (dialogState.type) {
DialogType.DISABLE_ACCOUNT -> MifosDialogBox(
showDialogState = remember { mutableStateOf(true) },
title = R.string.alert_disable_account,
message = R.string.alert_disable_account_desc,
confirmButtonText = R.string.ok,
dismissButtonText = R.string.cancel,
onConfirm = dialogState.onConfirm,
onDismiss = onDismiss
)

DialogType.LOGOUT -> MifosDialogBox(
showDialogState = remember { mutableStateOf(true) },
title = R.string.log_out_title,
message = R.string.empty,
confirmButtonText = R.string.yes,
dismissButtonText = R.string.no,
onConfirm = dialogState.onConfirm,
onDismiss = onDismiss
)

DialogType.NONE -> {}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.mifos.mobilewallet.mifospay.settings.ui

import MifosDialogBox
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
Expand All @@ -27,14 +26,26 @@ import org.mifos.mobilewallet.mifospay.designsystem.component.MifosItemCard
import org.mifos.mobilewallet.mifospay.designsystem.component.MifosTopBar
import org.mifos.mobilewallet.mifospay.designsystem.theme.mifosText
import org.mifos.mobilewallet.mifospay.designsystem.theme.styleSettingsButton
import org.mifos.mobilewallet.mifospay.ui.utility.DialogState
import org.mifos.mobilewallet.mifospay.ui.utility.DialogType

/**
* @author pratyush
* @since 12/02/2024
*/

@Composable
fun SettingsScreen(
backPress: () -> Unit,
disable: () -> Unit,
logout: () -> Unit
) {
var currentDialog by remember { mutableStateOf(DialogType.NONE) }
var dialogState by remember { mutableStateOf(DialogState()) }

DialogManager(
dialogState = dialogState,
onDismiss = { dialogState = DialogState(type = DialogType.NONE) }
)

Scaffold(
topBar = {
Expand Down Expand Up @@ -69,7 +80,12 @@ fun SettingsScreen(

Row(modifier = Modifier.padding(8.dp)) {
MifosItemCard(
onClick = { currentDialog = DialogType.DISABLE_ACCOUNT },
onClick = {
dialogState = DialogState(
type = DialogType.DISABLE_ACCOUNT,
onConfirm = { disable.invoke() }
)
},
colors = CardDefaults.cardColors(Color.Black)
) {
Text(
Expand All @@ -84,7 +100,12 @@ fun SettingsScreen(

Row(modifier = Modifier.padding(8.dp)) {
MifosItemCard(
onClick = { currentDialog = DialogType.LOGOUT },
onClick = {
dialogState = DialogState(
type = DialogType.LOGOUT,
onConfirm = { logout() }
)
},
colors = CardDefaults.cardColors(Color.Black)
) {
Text(
Expand All @@ -99,40 +120,6 @@ fun SettingsScreen(
}
}

if (currentDialog != DialogType.NONE) {
MifosDialogBox(
showDialog = true,
onDismiss = { currentDialog = DialogType.NONE },
title = when (currentDialog) {
DialogType.DISABLE_ACCOUNT -> R.string.alert_disable_account
DialogType.LOGOUT -> R.string.log_out_title
else -> R.string.empty
},
message = when (currentDialog) {
DialogType.DISABLE_ACCOUNT -> R.string.alert_disable_account_desc
else -> R.string.empty
},
confirmButtonText = when (currentDialog) {
DialogType.DISABLE_ACCOUNT -> R.string.ok
DialogType.LOGOUT -> R.string.yes
else -> R.string.empty
},
dismissButtonText = when (currentDialog) {
DialogType.DISABLE_ACCOUNT -> R.string.cancel
DialogType.LOGOUT -> R.string.no
else -> R.string.empty
},
onConfirm = {
when (currentDialog) {
DialogType.DISABLE_ACCOUNT -> disable()
DialogType.LOGOUT -> logout()
else -> {}
}
currentDialog = DialogType.NONE
}
)
}

}

@Preview(showSystemUi = true)
Expand Down

0 comments on commit e203eab

Please sign in to comment.