- Adding Fields Validators to validate user entries in synchronous or asynchronous way (Work in progress...)
This commit is contained in:
parent
f5ce56611f
commit
42c50724a2
10 changed files with 207 additions and 13 deletions
|
@ -5,6 +5,7 @@ import android.arch.lifecycle.ViewModelProviders;
|
|||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.text.Editable;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.TextView;
|
||||
|
@ -14,16 +15,20 @@ import java.util.List;
|
|||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.OnClick;
|
||||
import butterknife.OnTextChanged;
|
||||
import cy.agorise.crystalwallet.R;
|
||||
import cy.agorise.crystalwallet.models.AccountSeed;
|
||||
import cy.agorise.crystalwallet.viewmodels.AccountSeedListViewModel;
|
||||
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.views.TransactionListView;
|
||||
|
||||
public class ImportSeedActivity extends AppCompatActivity {
|
||||
public class ImportSeedActivity extends AppCompatActivity implements ImportSeedValidatorListener {
|
||||
|
||||
AccountSeedViewModel accountSeedViewModel;
|
||||
ImportSeedValidator importSeedValidator;
|
||||
|
||||
@BindView(R.id.tvPin)
|
||||
TextView tvPin;
|
||||
|
@ -48,11 +53,20 @@ public class ImportSeedActivity extends AppCompatActivity {
|
|||
ButterKnife.bind(this);
|
||||
|
||||
accountSeedViewModel = ViewModelProviders.of(this).get(AccountSeedViewModel.class);
|
||||
//this.seed = new AccountSeed();
|
||||
importSeedValidator = accountSeedViewModel.getValidator();
|
||||
|
||||
importSeedValidator.setListener(this);
|
||||
}
|
||||
|
||||
@OnTextChanged(value = R.id.etAccountName,
|
||||
callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
|
||||
void afterAccountNameChanged(Editable editable) {
|
||||
this.validator.validateAccountName(editable.getT);
|
||||
}
|
||||
|
||||
@OnClick(R.id.btnImport)
|
||||
public void importSeed(){
|
||||
if (this.validator)
|
||||
AccountSeed seed = new AccountSeed();
|
||||
|
||||
//TODO verify if PIN and PIN confirmation are not null and are the same
|
||||
|
@ -63,4 +77,14 @@ public class ImportSeedActivity extends AppCompatActivity {
|
|||
|
||||
accountSeedViewModel.addSeed(seed);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onValidationSucceeded() {
|
||||
//Clear all errors
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onValidationFailed(String error) {
|
||||
//Show errors
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ import cy.agorise.crystalwallet.enums.CryptoCoin;
|
|||
* Created by Henry Varona on 1/10/2017.
|
||||
*/
|
||||
|
||||
abstract class CryptoNetInfoRequest {
|
||||
public abstract class CryptoNetInfoRequest {
|
||||
protected CryptoCoin coin;
|
||||
protected CryptoNetInfoRequestListener listener;
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ package cy.agorise.crystalwallet.cryptonetinforequests;
|
|||
* Created by Henry Varona on 1/10/2017.
|
||||
*/
|
||||
|
||||
interface CryptoNetInfoRequestListener {
|
||||
public interface CryptoNetInfoRequestListener {
|
||||
|
||||
public void onCarryOut();
|
||||
}
|
||||
|
|
|
@ -10,20 +10,20 @@ import java.util.List;
|
|||
public class CryptoNetInfoRequests {
|
||||
private List<CryptoNetInfoRequest> requests;
|
||||
private List<CryptoNetInfoRequestsListener> listeners;
|
||||
private CryptoNetInfoRequests instance;
|
||||
private static CryptoNetInfoRequests instance;
|
||||
|
||||
private void CryptoNetInfoRequests(){
|
||||
//Private constructor for singleton pattern
|
||||
}
|
||||
|
||||
public CryptoNetInfoRequests getInstance(){
|
||||
if (this.instance == null){
|
||||
this.instance = new CryptoNetInfoRequests();
|
||||
this.requests = new ArrayList<CryptoNetInfoRequest>();
|
||||
this.listeners = new ArrayList<CryptoNetInfoRequestsListener>();
|
||||
public static CryptoNetInfoRequests getInstance(){
|
||||
if (CryptoNetInfoRequests.instance == null){
|
||||
CryptoNetInfoRequests.instance = new CryptoNetInfoRequests();
|
||||
CryptoNetInfoRequests.instance.requests = new ArrayList<CryptoNetInfoRequest>();
|
||||
CryptoNetInfoRequests.instance.listeners = new ArrayList<CryptoNetInfoRequestsListener>();
|
||||
}
|
||||
|
||||
return this.instance;
|
||||
return CryptoNetInfoRequests.instance;
|
||||
}
|
||||
|
||||
public void addRequest(CryptoNetInfoRequest request){
|
||||
|
|
|
@ -4,6 +4,6 @@ package cy.agorise.crystalwallet.cryptonetinforequests;
|
|||
* Created by Henry Varona on 1/10/2017.
|
||||
*/
|
||||
|
||||
interface CryptoNetInfoRequestsListener {
|
||||
public interface CryptoNetInfoRequestsListener {
|
||||
public void onNewRequest(CryptoNetInfoRequest request);
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ import cy.agorise.crystalwallet.enums.CryptoCoin;
|
|||
* Created by Henry Varona on 1/10/2017.
|
||||
*/
|
||||
|
||||
class ValidateImportBitsharesAccountRequest extends CryptoNetInfoRequest {
|
||||
public class ValidateImportBitsharesAccountRequest extends CryptoNetInfoRequest {
|
||||
|
||||
private String accountName;
|
||||
private String mnemonic;
|
||||
|
@ -22,10 +22,12 @@ class ValidateImportBitsharesAccountRequest extends CryptoNetInfoRequest {
|
|||
|
||||
public void setAccountExists(boolean value){
|
||||
this.accountExists = value;
|
||||
this.validate();
|
||||
}
|
||||
|
||||
public void setMnemonicIsCorrect(boolean value){
|
||||
this.mnemonicIsCorrect = value;
|
||||
this.validate();
|
||||
}
|
||||
|
||||
public boolean getAccountExists(){
|
||||
|
|
|
@ -7,8 +7,11 @@ import android.arch.lifecycle.MutableLiveData;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import cy.agorise.crystalwallet.cryptonetinforequests.CryptoNetInfoRequestListener;
|
||||
import cy.agorise.crystalwallet.dao.CrystalDatabase;
|
||||
import cy.agorise.crystalwallet.models.AccountSeed;
|
||||
import cy.agorise.crystalwallet.cryptonetinforequests.ValidateImportBitsharesAccountRequest;
|
||||
import cy.agorise.crystalwallet.viewmodels.validators.ImportSeedValidator;
|
||||
|
||||
/**
|
||||
* Created by Henry Varona on 27/9/2017.
|
||||
|
@ -18,6 +21,7 @@ public class AccountSeedViewModel extends AndroidViewModel {
|
|||
|
||||
private LiveData<AccountSeed> accountSeed;
|
||||
private CrystalDatabase db;
|
||||
private MutableLiveData<ImportSeedValidator> importSeedValidator;
|
||||
|
||||
public AccountSeedViewModel(Application application) {
|
||||
super(application);
|
||||
|
@ -28,6 +32,13 @@ public class AccountSeedViewModel extends AndroidViewModel {
|
|||
this.accountSeed = this.db.accountSeedDao().findById(seedId);
|
||||
}
|
||||
|
||||
public ImportSeedValidator getValidator(){
|
||||
if (this.importSeedValidator == null){
|
||||
this.importSeedValidator = new ImportSeedValidator();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void addSeed(AccountSeed seed){
|
||||
this.db.accountSeedDao().insertAccountSeed(seed);
|
||||
}
|
||||
|
@ -35,4 +46,18 @@ public class AccountSeedViewModel extends AndroidViewModel {
|
|||
public LiveData<AccountSeed> getAccountSeed(){
|
||||
return this.accountSeed;
|
||||
}
|
||||
|
||||
public void validateAccountSeed(){
|
||||
if (this.accountSeed != null){
|
||||
AccountSeed seed = this.accountSeed.getValue();
|
||||
|
||||
ValidateImportBitsharesAccountRequest request = new ValidateImportBitsharesAccountRequest(seed.getName(),seed.getMasterSeed());
|
||||
request.setListener(new CryptoNetInfoRequestListener() {
|
||||
@Override
|
||||
public void onCarryOut() {
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,78 @@
|
|||
package cy.agorise.crystalwallet.viewmodels.validators;
|
||||
|
||||
import android.accounts.Account;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import cy.agorise.crystalwallet.R;
|
||||
import cy.agorise.crystalwallet.cryptonetinforequests.CryptoNetInfoRequestListener;
|
||||
import cy.agorise.crystalwallet.cryptonetinforequests.CryptoNetInfoRequests;
|
||||
import cy.agorise.crystalwallet.cryptonetinforequests.ValidateImportBitsharesAccountRequest;
|
||||
import cy.agorise.crystalwallet.models.AccountSeed;
|
||||
|
||||
/**
|
||||
* Created by Henry Varona on 2/10/2017.
|
||||
*/
|
||||
|
||||
public class ImportSeedValidator {
|
||||
|
||||
private ImportSeedValidatorListener listener;
|
||||
|
||||
private List<ValidationField> validationFields;
|
||||
private AccountSeed accountSeed;
|
||||
|
||||
private boolean isValid = false;
|
||||
|
||||
public ImportSeedValidator(AccountSeed seed){
|
||||
this.accountSeed = seed;
|
||||
this.validationFields = new ArrayList<ValidationField>();
|
||||
//this.validationFields.add(new ValidationField("pin"));
|
||||
//this.validationFields.add(new ValidationField("pinConfirmation"));
|
||||
this.validationFields.add(new ValidationField("accountname"));
|
||||
}
|
||||
|
||||
public void setListener(ImportSeedValidatorListener listener){
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
public void validate(){
|
||||
//validatePin();
|
||||
//validatePinConfirmation();
|
||||
validateAccountName();
|
||||
}
|
||||
|
||||
public ValidationField getValidationField(String name){
|
||||
for (int i=0;i<this.validationFields.size();i++){
|
||||
if (this.validationFields.get(i).getName().equals(name)){
|
||||
return this.validationFields.get(i);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
//public validatePin(){
|
||||
|
||||
//}
|
||||
|
||||
public void validateAccountName(String accountName){
|
||||
final ValidationField validationField = getValidationField("accountname");
|
||||
|
||||
if (this.accountSeed != null){
|
||||
final ValidateImportBitsharesAccountRequest request = new ValidateImportBitsharesAccountRequest(this.accountSeed.getName(),this.accountSeed.getMasterSeed());
|
||||
request.setListener(new CryptoNetInfoRequestListener() {
|
||||
@Override
|
||||
public void onCarryOut() {
|
||||
if (!request.getAccountExists()){
|
||||
validationField.setValid(false);
|
||||
validationField.setMessage(R.string.account_name_not_exist);
|
||||
} else {
|
||||
validationField.setValid(true);
|
||||
}
|
||||
}
|
||||
});
|
||||
CryptoNetInfoRequests.getInstance().addRequest(request);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package cy.agorise.crystalwallet.viewmodels.validators;
|
||||
|
||||
/**
|
||||
* Created by Henry Varona on 2/10/2017.
|
||||
*/
|
||||
|
||||
public interface ImportSeedValidatorListener {
|
||||
|
||||
public void onValidationSucceeded();
|
||||
public void onValidationFailed(String error);
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
package cy.agorise.crystalwallet.viewmodels.validators;
|
||||
|
||||
/**
|
||||
* Created by Henry Varona on 2/10/2017.
|
||||
*/
|
||||
|
||||
public class ValidationField {
|
||||
|
||||
public String name;
|
||||
public String lastValue;
|
||||
public String message;
|
||||
public boolean validating;
|
||||
public boolean valid;
|
||||
|
||||
public ValidationField(String name){
|
||||
this.name = name;
|
||||
this.lastValue = "";
|
||||
this.message = "";
|
||||
this.validating = false;
|
||||
this.valid = false;
|
||||
}
|
||||
|
||||
public String getName(){
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void startValidating(){
|
||||
this.validating = true;
|
||||
}
|
||||
|
||||
public void stopValidating(){
|
||||
this.validating = false;
|
||||
}
|
||||
|
||||
public void setValid(boolean newValue){
|
||||
this.valid = newValue;
|
||||
}
|
||||
|
||||
public void setMessage(String newValue){
|
||||
this.message = newValue;
|
||||
}
|
||||
|
||||
public String getMessage(){
|
||||
return this.message;
|
||||
}
|
||||
|
||||
public boolean getValidating(){
|
||||
return this.validating;
|
||||
}
|
||||
|
||||
public boolean getValid(){
|
||||
return this.valid;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue