From 8db09a67c4f56be7838a595f5c292035fc06a967 Mon Sep 17 00:00:00 2001 From: Javier Varona Date: Tue, 26 Sep 2017 22:24:23 -0400 Subject: [PATCH] - Now Room creates the database using the constructed entities and not with Migration queries - Added some random data generators for improving testing --- .../agorise/crystalwallet/IntroActivity.java | 20 +++++++++-- .../crystalwallet/dao/AccountSeedDao.java | 6 ++++ .../dao/CryptoNetAccountDao.java | 6 ++++ .../crystalwallet/dao/CrystalDatabase.java | 6 ++-- .../crystalwallet/dao/TransactionDao.java | 2 +- .../dao/converters/Converters.java | 4 +-- .../crystalwallet/models/AccountSeed.java | 6 ++-- .../models/CryptoCoinTransaction.java | 12 +++---- .../models/CryptoNetAccount.java | 12 +++---- .../RandomCryptoNetAccountGenerator.java | 36 +++++++++++++++++++ .../RandomSeedGenerator.java | 33 +++++++++++++++++ .../RandomTransactionsGenerator.java | 6 +++- .../viewmodels/TransactionListViewModel.java | 1 + .../views/TransactionListView.java | 24 ++++++++++++- .../views/TransactionViewHolder.java | 25 ++++++++----- 15 files changed, 164 insertions(+), 35 deletions(-) create mode 100644 app/src/main/java/cy/agorise/crystalwallet/randomdatagenerators/RandomCryptoNetAccountGenerator.java create mode 100644 app/src/main/java/cy/agorise/crystalwallet/randomdatagenerators/RandomSeedGenerator.java diff --git a/app/src/main/java/cy/agorise/crystalwallet/IntroActivity.java b/app/src/main/java/cy/agorise/crystalwallet/IntroActivity.java index 25004d6..19acc98 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/IntroActivity.java +++ b/app/src/main/java/cy/agorise/crystalwallet/IntroActivity.java @@ -15,7 +15,11 @@ import android.widget.Button; import java.util.List; import cy.agorise.crystalwallet.dao.CrystalDatabase; +import cy.agorise.crystalwallet.models.AccountSeed; import cy.agorise.crystalwallet.models.CryptoCoinTransaction; +import cy.agorise.crystalwallet.models.CryptoNetAccount; +import cy.agorise.crystalwallet.randomdatagenerators.RandomCryptoNetAccountGenerator; +import cy.agorise.crystalwallet.randomdatagenerators.RandomSeedGenerator; import cy.agorise.crystalwallet.randomdatagenerators.RandomTransactionsGenerator; import cy.agorise.crystalwallet.viewmodels.TransactionListViewModel; import cy.agorise.crystalwallet.views.TransactionListView; @@ -33,10 +37,20 @@ public class IntroActivity extends LifecycleActivity { setContentView(R.layout.activity_intro); /*CrystalDatabase db = CrystalDatabase.getAppDatabase(getApplicationContext()); - List transactions = RandomTransactionsGenerator.generateTransactions(100,1262304001,1496275201,1,999999999); - + List seeds = RandomSeedGenerator.generateSeeds(2); + for(int i=0;i accounts = RandomCryptoNetAccountGenerator.generateAccounts(5,seeds); + for(int i=0;i transactions = RandomTransactionsGenerator.generateTransactions(accounts,100,1262304001,1496275201,1,999999999); for(int i=0;i getAll(); + + @Insert(onConflict = OnConflictStrategy.REPLACE) + public long[] insertAccountSeed(AccountSeed... seeds); + } 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 11f6dc3..ee072ea 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/dao/CryptoNetAccountDao.java +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/CryptoNetAccountDao.java @@ -1,6 +1,8 @@ package cy.agorise.crystalwallet.dao; import android.arch.persistence.room.Dao; +import android.arch.persistence.room.Insert; +import android.arch.persistence.room.OnConflictStrategy; import android.arch.persistence.room.Query; import java.util.List; @@ -17,4 +19,8 @@ public interface CryptoNetAccountDao { @Query("SELECT * FROM crypto_net_account") List getAll(); + + @Insert(onConflict = OnConflictStrategy.REPLACE) + public long[] insertCryptoNetAccount(CryptoNetAccount... accounts); + } diff --git a/app/src/main/java/cy/agorise/crystalwallet/dao/CrystalDatabase.java b/app/src/main/java/cy/agorise/crystalwallet/dao/CrystalDatabase.java index 18e261e..5ead245 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/dao/CrystalDatabase.java +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/CrystalDatabase.java @@ -35,13 +35,13 @@ public abstract class CrystalDatabase extends RoomDatabase { Room.databaseBuilder(context, CrystalDatabase.class, "CrystalWallet.db") .allowMainThreadQueries() - .addMigrations(MIGRATION_1_2) + //.addMigrations(MIGRATION_1_2) .build(); } return instance; } - static final Migration MIGRATION_1_2 = new Migration(1, 2) { + /*static final Migration MIGRATION_1_2 = new Migration(1, 2) { @Override public void migrate(SupportSQLiteDatabase database) { database.execSQL("CREATE TABLE 'account_seed' ('id' INTEGER PRIMARY KEY AUTOINCREMENT, " @@ -55,5 +55,5 @@ public abstract class CrystalDatabase extends RoomDatabase { + "'date' INT, 'is_input' INT, amount INT, crypto_coin TEXT, is_confirmed INT, " + "FOREIGN_KEY(account_id) REFERENCES crypto_net_account(id))"); } - }; + };*/ } diff --git a/app/src/main/java/cy/agorise/crystalwallet/dao/TransactionDao.java b/app/src/main/java/cy/agorise/crystalwallet/dao/TransactionDao.java index 281bc3e..2149ec3 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/dao/TransactionDao.java +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/TransactionDao.java @@ -24,7 +24,7 @@ public interface TransactionDao { LivePagedListProvider transactionsByDate(); @Insert(onConflict = OnConflictStrategy.REPLACE) - public void insertTransaction(CryptoCoinTransaction... transactions); + public long[] insertTransaction(CryptoCoinTransaction... transactions); @Query("DELETE FROM crypto_coin_transaction") public void deleteAllTransactions(); diff --git a/app/src/main/java/cy/agorise/crystalwallet/dao/converters/Converters.java b/app/src/main/java/cy/agorise/crystalwallet/dao/converters/Converters.java index aabe614..020933c 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/dao/converters/Converters.java +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/converters/Converters.java @@ -30,7 +30,7 @@ public class Converters { } @TypeConverter - public int cryptoNetAccountToId(CryptoNetAccount account) { + public long cryptoNetAccountToId(CryptoNetAccount account) { if (account == null) { return -1; } else { @@ -39,7 +39,7 @@ public class Converters { } @TypeConverter - public CryptoNetAccount fromCryptoNetAccountId(int value) { + public CryptoNetAccount fromCryptoNetAccountId(long value) { if (value == -1){ return null; } else { diff --git a/app/src/main/java/cy/agorise/crystalwallet/models/AccountSeed.java b/app/src/main/java/cy/agorise/crystalwallet/models/AccountSeed.java index aff26d1..b00cbf4 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/models/AccountSeed.java +++ b/app/src/main/java/cy/agorise/crystalwallet/models/AccountSeed.java @@ -18,7 +18,7 @@ public class AccountSeed { */ @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "id") - private int mId; + private long mId; /** * The name or tag of this seed @@ -32,11 +32,11 @@ public class AccountSeed { @ColumnInfo(name = "master_seed") private String mMasterSeed; - public int getId() { + public long getId() { return mId; } - public void setId(int id){ + public void setId(long id){ this.mId = id; } diff --git a/app/src/main/java/cy/agorise/crystalwallet/models/CryptoCoinTransaction.java b/app/src/main/java/cy/agorise/crystalwallet/models/CryptoCoinTransaction.java index 00f173d..ec4e2e2 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/models/CryptoCoinTransaction.java +++ b/app/src/main/java/cy/agorise/crystalwallet/models/CryptoCoinTransaction.java @@ -39,7 +39,7 @@ public class CryptoCoinTransaction { */ @PrimaryKey(autoGenerate = true) @ColumnInfo(name="id") - protected int id; + protected long id; /** * The full date of this transaction */ @@ -54,7 +54,7 @@ public class CryptoCoinTransaction { * The id of the account assoiciated, this is used for the foreign key definition */ @ColumnInfo(name="account_id") - protected int accountId; + protected long accountId; /** * The amount of asset is moved in this transaction */ @@ -92,11 +92,11 @@ public class CryptoCoinTransaction { public void setTo(String to) { this.to = to; } - public int getAccountId() { + public long getAccountId() { return accountId; } - public void setAccountId(int accountId) { + public void setAccountId(long accountId) { this.accountId = accountId; } @@ -108,11 +108,11 @@ public class CryptoCoinTransaction { this.account = account; } - public int getId() { + public long getId() { return id; } - public void setId(int id) { + public void setId(long id) { this.id = id; } diff --git a/app/src/main/java/cy/agorise/crystalwallet/models/CryptoNetAccount.java b/app/src/main/java/cy/agorise/crystalwallet/models/CryptoNetAccount.java index 8eb8640..21cabb0 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/models/CryptoNetAccount.java +++ b/app/src/main/java/cy/agorise/crystalwallet/models/CryptoNetAccount.java @@ -25,13 +25,13 @@ public class CryptoNetAccount { */ @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "id") - private int mId; + private long mId; /** * The id of the seed used by this account */ @ColumnInfo(name = "seed_id") - private int mSeedId; + private long mSeedId; /** * The account number on the bip44 or slip44 @@ -45,19 +45,19 @@ public class CryptoNetAccount { @ColumnInfo(name = "account_index") private int mAccountIndex; - public int getId() { + public long getId() { return mId; } - public void setId(int id){ + public void setId(long id){ this.mId = id; } - public int getSeedId() { + public long getSeedId() { return mSeedId; } - public void setSeedId(int mSeedId) { + public void setSeedId(long mSeedId) { this.mSeedId = mSeedId; } diff --git a/app/src/main/java/cy/agorise/crystalwallet/randomdatagenerators/RandomCryptoNetAccountGenerator.java b/app/src/main/java/cy/agorise/crystalwallet/randomdatagenerators/RandomCryptoNetAccountGenerator.java new file mode 100644 index 0000000..b26f029 --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/randomdatagenerators/RandomCryptoNetAccountGenerator.java @@ -0,0 +1,36 @@ +package cy.agorise.crystalwallet.randomdatagenerators; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +import cy.agorise.crystalwallet.models.AccountSeed; +import cy.agorise.crystalwallet.models.CryptoNetAccount; + +/** + * Created by Henry Varona on 20/9/2017. + */ + +public class RandomCryptoNetAccountGenerator { + + static public List generateAccounts(int numberOfAccounts,List seeds){ + ArrayList result = new ArrayList(); + Random randomGenerator = new Random(); + CryptoNetAccount randomAccount; + + for (int i=0;i generateSeeds(int numberOfSeeds){ + ArrayList result = new ArrayList(); + Random randomGenerator = new Random(); + AccountSeed randomSeed; + + for (int i=0;i generateTransactions(int numberOfTransactions, int minTimestamp, int maxTimestamp, int minAmount, int maxAmount){ + static public List generateTransactions(List accounts, int numberOfTransactions, int minTimestamp, int maxTimestamp, int minAmount, int maxAmount){ ArrayList result = new ArrayList(); Random randomGenerator = new Random(); Calendar cal = Calendar.getInstance(); @@ -22,11 +23,14 @@ public class RandomTransactionsGenerator { CryptoCoinTransaction randomTransaction; for (int i=0;i data){ if (this.listAdapter == null) { this.listAdapter = new TransactionListAdapter(); diff --git a/app/src/main/java/cy/agorise/crystalwallet/views/TransactionViewHolder.java b/app/src/main/java/cy/agorise/crystalwallet/views/TransactionViewHolder.java index 0ebc533..b10c454 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/views/TransactionViewHolder.java +++ b/app/src/main/java/cy/agorise/crystalwallet/views/TransactionViewHolder.java @@ -22,6 +22,7 @@ public class TransactionViewHolder extends RecyclerView.ViewHolder { transactionFrom = (TextView) itemView.findViewById(R.id.fromText); transactionTo = (TextView) itemView.findViewById(R.id.toText); transactionAmount = (TextView) itemView.findViewById(R.id.amountText); + } public void clear(){ @@ -31,14 +32,20 @@ public class TransactionViewHolder extends RecyclerView.ViewHolder { } public void bindTo(final CryptoCoinTransaction transaction/*, final OnTransactionClickListener listener*/) { - transactionFrom.setText(transaction.getFrom()); - transactionTo.setText(transaction.getTo()); - transactionAmount.setText(""+transaction.getAmount()); - /*itemView.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - listener.onUserClick(user); - } - });*/ + if (transaction == null){ + transactionFrom.setText("loading..."); + transactionTo.setText(""); + transactionAmount.setText(""); + } else { + transactionFrom.setText(transaction.getFrom()); + transactionTo.setText(transaction.getTo()); + transactionAmount.setText("" + transaction.getAmount()); + /*itemView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + listener.onUserClick(user); + } + });*/ + } } }