Skip to content

Commit

Permalink
Highlight "Google Pay" option in payment methods screen if selected (#…
Browse files Browse the repository at this point in the history
…2220)

- Conditionally highlight "Google Pay" item in `PaymentMethodsAdapter`
- Update `PaymentMethodsActivity` to include `useGooglePay` in result
  • Loading branch information
mshafrir-stripe authored Feb 24, 2020
1 parent 6461497 commit 0a5abbb
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 31 deletions.
1 change: 1 addition & 0 deletions stripe/res/layout/google_pay_row.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
/>

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
Expand Down
1 change: 1 addition & 0 deletions stripe/src/main/java/com/stripe/android/PaymentSession.kt
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ class PaymentSession @VisibleForTesting internal constructor(
.setShouldShowGooglePay(config.shouldShowGooglePay)
.setWindowFlags(config.windowFlags)
.setBillingAddressFields(config.billingAddressFields)
.setUseGooglePay(viewModel.paymentSessionData.useGooglePay)
.build()
)
}
Expand Down
18 changes: 11 additions & 7 deletions stripe/src/main/java/com/stripe/android/PaymentSessionViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,18 @@ internal class PaymentSessionViewModel(

@JvmSynthetic
fun getSelectedPaymentMethodId(userSelectedPaymentMethodId: String? = null): String? {
return userSelectedPaymentMethodId
?: if (paymentSessionData.paymentMethod != null) {
paymentSessionData.paymentMethod?.id
} else {
customerSession.cachedCustomer?.id?.let { customerId ->
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ class PaymentMethodsActivity : AppCompatActivity() {
args,
addableTypes = args.paymentMethodTypes,
initiallySelectedPaymentMethodId = viewModel.selectedPaymentMethodId,
shouldShowGooglePay = args.shouldShowGooglePay
shouldShowGooglePay = args.shouldShowGooglePay,
useGooglePay = args.useGooglePay
)
}

Expand Down Expand Up @@ -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(
Expand All @@ -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
}

Expand All @@ -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() {
Expand Down Expand Up @@ -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())
}
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ class PaymentMethodsActivityStarter : ActivityStarter<PaymentMethodsActivity, Ar
internal val paymentConfiguration: PaymentConfiguration?,
internal val windowFlags: Int? = null,
internal val billingAddressFields: BillingAddressFields,
internal val shouldShowGooglePay: Boolean = false
internal val shouldShowGooglePay: Boolean = false,
internal val useGooglePay: Boolean = false
) : ActivityStarter.Args {
class Builder : ObjectBuilder<Args> {
private var billingAddressFields: BillingAddressFields = BillingAddressFields.None
private var initialPaymentMethodId: String? = null
private var isPaymentSessionActive = false
private var paymentMethodTypes: List<PaymentMethod.Type>? = null
private var shouldShowGooglePay: Boolean = false
private var useGooglePay: Boolean = false
private var paymentConfiguration: PaymentConfiguration? = null
@LayoutRes
private var addPaymentMethodFooterLayoutId: Int = 0
Expand Down Expand Up @@ -127,12 +129,17 @@ class PaymentMethodsActivityStarter : ActivityStarter<PaymentMethodsActivity, Ar
this.windowFlags = windowFlags
}

internal fun setUseGooglePay(useGooglePay: Boolean): Builder = apply {
this.useGooglePay = useGooglePay
}

override fun build(): Args {
return Args(
initialPaymentMethodId = initialPaymentMethodId,
isPaymentSessionActive = isPaymentSessionActive,
paymentMethodTypes = paymentMethodTypes ?: listOf(PaymentMethod.Type.Card),
shouldShowGooglePay = shouldShowGooglePay,
useGooglePay = useGooglePay,
paymentConfiguration = paymentConfiguration,
addPaymentMethodFooterLayoutId = addPaymentMethodFooterLayoutId,
windowFlags = windowFlags,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.ViewCompat
import androidx.core.widget.ImageViewCompat
import androidx.recyclerview.widget.RecyclerView
Expand All @@ -24,7 +25,8 @@ internal class PaymentMethodsAdapter constructor(
private val intentArgs: PaymentMethodsActivityStarter.Args,
private val addableTypes: List<PaymentMethod.Type> = listOf(PaymentMethod.Type.Card),
initiallySelectedPaymentMethodId: String? = null,
private val shouldShowGooglePay: Boolean = false
private val shouldShowGooglePay: Boolean = false,
private val useGooglePay: Boolean = false
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

internal val paymentMethods = ArrayList<PaymentMethod>()
Expand Down Expand Up @@ -117,7 +119,7 @@ internal class PaymentMethodsAdapter constructor(
selectedPaymentMethodId = null
listener?.onGooglePayClick()
}
holder.bind(true)
holder.bind(useGooglePay)
}
}

Expand Down Expand Up @@ -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
}
}

Expand Down

0 comments on commit 0a5abbb

Please sign in to comment.