Skip to content

Commit b6aaa1f

Browse files
Handle backs.
1 parent 125cace commit b6aaa1f

File tree

4 files changed

+42
-43
lines changed

4 files changed

+42
-43
lines changed

paymentsheet/src/main/java/com/stripe/android/link/LinkActivity.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.content.Context
55
import android.content.Intent
66
import android.os.Bundle
77
import androidx.activity.ComponentActivity
8+
import androidx.activity.addCallback
89
import androidx.activity.compose.setContent
910
import androidx.activity.result.ActivityResultLauncher
1011
import androidx.annotation.VisibleForTesting
@@ -46,6 +47,7 @@ internal class LinkActivity : ComponentActivity() {
4647
vm.launchWebFlow = ::launchWebFlow
4748
vm.dismissWithResult = ::dismissWithResult
4849
lifecycle.addObserver(vm)
50+
observeBackPress()
4951

5052
setContent {
5153
LinkScreenContent(
@@ -55,6 +57,10 @@ internal class LinkActivity : ComponentActivity() {
5557
}
5658
}
5759

60+
private fun observeBackPress() {
61+
onBackPressedDispatcher.addCallback { viewModel?.handleBackPressed() }
62+
}
63+
5864
private fun dismissWithResult(result: LinkActivityResult) {
5965
val bundle = bundleOf(
6066
LinkActivityContract.EXTRA_RESULT to result

paymentsheet/src/main/java/com/stripe/android/link/LinkActivityViewModel.kt

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import androidx.lifecycle.viewModelScope
1414
import androidx.lifecycle.viewmodel.initializer
1515
import androidx.lifecycle.viewmodel.viewModelFactory
1616
import androidx.navigation.NavBackStackEntry
17+
import androidx.navigation.compose.NavHost
1718
import com.stripe.android.link.LinkActivity.Companion.getArgs
1819
import com.stripe.android.link.account.LinkAccountHolder
1920
import com.stripe.android.link.account.LinkAccountManager
@@ -84,8 +85,7 @@ internal class LinkActivityViewModel @Inject constructor(
8485

8586
fun onVerificationSucceeded() {
8687
viewModelScope.launch {
87-
val initialDestination = getInitialDestination()
88-
_linkScreenState.value = ScreenState.FullScreen(initialDestination)
88+
_linkScreenState.value = buildFullScreenState()
8989
}
9090
}
9191

@@ -138,16 +138,16 @@ internal class LinkActivityViewModel @Inject constructor(
138138
}
139139
}
140140

141-
private fun handleBackPressed() {
142-
// navController?.let { navController ->
143-
// if (!navController.popBackStack()) {
144-
// dismissWithResult?.invoke(
145-
// LinkActivityResult.Canceled(
146-
// linkAccountUpdate = linkAccountManager.linkAccountUpdate
147-
// )
148-
// )
149-
// }
150-
// }
141+
/**
142+
* [NavHost] handles back presses except for when backstack is empty, where it delegates
143+
* to the container activity. [onBackPressed] will be triggered on these empty backstack cases.
144+
*/
145+
fun handleBackPressed() {
146+
dismissWithResult?.invoke(
147+
LinkActivityResult.Canceled(
148+
linkAccountUpdate = linkAccountManager.linkAccountUpdate
149+
)
150+
)
151151
}
152152

153153
fun registerActivityForConfirmation(
@@ -158,8 +158,7 @@ internal class LinkActivityViewModel @Inject constructor(
158158
}
159159

160160
fun navigate(screen: LinkScreen, clearStack: Boolean, launchSingleTop: Boolean = false) {
161-
Log.d("LinkActivityViewModel", "navigate: $screen, clearStack: $clearStack, launchSingleTop: $launchSingleTop")
162-
val popupTo = if (clearStack) { // TODO if clear stack
161+
val popupTo = if (clearStack) {
163162
PopUpToBehavior.Current(
164163
inclusive = true
165164
)
@@ -172,13 +171,7 @@ internal class LinkActivityViewModel @Inject constructor(
172171
}
173172

174173
fun goBack() {
175-
// if (navController?.popBackStack() == false) {
176-
// dismissWithResult?.invoke(
177-
// LinkActivityResult.Canceled(
178-
// linkAccountUpdate = linkAccountManager.linkAccountUpdate
179-
// )
180-
// )
181-
// }
174+
navigationManager.tryNavigateBack()
182175
}
183176

184177
fun changeEmail() {
@@ -218,38 +211,34 @@ internal class LinkActivityViewModel @Inject constructor(
218211
AccountStatus.Verified,
219212
AccountStatus.SignedOut,
220213
AccountStatus.Error -> {
221-
_linkScreenState.value = ScreenState.FullScreen(
222-
getInitialDestination()
223-
)
214+
_linkScreenState.value = buildFullScreenState()
224215
}
225216
AccountStatus.NeedsVerification,
226217
AccountStatus.VerificationStarted -> {
227218
if (linkAccount != null && startWithVerificationDialog) {
228219
_linkScreenState.value = ScreenState.VerificationDialog(linkAccount)
229220
} else {
230-
_linkScreenState.value = ScreenState.FullScreen(
231-
getInitialDestination()
232-
)
221+
_linkScreenState.value = buildFullScreenState()
233222
}
234223
}
235224
}
236225
}
237226

238-
239-
private suspend fun getInitialDestination(): LinkScreen {
227+
private suspend fun buildFullScreenState(): ScreenState.FullScreen {
240228
val accountStatus = linkAccountManager.accountStatus.first()
241-
val screen = when (accountStatus) {
242-
AccountStatus.Verified -> {
243-
LinkScreen.Wallet
244-
}
245-
AccountStatus.NeedsVerification, AccountStatus.VerificationStarted -> {
246-
LinkScreen.Verification
247-
}
248-
AccountStatus.SignedOut, AccountStatus.Error -> {
249-
LinkScreen.SignUp
229+
return ScreenState.FullScreen(
230+
initialDestination = when (accountStatus) {
231+
AccountStatus.Verified -> {
232+
LinkScreen.Wallet
233+
}
234+
AccountStatus.NeedsVerification, AccountStatus.VerificationStarted -> {
235+
LinkScreen.Verification
236+
}
237+
AccountStatus.SignedOut, AccountStatus.Error -> {
238+
LinkScreen.SignUp
239+
}
250240
}
251-
}
252-
return screen
241+
)
253242
}
254243

255244
private suspend fun handleAccountError() {

paymentsheet/src/main/java/com/stripe/android/link/LinkScreenContent.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ internal fun LinkScreenContent(
2929
onVerificationSucceeded = viewModel::onVerificationSucceeded,
3030
onDismissClicked = viewModel::onDismissVerificationClicked,
3131
onBackPressed = onBackPressed,
32-
// TODO remove too.
32+
// TODO replace by NavigationManager.
3333
navigate = viewModel::navigate,
3434
dismissWithResult = { result ->
3535
viewModel.dismissWithResult?.invoke(result)
@@ -39,6 +39,7 @@ internal fun LinkScreenContent(
3939
},
4040
handleViewAction = viewModel::handleViewAction,
4141
moveToWeb = viewModel::moveToWeb,
42+
// TODO replace by NavigationManager.
4243
goBack = viewModel::goBack,
4344
changeEmail = viewModel::changeEmail,
4445
onNavBackStackEntryChanged = viewModel::onNavEntryChanged,
@@ -69,8 +70,8 @@ internal fun LinkScreenContentBody(
6970
FullScreenContent(
7071
modifier = Modifier
7172
.testTag(FULL_SCREEN_CONTENT_TAG),
72-
onBackPressed = onBackPressed,
7373
initialDestination = screenState.initialDestination,
74+
onBackPressed = onBackPressed,
7475
appBarState = appBarState,
7576
navigate = navigate,
7677
dismissWithResult = dismissWithResult,

paymentsheet/src/main/java/com/stripe/android/link/ui/LinkContent.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import androidx.compose.runtime.rememberCoroutineScope
1818
import androidx.compose.ui.Alignment
1919
import androidx.compose.ui.Modifier
2020
import androidx.compose.ui.unit.dp
21+
import androidx.lifecycle.viewmodel.compose.viewModel
2122
import androidx.navigation.NavHostController
2223
import androidx.navigation.compose.NavHost
2324
import androidx.navigation.compose.composable
@@ -83,7 +84,9 @@ internal fun LinkContent(
8384
.fillMaxWidth()
8485
) {
8586
BackHandler {
86-
handleViewAction(LinkAction.BackPressed)
87+
if (navController.popBackStack().not()) {
88+
handleViewAction(LinkAction.BackPressed)
89+
}
8790
}
8891

8992
LinkAppBar(

0 commit comments

Comments
 (0)