From 8745b32446572b0c9a9e9b9360a3286799b9ba6f Mon Sep 17 00:00:00 2001 From: Javier Varona Date: Wed, 15 Nov 2017 23:17:34 -0400 Subject: [PATCH] - Equivalence values working! --- .../service/CrystalWalletService.java | 73 ++++++++++--------- .../service/EquivalencesThread.java | 6 +- .../views/CryptoCoinBalanceViewHolder.java | 19 +++-- 3 files changed, 56 insertions(+), 42 deletions(-) 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 ebb55ef..5cecb10 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/service/CrystalWalletService.java +++ b/app/src/main/java/cy/agorise/crystalwallet/service/CrystalWalletService.java @@ -72,45 +72,50 @@ public class CrystalWalletService extends LifecycleService { preferredCurrencySetting.observe(service, new Observer() { @Override - public void onChanged(@Nullable GeneralSetting generalSetting) { - CryptoCurrency preferredCurrency = CrystalDatabase.getAppDatabase(service).cryptoCurrencyDao().getByNameAndCryptoNet("EUR", CryptoNet.BITSHARES.name()); + public void onChanged(final @Nullable GeneralSetting generalSetting) { + if (generalSetting != null) { + CryptoCurrency preferredCurrency = CrystalDatabase.getAppDatabase(service).cryptoCurrencyDao().getByNameAndCryptoNet("EUR", CryptoNet.BITSHARES.name()); - if (preferredCurrency != null) { - BitsharesAssetInfo preferredCurrencyBitsharesInfo = CrystalDatabase.getAppDatabase(service).bitsharesAssetDao().getBitsharesAssetInfoFromCurrencyId(preferredCurrency.getId()); + if (preferredCurrency != null) { + BitsharesAssetInfo preferredCurrencyBitsharesInfo = CrystalDatabase.getAppDatabase(service).bitsharesAssetDao().getBitsharesAssetInfoFromCurrencyId(preferredCurrency.getId()); - if (preferredCurrencyBitsharesInfo != null) { - final BitsharesAsset preferredCurrencyBitshareAsset = new BitsharesAsset(preferredCurrency); - preferredCurrencyBitshareAsset.loadInfo(preferredCurrencyBitsharesInfo); + if (preferredCurrencyBitsharesInfo != null) { + final BitsharesAsset preferredCurrencyBitshareAsset = new BitsharesAsset(preferredCurrency); + preferredCurrencyBitshareAsset.loadInfo(preferredCurrencyBitsharesInfo); - //Loading "from" currencies - final LiveData> bitsharesAssetInfo = - CrystalDatabase.getAppDatabase(service).bitsharesAssetDao().getAll(); + //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()); + 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); + } + + if (LoadEquivalencesThread != null) { + LoadEquivalencesThread.stopLoadingEquivalences(); + } + ; + LoadEquivalencesThread = new EquivalencesThread(service, generalSetting.getValue(), bitsharesAssets); + LoadEquivalencesThread.start(); } - ; - 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); - } - - if (LoadEquivalencesThread != null){LoadEquivalencesThread.stopLoadingEquivalences();}; - LoadEquivalencesThread = new EquivalencesThread(service, preferredCurrencyBitshareAsset, bitsharesAssets); - LoadEquivalencesThread.start(); - } - }); + }); + } } } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/service/EquivalencesThread.java b/app/src/main/java/cy/agorise/crystalwallet/service/EquivalencesThread.java index 1248ccc..9b4cd80 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/service/EquivalencesThread.java +++ b/app/src/main/java/cy/agorise/crystalwallet/service/EquivalencesThread.java @@ -15,10 +15,10 @@ import cy.agorise.crystalwallet.models.BitsharesAsset; public class EquivalencesThread extends Thread{ private boolean keepLoadingEquivalences = true; private LifecycleService service; - private BitsharesAsset fromAsset; + private String fromAsset; private List bitsharesAssets; - public EquivalencesThread(LifecycleService service, BitsharesAsset fromAsset, List bitsharesAssets){ + public EquivalencesThread(LifecycleService service, String fromAsset, List bitsharesAssets){ this.service = service; this.fromAsset = fromAsset; this.bitsharesAssets = bitsharesAssets; @@ -30,7 +30,7 @@ public class EquivalencesThread extends Thread{ while(this.keepLoadingEquivalences){ try { - GrapheneApiGenerator.getEquivalentValue(fromAsset, bitsharesAssets, this.service); + GrapheneApiGenerator.getEquivalenValue(fromAsset, bitsharesAssets, this.service); Log.i("Equivalences Thread", "In loop"); Thread.sleep(1000); } catch (InterruptedException e) { diff --git a/app/src/main/java/cy/agorise/crystalwallet/views/CryptoCoinBalanceViewHolder.java b/app/src/main/java/cy/agorise/crystalwallet/views/CryptoCoinBalanceViewHolder.java index abdc438..a17cb05 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/views/CryptoCoinBalanceViewHolder.java +++ b/app/src/main/java/cy/agorise/crystalwallet/views/CryptoCoinBalanceViewHolder.java @@ -70,8 +70,9 @@ public class CryptoCoinBalanceViewHolder extends RecyclerView.ViewHolder { //Retrieves the currency of this balance final CryptoCurrency currencyFrom = CrystalDatabase.getAppDatabase(context).cryptoCurrencyDao().getById(balance.getCryptoCurrencyId()); //Sets the name and amount of the balance in the view + String balanceString = String.format("%.2f",balance.getBalance()/Math.pow(10,currencyFrom.getPrecision())); cryptoCoinName.setText(currencyFrom.getName()); - cryptoCoinBalance.setText("" + balance.getBalance()); + cryptoCoinBalance.setText(balanceString); //Observes the preferred currency of the user. If the user changes it, this will change the equivalent value preferedCurrencySetting.observe((LifecycleOwner) this.context, new Observer() { @@ -84,8 +85,9 @@ public class CryptoCoinBalanceViewHolder extends RecyclerView.ViewHolder { //Retrieves the equivalent value of this balance using the "From" currency and the "To" currency LiveData currencyEquivalenceLiveData = CrystalDatabase.getAppDatabase(context) .cryptoCurrencyEquivalenceDao().getByFromTo( - currencyFrom.getId(), - currencyTo.getId() + currencyTo.getId(), + currencyFrom.getId() + ); //Observes the equivalent value. If the equivalent value changes, this will keep the value showed correct @@ -93,8 +95,15 @@ public class CryptoCoinBalanceViewHolder extends RecyclerView.ViewHolder { @Override public void onChanged(@Nullable CryptoCurrencyEquivalence cryptoCurrencyEquivalence) { if (cryptoCurrencyEquivalence != null) { - CryptoCurrency toCurrency = CrystalDatabase.getAppDatabase(context).cryptoCurrencyDao().getById(cryptoCurrencyEquivalence.getToCurrencyId()); - cryptoCoinBalanceEquivalence.setText(cryptoCurrencyEquivalence.getValue() + " " + toCurrency.getName()); + CryptoCurrency toCurrency = CrystalDatabase.getAppDatabase(context).cryptoCurrencyDao().getById(cryptoCurrencyEquivalence.getFromCurrencyId()); + String equivalenceString = String.format( + "%.2f", + (balance.getBalance()/Math.pow(10,currencyFrom.getPrecision()))/ + (cryptoCurrencyEquivalence.getValue()/Math.pow(10,toCurrency.getPrecision())) + ); + + cryptoCoinBalanceEquivalence.setText( + equivalenceString + " " + toCurrency.getName()); } } });