Skip to content

Commit

Permalink
Simplify CustomerSheetViewState (#9274)
Browse files Browse the repository at this point in the history
  • Loading branch information
samer-stripe authored Sep 13, 2024
1 parent 50accc7 commit e09c28b
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 108 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.stripe.android.customersheet

import android.app.Application
import android.content.res.Resources
import androidx.activity.result.ActivityResultCaller
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ViewModel
Expand Down Expand Up @@ -83,7 +82,6 @@ internal class CustomerSheetViewModel(
private var originalPaymentSelection: PaymentSelection?,
private val paymentConfigurationProvider: Provider<PaymentConfiguration>,
private val customerAdapterProvider: Deferred<CustomerAdapter>,
private val resources: Resources,
private val configuration: CustomerSheet.Configuration,
private val logger: Logger,
private val stripeRepository: StripeRepository,
Expand All @@ -101,7 +99,6 @@ internal class CustomerSheetViewModel(
application: Application,
originalPaymentSelection: PaymentSelection?,
paymentConfigurationProvider: Provider<PaymentConfiguration>,
resources: Resources,
configuration: CustomerSheet.Configuration,
logger: Logger,
stripeRepository: StripeRepository,
Expand All @@ -118,7 +115,6 @@ internal class CustomerSheetViewModel(
originalPaymentSelection = originalPaymentSelection,
paymentConfigurationProvider = paymentConfigurationProvider,
customerAdapterProvider = CustomerSheetHacks.adapter,
resources = resources,
configuration = configuration,
logger = logger,
stripeRepository = stripeRepository,
Expand Down Expand Up @@ -184,14 +180,13 @@ internal class CustomerSheetViewModel(
paymentSelection = paymentSelection,
isLiveMode = isLiveModeProvider(),
canRemovePaymentMethods = customerState.canRemove,
cbcEligibility = customerState.cbcEligibility,
primaryButtonVisible = primaryButtonVisible,
isGooglePayEnabled = paymentMethodMetadata?.isGooglePayReady == true,
isEditing = userCanEditAndIsEditing,
isProcessing = selectionConfirmationState.isConfirming,
errorMessage = selectionConfirmationState.error,
allowsRemovalOfLastSavedPaymentMethod = configuration.allowsRemovalOfLastSavedPaymentMethod,
primaryButtonLabel = resources.getString(R.string.stripe_paymentsheet_confirm),
isCbcEligible = customerState.cbcEligibility is CardBrandChoiceEligibility.Eligible,
canEdit = customerState.canEdit,
mandateText = paymentSelection?.mandateText(
merchantName = configuration.merchantDisplayName,
isSetupFlow = false,
Expand Down Expand Up @@ -593,10 +588,6 @@ internal class CustomerSheetViewModel(
isLiveMode = requireNotNull(customerState.metadata).stripeIntent.isLiveMode,
),
isLiveMode = isLiveModeProvider(),
cbcEligibility = customerState.cbcEligibility,
savedPaymentMethods = emptyList(),
allowsRemovalOfLastSavedPaymentMethod = configuration.allowsRemovalOfLastSavedPaymentMethod,
canRemovePaymentMethods = customerState.canRemove,
)
)
}
Expand Down Expand Up @@ -819,7 +810,6 @@ internal class CustomerSheetViewModel(
customPrimaryButtonUiState = null,
bankAccountResult = null,
errorReporter = errorReporter,
cbcEligibility = customerState.cbcEligibility
),
reset = isFirstPaymentMethod
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.stripe.android.customersheet

import com.stripe.android.core.strings.ResolvableString
import com.stripe.android.core.strings.resolvableString
import com.stripe.android.financialconnections.model.FinancialConnectionsAccount
import com.stripe.android.lpmfoundations.luxe.SupportedPaymentMethod
import com.stripe.android.model.PaymentMethod
import com.stripe.android.model.PaymentMethodCode
import com.stripe.android.payments.bankaccount.navigation.CollectBankAccountResultInternal
import com.stripe.android.payments.core.analytics.ErrorReporter
import com.stripe.android.payments.financialconnections.IsFinancialConnectionsAvailable
import com.stripe.android.paymentsheet.R
import com.stripe.android.paymentsheet.forms.FormFieldValues
import com.stripe.android.paymentsheet.model.PaymentSelection
import com.stripe.android.paymentsheet.paymentdatacollection.FormArguments
Expand All @@ -20,26 +22,11 @@ import com.stripe.android.ui.core.cbc.CardBrandChoiceEligibility
import com.stripe.android.uicore.elements.FormElement

internal sealed class CustomerSheetViewState(
open val savedPaymentMethods: List<PaymentMethod>,
open val isLiveMode: Boolean,
open val isProcessing: Boolean,
open val isEditing: Boolean,
open val canNavigateBack: Boolean,
open val cbcEligibility: CardBrandChoiceEligibility,
open val allowsRemovalOfLastSavedPaymentMethod: Boolean,
open val canRemovePaymentMethods: Boolean,
) {
fun topBarState(onEditIconPressed: () -> Unit): PaymentSheetTopBarState {
return PaymentSheetTopBarStateFactory.create(
hasBackStack = canNavigateBack,
isLiveMode = isLiveMode,
editable = PaymentSheetTopBarState.Editable.Maybe(
isEditing = isEditing,
canEdit = canEdit(allowsRemovalOfLastSavedPaymentMethod, savedPaymentMethods, cbcEligibility),
onEditIconPressed = onEditIconPressed,
),
)
}
abstract fun topBarState(onEditIconPressed: () -> Unit): PaymentSheetTopBarState

fun shouldDisplayDismissConfirmationModal(
isFinancialConnectionsAvailable: IsFinancialConnectionsAvailable,
Expand All @@ -56,44 +43,54 @@ internal sealed class CustomerSheetViewState(
data class Loading(
override val isLiveMode: Boolean,
) : CustomerSheetViewState(
savedPaymentMethods = emptyList(),
isLiveMode = isLiveMode,
isProcessing = false,
isEditing = false,
canNavigateBack = false,
cbcEligibility = CardBrandChoiceEligibility.Ineligible,
allowsRemovalOfLastSavedPaymentMethod = true,
canRemovePaymentMethods = false,
)
) {
override fun topBarState(onEditIconPressed: () -> Unit): PaymentSheetTopBarState {
return PaymentSheetTopBarStateFactory.create(
hasBackStack = canNavigateBack,
isLiveMode = isLiveMode,
editable = PaymentSheetTopBarState.Editable.Never,
)
}
}

data class SelectPaymentMethod(
val title: String?,
override val savedPaymentMethods: List<PaymentMethod>,
val savedPaymentMethods: List<PaymentMethod>,
val paymentSelection: PaymentSelection?,
override val isLiveMode: Boolean,
override val isProcessing: Boolean,
override val isEditing: Boolean,
val isEditing: Boolean,
val isGooglePayEnabled: Boolean,
val primaryButtonVisible: Boolean,
val primaryButtonLabel: String?,
override val allowsRemovalOfLastSavedPaymentMethod: Boolean,
override val canRemovePaymentMethods: Boolean,
val canEdit: Boolean,
val canRemovePaymentMethods: Boolean,
val errorMessage: String? = null,
val unconfirmedPaymentMethod: PaymentMethod? = null,
val mandateText: ResolvableString? = null,
override val cbcEligibility: CardBrandChoiceEligibility,
val isCbcEligible: Boolean,
) : CustomerSheetViewState(
savedPaymentMethods = savedPaymentMethods,
isLiveMode = isLiveMode,
isProcessing = isProcessing,
isEditing = isEditing,
canNavigateBack = false,
cbcEligibility = cbcEligibility,
allowsRemovalOfLastSavedPaymentMethod = allowsRemovalOfLastSavedPaymentMethod,
canRemovePaymentMethods = canRemovePaymentMethods,
) {
val primaryButtonLabel: ResolvableString = R.string.stripe_paymentsheet_confirm.resolvableString

val primaryButtonEnabled: Boolean
get() = !isProcessing

override fun topBarState(onEditIconPressed: () -> Unit): PaymentSheetTopBarState {
return PaymentSheetTopBarStateFactory.create(
hasBackStack = canNavigateBack,
isLiveMode = isLiveMode,
editable = PaymentSheetTopBarState.Editable.Maybe(
isEditing = isEditing,
canEdit = canEdit,
onEditIconPressed = onEditIconPressed,
),
)
}
}

data class AddPaymentMethod(
Expand All @@ -116,36 +113,37 @@ internal sealed class CustomerSheetViewState(
val showMandateAbovePrimaryButton: Boolean = false,
val displayDismissConfirmationModal: Boolean = false,
val bankAccountResult: CollectBankAccountResultInternal?,
override val cbcEligibility: CardBrandChoiceEligibility,
val errorReporter: ErrorReporter,
) : CustomerSheetViewState(
savedPaymentMethods = emptyList(),
isLiveMode = isLiveMode,
isProcessing = isProcessing,
isEditing = false,
canNavigateBack = !isFirstPaymentMethod,
cbcEligibility = cbcEligibility,
allowsRemovalOfLastSavedPaymentMethod = true,
canRemovePaymentMethods = false,
)
) {
override fun topBarState(onEditIconPressed: () -> Unit): PaymentSheetTopBarState {
return PaymentSheetTopBarStateFactory.create(
hasBackStack = canNavigateBack,
isLiveMode = isLiveMode,
editable = PaymentSheetTopBarState.Editable.Never,
)
}
}

data class EditPaymentMethod(
val editPaymentMethodInteractor: ModifiableEditPaymentMethodViewInteractor,
override val isLiveMode: Boolean,
override val cbcEligibility: CardBrandChoiceEligibility,
override val savedPaymentMethods: List<PaymentMethod>,
override val allowsRemovalOfLastSavedPaymentMethod: Boolean,
override val canRemovePaymentMethods: Boolean,
) : CustomerSheetViewState(
savedPaymentMethods = savedPaymentMethods,
isLiveMode = isLiveMode,
isProcessing = false,
isEditing = false,
canNavigateBack = true,
cbcEligibility = cbcEligibility,
allowsRemovalOfLastSavedPaymentMethod = allowsRemovalOfLastSavedPaymentMethod,
canRemovePaymentMethods = canRemovePaymentMethods,
)
) {
override fun topBarState(onEditIconPressed: () -> Unit): PaymentSheetTopBarState {
return PaymentSheetTopBarStateFactory.create(
hasBackStack = canNavigateBack,
isLiveMode = isLiveMode,
editable = PaymentSheetTopBarState.Editable.Never,
)
}
}
}

internal fun canEdit(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import com.stripe.android.paymentsheet.ui.PaymentSheetScaffold
import com.stripe.android.paymentsheet.ui.PaymentSheetTopBar
import com.stripe.android.paymentsheet.ui.SavedPaymentMethodTabLayoutUI
import com.stripe.android.paymentsheet.utils.PaymentSheetContentPadding
import com.stripe.android.ui.core.cbc.CardBrandChoiceEligibility
import com.stripe.android.ui.core.elements.H4Text
import com.stripe.android.ui.core.elements.SimpleDialogElementUI
import com.stripe.android.ui.core.elements.events.CardNumberCompletedEventReporter
Expand Down Expand Up @@ -122,7 +121,7 @@ internal fun SelectPaymentMethod(
currentSelection = viewState.paymentSelection,
nameProvider = paymentMethodNameProvider,
canRemovePaymentMethods = viewState.canRemovePaymentMethods,
isCbcEligible = viewState.cbcEligibility is CardBrandChoiceEligibility.Eligible,
isCbcEligible = viewState.isCbcEligible,
)

SavedPaymentMethodTabLayoutUI(
Expand All @@ -147,20 +146,18 @@ internal fun SelectPaymentMethod(
}

if (viewState.primaryButtonVisible) {
viewState.primaryButtonLabel?.let {
PrimaryButton(
label = it,
isEnabled = viewState.primaryButtonEnabled,
isLoading = viewState.isProcessing,
onButtonClick = {
viewActionHandler(CustomerSheetViewAction.OnPrimaryButtonPressed)
},
modifier = Modifier
.testTag(CUSTOMER_SHEET_CONFIRM_BUTTON_TEST_TAG)
.padding(top = 20.dp)
.padding(horizontal = horizontalPadding),
)
}
PrimaryButton(
label = viewState.primaryButtonLabel.resolve(),
isEnabled = viewState.primaryButtonEnabled,
isLoading = viewState.isProcessing,
onButtonClick = {
viewActionHandler(CustomerSheetViewAction.OnPrimaryButtonPressed)
},
modifier = Modifier
.testTag(CUSTOMER_SHEET_CONFIRM_BUTTON_TEST_TAG)
.padding(top = 20.dp)
.padding(horizontal = horizontalPadding),
)
}

Mandate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,9 @@ internal class CustomerSheetScreenshotTest {
isEditing = false,
isGooglePayEnabled = false,
primaryButtonVisible = false,
primaryButtonLabel = null,
cbcEligibility = CardBrandChoiceEligibility.Ineligible,
allowsRemovalOfLastSavedPaymentMethod = true,
canEdit = true,
canRemovePaymentMethods = true,
isCbcEligible = false,
)

private val addPaymentMethodViewState = CustomerSheetViewState.AddPaymentMethod(
Expand Down Expand Up @@ -97,7 +96,6 @@ internal class CustomerSheetScreenshotTest {
customPrimaryButtonUiState = null,
bankAccountResult = null,
draftPaymentSelection = null,
cbcEligibility = CardBrandChoiceEligibility.Ineligible,
errorReporter = FakeErrorReporter(),
)

Expand Down Expand Up @@ -135,7 +133,6 @@ internal class CustomerSheetScreenshotTest {
paymentSelection = PaymentSelection.Saved(
savedPaymentMethods.first()
),
primaryButtonLabel = "Continue",
errorMessage = "This is an error message.",
),
paymentMethodNameProvider = {
Expand Down Expand Up @@ -172,7 +169,6 @@ internal class CustomerSheetScreenshotTest {
),
isEditing = true,
isGooglePayEnabled = true,
primaryButtonLabel = "Continue",
errorMessage = "This is an error message.",
),
paymentMethodNameProvider = {
Expand All @@ -191,7 +187,6 @@ internal class CustomerSheetScreenshotTest {
title = "Screenshot testing",
paymentSelection = PaymentSelection.GooglePay,
isGooglePayEnabled = true,
primaryButtonLabel = "Continue",
errorMessage = "This is an error message.",
),
paymentMethodNameProvider = { it!!.resolvableString },
Expand All @@ -213,7 +208,6 @@ internal class CustomerSheetScreenshotTest {
PaymentMethodFixtures.US_BANK_ACCOUNT
),
isGooglePayEnabled = false,
primaryButtonLabel = "Continue",
primaryButtonVisible = true,
mandateText = "Some mandate text.".resolvableString
),
Expand Down Expand Up @@ -292,10 +286,6 @@ internal class CustomerSheetScreenshotTest {
isLiveMode = true,
),
isLiveMode = true,
cbcEligibility = CardBrandChoiceEligibility.Eligible(preferredNetworks = emptyList()),
savedPaymentMethods = emptyList(),
allowsRemovalOfLastSavedPaymentMethod = true,
canRemovePaymentMethods = true,
)

paparazzi.snapshot {
Expand Down Expand Up @@ -328,10 +318,6 @@ internal class CustomerSheetScreenshotTest {
isLiveMode = true,
),
isLiveMode = true,
cbcEligibility = CardBrandChoiceEligibility.Eligible(preferredNetworks = emptyList()),
savedPaymentMethods = emptyList(),
allowsRemovalOfLastSavedPaymentMethod = false,
canRemovePaymentMethods = true,
)

paparazzi.snapshot {
Expand Down
Loading

0 comments on commit e09c28b

Please sign in to comment.