From 270f2d40b6e398730d2f859ebf7dc9c231161489 Mon Sep 17 00:00:00 2001 From: Javier Varona Date: Fri, 21 Sep 2018 00:43:29 -0400 Subject: [PATCH] - Fixing inconsistent loading of transactions --- .../apigenerator/GrapheneApiGenerator.java | 46 +++++++++++++++---- .../crystalwallet/dao/BitsharesAssetDao.java | 2 +- .../crystalwallet/dao/CryptoCurrencyDao.java | 2 +- .../manager/BitsharesAccountManager.java | 44 ++++++++++-------- 4 files changed, 66 insertions(+), 28 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 e393486..b654b48 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/apigenerator/GrapheneApiGenerator.java +++ b/app/src/main/java/cy/agorise/crystalwallet/apigenerator/GrapheneApiGenerator.java @@ -1,6 +1,7 @@ package cy.agorise.crystalwallet.apigenerator; import android.content.Context; +import android.util.Log; import java.io.Serializable; import java.util.ArrayList; @@ -380,10 +381,20 @@ public abstract class GrapheneApiGenerator { public void success(Object answer, int idPetition) { ArrayList assets = (ArrayList) answer; for(BitsharesAsset asset : assets){ - long idCryptoCurrency = cryptoCurrencyDao.insertCryptoCurrency(asset)[0]; + + long currencyId = -1; + CryptoCurrency cryptoCurrencyDb = cryptoCurrencyDao.getByNameAndCryptoNet(((BitsharesAsset) answer).getName(),((BitsharesAsset) answer).getCryptoNet().name()); + + if (cryptoCurrencyDb != null){ + currencyId = cryptoCurrencyDb.getId(); + } else { + long idCryptoCurrency = cryptoCurrencyDao.insertCryptoCurrency(asset)[0]; + currencyId = idCryptoCurrency; + } + BitsharesAssetInfo info = new BitsharesAssetInfo(asset); - info.setCryptoCurrencyId(idCryptoCurrency); - asset.setId((int)idCryptoCurrency); + info.setCryptoCurrencyId(currencyId); + asset.setId((int)currencyId); bitsharesAssetDao.insertBitsharesAssetInfo(info); saveTransaction(transaction,cryptoCurrencyDao.getById(info.getCryptoCurrencyId()),accountBitsharesId,tOperation,context); } @@ -484,10 +495,19 @@ public abstract class GrapheneApiGenerator { List assets = (List) answer; for(BitsharesAsset asset : assets) { BitsharesAssetInfo info = new BitsharesAssetInfo(asset); - long[] cryptoCurrencyId = cryptoCurrencyDao.insertCryptoCurrency((CryptoCurrency) asset); - info.setCryptoCurrencyId(cryptoCurrencyId[0]); + + long currencyId = -1; + CryptoCurrency cryptoCurrencyDb = cryptoCurrencyDao.getByNameAndCryptoNet(asset.getName(),asset.getCryptoNet().name()); + + if (cryptoCurrencyDb != null){ + currencyId = cryptoCurrencyDb.getId(); + } else { + long[] cryptoCurrencyId = cryptoCurrencyDao.insertCryptoCurrency((CryptoCurrency) asset); + currencyId = cryptoCurrencyId[0]; + } + info.setCryptoCurrencyId(currencyId); bitsharesAssetDao.insertBitsharesAssetInfo(info); - ccBalance.setCryptoCurrencyId(cryptoCurrencyId[0]); + ccBalance.setCryptoCurrencyId(currencyId); balanceDao.insertCryptoCoinBalance(ccBalance); } } @@ -616,8 +636,18 @@ public abstract class GrapheneApiGenerator { public void success(Object answer, int idPetition) { if(answer instanceof BitsharesAsset){ BitsharesAssetInfo info = new BitsharesAssetInfo((BitsharesAsset) answer); - long cryptoCurrencyId = cryptoCurrencyDao.insertCryptoCurrency((CryptoCurrency)answer )[0]; - info.setCryptoCurrencyId(cryptoCurrencyId); + + long currencyId = -1; + CryptoCurrency cryptoCurrencyDb = cryptoCurrencyDao.getByNameAndCryptoNet(((BitsharesAsset) answer).getName(),((BitsharesAsset) answer).getCryptoNet().name()); + + if (cryptoCurrencyDb != null){ + currencyId = cryptoCurrencyDb.getId(); + } else { + long cryptoCurrencyId = cryptoCurrencyDao.insertCryptoCurrency((CryptoCurrency)answer )[0]; + currencyId = cryptoCurrencyId; + } + + info.setCryptoCurrencyId(currencyId); bitsharesAssetDao.insertBitsharesAssetInfo(info); GrapheneApiGenerator.getEquivalentValue((BitsharesAsset) answer, quoteAssets, context); } 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 75662b8..8e8d3c4 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/dao/BitsharesAssetDao.java +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/BitsharesAssetDao.java @@ -28,6 +28,6 @@ public interface BitsharesAssetDao { @Query("SELECT * FROM bitshares_asset WHERE bitshares_id = :bitsharesId") BitsharesAssetInfo getBitsharesAssetInfoById(String bitsharesId); - @Insert(onConflict = OnConflictStrategy.REPLACE) + @Insert(onConflict = OnConflictStrategy.IGNORE) public long[] insertBitsharesAssetInfo(BitsharesAssetInfo... accounts); } diff --git a/app/src/main/java/cy/agorise/crystalwallet/dao/CryptoCurrencyDao.java b/app/src/main/java/cy/agorise/crystalwallet/dao/CryptoCurrencyDao.java index f821a35..3f59f0e 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/dao/CryptoCurrencyDao.java +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/CryptoCurrencyDao.java @@ -35,7 +35,7 @@ public interface CryptoCurrencyDao { @Query("SELECT * FROM crypto_currency WHERE name = :name") CryptoCurrency getByName(String name); - @Insert(onConflict = OnConflictStrategy.REPLACE) + @Insert(onConflict = OnConflictStrategy.IGNORE) public long[] insertCryptoCurrency(CryptoCurrency... currencies); } 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 c0d5194..a3871fb 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java +++ b/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java @@ -333,23 +333,21 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI GrapheneAccount account = new GrapheneAccount(); account.setName(createRequest.getAccountName()); account.setSeedId(idSeed); - account.setAccountIndex(0); - account.setCryptoNet(CryptoNet.BITSHARES); - this.createAccountFromSeed(account, new ManagerRequest() { - - @Override - public void success(Object answer) { - createRequest.setAccount((GrapheneAccount) answer); - createRequest.setStatus(ValidateCreateBitsharesAccountRequest.StatusCode.SUCCEEDED); - } - - @Override - public void fail() { - createRequest.setStatus(ValidateCreateBitsharesAccountRequest.StatusCode.ACCOUNT_EXIST); - } - }, context); + account.setAccountIndex(0); + account.setCryptoNet(CryptoNet.BITSHARES); + this.createAccountFromSeed(account, new ManagerRequest() { + @Override + public void success(Object answer) { + createRequest.setAccount((GrapheneAccount) answer); + createRequest.setStatus(ValidateCreateBitsharesAccountRequest.StatusCode.SUCCEEDED); + } + @Override + public void fail() { + createRequest.setStatus(ValidateCreateBitsharesAccountRequest.StatusCode.ACCOUNT_EXIST); + } + }, context); } /** @@ -681,10 +679,20 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI public void success(Object answer, int idPetition) { ArrayList assets = (ArrayList) answer; for(BitsharesAsset asset : assets){ - long idCryptoCurrency = db.cryptoCurrencyDao().insertCryptoCurrency(asset)[0]; + long currencyId = -1; + CryptoCurrency cryptoCurrencyDb = db.cryptoCurrencyDao().getByNameAndCryptoNet(asset.getName(),asset.getCryptoNet().name()); + + if (cryptoCurrencyDb != null){ + currencyId = cryptoCurrencyDb.getId(); + } else { + long idCryptoCurrency = db.cryptoCurrencyDao().insertCryptoCurrency(asset)[0]; + currencyId = idCryptoCurrency; + } + + BitsharesAssetInfo info = new BitsharesAssetInfo(asset); - info.setCryptoCurrencyId(idCryptoCurrency); - asset.setId((int)idCryptoCurrency); + info.setCryptoCurrencyId(currencyId); + asset.setId((int)currencyId); db.bitsharesAssetDao().insertBitsharesAssetInfo(info); saveTransaction(transaction,info,transfer); }