From 62b3057593ab85ea616e66c7a74482a15f5388b7 Mon Sep 17 00:00:00 2001 From: Michael Shafrir Date: Fri, 20 Mar 2020 10:34:23 -0400 Subject: [PATCH] Force Canadian postal codes to be uppercase Fixes #2302 --- .../stripe/android/view/ShippingInfoWidget.kt | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/stripe/src/main/java/com/stripe/android/view/ShippingInfoWidget.kt b/stripe/src/main/java/com/stripe/android/view/ShippingInfoWidget.kt index 63cc4afa964..1a67ef7e8dd 100644 --- a/stripe/src/main/java/com/stripe/android/view/ShippingInfoWidget.kt +++ b/stripe/src/main/java/com/stripe/android/view/ShippingInfoWidget.kt @@ -3,6 +3,8 @@ package com.stripe.android.view import android.content.Context import android.os.Build import android.telephony.PhoneNumberFormattingTextWatcher +import android.text.InputFilter +import android.text.InputFilter.AllCaps import android.util.AttributeSet import android.view.LayoutInflater import android.view.View @@ -123,7 +125,7 @@ class ShippingInfoWidget @JvmOverloads constructor( optionalShippingInfoFields = optionalAddressFields.orEmpty() renderLabels() - countryAutoCompleteTextView.selectedCountry?.let(::renderCountrySpecificLabels) + countryAutoCompleteTextView.selectedCountry?.let(::updateConfigForCountry) } /** @@ -134,7 +136,7 @@ class ShippingInfoWidget @JvmOverloads constructor( hiddenShippingInfoFields = hiddenAddressFields.orEmpty() renderLabels() - countryAutoCompleteTextView.selectedCountry?.let(::renderCountrySpecificLabels) + countryAutoCompleteTextView.selectedCountry?.let(::updateConfigForCountry) } /** @@ -228,13 +230,13 @@ class ShippingInfoWidget @JvmOverloads constructor( } private fun initView() { - countryAutoCompleteTextView.countryChangeCallback = ::renderCountrySpecificLabels + countryAutoCompleteTextView.countryChangeCallback = ::updateConfigForCountry phoneNumberEditText.addTextChangedListener(PhoneNumberFormattingTextWatcher()) setupErrorHandling() renderLabels() - countryAutoCompleteTextView.selectedCountry?.let(::renderCountrySpecificLabels) + countryAutoCompleteTextView.selectedCountry?.let(::updateConfigForCountry) } private fun setupErrorHandling() { @@ -289,7 +291,7 @@ class ShippingInfoWidget @JvmOverloads constructor( } } - private fun renderCountrySpecificLabels(country: Country) { + private fun updateConfigForCountry(country: Country) { when (country.code) { Locale.US.country -> renderUSForm() Locale.UK.country -> renderGreatBritainForm() @@ -297,6 +299,8 @@ class ShippingInfoWidget @JvmOverloads constructor( else -> renderInternationalForm() } + updatePostalCodeInputFilter(country) + postalCodeTextInputLayout.visibility = if (CountryUtils.doesCountryUsePostalCode(country.code) && !isFieldHidden(CustomizableShippingField.POSTAL_CODE_FIELD)) { @@ -306,6 +310,13 @@ class ShippingInfoWidget @JvmOverloads constructor( } } + private fun updatePostalCodeInputFilter(country: Country) { + postalCodeEditText.filters = when (country.code) { + Locale.CANADA.country -> arrayOf(AllCaps()) + else -> arrayOf() + } + } + private fun renderUSForm() { addressLine1TextInputLayout.hint = if (isFieldOptional(CustomizableShippingField.ADDRESS_LINE_ONE_FIELD)) {