From 62edb371436ae5e93cb0858fa1526c124d241049 Mon Sep 17 00:00:00 2001 From: Javier Varona Date: Wed, 10 Oct 2018 21:57:40 -0400 Subject: [PATCH] - Changing main thread db queries to be executed in a room observer way --- .../crystalwallet/dao/CryptoCurrencyDao.java | 3 + .../views/CryptoCoinBalanceViewHolder.java | 113 ++++++++++-------- 2 files changed, 67 insertions(+), 49 deletions(-) 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 3f59f0e..47888aa 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/dao/CryptoCurrencyDao.java +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/CryptoCurrencyDao.java @@ -23,6 +23,9 @@ public interface CryptoCurrencyDao { @Query("SELECT * FROM crypto_currency WHERE id = :id") CryptoCurrency getById(long id); + @Query("SELECT * FROM crypto_currency WHERE id = :id") + LiveData getLDById(long id); + @Query("SELECT * FROM crypto_currency WHERE name = :name AND crypto_net = :cryptoNet") CryptoCurrency getByNameAndCryptoNet(String name,String cryptoNet); 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 fbab252..916e6c1 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/views/CryptoCoinBalanceViewHolder.java +++ b/app/src/main/java/cy/agorise/crystalwallet/views/CryptoCoinBalanceViewHolder.java @@ -69,66 +69,81 @@ public class CryptoCoinBalanceViewHolder extends RecyclerView.ViewHolder { this.clear(); } else { //Retrieves the preferred currency selected by the user - LiveData preferedCurrencySetting = CrystalDatabase.getAppDatabase(this.context).generalSettingDao().getByName(GeneralSetting.SETTING_NAME_PREFERRED_CURRENCY); + final LiveData preferedCurrencySetting = CrystalDatabase.getAppDatabase(this.context).generalSettingDao().getByName(GeneralSetting.SETTING_NAME_PREFERRED_CURRENCY); //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(balanceString); + //final CryptoCurrency currencyFrom = CrystalDatabase.getAppDatabase(context).cryptoCurrencyDao().getById(balance.getCryptoCurrencyId()); + LiveData currencyFromLD = CrystalDatabase.getAppDatabase(context).cryptoCurrencyDao().getLDById(balance.getCryptoCurrencyId()); - //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() { + + currencyFromLD.observe((LifecycleOwner) this.context, new Observer() { @Override - public void onChanged(@Nullable GeneralSetting generalSetting) { - if (generalSetting != null) { - //Gets the currency object of the preferred currency - LiveData currencyToLiveData = CrystalDatabase.getAppDatabase(context).cryptoCurrencyDao().getLiveDataByName(generalSetting.getValue()); + public void onChanged(@Nullable final CryptoCurrency currencyFrom) { + //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(balanceString); - currencyToLiveData.observe((LifecycleOwner) context, new Observer() { - @Override - public void onChanged(@Nullable CryptoCurrency cryptoCurrency) { - if (cryptoCurrency != null) { - CryptoCurrency currencyTo = cryptoCurrency; + //Observes the preferred currency of the user. If the user changes it, this will change the equivalent value + preferedCurrencySetting.observe((LifecycleOwner) context, new Observer() { + @Override + public void onChanged(@Nullable GeneralSetting generalSetting) { + if (generalSetting != null) { + //Gets the currency object of the preferred currency + LiveData currencyToLiveData = CrystalDatabase.getAppDatabase(context).cryptoCurrencyDao().getLiveDataByName(generalSetting.getValue()); - //Retrieves the equivalent value of this balance using the "From" currency and the "To" currency - LiveData currencyEquivalenceLiveData = CrystalDatabase.getAppDatabase(context) - .cryptoCurrencyEquivalenceDao().getByFromTo( - currencyTo.getId(), - currencyFrom.getId() + currencyToLiveData.observe((LifecycleOwner) context, new Observer() { + @Override + public void onChanged(@Nullable CryptoCurrency cryptoCurrency) { + if (cryptoCurrency != null) { + CryptoCurrency currencyTo = cryptoCurrency; - ); + //Retrieves the equivalent value of this balance using the "From" currency and the "To" currency + LiveData currencyEquivalenceLiveData = CrystalDatabase.getAppDatabase(context) + .cryptoCurrencyEquivalenceDao().getByFromTo( + currencyTo.getId(), + currencyFrom.getId() - //Observes the equivalent value. If the equivalent value changes, this will keep the value showed correct - currencyEquivalenceLiveData.observe((LifecycleOwner) context, new Observer() { - @Override - public void onChanged(@Nullable CryptoCurrencyEquivalence cryptoCurrencyEquivalence) { - if (cryptoCurrencyEquivalence != null) { - CryptoCurrency toCurrency = CrystalDatabase.getAppDatabase(context).cryptoCurrencyDao().getById(cryptoCurrencyEquivalence.getFromCurrencyId()); - double equivalentValue = 0; - String equivalenceString = ""; - if (cryptoCurrencyEquivalence.getValue() > 0) { - equivalentValue = (balance.getBalance() / Math.pow(10, currencyFrom.getPrecision())) / - (cryptoCurrencyEquivalence.getValue() / Math.pow(10, toCurrency.getPrecision())); - equivalenceString = String.format( - "%.2f", - equivalentValue ); - } else { - equivalentValue = 0; - equivalenceString = "0"; - } - cryptoNetBalanceViewHolder.setEquivalentBalance(balance,equivalentValue); - cryptoCoinBalanceEquivalence.setText( - equivalenceString + " " + toCurrency.getName()); - } + //Observes the equivalent value. If the equivalent value changes, this will keep the value showed correct + currencyEquivalenceLiveData.observe((LifecycleOwner) context, new Observer() { + @Override + public void onChanged(final @Nullable CryptoCurrencyEquivalence cryptoCurrencyEquivalence) { + if (cryptoCurrencyEquivalence != null) { + LiveData toCurrencyLD = CrystalDatabase.getAppDatabase(context).cryptoCurrencyDao().getLDById(cryptoCurrencyEquivalence.getFromCurrencyId()); + + toCurrencyLD.observe((LifecycleOwner) context, new Observer() { + @Override + public void onChanged(@Nullable CryptoCurrency toCurrency) { + + double equivalentValue = 0; + String equivalenceString = ""; + if (cryptoCurrencyEquivalence.getValue() > 0) { + equivalentValue = (balance.getBalance() / Math.pow(10, currencyFrom.getPrecision())) / + (cryptoCurrencyEquivalence.getValue() / Math.pow(10, toCurrency.getPrecision())); + equivalenceString = String.format( + "%.2f", + equivalentValue + ); + } else { + equivalentValue = 0; + equivalenceString = "0"; + } + + cryptoNetBalanceViewHolder.setEquivalentBalance(balance,equivalentValue); + cryptoCoinBalanceEquivalence.setText( + equivalenceString + " " + toCurrency.getName()); + } + }); + } + } + }); } - }); - } + } + }); } - }); - } + } + }); } }); }