Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make CardInputListener.FocusField an enum #2561

Merged
merged 1 commit into from
Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions MIGRATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
- Changes to `AccountParams`
- Remove `AccountParams.create()` that takes a raw map; instead, use `create()` method that takes a
`AccountParams.BusinessTypeParams.Individual` or `AccountParams.BusinessTypeParams.Company`
- Changes to `CardInputListener`
- `FocusField` is now an enum

## Migrating from versions < 14.5.0
- Changes to `StripeIntent`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
package com.stripe.android.view

import androidx.annotation.StringDef

/**
* Represents a listener for card input events. Note that events are
* not one-time events. For instance, a user can "complete" the CVC many times
* by deleting and re-entering the value.
*/
interface CardInputListener {

@Retention(AnnotationRetention.SOURCE)
@StringDef(FocusField.FOCUS_CARD, FocusField.FOCUS_EXPIRY, FocusField.FOCUS_CVC,
FocusField.FOCUS_POSTAL)
annotation class FocusField {
companion object {
const val FOCUS_CARD: String = "focus_card"
const val FOCUS_EXPIRY: String = "focus_expiry"
const val FOCUS_CVC: String = "focus_cvc"
const val FOCUS_POSTAL: String = "focus_postal"
}
enum class FocusField {
CardNumber,
ExpiryDate,
Cvc,
PostalCode
}

/**
* Called whenever the field of focus within the widget changes.
*
* @param focusField a [FocusField] to which the focus has just changed.
*/
fun onFocusChange(@FocusField focusField: String)
fun onFocusChange(focusField: FocusField)

/**
* Called when a potentially valid card number has been completed in the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ import com.stripe.android.model.Card
import com.stripe.android.model.CardBrand
import com.stripe.android.model.PaymentMethod
import com.stripe.android.model.PaymentMethodCreateParams
import com.stripe.android.view.CardInputListener.FocusField.Companion.FOCUS_CARD
import com.stripe.android.view.CardInputListener.FocusField.Companion.FOCUS_CVC
import com.stripe.android.view.CardInputListener.FocusField.Companion.FOCUS_EXPIRY
import kotlin.properties.Delegates

/**
Expand Down Expand Up @@ -723,14 +720,14 @@ class CardInputWidget @JvmOverloads constructor(
cardNumberEditText.onFocusChangeListener = OnFocusChangeListener { _, hasFocus ->
if (hasFocus) {
scrollLeft()
cardInputListener?.onFocusChange(FOCUS_CARD)
cardInputListener?.onFocusChange(CardInputListener.FocusField.CardNumber)
}
}

expiryDateEditText.onFocusChangeListener = OnFocusChangeListener { _, hasFocus ->
if (hasFocus) {
scrollRight()
cardInputListener?.onFocusChange(FOCUS_EXPIRY)
cardInputListener?.onFocusChange(CardInputListener.FocusField.ExpiryDate)
}
}

Expand All @@ -741,7 +738,7 @@ class CardInputWidget @JvmOverloads constructor(
cvcNumberEditText.onFocusChangeListener = OnFocusChangeListener { _, hasFocus ->
if (hasFocus) {
scrollRight()
cardInputListener?.onFocusChange(FOCUS_CVC)
cardInputListener?.onFocusChange(CardInputListener.FocusField.Cvc)
}
updateIconCvc(hasFocus, cvcValue)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ import com.stripe.android.model.Card
import com.stripe.android.model.CardBrand
import com.stripe.android.model.PaymentMethod
import com.stripe.android.model.PaymentMethodCreateParams
import com.stripe.android.view.CardInputListener.FocusField.Companion.FOCUS_CARD
import com.stripe.android.view.CardInputListener.FocusField.Companion.FOCUS_CVC
import com.stripe.android.view.CardInputListener.FocusField.Companion.FOCUS_EXPIRY
import com.stripe.android.view.CardInputListener.FocusField.Companion.FOCUS_POSTAL
import java.math.BigDecimal
import java.math.RoundingMode
import kotlin.properties.Delegates
Expand Down Expand Up @@ -585,7 +581,7 @@ class CardMultilineWidget @JvmOverloads constructor(
cardNumberEditText.onFocusChangeListener = OnFocusChangeListener { _, hasFocus ->
if (hasFocus) {
cardNumberEditText.setHintDelayed(cardHintText, CARD_NUMBER_HINT_DELAY)
cardInputListener?.onFocusChange(FOCUS_CARD)
cardInputListener?.onFocusChange(CardInputListener.FocusField.CardNumber)
} else {
cardNumberEditText.hint = ""
}
Expand All @@ -594,7 +590,7 @@ class CardMultilineWidget @JvmOverloads constructor(
expiryDateEditText.onFocusChangeListener = OnFocusChangeListener { _, hasFocus ->
if (hasFocus) {
expiryDateEditText.setHintDelayed(R.string.expiry_date_hint, COMMON_HINT_DELAY)
cardInputListener?.onFocusChange(FOCUS_EXPIRY)
cardInputListener?.onFocusChange(CardInputListener.FocusField.ExpiryDate)
} else {
expiryDateEditText.hint = ""
}
Expand All @@ -604,7 +600,7 @@ class CardMultilineWidget @JvmOverloads constructor(
if (hasFocus) {
flipToCvcIconIfNotFinished()
cvcEditText.setHintDelayed(cvcHelperText, COMMON_HINT_DELAY)
cardInputListener?.onFocusChange(FOCUS_CVC)
cardInputListener?.onFocusChange(CardInputListener.FocusField.Cvc)
} else {
updateBrandUi()
cvcEditText.hint = ""
Expand All @@ -617,7 +613,7 @@ class CardMultilineWidget @JvmOverloads constructor(
}
if (hasFocus) {
postalCodeEditText.setHintDelayed(R.string.zip_helper, COMMON_HINT_DELAY)
cardInputListener?.onFocusChange(FOCUS_POSTAL)
cardInputListener?.onFocusChange(CardInputListener.FocusField.PostalCode)
} else {
postalCodeEditText.hint = ""
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ import com.stripe.android.model.PaymentMethod
import com.stripe.android.model.PaymentMethodCreateParams
import com.stripe.android.testharness.TestFocusChangeListener
import com.stripe.android.testharness.ViewTestUtils
import com.stripe.android.view.CardInputListener.FocusField.Companion.FOCUS_CARD
import com.stripe.android.view.CardInputListener.FocusField.Companion.FOCUS_CVC
import com.stripe.android.view.CardInputListener.FocusField.Companion.FOCUS_EXPIRY
import com.stripe.android.view.CardInputWidget.Companion.LOGGING_TOKEN
import com.stripe.android.view.CardInputWidget.Companion.shouldIconShowBrand
import java.util.Calendar
Expand Down Expand Up @@ -505,7 +502,7 @@ internal class CardInputWidgetTest {
cardNumberEditText.setText(VISA_WITH_SPACES)

verify(cardInputListener).onCardComplete()
verify(cardInputListener).onFocusChange(FOCUS_EXPIRY)
verify(cardInputListener).onFocusChange(CardInputListener.FocusField.ExpiryDate)
assertEquals(cardNumberEditText.id, onGlobalFocusChangeListener.oldFocusId)
assertEquals(expiryEditText.id, onGlobalFocusChangeListener.newFocusId)
}
Expand All @@ -520,7 +517,7 @@ internal class CardInputWidgetTest {
reset(cardInputListener)

ViewTestUtils.sendDeleteKeyEvent(expiryEditText)
verify(cardInputListener).onFocusChange(FOCUS_CARD)
verify(cardInputListener).onFocusChange(CardInputListener.FocusField.CardNumber)
assertEquals(expiryEditText.id, onGlobalFocusChangeListener.oldFocusId)
assertEquals(cardNumberEditText.id, onGlobalFocusChangeListener.newFocusId)

Expand Down Expand Up @@ -550,20 +547,20 @@ internal class CardInputWidgetTest {
cardNumberEditText.setText(VISA_WITH_SPACES)

verify(cardInputListener).onCardComplete()
verify(cardInputListener).onFocusChange(FOCUS_EXPIRY)
verify(cardInputListener).onFocusChange(CardInputListener.FocusField.ExpiryDate)

expiryEditText.append("12")
expiryEditText.append("79")

verify(cardInputListener).onExpirationComplete()
verify(cardInputListener).onFocusChange(FOCUS_CVC)
verify(cardInputListener).onFocusChange(CardInputListener.FocusField.Cvc)
assertTrue(cvcEditText.hasFocus())

// Clearing already-verified data.
reset(cardInputListener)

ViewTestUtils.sendDeleteKeyEvent(cvcEditText)
verify(cardInputListener).onFocusChange(FOCUS_EXPIRY)
verify(cardInputListener).onFocusChange(CardInputListener.FocusField.ExpiryDate)
assertEquals(cvcEditText.id, onGlobalFocusChangeListener.oldFocusId)
assertEquals(expiryEditText.id, onGlobalFocusChangeListener.newFocusId)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ import com.stripe.android.model.Address
import com.stripe.android.model.PaymentMethod
import com.stripe.android.model.PaymentMethodCreateParams
import com.stripe.android.testharness.ViewTestUtils
import com.stripe.android.view.CardInputListener.FocusField.Companion.FOCUS_CARD
import com.stripe.android.view.CardInputListener.FocusField.Companion.FOCUS_CVC
import com.stripe.android.view.CardInputListener.FocusField.Companion.FOCUS_EXPIRY
import com.stripe.android.view.CardInputListener.FocusField.Companion.FOCUS_POSTAL
import java.util.Calendar
import kotlin.test.BeforeTest
import kotlin.test.Test
Expand Down Expand Up @@ -481,12 +477,12 @@ internal class CardMultilineWidgetTest {

fullGroup.cardNumberEditText.setText(VISA_WITH_SPACES)
verify(fullCardListener).onCardComplete()
verify(fullCardListener).onFocusChange(FOCUS_EXPIRY)
verify(fullCardListener).onFocusChange(CardInputListener.FocusField.ExpiryDate)
assertTrue(fullGroup.expiryDateEditText.hasFocus())

noZipGroup.cardNumberEditText.setText(AMEX_WITH_SPACES)
verify(noZipCardListener).onCardComplete()
verify(noZipCardListener).onFocusChange(FOCUS_EXPIRY)
verify(noZipCardListener).onFocusChange(CardInputListener.FocusField.ExpiryDate)
assertTrue(noZipGroup.expiryDateEditText.hasFocus())
}

Expand All @@ -498,13 +494,13 @@ internal class CardMultilineWidgetTest {
fullGroup.expiryDateEditText.append("12")
fullGroup.expiryDateEditText.append("50")
verify(fullCardListener).onExpirationComplete()
verify(fullCardListener).onFocusChange(FOCUS_CVC)
verify(fullCardListener).onFocusChange(CardInputListener.FocusField.Cvc)
assertTrue(fullGroup.cvcEditText.hasFocus())

noZipGroup.expiryDateEditText.append("12")
noZipGroup.expiryDateEditText.append("50")
verify(noZipCardListener).onExpirationComplete()
verify(noZipCardListener).onFocusChange(FOCUS_CVC)
verify(noZipCardListener).onFocusChange(CardInputListener.FocusField.Cvc)
assertTrue(noZipGroup.cvcEditText.hasFocus())
}

Expand All @@ -518,15 +514,15 @@ internal class CardMultilineWidgetTest {
fullGroup.expiryDateEditText.append("50")
fullGroup.cvcEditText.append("123")
verify(fullCardListener).onCvcComplete()
verify(fullCardListener).onFocusChange(FOCUS_POSTAL)
verify(fullCardListener).onFocusChange(CardInputListener.FocusField.PostalCode)
assertTrue(fullGroup.postalCodeEditText.hasFocus())

noZipGroup.cardNumberEditText.setText(VISA_WITH_SPACES)
noZipGroup.expiryDateEditText.append("12")
noZipGroup.expiryDateEditText.append("50")
noZipGroup.cvcEditText.append("123")
verify(noZipCardListener).onCvcComplete()
verify(noZipCardListener, never()).onFocusChange(FOCUS_POSTAL)
verify(noZipCardListener, never()).onFocusChange(CardInputListener.FocusField.PostalCode)
assertTrue(noZipGroup.cvcEditText.hasFocus())
}

Expand All @@ -543,7 +539,7 @@ internal class CardMultilineWidgetTest {
assertTrue(fullGroup.expiryDateEditText.hasFocus())
ViewTestUtils.sendDeleteKeyEvent(fullGroup.expiryDateEditText)

verify(fullCardListener).onFocusChange(FOCUS_CARD)
verify(fullCardListener).onFocusChange(CardInputListener.FocusField.CardNumber)
assertTrue(fullGroup.cardNumberEditText.hasFocus())
assertEquals(deleteOneCharacterString, fullGroup.cardNumberEditText.text?.toString())

Expand All @@ -553,7 +549,7 @@ internal class CardMultilineWidgetTest {
assertTrue(noZipGroup.expiryDateEditText.hasFocus())
ViewTestUtils.sendDeleteKeyEvent(noZipGroup.expiryDateEditText)

verify(noZipCardListener).onFocusChange(FOCUS_CARD)
verify(noZipCardListener).onFocusChange(CardInputListener.FocusField.CardNumber)
assertTrue(noZipGroup.cardNumberEditText.hasFocus())
assertEquals(deleteOneCharacterString, noZipGroup.cardNumberEditText.text?.toString())
}
Expand All @@ -570,7 +566,7 @@ internal class CardMultilineWidgetTest {
assertTrue(fullGroup.cvcEditText.hasFocus())
ViewTestUtils.sendDeleteKeyEvent(fullGroup.cvcEditText)

verify(fullCardListener).onFocusChange(FOCUS_EXPIRY)
verify(fullCardListener).onFocusChange(CardInputListener.FocusField.ExpiryDate)
assertTrue(fullGroup.expiryDateEditText.hasFocus())
assertEquals("12/5", fullGroup.expiryDateEditText.text?.toString())

Expand All @@ -581,7 +577,7 @@ internal class CardMultilineWidgetTest {
assertTrue(noZipGroup.cvcEditText.hasFocus())
ViewTestUtils.sendDeleteKeyEvent(noZipGroup.cvcEditText)

verify(noZipCardListener).onFocusChange(FOCUS_EXPIRY)
verify(noZipCardListener).onFocusChange(CardInputListener.FocusField.ExpiryDate)
assertTrue(noZipGroup.expiryDateEditText.hasFocus())
assertEquals("12/5", noZipGroup.expiryDateEditText.text.toString())
}
Expand All @@ -598,7 +594,7 @@ internal class CardMultilineWidgetTest {
reset(fullCardListener)
ViewTestUtils.sendDeleteKeyEvent(fullGroup.postalCodeEditText)

verify(fullCardListener).onFocusChange(FOCUS_CVC)
verify(fullCardListener).onFocusChange(CardInputListener.FocusField.Cvc)
assertEquals("12", fullGroup.cvcEditText.text?.toString())
}

Expand Down