From e63c2b649fee7dee284f3d992407f3c4ab3e94a6 Mon Sep 17 00:00:00 2001 From: Javier Varona Date: Sun, 7 Jan 2018 20:16:03 -0400 Subject: [PATCH] - Create Seed Activity created --- .../activities/CreateSeedActivity.java | 167 +++++++++++++ .../validators/CreateSeedValidator.java | 25 ++ ...ccountNameDoesntExistsValidationField.java | 45 ++++ app/src/main/res/layout/create_seed.xml | 230 ++++++++++++++++++ 4 files changed, 467 insertions(+) create mode 100644 app/src/main/java/cy/agorise/crystalwallet/activities/CreateSeedActivity.java create mode 100644 app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/CreateSeedValidator.java create mode 100644 app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/validationfields/BitsharesAccountNameDoesntExistsValidationField.java create mode 100644 app/src/main/res/layout/create_seed.xml diff --git a/app/src/main/java/cy/agorise/crystalwallet/activities/CreateSeedActivity.java b/app/src/main/java/cy/agorise/crystalwallet/activities/CreateSeedActivity.java new file mode 100644 index 0000000..b73db04 --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/activities/CreateSeedActivity.java @@ -0,0 +1,167 @@ +package cy.agorise.crystalwallet.activities; + +import android.arch.lifecycle.ViewModelProviders; +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; + +import butterknife.BindView; +import butterknife.ButterKnife; +import butterknife.OnClick; +import butterknife.OnTextChanged; +import cy.agorise.crystalwallet.R; +import cy.agorise.crystalwallet.enums.SeedType; +import cy.agorise.crystalwallet.models.AccountSeed; +import cy.agorise.crystalwallet.models.CryptoNetAccount; +import cy.agorise.crystalwallet.models.GrapheneAccountInfo; +import cy.agorise.crystalwallet.viewmodels.AccountSeedViewModel; +import cy.agorise.crystalwallet.viewmodels.CryptoNetAccountViewModel; +import cy.agorise.crystalwallet.viewmodels.GrapheneAccountInfoViewModel; +import cy.agorise.crystalwallet.viewmodels.validators.CreateSeedValidator; +import cy.agorise.crystalwallet.viewmodels.validators.ImportSeedValidator; +import cy.agorise.crystalwallet.viewmodels.validators.UIValidatorListener; +import cy.agorise.crystalwallet.viewmodels.validators.validationfields.ValidationField; + +public class CreateSeedActivity extends AppCompatActivity implements UIValidatorListener { + + AccountSeedViewModel accountSeedViewModel; + CreateSeedValidator createSeedValidator; + + @BindView(R.id.etPin) + EditText etPin; + @BindView(R.id.tvPinError) + TextView tvPinError; + + @BindView(R.id.etPinConfirmation) + EditText etPinConfirmation; + @BindView(R.id.tvPinConfirmationError) + TextView tvPinConfirmationError; + + @BindView(R.id.tvSeedWords) + TextView tvSeedWords; + + @BindView (R.id.etAccountName) + EditText etAccountName; + @BindView(R.id.tvAccountNameError) + TextView tvAccountNameError; + + @BindView(R.id.btnCreate) + Button btnCreate; + + @BindView(R.id.btnCancel) + Button btnCancel; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.create_seed); + + ButterKnife.bind(this); + + btnCreate.setEnabled(false); + accountSeedViewModel = ViewModelProviders.of(this).get(AccountSeedViewModel.class); + createSeedValidator = new CreateSeedValidator(this.getApplicationContext(),etPin,etPinConfirmation,etAccountName,tvSeedWords); + createSeedValidator.setListener(this); + } + + @OnTextChanged(value = R.id.etPin, + callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED) + void afterPinChanged(Editable editable) { + this.createSeedValidator.validate(); + } + + @OnTextChanged(value = R.id.etPinConfirmation, + callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED) + void afterPinConfirmationChanged(Editable editable) { + this.createSeedValidator.validate(); + } + + @OnTextChanged(value = R.id.etSeedWords, + callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED) + void afterSeedWordsChanged(Editable editable) { + this.createSeedValidator.validate(); + } + + + @OnTextChanged(value = R.id.etAccountName, + callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED) + void afterAccountNameChanged(Editable editable) { + this.createSeedValidator.validate(); + } + + @OnClick(R.id.btnCancel) + public void cancel(){ + this.finish(); + } + + @OnClick(R.id.btnImport) + public void createSeed(){ + if (this.createSeedValidator.isValid()) { + AccountSeed seed = new AccountSeed(); + + //TODO verify if words are already in the db + //TODO check if name has been asigned to other seed + seed.setMasterSeed(tvSeedWords.getText().toString()); + seed.setName(etAccountName.getText().toString()); + seed.setType(SeedType.BIP39); + + accountSeedViewModel.addSeed(seed); + + CryptoNetAccountViewModel cryptoNetAccountViewModel = ViewModelProviders.of(this).get(CryptoNetAccountViewModel.class); + GrapheneAccountInfoViewModel grapheneAccountInfoViewModel = ViewModelProviders.of(this).get(GrapheneAccountInfoViewModel.class); + CryptoNetAccount cryptoNetAccount = new CryptoNetAccount(); + cryptoNetAccount.setSeedId(seed.getId()); + cryptoNetAccount.setAccountIndex(0); + cryptoNetAccount.setCryptoNet(cy.agorise.crystalwallet.enums.CryptoNet.BITSHARES); + cryptoNetAccountViewModel.addCryptoNetAccount(cryptoNetAccount); + GrapheneAccountInfo grapheneAccountInfo = new GrapheneAccountInfo(cryptoNetAccount.getId()); + grapheneAccountInfo.setName(etAccountName.getText().toString()); + grapheneAccountInfoViewModel.addGrapheneAccountInfo(grapheneAccountInfo); + + this.finish(); + } + } + + @Override + public void onValidationSucceeded(final ValidationField field) { + final CreateSeedActivity activity = this; + + activity.runOnUiThread(new Runnable() { + public void run() { + + if (field.getView() == etPin) { + tvPinError.setText(""); + } else if (field.getView() == etPinConfirmation){ + tvPinConfirmationError.setText(""); + } else if (field.getView() == etAccountName){ + tvAccountNameError.setText(""); + } else if (field.getView() == etSeedWords){ + tvSeedWordsError.setText(""); + } + + if (activity.importSeedValidator.isValid()){ + btnImport.setEnabled(true); + } else { + btnImport.setEnabled(false); + } + + } + }); + } + + @Override + public void onValidationFailed(ValidationField field) { + if (field.getView() == etPin) { + tvPinError.setText(field.getMessage()); + } else if (field.getView() == etPinConfirmation){ + tvPinConfirmationError.setText(field.getMessage()); + } else if (field.getView() == etAccountName){ + tvAccountNameError.setText(field.getMessage()); + } else if (field.getView() == etSeedWords){ + tvSeedWordsError.setText(field.getMessage()); + } + } +} diff --git a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/CreateSeedValidator.java b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/CreateSeedValidator.java new file mode 100644 index 0000000..aff977d --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/CreateSeedValidator.java @@ -0,0 +1,25 @@ +package cy.agorise.crystalwallet.viewmodels.validators; + +import android.content.Context; +import android.widget.EditText; + +import cy.agorise.crystalwallet.viewmodels.validators.validationfields.BitsharesAccountMnemonicValidationField; +import cy.agorise.crystalwallet.viewmodels.validators.validationfields.BitsharesAccountNameDoesntExistsValidationField; +import cy.agorise.crystalwallet.viewmodels.validators.validationfields.BitsharesAccountNameValidationField; +import cy.agorise.crystalwallet.viewmodels.validators.validationfields.PinConfirmationValidationField; +import cy.agorise.crystalwallet.viewmodels.validators.validationfields.PinValidationField; + +/** + * Created by Henry Varona on 2/10/2017. + */ + +public class CreateSeedValidator extends UIValidator { + + public CreateSeedValidator(Context context, EditText pinEdit, EditText pinConfirmationEdit, EditText bitsharesAccountNameEdit, TextView mnemonicTextView){ + super(context); + this.addField(new PinValidationField(pinEdit)); + this.addField(new PinConfirmationValidationField(pinEdit,pinConfirmationEdit)); + this.addField(new BitsharesAccountNameDoesntExistsValidationField(bitsharesAccountNameEdit)); + this.addField(new BitsharesAccountMnemonicValidationField(mnemonicTextView)); + } +} diff --git a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/validationfields/BitsharesAccountNameDoesntExistsValidationField.java b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/validationfields/BitsharesAccountNameDoesntExistsValidationField.java new file mode 100644 index 0000000..152effb --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/validationfields/BitsharesAccountNameDoesntExistsValidationField.java @@ -0,0 +1,45 @@ +package cy.agorise.crystalwallet.viewmodels.validators.validationfields; + +import android.widget.EditText; + +import cy.agorise.crystalwallet.R; +import cy.agorise.crystalwallet.cryptonetinforequests.CryptoNetInfoRequestListener; +import cy.agorise.crystalwallet.cryptonetinforequests.CryptoNetInfoRequests; +import cy.agorise.crystalwallet.cryptonetinforequests.ValidateExistBitsharesAccountRequest; + +/** + * Created by Henry Varona on 7/10/2017. + */ + +public class BitsharesAccountNameDoesntExistsValidationField extends ValidationField { + + private EditText accountNameField; + + public BitsharesAccountNameDoesntExistsValidationField(EditText accountNameField){ + super(accountNameField); + this.accountNameField = accountNameField; + } + + public void validate(){ + final String newValue = accountNameField.getText().toString(); + this.setLastValue(newValue); + this.startValidating(); + final ValidationField field = this; + + final ValidateExistBitsharesAccountRequest request = new ValidateExistBitsharesAccountRequest(newValue); + request.setListener(new CryptoNetInfoRequestListener() { + @Override + public void onCarryOut() { + if (request.getAccountExists()){ + setValidForValue(newValue, false); + setMessage(validator.getContext().getResources().getString(R.string.account_name_already_exist)); + validator.validationFailed(field); + } else { + setValidForValue(newValue, true); + validator.validationSucceeded(field); + } + } + }); + CryptoNetInfoRequests.getInstance().addRequest(request); + } +} diff --git a/app/src/main/res/layout/create_seed.xml b/app/src/main/res/layout/create_seed.xml new file mode 100644 index 0000000..c8adc6d --- /dev/null +++ b/app/src/main/res/layout/create_seed.xml @@ -0,0 +1,230 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +