From 2cf1104a3017ec7f54539d70ce074192686ef0ad Mon Sep 17 00:00:00 2001 From: Javier Varona Date: Wed, 4 Oct 2017 22:46:55 -0400 Subject: [PATCH] - Adding TextView under EditText to display errors --- app/build.gradle | 2 + app/src/main/AndroidManifest.xml | 2 +- .../activities/ImportSeedActivity.java | 58 ++++- .../validators/ImportSeedValidator.java | 34 ++- .../ImportSeedValidatorListener.java | 4 +- app/src/main/res/layout/import_seed.xml | 201 +++++++++++------- app/src/main/res/values/strings.xml | 2 +- 7 files changed, 204 insertions(+), 99 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index d2d1aca..45cfff8 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -33,6 +33,8 @@ dependencies { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:26.1.0' + compile 'com.android.support:support-v4:26.1.0' + compile 'com.android.support:design:26.1.0' compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' testCompile 'org.mockito:mockito-core:1.10.19' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index dd7ab54..93baa0c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -9,7 +9,7 @@ android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" - android:theme="@style/AppTheme"> + android:theme="@style/Theme.AppCompat"> diff --git a/app/src/main/java/cy/agorise/crystalwallet/activities/ImportSeedActivity.java b/app/src/main/java/cy/agorise/crystalwallet/activities/ImportSeedActivity.java index 978a38c..3b6ccdc 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/activities/ImportSeedActivity.java +++ b/app/src/main/java/cy/agorise/crystalwallet/activities/ImportSeedActivity.java @@ -24,6 +24,7 @@ import cy.agorise.crystalwallet.viewmodels.AccountSeedViewModel; import cy.agorise.crystalwallet.viewmodels.TransactionListViewModel; import cy.agorise.crystalwallet.viewmodels.validators.ImportSeedValidator; import cy.agorise.crystalwallet.viewmodels.validators.ImportSeedValidatorListener; +import cy.agorise.crystalwallet.viewmodels.validators.ValidationField; import cy.agorise.crystalwallet.views.TransactionListView; public class ImportSeedActivity extends AppCompatActivity implements ImportSeedValidatorListener { @@ -31,17 +32,23 @@ public class ImportSeedActivity extends AppCompatActivity implements ImportSeedV AccountSeedViewModel accountSeedViewModel; ImportSeedValidator importSeedValidator; - @BindView(R.id.tvPin) - TextView tvPin; + @BindView(R.id.etPin) + EditText etPin; + @BindView(R.id.tvPinError) + TextView tvPinError; - @BindView(R.id.tvPinConfirmation) - TextView tvPinConfirmation; + @BindView(R.id.etPinConfirmation) + EditText etPinConfirmation; + @BindView(R.id.tvPinConfirmationError) + TextView tvPinConfirmationError; @BindView(R.id.etSeedWords) EditText etSeedWords; @BindView (R.id.etAccountName) EditText etAccountName; + @BindView(R.id.tvAccountNameError) + TextView tvAccountNameError; @BindView(R.id.btnImport) Button btnImport; @@ -59,6 +66,19 @@ public class ImportSeedActivity extends AppCompatActivity implements ImportSeedV importSeedValidator.setListener(this); } + @OnTextChanged(value = R.id.etPin, + callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED) + void afterPinChanged(Editable editable) { + this.importSeedValidator.validatePin(editable.toString()); + this.importSeedValidator.validatePinConfirmation(etPinConfirmation.getText().toString(),editable.toString()); + } + + @OnTextChanged(value = R.id.etPinConfirmation, + callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED) + void afterPinConfirmationChanged(Editable editable) { + this.importSeedValidator.validatePinConfirmation(editable.toString(), etPin.getText().toString()); + } + @OnTextChanged(value = R.id.etAccountName, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED) void afterAccountNameChanged(Editable editable) { @@ -82,14 +102,32 @@ public class ImportSeedActivity extends AppCompatActivity implements ImportSeedV } @Override - public void onValidationSucceeded() { - //Clear all errors + public void onValidationSucceeded(ValidationField field) { + switch (field.getName()){ + case "pin": + tvPinError.setText(""); + break; + case "pinconfirmation": + tvPinConfirmationError.setText(""); + break; + case "accountname": + tvAccountNameError.setText(""); + break; + } } @Override - public void onValidationFailed(String error) { - //Show errors - Toast.makeText(this, error, - Toast.LENGTH_LONG).show(); + public void onValidationFailed(ValidationField field) { + switch (field.getName()){ + case "pin": + tvPinError.setText(field.getMessage()); + break; + case "pinconfirmation": + tvPinConfirmationError.setText(field.getMessage()); + break; + case "accountname": + tvAccountNameError.setText(field.getMessage()); + break; + } } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/ImportSeedValidator.java b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/ImportSeedValidator.java index 09ee261..e000bc2 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/ImportSeedValidator.java +++ b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/ImportSeedValidator.java @@ -28,8 +28,8 @@ public class ImportSeedValidator { public ImportSeedValidator(Resources res){ this.res = res; this.validationFields = new ArrayList(); - //this.validationFields.add(new ValidationField("pin")); - //this.validationFields.add(new ValidationField("pinConfirmation")); + this.validationFields.add(new ValidationField("pin")); + this.validationFields.add(new ValidationField("pinconfirmation")); this.validationFields.add(new ValidationField("accountname")); } @@ -59,9 +59,33 @@ public class ImportSeedValidator { return null; } - //public validatePin(){ + public void validatePin(final String pin){ + final ValidationField validationField = getValidationField("pin"); + validationField.setLastValue(pin); - //} + if ((pin.length() < 6)){ + validationField.setValidForValue(pin,false); + validationField.setMessage(res.getString(R.string.pin_number_warning)); + listener.onValidationFailed(validationField); + } else { + validationField.setValidForValue(pin, true); + listener.onValidationSucceeded(validationField); + } + } + + public void validatePinConfirmation(final String pinConfirmation, final String pin){ + final ValidationField validationField = getValidationField("pinconfirmation"); + validationField.setLastValue(pinConfirmation); + + if (!pinConfirmation.equals(pin)){ + validationField.setValidForValue(pinConfirmation,false); + validationField.setMessage(res.getString(R.string.mismatch_pin)); + listener.onValidationFailed(validationField); + } else { + validationField.setValidForValue(pinConfirmation, true); + listener.onValidationSucceeded(validationField); + } + } public void validateAccountName(final String accountName, final String mnemonic){ final ValidationField validationField = getValidationField("accountname"); @@ -74,8 +98,10 @@ public class ImportSeedValidator { if (!request.getAccountExists()){ validationField.setValidForValue(accountName, false); validationField.setMessage(res.getString(R.string.account_name_not_exist)); + listener.onValidationFailed(validationField); } else { validationField.setValidForValue(accountName, true); + listener.onValidationSucceeded(validationField); } } }); diff --git a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/ImportSeedValidatorListener.java b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/ImportSeedValidatorListener.java index e3c7328..590f3fc 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/ImportSeedValidatorListener.java +++ b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/ImportSeedValidatorListener.java @@ -6,6 +6,6 @@ package cy.agorise.crystalwallet.viewmodels.validators; public interface ImportSeedValidatorListener { - public void onValidationSucceeded(); - public void onValidationFailed(String error); + public void onValidationSucceeded(ValidationField field); + public void onValidationFailed(ValidationField field); } diff --git a/app/src/main/res/layout/import_seed.xml b/app/src/main/res/layout/import_seed.xml index a0e699e..ee606cd 100644 --- a/app/src/main/res/layout/import_seed.xml +++ b/app/src/main/res/layout/import_seed.xml @@ -2,62 +2,85 @@ + android:paddingTop="@dimen/activity_vertical_margin"> + - - + + - + + + + + android:textStyle="bold" /> + android:singleLine="true" + android:textColor="@color/black" /> + + + android:textStyle="bold" /> + android:inputType="textMultiLine" + android:textColor="@color/black" /> + android:textStyle="bold" /> + android:inputType="textMultiLine" + android:textColor="@color/black" /> + + + android:visibility="gone" /> + android:gravity="center" + android:orientation="horizontal"> +