From 3008415316331da63f05e87515c228c7825924d5 Mon Sep 17 00:00:00 2001 From: Severiano Jaramillo Date: Thu, 12 Jul 2018 10:32:01 -0500 Subject: [PATCH] Improve create/modify contact UI --- .../activities/CreateContactActivity.java | 126 +++++----- .../validators/CreateContactValidator.java | 3 +- .../validators/ModifyContactValidator.java | 3 +- app/src/main/res/layout/activity_account.xml | 146 ----------- .../res/layout/activity_create_contact.xml | 228 ++++++------------ .../res/layout/activity_existing_account.xml | 34 --- .../res/layout/contact_address_list_item.xml | 29 +-- app/src/main/res/layout/create_seed.xml | 40 --- app/src/main/res/values/dimens.xml | 5 + app/src/main/res/values/strings.xml | 4 +- app/src/main/res/values/styles.xml | 14 +- 11 files changed, 152 insertions(+), 480 deletions(-) delete mode 100644 app/src/main/res/layout/activity_account.xml delete mode 100644 app/src/main/res/layout/activity_existing_account.xml diff --git a/app/src/main/java/cy/agorise/crystalwallet/activities/CreateContactActivity.java b/app/src/main/java/cy/agorise/crystalwallet/activities/CreateContactActivity.java index 0791d40..373d41c 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/activities/CreateContactActivity.java +++ b/app/src/main/java/cy/agorise/crystalwallet/activities/CreateContactActivity.java @@ -4,15 +4,14 @@ import android.arch.lifecycle.LiveData; import android.arch.lifecycle.Observer; import android.arch.lifecycle.ViewModelProviders; import android.support.annotation.Nullable; +import android.support.design.widget.TextInputEditText; +import android.support.design.widget.TextInputLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; -import android.text.Editable; import android.view.View; import android.widget.Button; -import android.widget.EditText; -import android.widget.TextView; import java.util.ArrayList; import java.util.List; @@ -33,20 +32,14 @@ import cy.agorise.crystalwallet.views.ContactAddressListAdapter; public class CreateContactActivity extends AppCompatActivity implements UIValidatorListener { - @BindView(R.id.etName) - EditText etName; - @BindView(R.id.tvNameError) - TextView tvNameError; - @BindView(R.id.etEmail) - EditText etEmail; - @BindView(R.id.tvEmailError) - TextView tvEmailError; + @BindView(R.id.tilName) + TextInputLayout tilName; + @BindView(R.id.tietName) + TextInputEditText tietName; @BindView(R.id.btnCancel) Button btnCancel; @BindView(R.id.btnCreate) Button btnCreate; - @BindView(R.id.btnModify) - Button btnModify; @BindView(R.id.rvContactAddresses) RecyclerView rvContactAddresses; @BindView(R.id.btnAddAddress) @@ -59,6 +52,8 @@ public class CreateContactActivity extends AppCompatActivity implements UIValida Contact contact; + long contactId; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -71,10 +66,7 @@ public class CreateContactActivity extends AppCompatActivity implements UIValida rvContactAddresses.setLayoutManager(new LinearLayoutManager(getApplicationContext())); rvContactAddresses.setAdapter(listAdapter); - long contactId = this.getIntent().getLongExtra("CONTACT_ID",-1); - - btnCreate.setVisibility(View.GONE); - btnModify.setVisibility(View.GONE); + contactId = getIntent().getLongExtra("CONTACT_ID",-1); if (contactId >= 0){ final ContactViewModel contactViewModel = ViewModelProviders.of(this).get(ContactViewModel.class); @@ -88,8 +80,7 @@ public class CreateContactActivity extends AppCompatActivity implements UIValida public void onChanged(@Nullable Contact contactChanged) { if (contactChanged != null){ contact = contactChanged; - etName.setText(contact.getName()); - etEmail.setText(contact.getEmail()); + tietName.setText(contact.getName()); LiveData> contactAddresses = contactViewModel.getContactAddresses(); @@ -102,9 +93,10 @@ public class CreateContactActivity extends AppCompatActivity implements UIValida } }); - modifyContactValidator = new ModifyContactValidator(thisActivity.getApplicationContext(),contact,etName,etEmail); + modifyContactValidator = new ModifyContactValidator( + thisActivity.getApplicationContext(), contact, tietName); modifyContactValidator.setListener(thisActivity); - btnModify.setVisibility(View.VISIBLE); + btnCreate.setText(R.string.modify); } else { //No contact was found, this will exit the activity finish(); @@ -112,9 +104,9 @@ public class CreateContactActivity extends AppCompatActivity implements UIValida } }); } else { - contactAddressList = new ArrayList(); + contactAddressList = new ArrayList<>(); listAdapter.submitList(contactAddressList); - createContactValidator = new CreateContactValidator(this.getApplicationContext(),etName,etEmail); + createContactValidator = new CreateContactValidator(this.getApplicationContext(),tietName); createContactValidator.setListener(this); btnCreate.setVisibility(View.VISIBLE); @@ -139,19 +131,12 @@ public class CreateContactActivity extends AppCompatActivity implements UIValida return false; } - @OnTextChanged(value = R.id.etName, + @OnTextChanged(value = R.id.tietName, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED) - void afterContactNameChanged(Editable editable) { + void afterContactNameChanged() { this.validate(); } - @OnTextChanged(value = R.id.etEmail, - callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED) - void afterEmailChanged(Editable editable) { - this.validate(); - } - - @OnClick(R.id.btnAddAddress) public void addAddress(){ ContactAddress newContactAddress = new ContactAddress(); @@ -164,42 +149,43 @@ public class CreateContactActivity extends AppCompatActivity implements UIValida this.finish(); } - @OnClick(R.id.btnModify) - public void modifyContact(){ - if (this.modifyContactValidator.isValid()) { - this.contact.setName(etName.getText().toString()); - this.contact.setEmail(etEmail.getText().toString()); - this.contact.clearAddresses(); - - for (ContactAddress contactAddress : contactAddressList){ - this.contact.addAddress(contactAddress); - } - - ContactViewModel contactViewModel = ViewModelProviders.of(this).get(ContactViewModel.class); - if (contactViewModel.modifyContact(this.contact)){ - this.finish(); - } else { - this.modifyContactValidator.validate(); - } - } - } - @OnClick(R.id.btnCreate) - public void createContact(){ - if (this.createContactValidator.isValid()) { - Contact newContact = new Contact(); - newContact.setName(etName.getText().toString()); - newContact.setEmail(etEmail.getText().toString()); + public void createOrModifyContact(){ + if(contactId >= 0) { + // Modifying existing contact - for (ContactAddress contactAddress : contactAddressList){ - newContact.addAddress(contactAddress); + if (this.modifyContactValidator.isValid()) { + this.contact.setName(tietName.getText().toString()); + this.contact.clearAddresses(); + + for (ContactAddress contactAddress : contactAddressList){ + this.contact.addAddress(contactAddress); + } + + ContactViewModel contactViewModel = ViewModelProviders.of(this).get(ContactViewModel.class); + if (contactViewModel.modifyContact(this.contact)){ + this.finish(); + } else { + this.modifyContactValidator.validate(); + } } + } else { + // Creating a new contact - ContactViewModel contactViewModel = ViewModelProviders.of(this).get(ContactViewModel.class); - if (contactViewModel.addContact(newContact)){ - this.finish(); - } else { - createContactValidator.validate(); + if (this.createContactValidator.isValid()) { + Contact newContact = new Contact(); + newContact.setName(tietName.getText().toString()); + + for (ContactAddress contactAddress : contactAddressList){ + newContact.addAddress(contactAddress); + } + + ContactViewModel contactViewModel = ViewModelProviders.of(this).get(ContactViewModel.class); + if (contactViewModel.addContact(newContact)){ + this.finish(); + } else { + createContactValidator.validate(); + } } } } @@ -211,10 +197,8 @@ public class CreateContactActivity extends AppCompatActivity implements UIValida activity.runOnUiThread(new Runnable() { public void run() { - if (field.getView() == etName) { - tvNameError.setText(""); - } else if (field.getView() == etEmail) { - tvEmailError.setText(""); + if (field.getView() == tietName) { + tilName.setError(""); } if (activity.isValid()){ @@ -232,10 +216,8 @@ public class CreateContactActivity extends AppCompatActivity implements UIValida @Override public void run() { - if (field.getView() == etName) { - tvNameError.setText(field.getMessage()); - } else if (field.getView() == etEmail) { - tvEmailError.setText(field.getMessage()); + if (field.getView() == tietName) { + tilName.setError(field.getMessage()); } } }); diff --git a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/CreateContactValidator.java b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/CreateContactValidator.java index 1b84e43..2c91b83 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/CreateContactValidator.java +++ b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/CreateContactValidator.java @@ -15,9 +15,8 @@ import cy.agorise.crystalwallet.viewmodels.validators.validationfields.PinValida public class CreateContactValidator extends UIValidator { - public CreateContactValidator(Context context, EditText nameEdit, EditText emailEdit){ + public CreateContactValidator(Context context, EditText nameEdit){ super(context); this.addField(new ContactNameValidationField(nameEdit)); - this.addField(new EmailValidationField(emailEdit)); } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/ModifyContactValidator.java b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/ModifyContactValidator.java index ec640fd..b629434 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/ModifyContactValidator.java +++ b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/ModifyContactValidator.java @@ -13,9 +13,8 @@ import cy.agorise.crystalwallet.viewmodels.validators.validationfields.EmailVali public class ModifyContactValidator extends UIValidator { - public ModifyContactValidator(Context context, Contact contact, EditText nameEdit, EditText emailEdit){ + public ModifyContactValidator(Context context, Contact contact, EditText nameEdit){ super(context); this.addField(new ContactNameValidationField(nameEdit, contact)); - this.addField(new EmailValidationField(emailEdit)); } } diff --git a/app/src/main/res/layout/activity_account.xml b/app/src/main/res/layout/activity_account.xml deleted file mode 100644 index 285ab12..0000000 --- a/app/src/main/res/layout/activity_account.xml +++ /dev/null @@ -1,146 +0,0 @@ - - - - - - - - - - - - - - - - -