Skip to content

Commit 9815118

Browse files
committed
feat(flipcash): add support for switching accounts from a login deeplink
Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent 71a3347 commit 9815118

2 files changed

Lines changed: 86 additions & 25 deletions

File tree

apps/flipcash/app/src/main/kotlin/com/flipcash/app/App.kt

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@ import com.flipcash.services.modals.ModalManager
3333
import com.getcode.navigation.core.BottomSheetNavigator
3434
import com.getcode.navigation.core.CombinedNavigator
3535
import com.getcode.navigation.core.LocalCodeNavigator
36+
import com.getcode.navigation.extensions.getActivityScopedViewModel
3637
import com.getcode.navigation.transitions.SheetSlideTransition
3738
import com.getcode.theme.LocalCodeColors
3839
import com.getcode.ui.components.OnLifecycleEvent
3940
import com.getcode.ui.components.bars.BottomBarContainer
4041
import com.getcode.ui.components.bars.TopBarContainer
4142
import com.getcode.ui.components.bars.rememberBarManager
43+
import com.getcode.ui.core.RestrictionType
4244
import com.getcode.ui.decor.ScrimSupport
4345
import com.getcode.ui.theme.CodeScaffold
4446
import dev.bmcreations.tipkit.TipScaffold
@@ -52,6 +54,8 @@ fun App(
5254
) {
5355
val router = LocalRouter.currentOrThrow
5456

57+
val viewModel = getActivityScopedViewModel<HomeViewModel>()
58+
5559
// We are obtaining deep link here to handle a login request while already logged in to
5660
// present the option for the user to switch accounts
5761
var deepLink by remember { mutableStateOf<DeepLink?>(null) }
@@ -126,36 +130,32 @@ fun App(
126130

127131
LaunchedEffect(loginRequest) {
128132
loginRequest?.let { entropy ->
129-
// homeViewModel.handleLoginEntropy(
130-
// entropy,
131-
// onSwitchAccounts = {
132-
// loginRequest = null
133-
// context.getActivity()?.let {
134-
// homeViewModel.logout(it) {
135-
// codeNavigator.replaceAll(
136-
// ScreenRegistry.get(
137-
// NavScreenProvider.Login.Home(
138-
// entropy
139-
// )
140-
// )
141-
// )
142-
// }
143-
// }
144-
// },
145-
// onCancel = {
146-
// loginRequest = null
147-
// }
148-
// )
133+
viewModel.handleLoginEntropy(
134+
entropy,
135+
onSwitchAccount = {
136+
loginRequest = null
137+
codeNavigator.replaceAll(
138+
ScreenRegistry.get(
139+
NavScreenProvider.Login.Home(
140+
entropy
141+
)
142+
)
143+
)
144+
},
145+
onCancel = {
146+
loginRequest = null
147+
}
148+
)
149149
}
150150
}
151151

152152
LaunchedEffect(userState.isTimelockUnlocked) {
153153
if (userState.isTimelockUnlocked) {
154-
// codeNavigator.replaceAll(
155-
// ScreenRegistry.get(
156-
// NavScreenProvider.AppRestricted(RestrictionType.TIMELOCK_UNLOCKED)
157-
// )
158-
// )
154+
codeNavigator.replaceAll(
155+
ScreenRegistry.get(
156+
NavScreenProvider.AppRestricted(RestrictionType.TIMELOCK_UNLOCKED)
157+
)
158+
)
159159
}
160160
}
161161

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.flipcash.app
2+
3+
import androidx.lifecycle.ViewModel
4+
import androidx.lifecycle.viewModelScope
5+
import com.flipcash.android.app.R
6+
import com.flipcash.app.auth.AuthManager
7+
import com.flipcash.services.user.UserManager
8+
import com.getcode.manager.BottomBarManager
9+
import com.getcode.manager.TopBarManager
10+
import com.getcode.util.resources.ResourceHelper
11+
import dagger.hilt.android.lifecycle.HiltViewModel
12+
import kotlinx.coroutines.delay
13+
import kotlinx.coroutines.launch
14+
import javax.inject.Inject
15+
16+
@HiltViewModel
17+
class HomeViewModel @Inject constructor(
18+
private val authManager: AuthManager,
19+
private val userManager: UserManager,
20+
private val resources: ResourceHelper,
21+
) : ViewModel() {
22+
23+
fun handleLoginEntropy(
24+
entropy: String,
25+
onSwitchAccount: () -> Unit,
26+
onCancel: () -> Unit,
27+
) {
28+
if (entropy != userManager.entropy) {
29+
BottomBarManager.showMessage(
30+
BottomBarManager.BottomBarMessage(
31+
title = resources.getString(R.string.subtitle_logoutAndLoginConfirmation),
32+
positiveText = resources.getString(R.string.action_logIn),
33+
tertiaryText = resources.getString(R.string.action_cancel),
34+
isDismissible = false,
35+
onPositive = {
36+
viewModelScope.launch {
37+
delay(150) // wait for dismiss
38+
authManager.logoutAndSwitchAccount(entropy)
39+
.onSuccess { onSwitchAccount() }
40+
.onFailure {
41+
TopBarManager.showMessage(
42+
TopBarManager.TopBarMessage(
43+
title = resources.getString(R.string.error_title_failedToLogOut),
44+
message = resources.getString(R.string.error_description_failedToLogOut),
45+
)
46+
)
47+
}
48+
}
49+
},
50+
onTertiary = onCancel
51+
)
52+
)
53+
}
54+
}
55+
56+
private fun onSwitchAccounts() {
57+
viewModelScope.launch {
58+
authManager.logout()
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)