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 63b4bdb..96f4584 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/dao/BitsharesAssetDao.java +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/BitsharesAssetDao.java @@ -22,6 +22,9 @@ public interface BitsharesAssetDao { @Query("SELECT * FROM bitshares_asset WHERE crypto_curreny_id = :cryptoCurrencyId") LiveData getBitsharesAssetInfo(long cryptoCurrencyId); + @Query("SELECT * FROM bitshares_asset WHERE crypto_curreny_id = :cryptoCurrencyId") + BitsharesAssetInfo getBitsharesAssetInfoFromCurrencyId(long cryptoCurrencyId); + @Query("SELECT * FROM bitshares_asset WHERE bitshares_id = :bitsharesId") BitsharesAssetInfo getBitsharesAssetInfoById(String bitsharesId); diff --git a/app/src/main/java/cy/agorise/crystalwallet/dao/CryptoCoinBalanceDao.java b/app/src/main/java/cy/agorise/crystalwallet/dao/CryptoCoinBalanceDao.java index 973a5c0..9f698e2 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/dao/CryptoCoinBalanceDao.java +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/CryptoCoinBalanceDao.java @@ -11,6 +11,7 @@ import java.util.List; import cy.agorise.crystalwallet.models.CryptoCoinBalance; import cy.agorise.crystalwallet.models.CryptoNetAccount; import cy.agorise.crystalwallet.models.CryptoNetBalance; +import cy.agorise.crystalwallet.enums.CryptoNet; /** * Created by Henry Varona on 10/9/2017. @@ -28,6 +29,9 @@ public interface CryptoCoinBalanceDao { @Query("SELECT * FROM crypto_coin_balance WHERE account_id = :accountId") LiveData> getBalancesFromAccount(long accountId); + @Query("SELECT * FROM crypto_coin_balance WHERE account_id IN (SELECT id FROM crypto_net_account WHERE crypto_net = 'BITSHARES')") + LiveData> getBalancesFromBitsharesAccount(); + @Query("SELECT * FROM crypto_coin_balance WHERE account_id = :accountId AND crypto_currency_id = :assetId") CryptoCoinBalance getBalanceFromAccount(long accountId, long assetId); 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 142e312..6826c4f 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/dao/CryptoCurrencyDao.java +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/CryptoCurrencyDao.java @@ -22,6 +22,9 @@ public interface CryptoCurrencyDao { @Query("SELECT * FROM crypto_currency WHERE id = :id") CryptoCurrency getById(long id); + @Query("SELECT * FROM crypto_currency WHERE name = :name AND crypto_net = :cryptoNet") + CryptoCurrency getByNameAndCryptoNet(String name,String cryptoNet); + @Query("SELECT * FROM crypto_currency WHERE id IN (:ids)") List getByIds(List ids); 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 70b68d1..453506c 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/service/CrystalWalletService.java +++ b/app/src/main/java/cy/agorise/crystalwallet/service/CrystalWalletService.java @@ -13,12 +13,20 @@ import android.os.Message; import android.support.annotation.Nullable; import android.util.Log; +import java.util.ArrayList; import java.util.List; +import cy.agorise.crystalwallet.apigenerator.GrapheneApiGenerator; import cy.agorise.crystalwallet.cryptonetinforequests.CryptoNetInfoRequests; import cy.agorise.crystalwallet.dao.CrystalDatabase; +import cy.agorise.crystalwallet.enums.CryptoNet; import cy.agorise.crystalwallet.manager.BitsharesAccountManager; +import cy.agorise.crystalwallet.models.BitsharesAsset; +import cy.agorise.crystalwallet.models.BitsharesAssetInfo; +import cy.agorise.crystalwallet.models.CryptoCoinBalance; +import cy.agorise.crystalwallet.models.CryptoCurrency; import cy.agorise.crystalwallet.models.CryptoNetAccount; +import cy.agorise.crystalwallet.models.GeneralSetting; import cy.agorise.crystalwallet.models.GrapheneAccount; import cy.agorise.crystalwallet.models.GrapheneAccountInfo; @@ -33,7 +41,9 @@ public class CrystalWalletService extends LifecycleService { private ServiceHandler mServiceHandler; private BitsharesAccountManager bitsharesAccountManager; private Thread LoadAccountTransactionsThread; + private Thread LoadEquivalencesThread; private boolean keepLoadingAccountTransactions; + private boolean keepLoadingEquivalences; private CryptoNetInfoRequests cryptoNetInfoRequests; // Handler that receives messages from the thread @@ -52,6 +62,68 @@ public class CrystalWalletService extends LifecycleService { } } + public void loadEquivalentsValues(){ + this.keepLoadingEquivalences = true; + final LifecycleService service = this; + + //getting the preferred currency of the user + final LiveData preferredCurrencySetting = + CrystalDatabase.getAppDatabase(service).generalSettingDao().getByName(GeneralSetting.SETTING_NAME_PREFERED_CURRENCY); + + preferredCurrencySetting.observe(service, new Observer() { + @Override + public void onChanged(@Nullable GeneralSetting generalSetting) { + CryptoCurrency preferredCurrency = CrystalDatabase.getAppDatabase(service).cryptoCurrencyDao().getByNameAndCryptoNet("EUR", CryptoNet.BITSHARES.name()); + + if (preferredCurrency != null) { + BitsharesAssetInfo preferredCurrencyBitsharesInfo = CrystalDatabase.getAppDatabase(service).bitsharesAssetDao().getBitsharesAssetInfoFromCurrencyId(preferredCurrency.getId()); + + if (preferredCurrencyBitsharesInfo != null) { + final BitsharesAsset preferredCurrencyBitshareAsset = new BitsharesAsset(preferredCurrency); + preferredCurrencyBitshareAsset.loadInfo(preferredCurrencyBitsharesInfo); + + //Loading "from" currencies + final LiveData> bitsharesAssetInfo = + CrystalDatabase.getAppDatabase(service).bitsharesAssetDao().getAll(); + + bitsharesAssetInfo.observe(service, new Observer>() { + @Override + public void onChanged(@Nullable List bitsharesAssetInfos) { + List bitsharesAssets = new ArrayList(); + List currenciesIds = new ArrayList(); + for (BitsharesAssetInfo bitsharesAssetInfo : bitsharesAssetInfos) { + currenciesIds.add(bitsharesAssetInfo.getCryptoCurrencyId()); + } + ; + List bitsharesCurrencies = CrystalDatabase.getAppDatabase(service).cryptoCurrencyDao().getByIds(currenciesIds); + + BitsharesAsset nextAsset; + for (int i = 0; i < bitsharesCurrencies.size(); i++) { + CryptoCurrency nextCurrency = bitsharesCurrencies.get(i); + BitsharesAssetInfo nextBitsharesInfo = bitsharesAssetInfos.get(i); + nextAsset = new BitsharesAsset(nextCurrency); + nextAsset.loadInfo(nextBitsharesInfo); + bitsharesAssets.add(nextAsset); + } + + while (keepLoadingEquivalences) { + try { + GrapheneApiGenerator.getEquivalentValue(preferredCurrencyBitshareAsset, bitsharesAssets, service); + Thread.sleep(60000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + } + }); + } + } + } + }); + + + } + public void loadAccountTransactions(){ this.keepLoadingAccountTransactions = true; final CrystalWalletService thisService = this; @@ -112,6 +184,15 @@ public class CrystalWalletService extends LifecycleService { LoadAccountTransactionsThread.start(); } + if (LoadEquivalencesThread == null) { + LoadEquivalencesThread = new Thread() { + public void run() { + loadEquivalentsValues(); + } + }; + LoadEquivalencesThread.start(); + } + // If we get killed, after returning from here, restart return START_STICKY; }