Skip to content

Commit cd18f3f

Browse files
committed
refactor: navigation issue fix with TFA phone solution update
1 parent 3200984 commit cd18f3f

26 files changed

+144
-134
lines changed

app/src/main/java/com/sap/cdc/bitsnbytes/navigation/ProfileNavHost.kt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ fun ProfileNavHost(appStateManager: AppStateManager) {
9393
// Get the single activity-scoped delegate (provided by MainActivity)
9494
val authDelegate = ViewModelScopeProvider.activityScopedAuthenticationDelegate(context)
9595

96-
NavHost(
97-
profileNavController, startDestination =
96+
NavHost(
97+
profileNavController, startDestination =
9898
when (authDelegate.hasValidSession()) {
9999
true -> {
100100
if (authDelegate.isBiometricActive()) {
@@ -250,12 +250,12 @@ fun ProfileNavHost(appStateManager: AppStateManager) {
250250
composable("${ProfileScreenRoute.AuthMethods.route}/{twoFactorContext}") { backStackEntry ->
251251
val encodedTwoFactorJson = backStackEntry.arguments?.getString("twoFactorContext")
252252
val decodedTwoFactorJson = Uri.decode(encodedTwoFactorJson!!)
253-
val resolvable = Json.decodeFromString<TwoFactorContext>(decodedTwoFactorJson)
253+
val twoFactorContext = Json.decodeFromString<TwoFactorContext>(decodedTwoFactorJson)
254254
val viewModel: AuthMethodsViewModel = ViewModelScopeProvider.screenScopedViewModel(
255255
factory = CustomViewModelFactory(context, authDelegate)
256256
)
257-
viewModel.initializeWithContext(decodedTwoFactorJson)
258-
AuthMethodsView(viewModel, resolvable)
257+
viewModel.initializeWithContext(twoFactorContext)
258+
AuthMethodsView(viewModel)
259259
}
260260

261261
composable("${ProfileScreenRoute.PhoneSelection.route}/{TwoFactorContext}") { backStackEntry ->
@@ -265,7 +265,8 @@ fun ProfileNavHost(appStateManager: AppStateManager) {
265265
val viewModel: PhoneSelectionViewModel = ViewModelScopeProvider.screenScopedViewModel(
266266
factory = CustomViewModelFactory(context, authDelegate)
267267
)
268-
PhoneSelectionView(viewModel, twoFactorContext)
268+
viewModel.initializeWithContext(twoFactorContext)
269+
PhoneSelectionView(viewModel)
269270
}
270271

271272
composable("${ProfileScreenRoute.PhoneVerification.route}/{twoFactorContext}") { backStackEntry ->
@@ -275,7 +276,8 @@ fun ProfileNavHost(appStateManager: AppStateManager) {
275276
val viewModel: PhoneVerificationViewModel = ViewModelScopeProvider.screenScopedViewModel(
276277
factory = CustomViewModelFactory(context, authDelegate)
277278
)
278-
PhoneVerificationView(viewModel, twoFactorContext)
279+
viewModel.initializeWithContext(twoFactorContext)
280+
PhoneVerificationView(viewModel)
279281
}
280282

281283
composable("${ProfileScreenRoute.TOTPVerification.route}/{twoFactorContext}") { backStackEntry ->
@@ -285,7 +287,8 @@ fun ProfileNavHost(appStateManager: AppStateManager) {
285287
val viewModel: TOTPVerificationViewModel = ViewModelScopeProvider.screenScopedViewModel(
286288
factory = CustomViewModelFactory(context, authDelegate)
287289
)
288-
TOTPVerificationView(viewModel, twoFactorContext)
290+
viewModel.initializeWithContext(twoFactorContext)
291+
TOTPVerificationView(viewModel)
289292
}
290293

291294
composable(ProfileScreenRoute.BiometricLocked.route) {

app/src/main/java/com/sap/cdc/bitsnbytes/ui/state/AuthMethodsState.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ data class AuthMethodsState(
2020
*/
2121
@Immutable
2222
sealed class AuthMethodsNavigationEvent {
23-
data object NavigateToPhoneSelection : AuthMethodsNavigationEvent()
23+
data class NavigateToPhoneSelection(val context: String) : AuthMethodsNavigationEvent()
2424
data class NavigateToTOTPVerification(val context: String) : AuthMethodsNavigationEvent()
2525
data object NavigateToLogin : AuthMethodsNavigationEvent()
2626
}

app/src/main/java/com/sap/cdc/bitsnbytes/ui/state/PhoneSelectionState.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.sap.cdc.bitsnbytes.ui.state
22

33
import androidx.compose.runtime.Immutable
4+
import com.sap.cdc.bitsnbytes.ui.view.model.Country
5+
import com.sap.cdc.bitsnbytes.ui.view.model.CountryData
46

57
/**
68
* Created by Tal Mirmelshtein on 09/11/2024
@@ -17,7 +19,8 @@ import androidx.compose.runtime.Immutable
1719
data class PhoneSelectionState(
1820
val isLoading: Boolean = false,
1921
val error: String? = null,
20-
val inputField: String = ""
22+
val inputField: String = "",
23+
val selectedCountry: Country = CountryData.getDefaultCountry()
2124
)
2225

2326
/**

app/src/main/java/com/sap/cdc/bitsnbytes/ui/view/screens/AuthMethods.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ import androidx.compose.ui.text.font.FontWeight
2020
import androidx.compose.ui.tooling.preview.Preview
2121
import androidx.compose.ui.unit.dp
2222
import androidx.compose.ui.unit.sp
23-
import com.sap.cdc.android.sdk.feature.TwoFactorContext
2423
import com.sap.cdc.bitsnbytes.R
2524
import com.sap.cdc.bitsnbytes.apptheme.AppTheme
26-
import com.sap.cdc.bitsnbytes.extensions.toJson
2725
import com.sap.cdc.bitsnbytes.navigation.NavigationCoordinator
2826
import com.sap.cdc.bitsnbytes.navigation.ProfileScreenRoute
2927
import com.sap.cdc.bitsnbytes.ui.state.AuthMethodsNavigationEvent
@@ -32,8 +30,7 @@ import com.sap.cdc.bitsnbytes.ui.view.composables.SmallActionTextButton
3230

3331
@Composable
3432
fun AuthMethodsView(
35-
viewModel: IAuthMethodsViewModel,
36-
twoFactorContext: TwoFactorContext,
33+
viewModel: IAuthMethodsViewModel
3734
) {
3835
val state by viewModel.state.collectAsState()
3936

@@ -42,7 +39,9 @@ fun AuthMethodsView(
4239
viewModel.navigationEvents.collect { event ->
4340
when (event) {
4441
is AuthMethodsNavigationEvent.NavigateToPhoneSelection -> {
45-
NavigationCoordinator.INSTANCE.navigate(ProfileScreenRoute.PhoneSelection.route)
42+
NavigationCoordinator.INSTANCE.navigate(
43+
"${ProfileScreenRoute.PhoneSelection.route}/${event.context}"
44+
)
4645
}
4746
is AuthMethodsNavigationEvent.NavigateToTOTPVerification -> {
4847
NavigationCoordinator.INSTANCE.navigate(
@@ -124,8 +123,7 @@ fun AuthMethodsView(
124123
fun AuthMethodsViewPreview() {
125124
AppTheme {
126125
AuthMethodsView(
127-
viewModel = AuthMethodsViewModelPreview(),
128-
twoFactorContext = TwoFactorContext()
126+
viewModel = AuthMethodsViewModelPreview()
129127
)
130128
}
131129
}

app/src/main/java/com/sap/cdc/bitsnbytes/ui/view/screens/AuthMethodsViewModel.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package com.sap.cdc.bitsnbytes.ui.view.screens
22

33
import android.content.Context
44
import androidx.lifecycle.viewModelScope
5+
import com.sap.cdc.android.sdk.feature.TwoFactorContext
6+
import com.sap.cdc.bitsnbytes.extensions.toJson
57
import com.sap.cdc.bitsnbytes.feature.auth.AuthenticationFlowDelegate
68
import com.sap.cdc.bitsnbytes.ui.state.AuthMethodsNavigationEvent
79
import com.sap.cdc.bitsnbytes.ui.state.AuthMethodsState
@@ -34,13 +36,13 @@ class AuthMethodsViewModel(
3436
override val state: StateFlow<AuthMethodsState> = _state.asStateFlow()
3537

3638
private val _navigationEvents = MutableSharedFlow<AuthMethodsNavigationEvent>(
37-
replay = 1,
38-
extraBufferCapacity = 0
39+
replay = 0,
40+
extraBufferCapacity = 1
3941
)
4042
override val navigationEvents: SharedFlow<AuthMethodsNavigationEvent> = _navigationEvents.asSharedFlow()
4143

42-
fun initializeWithContext(twoFactorContextJson: String) {
43-
_state.update { it.copy(twoFactorContext = twoFactorContextJson) }
44+
fun initializeWithContext(twoFactorContext: TwoFactorContext) {
45+
_state.update { it.copy(twoFactorContext = twoFactorContext.toJson()) }
4446
}
4547

4648
override fun onSendCodeToEmail() {
@@ -49,7 +51,7 @@ class AuthMethodsViewModel(
4951

5052
override fun onSendCodeToPhone() {
5153
viewModelScope.launch {
52-
_navigationEvents.emit(AuthMethodsNavigationEvent.NavigateToPhoneSelection)
54+
_navigationEvents.emit(AuthMethodsNavigationEvent.NavigateToPhoneSelection(_state.value.twoFactorContext))
5355
}
5456
}
5557

app/src/main/java/com/sap/cdc/bitsnbytes/ui/view/screens/BiometricLockedViewModel.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ class BiometricLockedViewModel(
3535
override val state: StateFlow<BiometricLockedState> = _state.asStateFlow()
3636

3737
private val _navigationEvents = MutableSharedFlow<BiometricLockedNavigationEvent>(
38-
replay = 1,
39-
extraBufferCapacity = 0
38+
replay = 0,
39+
extraBufferCapacity = 1
4040
)
4141
override val navigationEvents: SharedFlow<BiometricLockedNavigationEvent> = _navigationEvents.asSharedFlow()
4242

app/src/main/java/com/sap/cdc/bitsnbytes/ui/view/screens/CustomIDSignInViewModel.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ class CustomIDSignInViewModel(
5252
override val state: StateFlow<CustomIDSignInState> = _state.asStateFlow()
5353

5454
private val _navigationEvents = MutableSharedFlow<CustomIDSignInNavigationEvent>(
55-
replay = 1,
56-
extraBufferCapacity = 0
55+
replay = 0,
56+
extraBufferCapacity = 1
5757
)
5858
override val navigationEvents: SharedFlow<CustomIDSignInNavigationEvent> = _navigationEvents.asSharedFlow()
5959

app/src/main/java/com/sap/cdc/bitsnbytes/ui/view/screens/EmailRegistrationViewModel.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ class EmailRegistrationViewModel(
5959
override val state: StateFlow<EmailRegistrationState> = _state.asStateFlow()
6060

6161
private val _navigationEvents = MutableSharedFlow<EmailRegistrationNavigationEvent>(
62-
replay = 1,
63-
extraBufferCapacity = 0
62+
replay = 0,
63+
extraBufferCapacity = 1
6464
)
6565
override val navigationEvents: SharedFlow<EmailRegistrationNavigationEvent> = _navigationEvents.asSharedFlow()
6666

app/src/main/java/com/sap/cdc/bitsnbytes/ui/view/screens/EmailSignInViewModel.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ class EmailSignInViewModel(
5454
override val state: StateFlow<EmailSignInState> = _state.asStateFlow()
5555

5656
private val _navigationEvents = MutableSharedFlow<EmailSignInNavigationEvent>(
57-
replay = 1,
58-
extraBufferCapacity = 0
57+
replay = 0,
58+
extraBufferCapacity = 1
5959
)
6060
override val navigationEvents: SharedFlow<EmailSignInNavigationEvent> = _navigationEvents.asSharedFlow()
6161

app/src/main/java/com/sap/cdc/bitsnbytes/ui/view/screens/LinkAccountViewModel.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ class LinkAccountViewModel(context: Context, val flowDelegate: AuthenticationFlo
4343
override val state: StateFlow<LinkAccountState> = _state.asStateFlow()
4444

4545
private val _navigationEvents = MutableSharedFlow<LinkAccountNavigationEvent>(
46-
replay = 1,
47-
extraBufferCapacity = 0
46+
replay = 0,
47+
extraBufferCapacity = 1
4848
)
4949
override val navigationEvents: SharedFlow<LinkAccountNavigationEvent> = _navigationEvents.asSharedFlow()
5050

0 commit comments

Comments
 (0)