Skip to content

Commit

Permalink
Wallet update states
Browse files Browse the repository at this point in the history
  • Loading branch information
toluo-stripe committed Feb 7, 2025
1 parent 37bf7f6 commit 6479718
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ internal fun WalletBody(
showBottomSheetContent: (BottomSheetContent) -> Unit,
hideBottomSheetContent: () -> Unit
) {
val context = LocalContext.current
if (state.paymentDetailsList.isEmpty()) {
Loader()
return
Expand Down Expand Up @@ -382,7 +381,7 @@ private fun ExpandedPaymentDetails(
paymentDetails = item,
enabled = isEnabled,
isSelected = uiState.selectedItem?.id == item.id,
isUpdating = false,
isUpdating = uiState.cardBeingUpdated == item.id,
onClick = {
onItemSelected(item)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ internal data class WalletUiState(
val isProcessing: Boolean,
val primaryButtonLabel: ResolvableString,
val hasCompleted: Boolean,
val cardBeingUpdated: String? = null,
val errorMessage: ResolvableString? = null,
val expiryDateInput: FormFieldEntry = FormFieldEntry(null),
val cvcInput: FormFieldEntry = FormFieldEntry(null),
Expand All @@ -34,16 +35,21 @@ internal data class WalletUiState(
val isMissingCvcInput = cvcInput.isComplete.not()

val disableButton = (isExpired && isMissingExpiryDateInput) ||
(requiresCvcRecollection && isMissingCvcInput)
(requiresCvcRecollection && isMissingCvcInput) || (cardBeingUpdated != null)

return if (hasCompleted) {
PrimaryButtonState.Completed
} else if (isProcessing) {
PrimaryButtonState.Processing
} else if (disableButton) {
PrimaryButtonState.Disabled
} else {
PrimaryButtonState.Enabled
return when {
hasCompleted -> {
PrimaryButtonState.Completed
}
isProcessing -> {
PrimaryButtonState.Processing
}
disableButton -> {
PrimaryButtonState.Disabled
}
else -> {
PrimaryButtonState.Enabled
}
}
}

Expand All @@ -65,7 +71,8 @@ internal data class WalletUiState(
return copy(
paymentDetailsList = response.paymentDetails,
selectedItem = selectedItem,
isProcessing = false
isProcessing = false,
cardBeingUpdated = null
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,30 +96,34 @@ internal class WalletViewModel @Inject constructor(
}
}

private fun loadPaymentDetails(selectedItemId: String? = null) {
private fun loadPaymentDetails() {
_uiState.update {
it.setProcessing()
}

viewModelScope.launch {
linkAccountManager.listPaymentDetails(
paymentMethodTypes = stripeIntent.supportedPaymentMethodTypes(linkAccount)
).fold(
onSuccess = { response ->
_uiState.update {
it.updateWithResponse(response, selectedItemId = selectedItemId)
}

if (response.paymentDetails.isEmpty()) {
navigateAndClearStack(LinkScreen.PaymentMethod)
}
},
// If we can't load the payment details there's nothing to see here
onFailure = ::onFatal
)
loadPaymentDetailsHelper(selectedItemId = null)
}
}

private suspend fun loadPaymentDetailsHelper(selectedItemId: String?) {
linkAccountManager.listPaymentDetails(
paymentMethodTypes = stripeIntent.supportedPaymentMethodTypes(linkAccount)
).fold(
onSuccess = { response ->
_uiState.update {
it.updateWithResponse(response, selectedItemId = selectedItemId)
}

if (response.paymentDetails.isEmpty()) {
navigateAndClearStack(LinkScreen.PaymentMethod)
}
},
// If we can't load the payment details there's nothing to see here
onFailure = ::onFatal
)
}

private fun onFatal(fatalError: Throwable) {
logger.error("WalletViewModel Fatal error: ", fatalError)
dismissWithResult(
Expand Down Expand Up @@ -244,7 +248,7 @@ internal class WalletViewModel @Inject constructor(
linkAccountManager.deletePaymentDetails(item.id)
.fold(
onSuccess = {
loadPaymentDetails(selectedItemId = uiState.value.selectedItem?.id)
loadPaymentDetailsHelper(selectedItemId = uiState.value.selectedItem?.id)
},
onFailure = { error ->
updateErrorMessageAndStopProcessing(
Expand All @@ -258,7 +262,10 @@ internal class WalletViewModel @Inject constructor(

fun onSetDefaultClicked(item: ConsumerPaymentDetails.PaymentDetails) {
_uiState.update {
it.setProcessing()
it.copy(
cardBeingUpdated = item.id,
selectedItem = null
)
}
viewModelScope.launch {
val updateParams = ConsumerPaymentDetailsUpdateParams(
Expand All @@ -269,7 +276,9 @@ internal class WalletViewModel @Inject constructor(
linkAccountManager.updatePaymentDetails(updateParams)
.fold(
onSuccess = {
loadPaymentDetails()
loadPaymentDetailsHelper(
selectedItemId = item.id
)
},
onFailure = { error ->
updateErrorMessageAndStopProcessing(
Expand Down Expand Up @@ -307,7 +316,8 @@ internal class WalletViewModel @Inject constructor(
_uiState.update {
it.copy(
alertMessage = error.stripeErrorMessage(),
isProcessing = false
isProcessing = false,
cardBeingUpdated = null
)
}
}
Expand Down

0 comments on commit 6479718

Please sign in to comment.