-
Notifications
You must be signed in to change notification settings - Fork 662
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
LinkActivity, ViewModel and Navigation #9334
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a0d389f
LinkActivity, ViewModel and Navigator
toluo-stripe 2dc6173
Add Sign Up screen skeleton
toluo-stripe 6b6d332
rename VM
toluo-stripe 9deeb8e
remove mvi-viewmodel
toluo-stripe 22740d0
Delete StateAndEffectsVM.kt
toluo-stripe 08d681c
Delete unused code
toluo-stripe 07e8818
Fix potential memory leak
toluo-stripe fe8b66e
pop back stack on back pressed
toluo-stripe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.stripe.android.link | ||
|
||
internal sealed interface LinkAction { | ||
data object BackPressed : LinkAction | ||
} |
84 changes: 84 additions & 0 deletions
84
link/src/main/java/com/stripe/android/link/LinkActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.stripe.android.link | ||
|
||
import android.app.Activity | ||
import android.content.Intent | ||
import android.os.Bundle | ||
import androidx.activity.compose.setContent | ||
import androidx.activity.viewModels | ||
import androidx.annotation.VisibleForTesting | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.core.os.bundleOf | ||
import androidx.lifecycle.ViewModelProvider | ||
import androidx.navigation.NavHostController | ||
import androidx.navigation.compose.NavHost | ||
import androidx.navigation.compose.composable | ||
import androidx.navigation.compose.rememberNavController | ||
import com.stripe.android.link.ui.cardedit.CardEditScreen | ||
import com.stripe.android.link.ui.paymentmenthod.PaymentMethodScreen | ||
import com.stripe.android.link.ui.signup.SignUpScreen | ||
import com.stripe.android.link.ui.verification.VerificationScreen | ||
import com.stripe.android.link.ui.wallet.WalletScreen | ||
|
||
internal class LinkActivity : AppCompatActivity() { | ||
@VisibleForTesting | ||
internal var viewModelFactory: ViewModelProvider.Factory = LinkActivityViewModel.Factory() | ||
private val viewModel: LinkActivityViewModel by viewModels { viewModelFactory } | ||
|
||
@VisibleForTesting | ||
internal lateinit var navController: NavHostController | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
setContent { | ||
navController = rememberNavController() | ||
|
||
LaunchedEffect(Unit) { | ||
viewModel.navController = navController | ||
viewModel.dismissWithResult = ::dismissWithResult | ||
} | ||
|
||
NavHost( | ||
navController = navController, | ||
startDestination = LinkScreen.SignUp.route | ||
) { | ||
composable(LinkScreen.SignUp.route) { | ||
SignUpScreen() | ||
} | ||
|
||
composable(LinkScreen.Verification.route) { | ||
VerificationScreen() | ||
} | ||
|
||
composable(LinkScreen.Wallet.route) { | ||
WalletScreen() | ||
} | ||
|
||
composable(LinkScreen.CardEdit.route) { | ||
CardEditScreen() | ||
} | ||
|
||
composable(LinkScreen.PaymentMethod.route) { | ||
PaymentMethodScreen() | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun dismissWithResult(result: LinkActivityResult) { | ||
val bundle = bundleOf( | ||
LinkActivityContract.EXTRA_RESULT to LinkActivityContract.Result(result) | ||
) | ||
this@LinkActivity.setResult( | ||
Activity.RESULT_OK, | ||
Intent().putExtras(bundle) | ||
) | ||
this@LinkActivity.finish() | ||
} | ||
|
||
override fun onDestroy() { | ||
super.onDestroy() | ||
viewModel.unregisterActivity() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
link/src/main/java/com/stripe/android/link/LinkActivityViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.stripe.android.link | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import androidx.navigation.NavHostController | ||
|
||
internal class LinkActivityViewModel : ViewModel() { | ||
var navController: NavHostController? = null | ||
var dismissWithResult: ((LinkActivityResult) -> Unit)? = null | ||
|
||
fun handleViewAction(action: LinkAction) { | ||
when (action) { | ||
LinkAction.BackPressed -> handleBackPressed() | ||
} | ||
} | ||
|
||
private fun handleBackPressed() { | ||
navController?.let { navController -> | ||
if (!navController.popBackStack()) { | ||
dismissWithResult?.invoke(LinkActivityResult.Canceled()) | ||
} | ||
} | ||
} | ||
|
||
fun unregisterActivity() { | ||
navController = null | ||
dismissWithResult = null | ||
} | ||
|
||
internal class Factory : ViewModelProvider.Factory { | ||
override fun <T : ViewModel> create(modelClass: Class<T>): T { | ||
return LinkActivityViewModel() as T | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
package com.stripe.android.link | ||
|
||
internal sealed interface LinkScreen { | ||
data object Verification : LinkScreen | ||
data object Wallet : LinkScreen | ||
data object PaymentMethod : LinkScreen | ||
data object CardEdit : LinkScreen | ||
data object SignUp : LinkScreen | ||
internal sealed class LinkScreen(val route: String) { | ||
data object Verification : LinkScreen("verification") | ||
data object Wallet : LinkScreen("wallet") | ||
data object PaymentMethod : LinkScreen("paymentMethod") | ||
data object CardEdit : LinkScreen("cardEdit") | ||
data object SignUp : LinkScreen("signUp") | ||
} |
66 changes: 66 additions & 0 deletions
66
link/src/test/java/com/stripe/android/link/LinkActivityViewModelTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.stripe.android.link | ||
|
||
import androidx.navigation.NavHostController | ||
import com.google.common.truth.Truth.assertThat | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.test.UnconfinedTestDispatcher | ||
import kotlinx.coroutines.test.resetMain | ||
import kotlinx.coroutines.test.runTest | ||
import kotlinx.coroutines.test.setMain | ||
import org.junit.After | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.times | ||
import org.mockito.kotlin.verify | ||
import org.mockito.kotlin.whenever | ||
import org.robolectric.RobolectricTestRunner | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
internal class LinkActivityViewModelTest { | ||
private val dispatcher = UnconfinedTestDispatcher() | ||
private val vm = LinkActivityViewModel() | ||
private val navController: NavHostController = mock() | ||
private val dismissWithResult: (LinkActivityResult) -> Unit = mock() | ||
|
||
@Before | ||
fun setUp() { | ||
Dispatchers.setMain(dispatcher) | ||
vm.dismissWithResult = dismissWithResult | ||
vm.navController = navController | ||
} | ||
|
||
@After | ||
fun tearDown() { | ||
Dispatchers.resetMain() | ||
} | ||
|
||
@Test | ||
fun `test that cancel result is called on back pressed with empty stack`() = runTest(dispatcher) { | ||
whenever(navController.popBackStack()).thenReturn(false) | ||
|
||
vm.handleViewAction(LinkAction.BackPressed) | ||
|
||
verify(navController).popBackStack() | ||
verify(dismissWithResult).invoke(LinkActivityResult.Canceled()) | ||
} | ||
|
||
@Test | ||
fun `test that cancel result is called on back pressed with non-empty stack`() = runTest(dispatcher) { | ||
whenever(navController.popBackStack()).thenReturn(true) | ||
|
||
vm.handleViewAction(LinkAction.BackPressed) | ||
|
||
verify(navController).popBackStack() | ||
verify(dismissWithResult, times(0)).invoke(LinkActivityResult.Canceled()) | ||
} | ||
|
||
@Test | ||
fun `test that activity unregister removes dismissWithResult and nav controller`() = runTest(dispatcher) { | ||
vm.unregisterActivity() | ||
|
||
assertThat(vm.dismissWithResult).isEqualTo(null) | ||
assertThat(vm.navController).isEqualTo(null) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What will navigation between PaymentSheet and LinkActivity look like? Since this is its own activity with its own navigation, I'm assuming this will be totally separate but I'm curious what the transition will look like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yh, it'll be separate from PaymentSheet. PaymentSheet will launch this with an activityResultCaller.