Skip to content

Commit

Permalink
Make things that do work happen on the work context in view models. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jaynewstrom-stripe authored Apr 18, 2024
1 parent 03bd099 commit a992229
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion paymentsheet/detekt-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<ID>LargeClass:CustomerSheetViewModelTest.kt$CustomerSheetViewModelTest</ID>
<ID>LargeClass:DefaultFlowControllerTest.kt$DefaultFlowControllerTest</ID>
<ID>LargeClass:DefaultPaymentSheetLoaderTest.kt$DefaultPaymentSheetLoaderTest</ID>
<ID>LargeClass:PaymentOptionsViewModelTest.kt$PaymentOptionsViewModelTest</ID>
<ID>LargeClass:PaymentSheetActivityTest.kt$PaymentSheetActivityTest</ID>
<ID>LargeClass:PaymentSheetViewModel.kt$PaymentSheetViewModel : BaseSheetViewModel</ID>
<ID>LargeClass:PaymentSheetViewModelTest.kt$PaymentSheetViewModelTest</ID>
Expand All @@ -35,7 +36,6 @@
<ID>LongMethod:USBankAccountForm.kt$@Composable internal fun BillingDetailsForm( instantDebits: Boolean, formArgs: FormArguments, isProcessing: Boolean, isPaymentFlow: Boolean, nameController: TextFieldController, emailController: TextFieldController, phoneController: PhoneNumberController, addressController: AddressController, lastTextFieldIdentifier: IdentifierSpec?, sameAsShippingElement: SameAsShippingElement?, )</ID>
<ID>LongMethod:USBankAccountForm.kt$@Composable internal fun USBankAccountForm( formArgs: FormArguments, usBankAccountFormArgs: USBankAccountFormArguments, modifier: Modifier = Modifier, )</ID>
<ID>LongMethod:USBankAccountForm.kt$@Composable private fun AccountDetailsForm( showCheckbox: Boolean, isProcessing: Boolean, bankName: String?, last4: String?, saveForFutureUseElement: SaveForFutureUseElement, onRemoveAccount: () -> Unit, )</ID>
<ID>LongMethod:USBankAccountFormViewModelTest.kt$USBankAccountFormViewModelTest$@Test fun `Restores screen state when re-opening screen`()</ID>
<ID>MagicNumber:AutocompleteScreen.kt$0.07f</ID>
<ID>MagicNumber:BaseSheetActivity.kt$BaseSheetActivity$30</ID>
<ID>MagicNumber:BottomSheet.kt$BottomSheetState$10</ID>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import com.stripe.android.customersheet.CustomerSheetResultCallback
import com.stripe.android.customersheet.ExperimentalCustomerSheetApi
import com.stripe.android.link.account.LinkStore
import com.stripe.android.paymentsheet.MainActivity
import java.lang.IllegalStateException
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ internal class CustomerSheetViewModel(
configuration.appearance.parseAppearance()

if (viewState.value is CustomerSheetViewState.Loading) {
viewModelScope.launch {
viewModelScope.launch(workContext) {
loadCustomerSheetState()
}
}
Expand Down Expand Up @@ -465,7 +465,7 @@ internal class CustomerSheetViewModel(
}

private fun onItemRemoved(paymentMethod: PaymentMethod) {
viewModelScope.launch {
viewModelScope.launch(workContext) {
val result = removePaymentMethod(paymentMethod)

result.fold(
Expand Down Expand Up @@ -615,7 +615,7 @@ internal class CustomerSheetViewModel(
}

private fun removePaymentMethodFromState(paymentMethod: PaymentMethod) {
viewModelScope.launch {
viewModelScope.launch(workContext) {
delay(PaymentMethodRemovalDelayMillis)

val newSavedPaymentMethods = viewState.value.savedPaymentMethods - paymentMethod
Expand Down Expand Up @@ -725,7 +725,7 @@ internal class CustomerSheetViewModel(
private fun createAndAttach(
paymentMethodCreateParams: PaymentMethodCreateParams,
) {
viewModelScope.launch {
viewModelScope.launch(workContext) {
createPaymentMethod(paymentMethodCreateParams)
.onSuccess { paymentMethod ->
if (paymentMethod.isUnverifiedUSBankAccount()) {
Expand Down Expand Up @@ -908,7 +908,7 @@ internal class CustomerSheetViewModel(
}

private fun attachPaymentMethodToCustomer(paymentMethod: PaymentMethod) {
viewModelScope.launch {
viewModelScope.launch(workContext) {
if (awaitCustomerAdapter().canCreateSetupIntents) {
attachWithSetupIntent(paymentMethod = paymentMethod)
} else {
Expand Down Expand Up @@ -1098,7 +1098,7 @@ internal class CustomerSheetViewModel(
}

private fun selectSavedPaymentMethod(savedPaymentSelection: PaymentSelection.Saved?) {
viewModelScope.launch {
viewModelScope.launch(workContext) {
awaitCustomerAdapter().setSelectedPaymentOption(
savedPaymentSelection?.toPaymentOption()
).onSuccess {
Expand All @@ -1118,7 +1118,7 @@ internal class CustomerSheetViewModel(
}

private fun selectGooglePay() {
viewModelScope.launch {
viewModelScope.launch(workContext) {
awaitCustomerAdapter().setSelectedPaymentOption(CustomerAdapter.PaymentOption.GooglePay)
.onSuccess {
confirmPaymentSelection(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ internal class PaymentSheetViewModel @Inject internal constructor(
isDeferred = isDeferred,
)

viewModelScope.launch {
viewModelScope.launch(workContext) {
loadPaymentSheetState()
}
}
Expand Down Expand Up @@ -590,7 +590,7 @@ internal class PaymentSheetViewModel @Inject internal constructor(
}

private fun confirmPaymentSelection(paymentSelection: PaymentSelection?) {
viewModelScope.launch {
viewModelScope.launch(workContext) {
val stripeIntent = awaitStripeIntent()

val nextStep = intentConfirmationInterceptor.intercept(
Expand Down Expand Up @@ -622,7 +622,7 @@ internal class PaymentSheetViewModel @Inject internal constructor(
}

override fun onPaymentResult(paymentResult: PaymentResult) {
viewModelScope.launch {
viewModelScope.launch(workContext) {
val stripeIntent = awaitStripeIntent()
processPayment(stripeIntent, paymentResult)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ internal abstract class BaseSheetViewModel(
fun removePaymentMethod(paymentMethod: PaymentMethod) {
val paymentMethodId = paymentMethod.id ?: return

viewModelScope.launch {
viewModelScope.launch(workContext) {
removeDeletedPaymentMethodFromState(paymentMethodId)
removePaymentMethodInternal(paymentMethodId)
}
Expand Down Expand Up @@ -597,7 +597,7 @@ internal abstract class BaseSheetViewModel(
val result = removePaymentMethodInternal(paymentMethodId)

if (result.isSuccess) {
viewModelScope.launch {
viewModelScope.launch(workContext) {
onUserBack()
delay(PaymentMethodRemovalDelayMillis)
removeDeletedPaymentMethodFromState(paymentMethodId = paymentMethodId)
Expand Down Expand Up @@ -677,7 +677,7 @@ internal abstract class BaseSheetViewModel(
abstract val shouldCompleteLinkFlowInline: Boolean

private fun payWithLinkInline(userInput: UserInput?) {
viewModelScope.launch {
viewModelScope.launch(workContext) {
linkHandler.payWithLinkInline(
userInput = userInput,
paymentSelection = selection.value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import kotlinx.coroutines.test.StandardTestDispatcher
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.Rule
import org.junit.runner.RunWith
Expand All @@ -70,7 +71,8 @@ internal class PaymentOptionsViewModelTest {
private val customerRepository = mock<CustomerRepository>()

@Before
fun before() {
@After
fun resetMainDispatcher() {
Dispatchers.resetMain()
}

Expand Down Expand Up @@ -180,6 +182,7 @@ internal class PaymentOptionsViewModelTest {
)

viewModel.removePaymentMethod(cards[1])
testDispatcher.scheduler.advanceUntilIdle()

assertThat(viewModel.paymentMethods.value)
.containsExactly(cards[0], cards[2])
Expand All @@ -197,6 +200,7 @@ internal class PaymentOptionsViewModelTest {
assertThat(viewModel.selection.value).isEqualTo(selection)

viewModel.removePaymentMethod(selection.paymentMethod)
testDispatcher.scheduler.advanceUntilIdle()

assertThat(viewModel.selection.value).isNull()
}
Expand All @@ -211,6 +215,7 @@ internal class PaymentOptionsViewModelTest {
)

viewModel.removePaymentMethod(paymentMethod)
testDispatcher.scheduler.advanceUntilIdle()

assertThat(viewModel.paymentMethods.value).isEmpty()
assertThat(viewModel.primaryButtonUiState.value).isNull()
Expand Down Expand Up @@ -462,6 +467,7 @@ internal class PaymentOptionsViewModelTest {
viewModel.paymentOptionResult.test {
// Simulate user removing the selected payment method
viewModel.removePaymentMethod(selection.paymentMethod)
testDispatcher.scheduler.advanceUntilIdle()

viewModel.onUserCancel()

Expand Down

0 comments on commit a992229

Please sign in to comment.