Implement gateway ordering.

This commit is contained in:
Alex Hart
2023-10-11 08:55:18 -04:00
committed by Cody Henthorne
parent 5285dd1665
commit c17d6c2334
4 changed files with 131 additions and 54 deletions

View File

@@ -0,0 +1,47 @@
/*
* Copyright 2023 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.thoughtcrime.securesms.components.settings.app.subscription.donate.gateway
import com.google.i18n.phonenumbers.PhoneNumberUtil
import org.signal.core.util.orNull
import org.thoughtcrime.securesms.recipients.Recipient
sealed interface GatewayOrderStrategy {
val orderedGateways: Set<GatewayResponse.Gateway>
private object Default : GatewayOrderStrategy {
override val orderedGateways: Set<GatewayResponse.Gateway> = setOf(
GatewayResponse.Gateway.CREDIT_CARD,
GatewayResponse.Gateway.PAYPAL,
GatewayResponse.Gateway.GOOGLE_PAY,
GatewayResponse.Gateway.SEPA_DEBIT
)
}
private object NorthAmerica : GatewayOrderStrategy {
override val orderedGateways: Set<GatewayResponse.Gateway> = setOf(
GatewayResponse.Gateway.GOOGLE_PAY,
GatewayResponse.Gateway.PAYPAL,
GatewayResponse.Gateway.CREDIT_CARD,
GatewayResponse.Gateway.SEPA_DEBIT
)
}
companion object {
fun getStrategy(): GatewayOrderStrategy {
val self = Recipient.self()
val e164 = self.e164.orNull() ?: return Default
return if (PhoneNumberUtil.getInstance().parse(e164, "").countryCode == 1) {
NorthAmerica
} else {
Default
}
}
}
}

View File

@@ -72,7 +72,26 @@ class GatewaySelectorBottomSheet : DSLSettingsBottomSheetFragment() {
return@configure return@configure
} }
state.gatewayOrderStrategy.orderedGateways.forEachIndexed { index, gateway ->
val isFirst = index == 0
when (gateway) {
GatewayResponse.Gateway.GOOGLE_PAY -> renderGooglePayButton(state, isFirst)
GatewayResponse.Gateway.PAYPAL -> renderPayPalButton(state, isFirst)
GatewayResponse.Gateway.CREDIT_CARD -> renderCreditCardButton(state, isFirst)
GatewayResponse.Gateway.SEPA_DEBIT -> renderSEPADebitButton(state, isFirst)
}
}
space(16.dp)
}
}
private fun DSLConfiguration.renderGooglePayButton(state: GatewaySelectorState, isFirstButton: Boolean) {
if (state.isGooglePayAvailable) { if (state.isGooglePayAvailable) {
if (!isFirstButton) {
space(8.dp)
}
customPref( customPref(
GooglePayButton.Model( GooglePayButton.Model(
isEnabled = true, isEnabled = true,
@@ -84,9 +103,13 @@ class GatewaySelectorBottomSheet : DSLSettingsBottomSheetFragment() {
) )
) )
} }
}
private fun DSLConfiguration.renderPayPalButton(state: GatewaySelectorState, isFirstButton: Boolean) {
if (state.isPayPalAvailable) { if (state.isPayPalAvailable) {
if (!isFirstButton) {
space(8.dp) space(8.dp)
}
customPref( customPref(
PayPalButton.Model( PayPalButton.Model(
@@ -99,13 +122,17 @@ class GatewaySelectorBottomSheet : DSLSettingsBottomSheetFragment() {
) )
) )
} }
}
private fun DSLConfiguration.renderCreditCardButton(state: GatewaySelectorState, isFirstButton: Boolean) {
if (state.isCreditCardAvailable) { if (state.isCreditCardAvailable) {
if (!isFirstButton) {
space(8.dp) space(8.dp)
}
primaryButton( primaryButton(
text = DSLSettingsText.from(R.string.GatewaySelectorBottomSheet__credit_or_debit_card), text = DSLSettingsText.from(R.string.GatewaySelectorBottomSheet__credit_or_debit_card),
icon = DSLSettingsIcon.from(R.drawable.credit_card, R.color.signal_colorOnPrimary), icon = DSLSettingsIcon.from(R.drawable.credit_card, R.color.signal_colorOnCustom),
onClick = { onClick = {
findNavController().popBackStack() findNavController().popBackStack()
val response = GatewayResponse(GatewayResponse.Gateway.CREDIT_CARD, args.request) val response = GatewayResponse(GatewayResponse.Gateway.CREDIT_CARD, args.request)
@@ -113,9 +140,13 @@ class GatewaySelectorBottomSheet : DSLSettingsBottomSheetFragment() {
} }
) )
} }
}
private fun DSLConfiguration.renderSEPADebitButton(state: GatewaySelectorState, isFirstButton: Boolean) {
if (state.isSEPADebitAvailable) { if (state.isSEPADebitAvailable) {
if (!isFirstButton) {
space(8.dp) space(8.dp)
}
tonalButton( tonalButton(
text = DSLSettingsText.from(R.string.GatewaySelectorBottomSheet__bank_transfer), text = DSLSettingsText.from(R.string.GatewaySelectorBottomSheet__bank_transfer),
@@ -127,9 +158,6 @@ class GatewaySelectorBottomSheet : DSLSettingsBottomSheetFragment() {
} }
) )
} }
space(16.dp)
}
} }
companion object { companion object {

View File

@@ -3,6 +3,7 @@ package org.thoughtcrime.securesms.components.settings.app.subscription.donate.g
import org.thoughtcrime.securesms.badges.models.Badge import org.thoughtcrime.securesms.badges.models.Badge
data class GatewaySelectorState( data class GatewaySelectorState(
val gatewayOrderStrategy: GatewayOrderStrategy,
val loading: Boolean = true, val loading: Boolean = true,
val badge: Badge, val badge: Badge,
val isGooglePayAvailable: Boolean = false, val isGooglePayAvailable: Boolean = false,

View File

@@ -21,6 +21,7 @@ class GatewaySelectorViewModel(
private val store = RxStore( private val store = RxStore(
GatewaySelectorState( GatewaySelectorState(
gatewayOrderStrategy = GatewayOrderStrategy.getStrategy(),
badge = args.request.badge, badge = args.request.badge,
isGooglePayAvailable = InAppDonations.isPaymentSourceAvailable(PaymentSourceType.Stripe.GooglePay, args.request.donateToSignalType), isGooglePayAvailable = InAppDonations.isPaymentSourceAvailable(PaymentSourceType.Stripe.GooglePay, args.request.donateToSignalType),
isCreditCardAvailable = InAppDonations.isPaymentSourceAvailable(PaymentSourceType.Stripe.CreditCard, args.request.donateToSignalType), isCreditCardAvailable = InAppDonations.isPaymentSourceAvailable(PaymentSourceType.Stripe.CreditCard, args.request.donateToSignalType),