From cd873bcb3ff431e73edcb70b6f7d7ec0716b7668 Mon Sep 17 00:00:00 2001 From: Michael Shafrir <45020849+mshafrir-stripe@users.noreply.github.com> Date: Wed, 18 Mar 2020 14:50:41 -0400 Subject: [PATCH] Add BecsDebitWidget.ValidParamsCallback (#2297) When `BecsDebitWidget.validParamsCallback` is set, `onInputChanged(isValid)` will be called on that instance. --- example/res/layout/becs_debit_activity.xml | 1 + .../BecsDebitPaymentMethodActivity.kt | 7 ++++ .../stripe/android/view/BecsDebitWidget.kt | 40 ++++++++++++++++++ .../android/view/BecsDebitWidgetTest.kt | 42 +++++++++++++++++++ 4 files changed, 90 insertions(+) diff --git a/example/res/layout/becs_debit_activity.xml b/example/res/layout/becs_debit_activity.xml index e6333916f6d..41ac0c6afa4 100644 --- a/example/res/layout/becs_debit_activity.xml +++ b/example/res/layout/becs_debit_activity.xml @@ -27,6 +27,7 @@ android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="20dp" + android:enabled="false" android:text="@string/create_becs_debit_params" /> = Build.VERSION_CODES.O) { viewBinding.nameEditText.setAutofillHints(View.AUTOFILL_HINT_NAME) viewBinding.emailEditText.setAutofillHints(View.AUTOFILL_HINT_EMAIL_ADDRESS) } + setOf( + viewBinding.nameEditText, + viewBinding.emailEditText, + viewBinding.bsbEditText, + viewBinding.accountNumberEditText + ).forEach { + it.addTextChangedListener(validParamsTextWatcher) + } + viewBinding.bsbEditText.onBankChangedCallback = { bank -> if (bank != null) { viewBinding.bsbTextInputLayout.helperText = bank.name @@ -145,4 +178,11 @@ class BecsDebitWidget @JvmOverloads constructor( ) ) } + + interface ValidParamsCallback { + /** + * @param isValid if the current input is valid + */ + fun onInputChanged(isValid: Boolean) + } } diff --git a/stripe/src/test/java/com/stripe/android/view/BecsDebitWidgetTest.kt b/stripe/src/test/java/com/stripe/android/view/BecsDebitWidgetTest.kt index f1a23e1a2d8..933f1ba7a7c 100644 --- a/stripe/src/test/java/com/stripe/android/view/BecsDebitWidgetTest.kt +++ b/stripe/src/test/java/com/stripe/android/view/BecsDebitWidgetTest.kt @@ -112,6 +112,48 @@ internal class BecsDebitWidgetTest : BaseViewTest( .isEqualTo(4) } + @Test + fun validParamsCallback_shouldBeCalledWhenInputChanges() { + var isValidArg = false + var callbacks = 0 + becsDebitWidget.validParamsCallback = object : BecsDebitWidget.ValidParamsCallback { + override fun onInputChanged(isValid: Boolean) { + callbacks++ + isValidArg = isValid + } + } + + assertThat(isValidArg) + .isFalse() + assertThat(callbacks) + .isEqualTo(0) + + nameEditText.setText("Jenny Rosen") + assertThat(isValidArg) + .isFalse() + assertThat(callbacks) + .isEqualTo(1) + + emailEditText.setText("jrosen@example.com") + assertThat(isValidArg) + .isFalse() + assertThat(callbacks) + .isEqualTo(2) + + bsbEditText.setText("000000") + assertThat(isValidArg) + .isFalse() + // there is an additional callback because "000000" is formatted to "000-000" + assertThat(callbacks) + .isEqualTo(4) + + accountNumberEditText.setText("000123456") + assertThat(isValidArg) + .isTrue() + assertThat(callbacks) + .isEqualTo(5) + } + private companion object { private const val VALID_BSB_NUMBER = "000000" private const val VALID_ACCOUNT_NUMBER = "000123456"