- Adjusting import seed validators

This commit is contained in:
Javier Varona 2017-10-03 20:58:41 -04:00
parent 5083ad368e
commit 310d4332f7
3 changed files with 48 additions and 21 deletions

View file

@ -61,21 +61,23 @@ public class ImportSeedActivity extends AppCompatActivity implements ImportSeedV
@OnTextChanged(value = R.id.etAccountName, @OnTextChanged(value = R.id.etAccountName,
callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED) callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
void afterAccountNameChanged(Editable editable) { void afterAccountNameChanged(Editable editable) {
this.validator.validateAccountName(editable.getT); this.importSeedValidator.validateAccountName(editable.toString());
} }
@OnClick(R.id.btnImport) @OnClick(R.id.btnImport)
public void importSeed(){ public void importSeed(){
if (this.validator) if (this.importSeedValidator.isValid()) {
AccountSeed seed = new AccountSeed(); AccountSeed seed = new AccountSeed();
//TODO verify if PIN and PIN confirmation are not null and are the same //TODO verify if PIN and PIN confirmation are not null and are the same
//TODO verify if words are already in the db //TODO verify if words are already in the db
//TODO check if name has been asigned to other seed //TODO check if name has been asigned to other seed
seed.setMasterSeed(etSeedWords.getText().toString()); seed.setMasterSeed(etSeedWords.getText().toString());
seed.setName(etAccountName.getText().toString()); seed.setName(etAccountName.getText().toString());
accountSeedViewModel.addSeed(seed); accountSeedViewModel.addSeed(seed);
//TODO get back to the previous activity
}
} }
@Override @Override

View file

@ -1,6 +1,7 @@
package cy.agorise.crystalwallet.viewmodels.validators; package cy.agorise.crystalwallet.viewmodels.validators;
import android.accounts.Account; import android.accounts.Account;
import android.content.res.Resources;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -18,13 +19,14 @@ import cy.agorise.crystalwallet.models.AccountSeed;
public class ImportSeedValidator { public class ImportSeedValidator {
private ImportSeedValidatorListener listener; private ImportSeedValidatorListener listener;
private List<ValidationField> validationFields; private List<ValidationField> validationFields;
private AccountSeed accountSeed; private AccountSeed accountSeed;
private Resources res;
private boolean isValid = false; private boolean isValid = false;
public ImportSeedValidator(AccountSeed seed){ public ImportSeedValidator(Resources res, AccountSeed seed){
this.res = res;
this.accountSeed = seed; this.accountSeed = seed;
this.validationFields = new ArrayList<ValidationField>(); this.validationFields = new ArrayList<ValidationField>();
//this.validationFields.add(new ValidationField("pin")); //this.validationFields.add(new ValidationField("pin"));
@ -36,10 +38,16 @@ public class ImportSeedValidator {
this.listener = listener; this.listener = listener;
} }
public void validate(){ public boolean isValid(){
//validatePin(); for(int i=0;i<this.validationFields.size();i++){
//validatePinConfirmation(); ValidationField nextField = this.validationFields.get(i);
validateAccountName();
if (!nextField.getValid()){
return false;
}
}
return true;
} }
public ValidationField getValidationField(String name){ public ValidationField getValidationField(String name){
@ -56,8 +64,9 @@ public class ImportSeedValidator {
//} //}
public void validateAccountName(String accountName){ public void validateAccountName(final String accountName){
final ValidationField validationField = getValidationField("accountname"); final ValidationField validationField = getValidationField("accountname");
validationField.setLastValue(accountName);
if (this.accountSeed != null){ if (this.accountSeed != null){
final ValidateImportBitsharesAccountRequest request = new ValidateImportBitsharesAccountRequest(this.accountSeed.getName(),this.accountSeed.getMasterSeed()); final ValidateImportBitsharesAccountRequest request = new ValidateImportBitsharesAccountRequest(this.accountSeed.getName(),this.accountSeed.getMasterSeed());
@ -65,10 +74,10 @@ public class ImportSeedValidator {
@Override @Override
public void onCarryOut() { public void onCarryOut() {
if (!request.getAccountExists()){ if (!request.getAccountExists()){
validationField.setValid(false); validationField.setValidForValue(accountName, false);
validationField.setMessage(R.string.account_name_not_exist); validationField.setMessage(res.getString(R.string.account_name_not_exist));
} else { } else {
validationField.setValid(true); validationField.setValidForValue(accountName, true);
} }
} }
}); });

View file

@ -7,6 +7,7 @@ package cy.agorise.crystalwallet.viewmodels.validators;
public class ValidationField { public class ValidationField {
public String name; public String name;
public String lastValue; public String lastValue;
public String message; public String message;
public boolean validating; public boolean validating;
@ -32,8 +33,11 @@ public class ValidationField {
this.validating = false; this.validating = false;
} }
public void setValid(boolean newValue){ public void setValidForValue(String value, boolean newValue){
this.valid = newValue; if (this.lastValue.equals(value)) {
this.validating = false;
this.valid = newValue;
}
} }
public void setMessage(String newValue){ public void setMessage(String newValue){
@ -51,4 +55,16 @@ public class ValidationField {
public boolean getValid(){ public boolean getValid(){
return this.valid; return this.valid;
} }
public String getLastValue() {
return lastValue;
}
public void setLastValue(String lastValue) {
if (!this.lastValue.equals(lastValue)) {
this.valid = false;
this.validating = false;
this.lastValue = lastValue;
}
}
} }