Skip to content

Commit bd6df97

Browse files
Nagarjuna0033therajanmaurya
authored andcommitted
refactor: used enum for status
1 parent 9136406 commit bd6df97

File tree

5 files changed

+51
-11
lines changed

5 files changed

+51
-11
lines changed

feature/auth/src/commonMain/kotlin/org/mifos/mobile/feature/auth/otpAuthentication/OtpAuthenticationNavigation.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import androidx.navigation.NavGraphBuilder
1616
import androidx.navigation.NavOptions
1717
import kotlinx.serialization.Serializable
1818
import org.mifos.mobile.core.ui.composableWithStayTransitions
19+
import org.mifos.mobile.feature.auth.status.EventType
1920

2021
@Serializable
2122
data object OtpAuthenticationRoute
@@ -25,7 +26,7 @@ fun NavController.navigateToOtpAuthScreen(navOptions: NavOptions? = null) {
2526
}
2627

2728
fun NavGraphBuilder.otpAuthenticationDestination(
28-
navigateToStatusScreen: (String, String) -> Unit,
29+
navigateToStatusScreen: (EventType, String) -> Unit,
2930
) {
3031
composableWithStayTransitions<OtpAuthenticationRoute> {
3132
OtpAuthenticationScreen(

feature/auth/src/commonMain/kotlin/org/mifos/mobile/feature/auth/otpAuthentication/OtpAuthenticationScreen.kt

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import androidx.compose.ui.Modifier
3434
import androidx.compose.ui.text.input.KeyboardType
3535
import androidx.compose.ui.unit.dp
3636
import androidx.lifecycle.compose.collectAsStateWithLifecycle
37+
import kotlinx.serialization.SerialName
38+
import kotlinx.serialization.Serializable
3739
import mifos_mobile.feature.auth.generated.resources.Res
3840
import mifos_mobile.feature.auth.generated.resources.feature_common_cancel
3941
import mifos_mobile.feature.auth.generated.resources.feature_common_next
@@ -44,6 +46,7 @@ import mifos_mobile.feature.auth.generated.resources.feature_otp_subtitle
4446
import mifos_mobile.feature.auth.generated.resources.feature_otp_tip
4547
import mifos_mobile.feature.auth.generated.resources.feature_otp_title
4648
import org.jetbrains.compose.resources.stringResource
49+
import org.jetbrains.compose.ui.tooling.preview.Preview
4750
import org.koin.compose.viewmodel.koinViewModel
4851
import org.mifos.mobile.core.designsystem.component.BasicDialogState
4952
import org.mifos.mobile.core.designsystem.component.LoadingDialogState
@@ -56,13 +59,14 @@ import org.mifos.mobile.core.designsystem.component.MifosScaffold
5659
import org.mifos.mobile.core.designsystem.component.MifosTextFieldConfig
5760
import org.mifos.mobile.core.designsystem.icon.MifosIcons
5861
import org.mifos.mobile.core.designsystem.theme.DesignToken
62+
import org.mifos.mobile.core.designsystem.theme.MifosMobileTheme
5963
import org.mifos.mobile.core.designsystem.theme.MifosTypography
6064
import org.mifos.mobile.core.ui.component.MifosPoweredCard
6165
import org.mifos.mobile.core.ui.utils.EventsEffect
6266

6367
@Composable
6468
internal fun OtpAuthenticationScreen(
65-
navigateToStatusScreen: (String, String) -> Unit,
69+
navigateToStatusScreen: (EventType, String) -> Unit,
6670
modifier: Modifier = Modifier,
6771
viewModel: OtpAuthenticationViewModel = koinViewModel(),
6872
) {
@@ -277,3 +281,27 @@ internal fun OtpInputForm(
277281
}
278282
}
279283
}
284+
285+
@Serializable
286+
enum class EventType {
287+
@SerialName("success")
288+
SUCCESS,
289+
290+
@SerialName("failure")
291+
FAILURE,
292+
}
293+
294+
@Preview
295+
@Composable
296+
internal fun Otp_Auth_Preview() {
297+
MifosMobileTheme {
298+
Column(
299+
modifier = Modifier.padding(DesignToken.padding.large),
300+
) {
301+
OptAuthScreenContent(
302+
state = OtpAuthState(dialogState = null),
303+
onAction = { },
304+
)
305+
}
306+
}
307+
}

feature/auth/src/commonMain/kotlin/org/mifos/mobile/feature/auth/otpAuthentication/OtpAuthenticationViewModel.kt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@ import kotlinx.coroutines.Job
1414
import kotlinx.coroutines.delay
1515
import kotlinx.coroutines.flow.update
1616
import kotlinx.coroutines.launch
17+
import kotlinx.serialization.ExperimentalSerializationApi
18+
import kotlinx.serialization.InternalSerializationApi
19+
import kotlinx.serialization.serializer
1720
import mifos_mobile.feature.auth.generated.resources.Res
1821
import mifos_mobile.feature.auth.generated.resources.feature_otp_invalid_error
1922
import mifos_mobile.feature.auth.generated.resources.feature_otp_required_error
2023
import org.jetbrains.compose.resources.StringResource
2124
import org.mifos.mobile.core.ui.utils.BaseViewModel
25+
import org.mifos.mobile.feature.auth.login.LoginRoute
26+
import org.mifos.mobile.feature.auth.status.EventType
2227

2328
internal class OtpAuthenticationViewModel : BaseViewModel<OtpAuthState, OtpAuthEvent, OtpAuthAction>(
2429
initialState = OtpAuthState(dialogState = null),
@@ -60,8 +65,8 @@ internal class OtpAuthenticationViewModel : BaseViewModel<OtpAuthState, OtpAuthE
6065

6166
private fun validateOtp(otp: String): StringResource? {
6267
return when {
63-
!otp.isNotBlank() -> Res.string.feature_otp_required_error
64-
otp.length > 5 -> Res.string.feature_otp_invalid_error
68+
otp.isBlank() -> Res.string.feature_otp_required_error
69+
otp.length != 6 -> Res.string.feature_otp_invalid_error
6570
else -> null
6671
}
6772
}
@@ -79,6 +84,7 @@ internal class OtpAuthenticationViewModel : BaseViewModel<OtpAuthState, OtpAuthE
7984
}
8085
}
8186

87+
@OptIn(InternalSerializationApi::class, ExperimentalSerializationApi::class)
8288
private fun registerUser() {
8389
viewModelScope.launch {
8490
mutableStateFlow.update {
@@ -90,8 +96,8 @@ internal class OtpAuthenticationViewModel : BaseViewModel<OtpAuthState, OtpAuthE
9096
dismissDialog()
9197
sendEvent(
9298
OtpAuthEvent.NavigateToStatus(
93-
"success",
94-
"login",
99+
EventType.SUCCESS,
100+
LoginRoute::class.serializer().descriptor.serialName,
95101
),
96102
)
97103
}
@@ -147,7 +153,7 @@ internal sealed interface OtpAuthAction {
147153

148154
internal sealed interface OtpAuthEvent {
149155
data class NavigateToStatus(
150-
val eventType: String,
156+
val eventType: EventType,
151157
val eventDestination: String,
152158
) : OtpAuthEvent
153159
}

feature/auth/src/commonMain/kotlin/org/mifos/mobile/feature/auth/status/Components.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ import org.mifos.mobile.core.designsystem.component.MifosScaffold
2424
import org.mifos.mobile.core.designsystem.theme.DesignToken
2525
import org.mifos.mobile.core.ui.component.MifosPoweredCard
2626
import org.mifos.mobile.core.ui.component.MifosStatusComponent
27+
import org.mifos.mobile.feature.auth.otpAuthentication.EventType
2728

2829
@Composable
2930
internal fun StatusScreen(
30-
eventType: String,
31+
eventType: EventType,
3132
eventDestination: String,
3233
buttonText: String,
3334
title: String,
@@ -51,7 +52,11 @@ internal fun StatusScreen(
5152
verticalArrangement = Arrangement.Center,
5253
) {
5354
MifosStatusComponent(
54-
icon = if (eventType == "success") Res.drawable.ic_icon_success else Res.drawable.ic_icon_error,
55+
icon = if (eventType == EventType.SUCCESS) {
56+
Res.drawable.ic_icon_success
57+
} else {
58+
Res.drawable.ic_icon_error
59+
},
5560
title = title,
5661
subTitle = subtitle,
5762
buttonText = buttonText,

feature/auth/src/commonMain/kotlin/org/mifos/mobile/feature/auth/status/StatusNavigation.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ import org.mifos.mobile.feature.auth.otpAuthentication.OtpAuthenticationRoute
2020

2121
@Serializable
2222
data class StatusNavigationRoute(
23-
val eventType: String,
23+
val eventType: EventType,
2424
val eventDestination: String,
2525
val buttonText: String = "Continue",
2626
val title: String = "Success",
2727
val subtitle: String = "You have completed the action.",
2828
)
2929

3030
fun NavController.navigateToStatusScreen(
31-
eventType: String,
31+
eventType: EventType,
3232
eventDestination: String,
3333
buttonText: String = "Continue",
3434
title: String = "Success",

0 commit comments

Comments
 (0)