Skip to content

Commit

Permalink
Move CardMultilineWidget focus to first error field on error
Browse files Browse the repository at this point in the history
Fixes #1561
  • Loading branch information
mshafrir-stripe committed Sep 17, 2019
1 parent be4e375 commit 0195f6e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.stripe.example.controller

import android.content.Context
import android.widget.Button
import android.widget.Toast
import com.jakewharton.rxbinding2.view.RxView
import com.stripe.android.Stripe
import com.stripe.android.view.CardInputWidget
Expand All @@ -17,11 +18,10 @@ import io.reactivex.schedulers.Schedulers
*/
class RxTokenController(
button: Button,
private var mCardInputWidget: CardInputWidget?,
context: Context,
private val mErrorDialogHandler: ErrorDialogHandler,
private val mOutputListController: ListViewController,
private val mProgressDialogController: ProgressDialogController,
private var cardInputWidget: CardInputWidget?,
private val context: Context,
private val outputListController: ListViewController,
private val progressDialogController: ProgressDialogController,
publishableKey: String
) {

Expand All @@ -41,13 +41,14 @@ class RxTokenController(
*/
fun detach() {
mCompositeDisposable.dispose()
mCardInputWidget = null
cardInputWidget = null
}

private fun saveCard() {
val cardToSave = mCardInputWidget!!.card
val cardToSave = cardInputWidget!!.card
if (cardToSave == null) {
mErrorDialogHandler.show("Invalid Card Data")
Toast.makeText(context, "Invalid Card Data", Toast.LENGTH_SHORT)
.show()
return
}

Expand All @@ -60,11 +61,14 @@ class RxTokenController(
mCompositeDisposable.add(tokenObservable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe { mProgressDialogController.show(R.string.progressMessage) }
.doOnComplete { mProgressDialogController.dismiss() }
.doOnSubscribe { progressDialogController.show(R.string.progressMessage) }
.doOnComplete { progressDialogController.dismiss() }
.subscribe(
{ mOutputListController.addToList(it) },
{ throwable -> mErrorDialogHandler.show(throwable.localizedMessage) }
{ outputListController.addToList(it) },
{
Toast.makeText(context, it.localizedMessage, Toast.LENGTH_SHORT)
.show()
}
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ class DependencyHandler(
button,
cardInputWidget,
context,
errorDialogHandler,
listViewController,
progressDialogController,
publishableKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import static com.stripe.android.view.CardInputListener.FocusField.FOCUS_CARD;
Expand Down Expand Up @@ -344,6 +346,16 @@ public boolean validateAllFields() {
postalCodeIsValidOrGone = true;
}

final List<StripeEditText> fields = Arrays.asList(
mCardNumberEditText, mExpiryDateEditText, mCvcEditText, mPostalCodeEditText
);
for (StripeEditText field : fields) {
if (field.getShouldShowError()) {
field.requestFocus();
break;
}
}

return cardNumberIsValid
&& expiryIsValid
&& cvcIsValid
Expand Down

0 comments on commit 0195f6e

Please sign in to comment.