From 4434ad40a5ac21540c730da3e5215243ebcac9ac Mon Sep 17 00:00:00 2001 From: Javier Varona Date: Sun, 22 Oct 2017 14:17:09 -0400 Subject: [PATCH] - Now the seed import creates the bitshares account associated - The balance view is more readable --- .../activities/ImportSeedActivity.java | 74 ++- .../dao/CryptoNetAccountDao.java | 3 +- .../fragments/BalanceFragment.java | 2 +- .../service/CrystalWalletService.java | 28 +- .../viewmodels/AccountSeedViewModel.java | 3 +- .../viewmodels/CryptoNetAccountViewModel.java | 40 ++ .../GrapheneAccountInfoViewModel.java | 40 ++ app/src/main/res/layout/balance_list_item.xml | 5 +- .../layout/crypto_coin_balance_list_item.xml | 1 + app/src/main/res/layout/import_seed.xml | 10 + app/src/main/res/layout/send_transaction.xml | 629 ++++++++++++++++++ 11 files changed, 814 insertions(+), 21 deletions(-) create mode 100644 app/src/main/java/cy/agorise/crystalwallet/viewmodels/CryptoNetAccountViewModel.java create mode 100644 app/src/main/java/cy/agorise/crystalwallet/viewmodels/GrapheneAccountInfoViewModel.java create mode 100644 app/src/main/res/layout/send_transaction.xml diff --git a/app/src/main/java/cy/agorise/crystalwallet/activities/ImportSeedActivity.java b/app/src/main/java/cy/agorise/crystalwallet/activities/ImportSeedActivity.java index 15fd0b7..b7421c6 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/activities/ImportSeedActivity.java +++ b/app/src/main/java/cy/agorise/crystalwallet/activities/ImportSeedActivity.java @@ -1,5 +1,6 @@ package cy.agorise.crystalwallet.activities; +import android.app.Activity; import android.arch.lifecycle.ViewModelProviders; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; @@ -13,11 +14,18 @@ import butterknife.ButterKnife; import butterknife.OnClick; import butterknife.OnTextChanged; import cy.agorise.crystalwallet.R; +import cy.agorise.crystalwallet.enums.CryptoNet; +import cy.agorise.crystalwallet.enums.SeedType; import cy.agorise.crystalwallet.models.AccountSeed; +import cy.agorise.crystalwallet.models.CryptoNetAccount; +import cy.agorise.crystalwallet.models.GrapheneAccount; +import cy.agorise.crystalwallet.models.GrapheneAccountInfo; import cy.agorise.crystalwallet.viewmodels.AccountSeedViewModel; +import cy.agorise.crystalwallet.viewmodels.CryptoNetAccountViewModel; import cy.agorise.crystalwallet.viewmodels.validators.ImportSeedValidator; import cy.agorise.crystalwallet.viewmodels.validators.UIValidatorListener; import cy.agorise.crystalwallet.viewmodels.validators.ValidationField; +import cy.agorise.crystalwallet.views.CryptoNetBalanceListView; public class ImportSeedActivity extends AppCompatActivity implements UIValidatorListener { @@ -36,6 +44,8 @@ public class ImportSeedActivity extends AppCompatActivity implements UIValidator @BindView(R.id.etSeedWords) EditText etSeedWords; + @BindView(R.id.tvSeedWordsError) + TextView tvSeedWordsError; @BindView (R.id.etAccountName) EditText etAccountName; @@ -45,6 +55,9 @@ public class ImportSeedActivity extends AppCompatActivity implements UIValidator @BindView(R.id.btnImport) Button btnImport; + @BindView(R.id.btnCancel) + Button btnCancel; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -70,41 +83,74 @@ public class ImportSeedActivity extends AppCompatActivity implements UIValidator this.importSeedValidator.validate(); } + @OnTextChanged(value = R.id.etSeedWords, + callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED) + void afterSeedWordsChanged(Editable editable) { + this.importSeedValidator.validate(); + } + + @OnTextChanged(value = R.id.etAccountName, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED) void afterAccountNameChanged(Editable editable) { this.importSeedValidator.validate(); } + @OnClick(R.id.btnCancel) + public void cancel(){ + this.finish(); + } + @OnClick(R.id.btnImport) public void importSeed(){ if (this.importSeedValidator.isValid()) { AccountSeed seed = new AccountSeed(); - //TODO verify if PIN and PIN confirmation are not null and are the same //TODO verify if words are already in the db //TODO check if name has been asigned to other seed seed.setMasterSeed(etSeedWords.getText().toString()); seed.setName(etAccountName.getText().toString()); + seed.setType(SeedType.BRAINKEY); accountSeedViewModel.addSeed(seed); - //TODO get back to the previous activity + + CryptoNetAccountViewModel cryptoNetAccountViewModel = ViewModelProviders.of(this).get(CryptoNetAccountViewModel.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()); + + this.finish(); } } @Override - public void onValidationSucceeded(ValidationField field) { - if (field.getView() == etPin) { - tvPinError.setText(""); - } else if (field.getView() == etPinConfirmation){ - tvPinConfirmationError.setText(""); - } else if (field.getView() == etAccountName){ - tvAccountNameError.setText(""); - } + public void onValidationSucceeded(final ValidationField field) { + final ImportSeedActivity activity = this; - if (this.importSeedValidator.isValid()){ - btnImport.setEnabled(true); - } + 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 @@ -115,6 +161,8 @@ public class ImportSeedActivity extends AppCompatActivity implements UIValidator 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/dao/CryptoNetAccountDao.java b/app/src/main/java/cy/agorise/crystalwallet/dao/CryptoNetAccountDao.java index 1e2b18e..4536405 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/dao/CryptoNetAccountDao.java +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/CryptoNetAccountDao.java @@ -21,7 +21,7 @@ import cy.agorise.crystalwallet.models.CryptoNetBalance; public interface CryptoNetAccountDao { @Query("SELECT * FROM crypto_net_account") - List getAll(); + LiveData> getAll(); @Query("SELECT * FROM crypto_net_account WHERE id = :accountId") LiveData getById( long accountId); @@ -31,5 +31,4 @@ public interface CryptoNetAccountDao { @Insert(onConflict = OnConflictStrategy.REPLACE) public long[] insertCryptoNetAccount(CryptoNetAccount... accounts); - } diff --git a/app/src/main/java/cy/agorise/crystalwallet/fragments/BalanceFragment.java b/app/src/main/java/cy/agorise/crystalwallet/fragments/BalanceFragment.java index 0ef4437..39a12c6 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/fragments/BalanceFragment.java +++ b/app/src/main/java/cy/agorise/crystalwallet/fragments/BalanceFragment.java @@ -55,7 +55,7 @@ public class BalanceFragment extends Fragment { vCryptoNetBalanceListView.setData(null, this); final Fragment fragment = this; - + cryptoNetBalanceData.observe(this, new Observer>() { @Override public void onChanged(List cryptoNetBalances) { diff --git a/app/src/main/java/cy/agorise/crystalwallet/service/CrystalWalletService.java b/app/src/main/java/cy/agorise/crystalwallet/service/CrystalWalletService.java index 121b4bd..48b3fa5 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/service/CrystalWalletService.java +++ b/app/src/main/java/cy/agorise/crystalwallet/service/CrystalWalletService.java @@ -2,15 +2,22 @@ package cy.agorise.crystalwallet.service; import android.app.Service; +import android.arch.lifecycle.LiveData; +import android.arch.lifecycle.Observer; import android.content.Intent; import android.os.Handler; import android.os.IBinder; import android.os.Looper; import android.os.Message; +import android.support.annotation.Nullable; import android.util.Log; +import java.util.List; + import cy.agorise.crystalwallet.cryptonetinforequests.CryptoNetInfoRequests; +import cy.agorise.crystalwallet.dao.CrystalDatabase; import cy.agorise.crystalwallet.manager.BitsharesAccountManager; +import cy.agorise.crystalwallet.models.CryptoNetAccount; /** * Created by Henry Varona on 3/10/2017. @@ -43,17 +50,32 @@ public class CrystalWalletService extends Service { } public void loadAccountTransactions(){ - this.keepLoadingAccountTransactions = true; + /*this.keepLoadingAccountTransactions = true; + final CrystalWalletService thisService = this; - while(this.keepLoadingAccountTransactions){ + CrystalDatabase db = CrystalDatabase.getAppDatabase(this); + final LiveData> cryptoNetAccountList = db.cryptoNetAccountDao().getAll(); + cryptoNetAccountList.observe(this, new Observer>() { + @Override + public void onChanged(@Nullable List cryptoNetAccounts) { + for(CryptoNetAccount nextAccount : cryptoNetAccountList.getValue()) { + bitsharesAccountManager.loadAccountFromDB(nextAccount,thisService); + } + } + });*/ + + + + /*while(this.keepLoadingAccountTransactions){ try{ Log.i("Crystal Service","Searching for transactions..."); + this.bitsharesAccountManager.loadAccountFromDB(); Thread.sleep(60000);//Sleep for 1 minutes // TODO search for accounts and make managers find new transactions } catch (InterruptedException e) { Thread.currentThread().interrupt(); } - } + }*/ } @Override diff --git a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/AccountSeedViewModel.java b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/AccountSeedViewModel.java index 1632798..e068905 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/AccountSeedViewModel.java +++ b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/AccountSeedViewModel.java @@ -34,7 +34,8 @@ public class AccountSeedViewModel extends AndroidViewModel { } public void addSeed(AccountSeed seed){ - this.db.accountSeedDao().insertAccountSeed(seed); + long newId = this.db.accountSeedDao().insertAccountSeed(seed); + seed.setId(newId); } public LiveData getAccountSeed(){ diff --git a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/CryptoNetAccountViewModel.java b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/CryptoNetAccountViewModel.java new file mode 100644 index 0000000..e0fcbaf --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/CryptoNetAccountViewModel.java @@ -0,0 +1,40 @@ +package cy.agorise.crystalwallet.viewmodels; + +import android.app.Application; +import android.arch.lifecycle.AndroidViewModel; +import android.arch.lifecycle.LiveData; + +import java.util.List; + +import cy.agorise.crystalwallet.dao.CrystalDatabase; +import cy.agorise.crystalwallet.models.AccountSeed; +import cy.agorise.crystalwallet.models.CryptoNetAccount; + +/** + * Created by Henry Varona on 21/10/2017. + */ + +public class CryptoNetAccountViewModel extends AndroidViewModel { + + private LiveData cryptoNetAccount; + private CrystalDatabase db; + + public CryptoNetAccountViewModel(Application application) { + super(application); + this.db = CrystalDatabase.getAppDatabase(application.getApplicationContext()); + } + + public void loadCryptoNetAccount(int accountId){ + this.cryptoNetAccount = this.db.cryptoNetAccountDao().getById(accountId); + } + + public void addCryptoNetAccount(CryptoNetAccount account){ + long newId = this.db.cryptoNetAccountDao().insertCryptoNetAccount(account)[0]; + account.setId(newId); + } + + public LiveData getCryptoNetAccount(){ + return this.cryptoNetAccount; + } + +} diff --git a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/GrapheneAccountInfoViewModel.java b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/GrapheneAccountInfoViewModel.java new file mode 100644 index 0000000..950d563 --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/GrapheneAccountInfoViewModel.java @@ -0,0 +1,40 @@ +package cy.agorise.crystalwallet.viewmodels; + +import android.app.Application; +import android.arch.lifecycle.AndroidViewModel; +import android.arch.lifecycle.LiveData; + +import cy.agorise.crystalwallet.dao.CrystalDatabase; +import cy.agorise.crystalwallet.models.CryptoNetAccount; +import cy.agorise.crystalwallet.models.GrapheneAccount; +import cy.agorise.crystalwallet.models.GrapheneAccountInfo; + +/** + * Created by Henry Varona on 21/10/2017. + */ + +public class GrapheneAccountInfoViewModel extends AndroidViewModel { + + private LiveData grapheneAccountInfo; + private CrystalDatabase db; + + public GrapheneAccountInfoViewModel(Application application) { + super(application); + this.db = CrystalDatabase.getAppDatabase(application.getApplicationContext()); + } + + public void loadGrapheneAccountInfo(int accountId){ + this.grapheneAccountInfo = this.db.grapheneAccountInfoDao().getGrapheneAccountInfo(accountId); + } + + public void addGrapheneAccountInfo(GrapheneAccountInfo account){ + this.db.cryptoNetAccountDao().insertCryptoNetAccount(); + + this.db.grapheneAccountInfoDao().insertGrapheneAccountInfo(account); + } + + public LiveData getGrapheneAccountInfo(){ + return this.grapheneAccountInfo; + } + +} diff --git a/app/src/main/res/layout/balance_list_item.xml b/app/src/main/res/layout/balance_list_item.xml index e0f9ad0..1ff1487 100644 --- a/app/src/main/res/layout/balance_list_item.xml +++ b/app/src/main/res/layout/balance_list_item.xml @@ -15,6 +15,7 @@ android:layout_alignParentTop="true"> @@ -32,10 +33,12 @@ android:ems="10" android:text="unknown coin" /> + + android:layout_below="@+id/cryptoNetBalanceTitleBarLayout"> diff --git a/app/src/main/res/layout/crypto_coin_balance_list_item.xml b/app/src/main/res/layout/crypto_coin_balance_list_item.xml index b4bec1e..5321d1d 100644 --- a/app/src/main/res/layout/crypto_coin_balance_list_item.xml +++ b/app/src/main/res/layout/crypto_coin_balance_list_item.xml @@ -25,6 +25,7 @@ android:id="@+id/tvCryptoCoinBalanceAmount" android:layout_width="wrap_content" android:layout_height="wrap_content" + android:layout_alignParentRight="true" android:ems="10" android:text="0.00000000" /> diff --git a/app/src/main/res/layout/import_seed.xml b/app/src/main/res/layout/import_seed.xml index ee606cd..f043cfe 100644 --- a/app/src/main/res/layout/import_seed.xml +++ b/app/src/main/res/layout/import_seed.xml @@ -93,6 +93,16 @@ android:inputType="textMultiLine" android:textColor="@color/black" /> + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file