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

Add BecsDebitWidget.ValidParamsCallback #2297

Merged
merged 1 commit into from
Mar 18, 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
1 change: 1 addition & 0 deletions example/res/layout/becs_debit_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />

<TextView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.stripe.android.ApiResultCallback
import com.stripe.android.PaymentConfiguration
import com.stripe.android.Stripe
import com.stripe.android.model.PaymentMethod
import com.stripe.android.view.BecsDebitWidget
import com.stripe.example.databinding.BecsDebitActivityBinding

class BecsDebitPaymentMethodActivity : AppCompatActivity() {
Expand All @@ -23,6 +24,12 @@ class BecsDebitPaymentMethodActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(viewBinding.root)

viewBinding.element.validParamsCallback = object : BecsDebitWidget.ValidParamsCallback {
override fun onInputChanged(isValid: Boolean) {
viewBinding.submit.isEnabled = isValid
}
}

viewBinding.mandateAcceptance.merchantName = "Rocketship Inc."

viewBinding.submit.setOnClickListener {
Expand Down
40 changes: 40 additions & 0 deletions stripe/src/main/java/com/stripe/android/view/BecsDebitWidget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,45 @@ class BecsDebitWidget @JvmOverloads constructor(
)
}

var validParamsCallback: ValidParamsCallback = object : ValidParamsCallback {
override fun onInputChanged(isValid: Boolean) {
// no-op default implementation
}
}

private val validParamsTextWatcher = object : StripeTextWatcher() {
override fun afterTextChanged(s: Editable?) {
super.afterTextChanged(s)
validParamsCallback.onInputChanged(isInputValid)
}
}

private val isInputValid: Boolean
get() {
val name = viewBinding.nameEditText.fieldText
val email = viewBinding.emailEditText.email
val bsbNumber = viewBinding.bsbEditText.bsb
val accountNumber = viewBinding.accountNumberEditText.accountNumber

return !(name.isBlank() || email.isNullOrBlank() || bsbNumber.isNullOrBlank() ||
accountNumber.isNullOrBlank())
}

init {
if (Build.VERSION.SDK_INT >= 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
Expand Down Expand Up @@ -145,4 +178,11 @@ class BecsDebitWidget @JvmOverloads constructor(
)
)
}

interface ValidParamsCallback {
/**
* @param isValid if the current input is valid
*/
fun onInputChanged(isValid: Boolean)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,48 @@ internal class BecsDebitWidgetTest : BaseViewTest<BecsDebitWidgetTestActivity>(
.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"
Expand Down