From f80ac3b06db2029f879e2bc923768c65f9891ea2 Mon Sep 17 00:00:00 2001 From: henry Date: Tue, 24 Oct 2017 22:58:45 -0400 Subject: [PATCH] modified CryptoCoin Balance database to work as intended Modified to be able to save and retrieve the bitsharesasset from database --- .../apigenerator/GrapheneApiGenerator.java | 35 ++++++++-------- .../crystalwallet/dao/BitsharesAssetDao.java | 2 +- .../manager/BitsharesAccountManager.java | 42 +++++++++++++++---- .../models/CryptoCoinBalance.java | 2 +- .../crystalwallet/models/CryptoCurrency.java | 4 +- 5 files changed, 56 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/cy/agorise/crystalwallet/apigenerator/GrapheneApiGenerator.java b/app/src/main/java/cy/agorise/crystalwallet/apigenerator/GrapheneApiGenerator.java index 2706484..1706e93 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/apigenerator/GrapheneApiGenerator.java +++ b/app/src/main/java/cy/agorise/crystalwallet/apigenerator/GrapheneApiGenerator.java @@ -325,8 +325,8 @@ public abstract class GrapheneApiGenerator { final CryptoCoinBalance balance = new CryptoCoinBalance(); balance.setAccountId(accountId); balance.setBalance(((AccountBalanceUpdate) update).balance); - LiveData assetInfo = bitsharesAssetDao.getBitsharesAssetInfoById(((AccountBalanceUpdate) update).asset_type); - if(assetInfo == null || assetInfo.getValue() == null){ + BitsharesAssetInfo assetInfo = bitsharesAssetDao.getBitsharesAssetInfoById(((AccountBalanceUpdate) update).asset_type); + if(assetInfo == null ){ final String assetType = ((AccountBalanceUpdate) update).asset_type; ArrayList idAssets = new ArrayList<>(); idAssets.add(assetType); @@ -335,9 +335,10 @@ public abstract class GrapheneApiGenerator { public void success(Object answer, int idPetition) { if(answer instanceof BitsharesAsset){ BitsharesAssetInfo info = new BitsharesAssetInfo((BitsharesAsset) answer); - cryptoCurrencyDao.insertCryptoCurrency((CryptoCurrency)answer ); + long cryptoCurrencyId = cryptoCurrencyDao.insertCryptoCurrency((CryptoCurrency)answer )[0]; + info.setCryptoCurrencyId(cryptoCurrencyId); bitsharesAssetDao.insertBitsharesAssetInfo(info); - balance.setCryptoCurrencyId(bitsharesAssetDao.getBitsharesAssetInfoById(assetType).getValue().getCryptoCurrencyId()); + balance.setCryptoCurrencyId(cryptoCurrencyId); balanceDao.insertCryptoCoinBalance(balance); } } @@ -350,9 +351,8 @@ public abstract class GrapheneApiGenerator { getAssetById(idAssets,getAssetRequest); }else { - balance.setCryptoCurrencyId(assetInfo.getValue().getCryptoCurrencyId()); + balance.setCryptoCurrencyId(assetInfo.getCryptoCurrencyId()); balanceDao.insertCryptoCoinBalance(balance); - //TODO balance function } BitsharesAccountManager.refreshAccountTransactions(accountId,context); @@ -393,32 +393,33 @@ public abstract class GrapheneApiGenerator { final CryptoCoinBalance ccBalance = new CryptoCoinBalance(); ccBalance.setAccountId(accountId); ccBalance.setBalance(balance.getAmount().longValue()); - LiveData assetInfo = bitsharesAssetDao.getBitsharesAssetInfoById(balance.getAsset().getObjectId()); - if(assetInfo == null || assetInfo.getValue() == null){ + BitsharesAssetInfo assetInfo = bitsharesAssetDao.getBitsharesAssetInfoById(balance.getAsset().getObjectId()); + if(assetInfo == null ){ ArrayList idAssets = new ArrayList<>(); idAssets.add(balance.getAsset().getObjectId()); ApiRequest getAssetRequest = new ApiRequest(1, new ApiRequestListener() { @Override public void success(Object answer, int idPetition) { - if(answer instanceof BitsharesAsset){ - BitsharesAssetInfo info = new BitsharesAssetInfo((BitsharesAsset) answer); - cryptoCurrencyDao.insertCryptoCurrency((CryptoCurrency)answer ); - bitsharesAssetDao.insertBitsharesAssetInfo(info); - ccBalance.setCryptoCurrencyId(bitsharesAssetDao.getBitsharesAssetInfoById(balance.getAsset().getObjectId()).getValue().getCryptoCurrencyId()); - balanceDao.insertCryptoCoinBalance(ccBalance); - } + List assets = (List) answer; + for(BitsharesAsset asset : assets) { + BitsharesAssetInfo info = new BitsharesAssetInfo(asset); + long[] cryptoCurrencyId = cryptoCurrencyDao.insertCryptoCurrency((CryptoCurrency) asset); + info.setCryptoCurrencyId(cryptoCurrencyId[0]); + bitsharesAssetDao.insertBitsharesAssetInfo(info); + ccBalance.setCryptoCurrencyId(cryptoCurrencyId[0]); + balanceDao.insertCryptoCoinBalance(ccBalance); + } } @Override public void fail(int idPetition) { - } }); getAssetById(idAssets,getAssetRequest); }else { - ccBalance.setCryptoCurrencyId(assetInfo.getValue().getCryptoCurrencyId()); + ccBalance.setCryptoCurrencyId(assetInfo.getCryptoCurrencyId()); balanceDao.insertCryptoCoinBalance(ccBalance); } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/dao/BitsharesAssetDao.java b/app/src/main/java/cy/agorise/crystalwallet/dao/BitsharesAssetDao.java index 1b6e2f0..63b4bdb 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/dao/BitsharesAssetDao.java +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/BitsharesAssetDao.java @@ -23,7 +23,7 @@ public interface BitsharesAssetDao { LiveData getBitsharesAssetInfo(long cryptoCurrencyId); @Query("SELECT * FROM bitshares_asset WHERE bitshares_id = :bitsharesId") - LiveData getBitsharesAssetInfoById(String bitsharesId); + BitsharesAssetInfo getBitsharesAssetInfoById(String bitsharesId); @Insert(onConflict = OnConflictStrategy.REPLACE) public long[] insertBitsharesAssetInfo(BitsharesAssetInfo... accounts); diff --git a/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java b/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java index 9076196..37498da 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java +++ b/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java @@ -85,15 +85,22 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI if(grapheneAccount.getAccountId() == null){ System.out.println("Importing without accountid"); - grapheneAccount = this.getAccountInfoByName(grapheneAccount.getName()); + GrapheneAccount fetch = this.getAccountInfoByName(grapheneAccount.getName()); + if(fetch == null) { + //TODO grapaheneAccount null, error fetching + return null; + } + grapheneAccount.setAccountId(fetch.getAccountId()); }else if(grapheneAccount.getName() == null){ System.out.println("Importing without accountname"); - grapheneAccount = this.getAccountInfoById(grapheneAccount.getAccountId()); - } - if(grapheneAccount == null) { - //TODO grapaheneAccount null, error fetching - return null; + GrapheneAccount fetch = this.getAccountInfoById(grapheneAccount.getAccountId()); + if(fetch == null) { + //TODO grapaheneAccount null, error fetching + return null; + } + grapheneAccount.setName(fetch.getName()); } + System.out.println("Importing not null " + ((GrapheneAccount) account).getName() + " " + ((GrapheneAccount) account).getAccountId()); CrystalDatabase db = CrystalDatabase.getAppDatabase(context); @@ -110,14 +117,28 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI @Override public void loadAccountFromDB(CryptoNetAccount account, Context context) { + System.out.println("On loadAccount from db"); if(account instanceof GrapheneAccount){ GrapheneAccount grapheneAccount = (GrapheneAccount) account; CrystalDatabase db = CrystalDatabase.getAppDatabase(context); - grapheneAccount.loadInfo(db.grapheneAccountInfoDao().getByAccountId(account.getId())); + GrapheneAccountInfo info = db.grapheneAccountInfoDao().getByAccountId(account.getId()); + grapheneAccount.loadInfo(info); if(grapheneAccount.getAccountId() == null){ - grapheneAccount = this.getAccountInfoByName(grapheneAccount.getName()); + System.out.println("On loadAccount null id"); + GrapheneAccount fetch = this.getAccountInfoByName(grapheneAccount.getName()); + if(fetch != null){ + info.setAccountId(fetch.getAccountId()); + grapheneAccount.setAccountId(fetch.getAccountId()); + db.grapheneAccountInfoDao().insertGrapheneAccountInfo(info); + } }else if(grapheneAccount.getName() == null){ - grapheneAccount = this.getAccountInfoById(grapheneAccount.getAccountId()); + System.out.println("On loadAccount null name"); + GrapheneAccount fetch = this.getAccountInfoById(grapheneAccount.getAccountId()); + if(fetch != null) { + info.setName(fetch.getName()); + grapheneAccount.setName(fetch.getName()); + db.grapheneAccountInfoDao().insertGrapheneAccountInfo(info); + } } if(grapheneAccount == null) { @@ -125,6 +146,8 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI return; } + System.out.println("On loadAccount no nulls " + grapheneAccount.getName() + " " + grapheneAccount.getAccountId()); + GrapheneApiGenerator.subscribeBitsharesAccount(grapheneAccount.getId(),grapheneAccount.getAccountId(),context); this.refreshAccountTransactions(account.getId(),context); GrapheneApiGenerator.getAccountBalance(grapheneAccount.getId(),grapheneAccount.getAccountId(),context); @@ -321,6 +344,7 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI transaction.setAmount(transfer.getOperation().getAssetAmount().getAmount().longValue()); CryptoCurrency currency = db.cryptoCurrencyDao().getByName(transfer.getOperation().getAssetAmount().getAsset().getSymbol()); if(currency == null){ + System.out.println("CryptoCurrency not in database"); //CryptoCurrency not in database Asset asset = transfer.getOperation().getAssetAmount().getAsset(); BitsharesAsset.Type assetType = BitsharesAsset.Type.UIA; diff --git a/app/src/main/java/cy/agorise/crystalwallet/models/CryptoCoinBalance.java b/app/src/main/java/cy/agorise/crystalwallet/models/CryptoCoinBalance.java index c2eae46..10a868b 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/models/CryptoCoinBalance.java +++ b/app/src/main/java/cy/agorise/crystalwallet/models/CryptoCoinBalance.java @@ -18,7 +18,7 @@ import cy.agorise.crystalwallet.enums.CryptoCoin; */ @Entity(tableName="crypto_coin_balance", - indices = {@Index("id"),@Index("account_id")}, + indices = {@Index("id"),@Index("account_id"),@Index(value = {"account_id","crypto_currency_id"}, unique=true)}, foreignKeys = @ForeignKey(entity = CryptoNetAccount.class, parentColumns = "id", childColumns = "account_id")) diff --git a/app/src/main/java/cy/agorise/crystalwallet/models/CryptoCurrency.java b/app/src/main/java/cy/agorise/crystalwallet/models/CryptoCurrency.java index 78cb47c..03ce6e1 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/models/CryptoCurrency.java +++ b/app/src/main/java/cy/agorise/crystalwallet/models/CryptoCurrency.java @@ -2,6 +2,7 @@ package cy.agorise.crystalwallet.models; import android.arch.persistence.room.ColumnInfo; import android.arch.persistence.room.Entity; +import android.arch.persistence.room.Index; import android.arch.persistence.room.PrimaryKey; import android.arch.persistence.room.TypeConverters; @@ -13,7 +14,8 @@ import cy.agorise.crystalwallet.enums.CryptoNet; * * Created by henry Henry Varona on 11/9/2017. */ -@Entity(tableName="crypto_currency") +@Entity(tableName="crypto_currency", + indices = {@Index(value = {"crypto_net","name"}, unique=true)}) public class CryptoCurrency { /**