- Create Seed Activity created

master
Javier Varona 2018-01-07 20:16:03 -04:00
parent b5fafac118
commit e63c2b649f
4 changed files with 467 additions and 0 deletions

View File

@ -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());
}
}
}

View File

@ -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));
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,230 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="@dimen/activity_vertical_margin">
<TextView
android:id="@+id/tvPin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginTop="10dp"
android:text="@string/txt_6_digits_pin"
android:textStyle="bold" />
<EditText
android:id="@+id/etPin"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:background="@drawable/edittext_bg"
android:inputType="number"
android:maxLines="1"
android:textColor="@color/black" />
<TextView
android:id="@+id/tvPinError"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginTop="10dp"
android:textColor="@color/red"
android:textStyle="bold" />
<TextView
android:id="@+id/tvPinConfirmation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginTop="10dp"
android:text="@string/txt_6_digits_pin_confirm"
android:textStyle="bold" />
<EditText
android:id="@+id/etPinConfirmation"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:background="@drawable/edittext_bg"
android:inputType="number"
android:maxLines="1"
android:singleLine="true"
android:textColor="@color/black" />
<TextView
android:id="@+id/tvPinConfirmationError"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginTop="10dp"
android:textColor="@color/red"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginTop="10dp"
android:text="@string/seed_words"
android:textStyle="bold" />
<TextView
android:id="@+id/tvSeedWords"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:background="@drawable/edittext_bg"
android:gravity="top"
android:inputType="textMultiLine"
android:textColor="@color/black" />
<TextView
android:id="@+id/tvSeedWordsError"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginTop="10dp"
android:textColor="@color/red"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginTop="10dp"
android:text="@string/txt_account_name"
android:textStyle="bold" />
<EditText
android:id="@+id/etAccountName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:background="@drawable/edittext_bg"
android:gravity="top"
android:inputType="textMultiLine"
android:textColor="@color/black" />
<TextView
android:id="@+id/tvAccountNameError"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginTop="10dp"
android:textColor="@color/red"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginTop="10dp"
android:text="@string/txt_brain_key_info"
android:textSize="15dp"
android:visibility="gone" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginTop="10dp"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:background="@color/pink"
android:text="@string/cancel"
android:textColor="@color/white" />
<Button
android:id="@+id/btnCreate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:background="@color/green"
android:padding="10dp"
android:text="@string/create_wallet"
android:textColor="@color/white" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:gravity="bottom"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="@color/bottomBarColor"
android:gravity="bottom"
android:orientation="horizontal">
<TextView
android:id="@+id/tvAppVersion_brain_key_activity"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="@string/v_1_0_beta" />
<TextView
android:id="@+id/tvBlockNumberHead_brain_key_activity"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:gravity="center"
android:text="@string/block_number" />
<ImageView
android:id="@+id/ivSocketConnected_brain_key_activity"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.5" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.5"
android:src="@drawable/icon_setting"
android:visibility="invisible" />
</LinearLayout>
</LinearLayout>
</LinearLayout>