From 0a5abbb4400bbe76a1efc65fbeea312bf2982bf5 Mon Sep 17 00:00:00 2001 From: Michael Shafrir <45020849+mshafrir-stripe@users.noreply.github.com> Date: Mon, 24 Feb 2020 15:34:08 -0500 Subject: [PATCH] Highlight "Google Pay" option in payment methods screen if selected (#2220) - Conditionally highlight "Google Pay" item in `PaymentMethodsAdapter` - Update `PaymentMethodsActivity` to include `useGooglePay` in result --- stripe/res/layout/google_pay_row.xml | 1 + .../java/com/stripe/android/PaymentSession.kt | 1 + .../stripe/android/PaymentSessionViewModel.kt | 18 +++++++++------ .../com/stripe/android/view/MaskedCardView.kt | 7 +----- .../android/view/PaymentMethodsActivity.kt | 20 +++++++++-------- .../view/PaymentMethodsActivityStarter.kt | 9 +++++++- .../android/view/PaymentMethodsAdapter.kt | 22 ++++++++++++------- 7 files changed, 47 insertions(+), 31 deletions(-) diff --git a/stripe/res/layout/google_pay_row.xml b/stripe/res/layout/google_pay_row.xml index ab9ed8dee1c..0b9be7c9757 100644 --- a/stripe/res/layout/google_pay_row.xml +++ b/stripe/res/layout/google_pay_row.xml @@ -22,6 +22,7 @@ /> - paymentSessionPrefs.getPaymentMethodId(customerId) + return if (paymentSessionData.useGooglePay) { + null + } else { + userSelectedPaymentMethodId + ?: if (paymentSessionData.paymentMethod != null) { + paymentSessionData.paymentMethod?.id + } else { + customerSession.cachedCustomer?.id?.let { customerId -> + paymentSessionPrefs.getPaymentMethodId(customerId) + } } - } + } } @JvmSynthetic diff --git a/stripe/src/main/java/com/stripe/android/view/MaskedCardView.kt b/stripe/src/main/java/com/stripe/android/view/MaskedCardView.kt index c1c353ecda0..df07f8e7ecb 100644 --- a/stripe/src/main/java/com/stripe/android/view/MaskedCardView.kt +++ b/stripe/src/main/java/com/stripe/android/view/MaskedCardView.kt @@ -2,7 +2,6 @@ package com.stripe.android.view import android.content.Context import android.content.res.ColorStateList -import android.text.SpannableString import android.util.AttributeSet import android.view.View import android.widget.ImageView @@ -76,7 +75,7 @@ internal class MaskedCardView @JvmOverloads constructor( private fun updateUi() { updateBrandIcon() - cardInformationTextView.text = createDisplayString() + cardInformationTextView.text = cardDisplayFactory.createStyled(cardBrand, last4, isSelected) } private fun updateBrandIcon() { @@ -97,10 +96,6 @@ internal class MaskedCardView @JvmOverloads constructor( ) } - private fun createDisplayString(): SpannableString { - return cardDisplayFactory.createStyled(cardBrand, last4, isSelected) - } - private fun updateCheckMark() { checkImageView.visibility = if (isSelected) { View.VISIBLE diff --git a/stripe/src/main/java/com/stripe/android/view/PaymentMethodsActivity.kt b/stripe/src/main/java/com/stripe/android/view/PaymentMethodsActivity.kt index 6c0769f7621..4f6674eec28 100644 --- a/stripe/src/main/java/com/stripe/android/view/PaymentMethodsActivity.kt +++ b/stripe/src/main/java/com/stripe/android/view/PaymentMethodsActivity.kt @@ -60,7 +60,8 @@ class PaymentMethodsActivity : AppCompatActivity() { args, addableTypes = args.paymentMethodTypes, initiallySelectedPaymentMethodId = viewModel.selectedPaymentMethodId, - shouldShowGooglePay = args.shouldShowGooglePay + shouldShowGooglePay = args.shouldShowGooglePay, + useGooglePay = args.useGooglePay ) } @@ -123,7 +124,7 @@ class PaymentMethodsActivity : AppCompatActivity() { } payment_methods_recycler.adapter = adapter - payment_methods_recycler.paymentMethodSelectedCallback = { finishWithPaymentMethod(it) } + payment_methods_recycler.paymentMethodSelectedCallback = { finishWithResult(it) } payment_methods_recycler.attachItemTouchHelper( PaymentMethodSwipeCallback( @@ -142,7 +143,7 @@ class PaymentMethodsActivity : AppCompatActivity() { } override fun onSupportNavigateUp(): Boolean { - finishWithPaymentMethod(adapter.selectedPaymentMethod, Activity.RESULT_CANCELED) + finishWithResult(adapter.selectedPaymentMethod, Activity.RESULT_CANCELED) return true } @@ -165,12 +166,12 @@ class PaymentMethodsActivity : AppCompatActivity() { // If the added Payment Method is not reusable, it also can't be attached to a // customer, so immediately return to the launching host with the new // Payment Method. - finishWithPaymentMethod(paymentMethod) + finishWithResult(paymentMethod) } } override fun onBackPressed() { - finishWithPaymentMethod(adapter.selectedPaymentMethod, Activity.RESULT_CANCELED) + finishWithResult(adapter.selectedPaymentMethod, Activity.RESULT_CANCELED) } private fun fetchCustomerPaymentMethods() { @@ -206,16 +207,17 @@ class PaymentMethodsActivity : AppCompatActivity() { finish() } - private fun finishWithPaymentMethod( + private fun finishWithResult( paymentMethod: PaymentMethod?, resultCode: Int = Activity.RESULT_OK ) { setResult( resultCode, Intent().also { - if (paymentMethod != null) { - it.putExtras(PaymentMethodsActivityStarter.Result(paymentMethod).toBundle()) - } + it.putExtras(PaymentMethodsActivityStarter.Result( + paymentMethod = paymentMethod, + useGooglePay = args.useGooglePay && paymentMethod == null + ).toBundle()) } ) diff --git a/stripe/src/main/java/com/stripe/android/view/PaymentMethodsActivityStarter.kt b/stripe/src/main/java/com/stripe/android/view/PaymentMethodsActivityStarter.kt index 94de481f8ab..e166d6f0787 100644 --- a/stripe/src/main/java/com/stripe/android/view/PaymentMethodsActivityStarter.kt +++ b/stripe/src/main/java/com/stripe/android/view/PaymentMethodsActivityStarter.kt @@ -46,7 +46,8 @@ class PaymentMethodsActivityStarter : ActivityStarter { private var billingAddressFields: BillingAddressFields = BillingAddressFields.None @@ -54,6 +55,7 @@ class PaymentMethodsActivityStarter : ActivityStarter? = null private var shouldShowGooglePay: Boolean = false + private var useGooglePay: Boolean = false private var paymentConfiguration: PaymentConfiguration? = null @LayoutRes private var addPaymentMethodFooterLayoutId: Int = 0 @@ -127,12 +129,17 @@ class PaymentMethodsActivityStarter : ActivityStarter = listOf(PaymentMethod.Type.Card), initiallySelectedPaymentMethodId: String? = null, - private val shouldShowGooglePay: Boolean = false + private val shouldShowGooglePay: Boolean = false, + private val useGooglePay: Boolean = false ) : RecyclerView.Adapter() { internal val paymentMethods = ArrayList() @@ -117,7 +119,7 @@ internal class PaymentMethodsAdapter constructor( selectedPaymentMethodId = null listener?.onGooglePayClick() } - holder.bind(true) + holder.bind(useGooglePay) } } @@ -256,25 +258,29 @@ internal class PaymentMethodsAdapter constructor( .inflate(R.layout.google_pay_row, parent, false) ) + private val label: TextView = itemView.findViewById(R.id.label) private val checkIcon: ImageView = itemView.findViewById(R.id.check_icon) + private val themeConfig = ThemeConfig(itemView.context) init { ImageViewCompat.setImageTintList( checkIcon, - ColorStateList.valueOf( - ThemeConfig(itemView.context).getTintColor(true) - ) + ColorStateList.valueOf(themeConfig.getTintColor(true)) ) } - fun bind(isChecked: Boolean) { - checkIcon.visibility = if (isChecked) { + fun bind(isSelected: Boolean) { + label.setTextColor( + ColorStateList.valueOf(themeConfig.getTextColor(isSelected)) + ) + + checkIcon.visibility = if (isSelected) { View.VISIBLE } else { View.INVISIBLE } - itemView.isSelected = isChecked + itemView.isSelected = isSelected } }