Skip to content

Commit f265712

Browse files
refactor(loan-application): dialogs (#2959)
1 parent ba94568 commit f265712

File tree

8 files changed

+662
-561
lines changed

8 files changed

+662
-561
lines changed

feature/loan-application/src/commonMain/kotlin/org/mifos/mobile/feature/loan/application/confirmDetails/ConfirmDetailsScreen.kt

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ import org.mifos.mobile.core.designsystem.component.MifosElevatedScaffold
3737
import org.mifos.mobile.core.designsystem.theme.DesignToken
3838
import org.mifos.mobile.core.designsystem.theme.MifosTypography
3939
import org.mifos.mobile.core.ui.component.MifosDetailsCard
40+
import org.mifos.mobile.core.ui.component.MifosErrorComponent
4041
import org.mifos.mobile.core.ui.component.MifosPoweredCard
4142
import org.mifos.mobile.core.ui.component.MifosProgressIndicator
4243
import org.mifos.mobile.core.ui.component.MifosProgressIndicatorOverlay
4344
import org.mifos.mobile.core.ui.utils.EventsEffect
45+
import org.mifos.mobile.core.ui.utils.ScreenUiState
4446

4547
@Composable
4648
internal fun ConfirmDetailsScreen(
@@ -99,10 +101,6 @@ internal fun ConfirmDetailsDialog(
99101
)
100102
}
101103

102-
ConfirmDetailsDialogState.Loading -> MifosProgressIndicator()
103-
104-
ConfirmDetailsDialogState.OverlayLoading -> MifosProgressIndicatorOverlay()
105-
106104
null -> {}
107105
}
108106
}
@@ -126,27 +124,52 @@ internal fun ConfirmDetailsScreenContent(
126124
}
127125
},
128126
) {
129-
Column(
130-
modifier = modifier
131-
.padding(DesignToken.padding.large)
132-
.padding(top = DesignToken.padding.medium)
133-
.verticalScroll(rememberScrollState()),
134-
verticalArrangement = Arrangement.spacedBy(DesignToken.spacing.extraLarge),
135-
) {
136-
MifosDetailsCard(state.details)
127+
when (state.uiState) {
128+
is ScreenUiState.Error -> {
129+
MifosErrorComponent(
130+
isRetryEnabled = false,
131+
message = stringResource(state.uiState.message),
132+
)
133+
}
134+
135+
ScreenUiState.Loading -> MifosProgressIndicator()
137136

138-
MifosButton(
139-
modifier = Modifier.fillMaxWidth().height(DesignToken.sizes.inputHeight),
140-
onClick = {
141-
onAction(ConfirmDetailsAction.NavigateToAuthenticate)
142-
},
143-
shape = DesignToken.shapes.medium,
144-
) {
145-
Text(
146-
text = stringResource(Res.string.feature_apply_loan_title),
147-
style = MifosTypography.titleMedium,
137+
ScreenUiState.Network -> {
138+
MifosErrorComponent(
139+
isNetworkConnected = false,
140+
isRetryEnabled = false,
148141
)
149142
}
143+
144+
ScreenUiState.Success -> {
145+
Column(
146+
modifier = modifier
147+
.padding(DesignToken.padding.large)
148+
.padding(top = DesignToken.padding.medium)
149+
.verticalScroll(rememberScrollState()),
150+
verticalArrangement = Arrangement.spacedBy(DesignToken.spacing.extraLarge),
151+
) {
152+
MifosDetailsCard(state.details)
153+
154+
MifosButton(
155+
modifier = Modifier.fillMaxWidth().height(DesignToken.sizes.inputHeight),
156+
onClick = {
157+
onAction(ConfirmDetailsAction.NavigateToAuthenticate)
158+
},
159+
shape = DesignToken.shapes.medium,
160+
) {
161+
Text(
162+
text = stringResource(Res.string.feature_apply_loan_title),
163+
style = MifosTypography.titleMedium,
164+
)
165+
}
166+
}
167+
168+
if (state.showOverlay) {
169+
MifosProgressIndicatorOverlay()
170+
}
171+
}
172+
else -> { }
150173
}
151174
}
152175
}

feature/loan-application/src/commonMain/kotlin/org/mifos/mobile/feature/loan/application/confirmDetails/ConfirmDetailsViewModel.kt

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import mifos_mobile.feature.loan_application.generated.resources.feature_apply_l
2727
import mifos_mobile.feature.loan_application.generated.resources.feature_apply_loan_status_success
2828
import mifos_mobile.feature.loan_application.generated.resources.feature_apply_loan_status_success_action
2929
import mifos_mobile.feature.loan_application.generated.resources.feature_apply_loan_status_success_tip
30+
import okio.IOException
3031
import org.jetbrains.compose.resources.StringResource
3132
import org.jetbrains.compose.resources.getString
3233
import org.mifos.mobile.core.common.DataState
@@ -41,6 +42,7 @@ import org.mifos.mobile.core.model.enums.LoanState
4142
import org.mifos.mobile.core.ui.utils.AuthResult
4243
import org.mifos.mobile.core.ui.utils.BaseViewModel
4344
import org.mifos.mobile.core.ui.utils.ResultNavigator
45+
import org.mifos.mobile.core.ui.utils.ScreenUiState
4446
import org.mifos.mobile.core.ui.utils.observe
4547

4648
/**
@@ -122,21 +124,22 @@ internal class ConfirmDetailsViewModel(
122124
*/
123125
@Suppress("UnusedPrivateMember")
124126
private fun showLoading() {
125-
updateState { it.copy(dialogState = ConfirmDetailsDialogState.Loading) }
127+
updateState { it.copy(uiState = ScreenUiState.Loading) }
126128
}
127129

128130
/**
129131
* Sets the dialog state to an overlay loading spinner.
130132
*/
131133
private fun showOverlayLoading() {
132-
updateState { it.copy(dialogState = ConfirmDetailsDialogState.OverlayLoading) }
134+
updateState { it.copy(showOverlay = !state.showOverlay) }
133135
}
134136

135137
/**
136138
* Displays an error dialog with a given message.
137139
*
138140
* @param error The [StringResource] for the error message to display.
139141
*/
142+
@Suppress("UnusedPrivateMember")
140143
private fun showErrorDialog(error: StringResource) {
141144
updateState { it.copy(dialogState = ConfirmDetailsDialogState.Error(error)) }
142145
}
@@ -230,13 +233,23 @@ internal class ConfirmDetailsViewModel(
230233
is DataState.Success -> {
231234
updateState {
232235
it.copy(
236+
showOverlay = false,
233237
loanTemplate = template.data,
234238
)
235239
}
236240
sendAction(ConfirmDetailsAction.Internal.ApplyLoan)
237241
}
238242
is DataState.Error -> {
239-
showErrorDialog(Res.string.feature_apply_loan_error_server)
243+
updateState {
244+
it.copy(
245+
showOverlay = false,
246+
uiState = if (template.exception is IOException) {
247+
ScreenUiState.Network
248+
} else {
249+
ScreenUiState.Error(Res.string.feature_apply_loan_error_server)
250+
},
251+
)
252+
}
240253
}
241254
}
242255
}
@@ -268,6 +281,9 @@ internal class ConfirmDetailsViewModel(
268281
private suspend fun handleLoanApplyStatus(status: DataState<String>) {
269282
when (status) {
270283
is DataState.Error -> {
284+
updateState {
285+
it.copy(showOverlay = false)
286+
}
271287
sendEvent(
272288
ConfirmDetailsEvent.NavigateToStatus(
273289
eventType = EventType.FAILURE.name,
@@ -343,7 +359,10 @@ internal data class ConfirmDetailsState(
343359
val principalAmount: String,
344360
val details: Map<StringResource, String> = emptyMap(),
345361
val loanTemplate: LoanTemplate? = null,
362+
363+
val showOverlay: Boolean = false,
346364
val dialogState: ConfirmDetailsDialogState? = null,
365+
val uiState: ScreenUiState? = ScreenUiState.Success,
347366
)
348367

349368
/**
@@ -435,12 +454,6 @@ sealed interface ConfirmDetailsAction {
435454
* shown on the confirm details screen.
436455
*/
437456
internal sealed interface ConfirmDetailsDialogState {
438-
/** Represents an overlay loading state. */
439-
data object OverlayLoading : ConfirmDetailsDialogState
440-
441-
/** Represents a full-screen loading state. */
442-
data object Loading : ConfirmDetailsDialogState
443-
444457
/**
445458
* Represents a generic error dialog with a message.
446459
* @property message The [StringResource] for the error message.

0 commit comments

Comments
 (0)