Skip to content

Commit

Permalink
Add helper function for converting string resource identifiers to `Re…
Browse files Browse the repository at this point in the history
…solvableString`. (#8824)

* Add helper function for converting string resource identifiers to 'ResolvableString'.

* Replace all usages with `resolvableString` helper.
  • Loading branch information
samer-stripe authored Jul 19, 2024
1 parent e01a70a commit 3624057
Show file tree
Hide file tree
Showing 38 changed files with 140 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ private fun CardBrandChoiceDropdown(
val choices = brands.map { it.toChoice() }

SingleChoiceDropdown(
title = resolvableString(id = R.string.stripe_card_brand_choice_selection_header),
title = R.string.stripe_card_brand_choice_selection_header.resolvableString,
expanded = expanded,
currentChoice = currentBrand.takeIf { it != Unknown }?.toChoice(),
choices = choices,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ internal class DefaultCardNumberController(
if (isEligibleForCardBrandChoice && number.isNotEmpty()) {
val noSelection = TextFieldIcon.Dropdown.Item(
id = CardBrand.Unknown.code,
label = resolvableString(PaymentsCoreR.string.stripe_card_brand_choice_no_selection),
label = PaymentsCoreR.string.stripe_card_brand_choice_no_selection.resolvableString,
icon = CardBrand.Unknown.icon
)

Expand Down Expand Up @@ -213,7 +213,7 @@ internal class DefaultCardNumberController(
}

TextFieldIcon.Dropdown(
title = resolvableString(PaymentsCoreR.string.stripe_card_brand_choice_selection_header),
title = PaymentsCoreR.string.stripe_card_brand_choice_selection_header.resolvableString,
currentItem = selected ?: noSelection,
items = items,
hide = brands.size < 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,10 @@ internal class CardNumberControllerTest {
assertThat(awaitItem() as TextFieldIcon.Dropdown)
.isEqualTo(
TextFieldIcon.Dropdown(
title = resolvableString(R.string.stripe_card_brand_choice_selection_header),
title = R.string.stripe_card_brand_choice_selection_header.resolvableString,
currentItem = TextFieldIcon.Dropdown.Item(
id = CardBrand.Unknown.code,
label = resolvableString(R.string.stripe_card_brand_choice_no_selection),
label = R.string.stripe_card_brand_choice_no_selection.resolvableString,
icon = CardBrand.Unknown.icon
),
items = listOf(
Expand Down Expand Up @@ -247,7 +247,7 @@ internal class CardNumberControllerTest {
assertThat(awaitItem() as TextFieldIcon.Dropdown)
.isEqualTo(
TextFieldIcon.Dropdown(
title = resolvableString(R.string.stripe_card_brand_choice_selection_header),
title = R.string.stripe_card_brand_choice_selection_header.resolvableString,
currentItem = TextFieldIcon.Dropdown.Item(
id = CardBrand.CartesBancaires.code,
label = "Cartes Bancaires".resolvableString,
Expand Down Expand Up @@ -294,7 +294,7 @@ internal class CardNumberControllerTest {
assertThat(awaitItem() as TextFieldIcon.Dropdown)
.isEqualTo(
TextFieldIcon.Dropdown(
title = resolvableString(R.string.stripe_card_brand_choice_selection_header),
title = R.string.stripe_card_brand_choice_selection_header.resolvableString,
currentItem = TextFieldIcon.Dropdown.Item(
id = CardBrand.CartesBancaires.code,
label = "Cartes Bancaires".resolvableString,
Expand Down Expand Up @@ -343,7 +343,7 @@ internal class CardNumberControllerTest {
assertThat(awaitItem() as TextFieldIcon.Dropdown)
.isEqualTo(
TextFieldIcon.Dropdown(
title = resolvableString(R.string.stripe_card_brand_choice_selection_header),
title = R.string.stripe_card_brand_choice_selection_header.resolvableString,
currentItem = TextFieldIcon.Dropdown.Item(
id = CardBrand.CartesBancaires.code,
label = "Cartes Bancaires".resolvableString,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ internal fun Throwable?.stripeErrorMessage(context: Context): String {
@Suppress("ReturnCount")
internal fun Throwable.stripeErrorMessage(): ResolvableString {
(this as? APIConnectionException)?.let {
return resolvableString(R.string.stripe_network_error_message)
return R.string.stripe_network_error_message.resolvableString
}
(this as? LocalStripeException)?.displayMessage?.let {
return it.resolvableString
}
(this as? StripeException)?.stripeError?.message?.let {
return it.resolvableString
}
return resolvableString(R.string.stripe_something_went_wrong)
return R.string.stripe_something_went_wrong.resolvableString
}
Original file line number Diff line number Diff line change
Expand Up @@ -435,13 +435,9 @@ internal class CustomerSheetViewModel(
paymentMethod.code == PaymentMethod.Type.USBankAccount.code &&
it.bankAccountResult !is CollectBankAccountResultInternal.Completed
) {
resolvableString(
id = UiCoreR.string.stripe_continue_button_label
)
UiCoreR.string.stripe_continue_button_label.resolvableString
} else {
resolvableString(
id = R.string.stripe_paymentsheet_save
)
R.string.stripe_paymentsheet_save.resolvableString
},
mandateText = it.draftPaymentSelection?.mandateText(
merchantName = configuration.merchantDisplayName,
Expand Down Expand Up @@ -843,9 +839,7 @@ internal class CustomerSheetViewModel(
isLiveMode = isLiveModeProvider(),
isProcessing = false,
isFirstPaymentMethod = isFirstPaymentMethod,
primaryButtonLabel = resolvableString(
id = R.string.stripe_paymentsheet_save
),
primaryButtonLabel = R.string.stripe_paymentsheet_save.resolvableString,
primaryButtonEnabled = false,
customPrimaryButtonUiState = null,
bankAccountResult = null,
Expand Down Expand Up @@ -887,9 +881,9 @@ internal class CustomerSheetViewModel(
it.copy(
bankAccountResult = bankAccountResult,
primaryButtonLabel = if (bankAccountResult is CollectBankAccountResultInternal.Completed) {
resolvableString(id = R.string.stripe_paymentsheet_save)
R.string.stripe_paymentsheet_save.resolvableString
} else {
resolvableString(id = UiCoreR.string.stripe_continue_button_label)
UiCoreR.string.stripe_continue_button_label.resolvableString
},
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ internal data class SupportedPaymentMethod(
subtitle: ResolvableString? = null,
) : this(
code = paymentMethodDefinition.type.code,
displayName = resolvableString(id = displayNameResource),
displayName = displayNameResource.resolvableString,
iconResource = iconResource,
lightThemeIconUrl = sharedDataSpec?.selectorIcon?.lightThemePng,
darkThemeIconUrl = sharedDataSpec?.selectorIcon?.darkThemePng,
Expand All @@ -62,7 +62,7 @@ internal data class SupportedPaymentMethod(
subtitle: ResolvableString? = null,
) : this(
code = code,
displayName = resolvableString(id = displayNameResource),
displayName = displayNameResource.resolvableString,
iconResource = iconResource,
lightThemeIconUrl = lightThemeIconUrl,
darkThemeIconUrl = darkThemeIconUrl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ private object AffirmUiDefinitionFactory : UiDefinitionFactory.RequiresSharedDat
sharedDataSpec = sharedDataSpec,
displayNameResource = UiCoreR.string.stripe_paymentsheet_payment_method_affirm,
iconResource = UiCoreR.drawable.stripe_ic_paymentsheet_pm_affirm,
subtitle = resolvableString(id = StripeR.string.stripe_affirm_buy_now_pay_later_plaintext)
subtitle = StripeR.string.stripe_affirm_buy_now_pay_later_plaintext.resolvableString
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private object CardUiDefinitionFactory : UiDefinitionFactory.Simple {

override fun createFormHeaderInformation(): FormHeaderInformation {
return createSupportedPaymentMethod().asFormHeaderInformation().copy(
displayName = resolvableString(R.string.stripe_paymentsheet_add_new_card),
displayName = R.string.stripe_paymentsheet_add_new_card.resolvableString,
shouldShowIcon = false,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private object KlarnaUiDefinitionFactory : UiDefinitionFactory.RequiresSharedDat
sharedDataSpec = sharedDataSpec,
displayNameResource = R.string.stripe_paymentsheet_payment_method_klarna,
iconResource = R.drawable.stripe_ic_paymentsheet_pm_klarna,
subtitle = resolvableString(id = R.string.stripe_klarna_pay_later)
subtitle = R.string.stripe_klarna_pay_later.resolvableString
)

override fun createFormElements(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private object UsBankAccountUiDefinitionFactory : UiDefinitionFactory.Simple {

override fun createFormHeaderInformation(): FormHeaderInformation {
return createSupportedPaymentMethod().asFormHeaderInformation().copy(
displayName = resolvableString(R.string.stripe_paymentsheet_add_us_bank_account),
displayName = R.string.stripe_paymentsheet_add_us_bank_account.resolvableString,
shouldShowIcon = false,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,12 +470,12 @@ internal class PaymentSheetViewModel @Inject internal constructor(
)
}.onFailure {
resetViewState(
userErrorMessage = resolvableString(R.string.stripe_something_went_wrong)
userErrorMessage = R.string.stripe_something_went_wrong.resolvableString
)
}
} ?: run {
resetViewState(
userErrorMessage = resolvableString(R.string.stripe_something_went_wrong)
userErrorMessage = R.string.stripe_something_went_wrong.resolvableString
)
}
} else {
Expand Down Expand Up @@ -774,15 +774,12 @@ internal class PaymentSheetViewModel @Inject internal constructor(
paymentSelection = PaymentSelection.GooglePay,
error = PaymentSheetConfirmationError.GooglePay(result.errorCode),
)
onError(
resolvableString(
when (result.errorCode) {
GooglePayPaymentMethodLauncher.NETWORK_ERROR ->
StripeR.string.stripe_failure_connection_error
else -> StripeR.string.stripe_internal_error
}
)
)
val errorMessage = when (result.errorCode) {
GooglePayPaymentMethodLauncher.NETWORK_ERROR ->
StripeR.string.stripe_failure_connection_error
else -> StripeR.string.stripe_internal_error
}
onError(errorMessage.resolvableString)
}
is GooglePayPaymentMethodLauncher.Result.Canceled -> {
resetViewState()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ internal sealed interface PaymentSheetScreen {
if (isCompleteFlow && isWalletEnabled) {
null
} else {
resolvableString(R.string.stripe_paymentsheet_select_payment_method)
R.string.stripe_paymentsheet_select_payment_method.resolvableString
}
)
}
Expand Down Expand Up @@ -205,9 +205,9 @@ internal sealed interface PaymentSheetScreen {
null
} else {
if (state.supportedPaymentMethods.singleOrNull()?.code == PaymentMethod.Type.Card.code) {
resolvableString(PaymentsCoreR.string.stripe_title_add_a_card)
PaymentsCoreR.string.stripe_title_add_a_card.resolvableString
} else {
resolvableString(R.string.stripe_paymentsheet_choose_payment_method)
R.string.stripe_paymentsheet_choose_payment_method.resolvableString
}
}
}
Expand Down Expand Up @@ -250,12 +250,12 @@ internal sealed interface PaymentSheetScreen {
if (isWalletEnabled) {
null
} else if (isCompleteFlow) {
resolvableString(R.string.stripe_paymentsheet_add_payment_method_title)
R.string.stripe_paymentsheet_add_payment_method_title.resolvableString
} else {
if (state.supportedPaymentMethods.singleOrNull()?.code == PaymentMethod.Type.Card.code) {
resolvableString(PaymentsCoreR.string.stripe_title_add_a_card)
PaymentsCoreR.string.stripe_title_add_a_card.resolvableString
} else {
resolvableString(R.string.stripe_paymentsheet_choose_payment_method)
R.string.stripe_paymentsheet_choose_payment_method.resolvableString
}
}
}
Expand Down Expand Up @@ -295,7 +295,7 @@ internal sealed interface PaymentSheetScreen {
}

override fun title(isCompleteFlow: Boolean, isWalletEnabled: Boolean): StateFlow<ResolvableString?> {
return stateFlowOf(resolvableString(PaymentsCoreR.string.stripe_title_update_card))
return stateFlowOf(PaymentsCoreR.string.stripe_title_update_card.resolvableString)
}

override fun showsWalletsHeader(isCompleteFlow: Boolean): StateFlow<Boolean> {
Expand Down Expand Up @@ -333,9 +333,9 @@ internal sealed interface PaymentSheetScreen {
if (isWalletEnabled) {
null
} else if (isCompleteFlow) {
resolvableString(R.string.stripe_paymentsheet_select_payment_method)
R.string.stripe_paymentsheet_select_payment_method.resolvableString
} else {
resolvableString(R.string.stripe_paymentsheet_choose_payment_method)
R.string.stripe_paymentsheet_choose_payment_method.resolvableString
}
)
}
Expand Down Expand Up @@ -404,13 +404,13 @@ internal sealed interface PaymentSheetScreen {

override fun title(isCompleteFlow: Boolean, isWalletEnabled: Boolean): StateFlow<ResolvableString?> {
return interactor.state.mapAsStateFlow { state ->
resolvableString(
if (state.isEditing) {
R.string.stripe_paymentsheet_manage_payment_methods
} else {
R.string.stripe_paymentsheet_select_payment_method
}
)
val title = if (state.isEditing) {
R.string.stripe_paymentsheet_manage_payment_methods
} else {
R.string.stripe_paymentsheet_select_payment_method
}

title.resolvableString
}
}

Expand Down Expand Up @@ -444,7 +444,7 @@ internal sealed interface PaymentSheetScreen {
}

override fun title(isCompleteFlow: Boolean, isWalletEnabled: Boolean): StateFlow<ResolvableString?> {
return stateFlowOf(resolvableString(R.string.stripe_paymentsheet_remove_pm_title))
return stateFlowOf(R.string.stripe_paymentsheet_remove_pm_title.resolvableString)
}

override fun showsWalletsHeader(isCompleteFlow: Boolean): StateFlow<Boolean> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ internal class USBankAccountFormViewModel @Inject internal constructor(
}

is CollectBankAccountResultInternal.Failed -> {
reset(resolvableString(R.string.stripe_paymentsheet_ach_something_went_wrong))
reset(R.string.stripe_paymentsheet_ach_something_went_wrong.resolvableString)
}

is CollectBankAccountResultInternal.Cancelled -> {
Expand All @@ -295,7 +295,7 @@ internal class USBankAccountFormViewModel @Inject internal constructor(
handleCompletedInstantDebitsResult(result)
}
is CollectBankAccountForInstantDebitsResult.Failed -> {
reset(resolvableString(R.string.stripe_paymentsheet_ach_something_went_wrong))
reset(R.string.stripe_paymentsheet_ach_something_went_wrong.resolvableString)
}
is CollectBankAccountForInstantDebitsResult.Cancelled -> {
reset()
Expand All @@ -312,7 +312,7 @@ internal class USBankAccountFormViewModel @Inject internal constructor(
if (usBankAccountData != null) {
handleResultForACH(usBankAccountData, intentId)
} else {
reset(resolvableString(R.string.stripe_paymentsheet_ach_something_went_wrong))
reset(R.string.stripe_paymentsheet_ach_something_went_wrong.resolvableString)
}
}

Expand Down Expand Up @@ -364,7 +364,7 @@ internal class USBankAccountFormViewModel @Inject internal constructor(
}

null -> {
reset(resolvableString(R.string.stripe_paymentsheet_ach_something_went_wrong))
reset(R.string.stripe_paymentsheet_ach_something_went_wrong.resolvableString)
}
}
}
Expand Down Expand Up @@ -416,9 +416,7 @@ internal class USBankAccountFormViewModel @Inject internal constructor(
_currentScreenState.update {
USBankAccountFormScreenState.BillingDetailsCollection(
error = error,
primaryButtonText = resolvableString(
StripeUiCoreR.string.stripe_continue_button_label
),
primaryButtonText = StripeUiCoreR.string.stripe_continue_button_label.resolvableString,
isProcessing = false,
)
}
Expand All @@ -443,9 +441,7 @@ internal class USBankAccountFormViewModel @Inject internal constructor(
args.savedPaymentMethod.screenState
} else {
USBankAccountFormScreenState.BillingDetailsCollection(
primaryButtonText = resolvableString(
StripeUiCoreR.string.stripe_continue_button_label
),
primaryButtonText = StripeUiCoreR.string.stripe_continue_button_label.resolvableString,
isProcessing = false,
)
}
Expand Down Expand Up @@ -614,15 +610,11 @@ internal class USBankAccountFormViewModel @Inject internal constructor(
if (args.isPaymentFlow) {
args.formArgs.amount!!.buildPayButtonLabel()
} else {
resolvableString(
StripeUiCoreR.string.stripe_setup_button_label
)
StripeUiCoreR.string.stripe_setup_button_label.resolvableString
}
}

else -> resolvableString(
StripeUiCoreR.string.stripe_continue_button_label
)
else -> StripeUiCoreR.string.stripe_continue_button_label.resolvableString
}
}

Expand Down
Loading

0 comments on commit 3624057

Please sign in to comment.