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

Force Canadian postal codes to be uppercase #2304

Merged
merged 1 commit into from
Mar 20, 2020
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -123,7 +125,7 @@ class ShippingInfoWidget @JvmOverloads constructor(
optionalShippingInfoFields = optionalAddressFields.orEmpty()
renderLabels()

countryAutoCompleteTextView.selectedCountry?.let(::renderCountrySpecificLabels)
countryAutoCompleteTextView.selectedCountry?.let(::updateConfigForCountry)
}

/**
Expand All @@ -134,7 +136,7 @@ class ShippingInfoWidget @JvmOverloads constructor(
hiddenShippingInfoFields = hiddenAddressFields.orEmpty()
renderLabels()

countryAutoCompleteTextView.selectedCountry?.let(::renderCountrySpecificLabels)
countryAutoCompleteTextView.selectedCountry?.let(::updateConfigForCountry)
}

/**
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -289,14 +291,16 @@ 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()
Locale.CANADA.country -> renderCanadianForm()
else -> renderInternationalForm()
}

updatePostalCodeInputFilter(country)

postalCodeTextInputLayout.visibility =
if (CountryUtils.doesCountryUsePostalCode(country.code) &&
!isFieldHidden(CustomizableShippingField.POSTAL_CODE_FIELD)) {
Expand All @@ -306,6 +310,13 @@ class ShippingInfoWidget @JvmOverloads constructor(
}
}

private fun updatePostalCodeInputFilter(country: Country) {
postalCodeEditText.filters = when (country.code) {
Locale.CANADA.country -> arrayOf<InputFilter>(AllCaps())
else -> arrayOf<InputFilter>()
}
}

private fun renderUSForm() {
addressLine1TextInputLayout.hint =
if (isFieldOptional(CustomizableShippingField.ADDRESS_LINE_ONE_FIELD)) {
Expand Down