Skip to content

Commit 1d6ae5e

Browse files
biplab1niyajali
andauthored
refactor(feature:send-money): standardize dialog state error handling (#1886)
Co-authored-by: Sk Niyaj Ali <[email protected]>
1 parent 75f2c92 commit 1d6ae5e

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

feature/send-money/src/commonMain/kotlin/org/mifospay/feature/send/money/SendMoneyScreen.kt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -506,24 +506,27 @@ private fun SendMoneyDialogs(
506506
onDismissRequest: () -> Unit,
507507
) {
508508
when (dialogState) {
509-
is SendMoneyState.DialogState.Error -> MifosBasicDialog(
509+
is SendMoneyState.DialogState.Error.ResourceMessage -> MifosBasicDialog(
510510
visibilityState = Shown(
511-
message = dialogState.message,
511+
message = stringResource(dialogState.message),
512512
),
513513
onDismissRequest = onDismissRequest,
514514
)
515515

516-
is SendMoneyState.DialogState.Loading -> MifosLoadingDialog(
517-
visibilityState = LoadingDialogState.Shown,
518-
)
519-
520-
is SendMoneyState.DialogState.ValidationError -> MifosBasicDialog(
516+
is SendMoneyState.DialogState.Error.GenericResourceMessage -> MifosBasicDialog(
521517
visibilityState = Shown(
522-
message = stringResource(dialogState.res),
518+
message = stringResource(
519+
dialogState.message,
520+
*dialogState.args.toTypedArray(),
521+
),
523522
),
524523
onDismissRequest = onDismissRequest,
525524
)
526525

526+
is SendMoneyState.DialogState.Loading -> MifosLoadingDialog(
527+
visibilityState = LoadingDialogState.Shown,
528+
)
529+
527530
null -> Unit
528531
}
529532
}

feature/send-money/src/commonMain/kotlin/org/mifospay/feature/send/money/SendMoneyViewModel.kt

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import kotlinx.coroutines.flow.onEach
2525
import kotlinx.coroutines.flow.stateIn
2626
import kotlinx.coroutines.flow.update
2727
import kotlinx.coroutines.launch
28+
import kotlinx.serialization.Contextual
2829
import kotlinx.serialization.Serializable
2930
import mobile_wallet.feature.send_money.generated.resources.Res
3031
import mobile_wallet.feature.send_money.generated.resources.feature_send_money_error_account_cannot_be_empty
@@ -33,7 +34,6 @@ import mobile_wallet.feature.send_money.generated.resources.feature_send_money_e
3334
import mobile_wallet.feature.send_money.generated.resources.feature_send_money_error_requesting_payment_qr_but_found
3435
import mobile_wallet.feature.send_money.generated.resources.feature_send_money_error_requesting_payment_qr_data_missing
3536
import org.jetbrains.compose.resources.StringResource
36-
import org.jetbrains.compose.resources.getString
3737
import org.mifospay.core.common.DataState
3838
import org.mifospay.core.common.getSerialized
3939
import org.mifospay.core.common.setSerialized
@@ -45,7 +45,6 @@ import org.mifospay.core.model.utils.toAccount
4545
import org.mifospay.core.ui.utils.BaseViewModel
4646
import org.mifospay.feature.send.money.SendMoneyAction.HandleRequestData
4747
import org.mifospay.feature.send.money.SendMoneyState.DialogState.Error
48-
import org.mifospay.feature.send.money.SendMoneyState.DialogState.ValidationError
4948

5049
class SendMoneyViewModel(
5150
private val scanner: QrScanner,
@@ -170,7 +169,7 @@ class SendMoneyViewModel(
170169

171170
private fun updateErrorState(res: StringResource) {
172171
mutableStateFlow.update {
173-
it.copy(dialogState = ValidationError(res))
172+
it.copy(dialogState = Error.ResourceMessage(res))
174173
}
175174
}
176175

@@ -187,17 +186,19 @@ class SendMoneyViewModel(
187186
)
188187
}
189188
} catch (e: Exception) {
190-
val message = if (action.requestData.isNotEmpty()) {
191-
getString(
189+
val errorState = if (action.requestData.isNotEmpty()) {
190+
Error.GenericResourceMessage(
192191
Res.string.feature_send_money_error_requesting_payment_qr_but_found,
193-
action.requestData,
192+
listOf(action.requestData),
194193
)
195194
} else {
196-
getString(Res.string.feature_send_money_error_requesting_payment_qr_data_missing)
195+
Error.ResourceMessage(Res.string.feature_send_money_error_requesting_payment_qr_data_missing)
197196
}
198197

199198
mutableStateFlow.update {
200-
it.copy(dialogState = Error(message))
199+
it.copy(
200+
dialogState = errorState,
201+
)
201202
}
202203
}
203204
}
@@ -234,9 +235,16 @@ data class SendMoneyState(
234235
data object Loading : DialogState
235236

236237
@Serializable
237-
data class Error(val message: String) : DialogState
238-
239-
data class ValidationError(val res: StringResource) : DialogState
238+
sealed interface Error : DialogState {
239+
@Serializable
240+
data class ResourceMessage(@Contextual val message: StringResource) : Error
241+
242+
@Serializable
243+
data class GenericResourceMessage(
244+
@Contextual val message: StringResource,
245+
val args: List<String>,
246+
) : Error
247+
}
240248
}
241249
}
242250

0 commit comments

Comments
 (0)