- Changing main thread db queries to be executed in a room observer way

This commit is contained in:
Javier Varona 2018-10-10 21:57:40 -04:00
parent 303011e419
commit 62edb37143
2 changed files with 67 additions and 49 deletions

View file

@ -23,6 +23,9 @@ public interface CryptoCurrencyDao {
@Query("SELECT * FROM crypto_currency WHERE id = :id") @Query("SELECT * FROM crypto_currency WHERE id = :id")
CryptoCurrency getById(long id); CryptoCurrency getById(long id);
@Query("SELECT * FROM crypto_currency WHERE id = :id")
LiveData<CryptoCurrency> getLDById(long id);
@Query("SELECT * FROM crypto_currency WHERE name = :name AND crypto_net = :cryptoNet") @Query("SELECT * FROM crypto_currency WHERE name = :name AND crypto_net = :cryptoNet")
CryptoCurrency getByNameAndCryptoNet(String name,String cryptoNet); CryptoCurrency getByNameAndCryptoNet(String name,String cryptoNet);

View file

@ -69,16 +69,22 @@ public class CryptoCoinBalanceViewHolder extends RecyclerView.ViewHolder {
this.clear(); this.clear();
} else { } else {
//Retrieves the preferred currency selected by the user //Retrieves the preferred currency selected by the user
LiveData<GeneralSetting> preferedCurrencySetting = CrystalDatabase.getAppDatabase(this.context).generalSettingDao().getByName(GeneralSetting.SETTING_NAME_PREFERRED_CURRENCY); final LiveData<GeneralSetting> preferedCurrencySetting = CrystalDatabase.getAppDatabase(this.context).generalSettingDao().getByName(GeneralSetting.SETTING_NAME_PREFERRED_CURRENCY);
//Retrieves the currency of this balance //Retrieves the currency of this balance
final CryptoCurrency currencyFrom = CrystalDatabase.getAppDatabase(context).cryptoCurrencyDao().getById(balance.getCryptoCurrencyId()); //final CryptoCurrency currencyFrom = CrystalDatabase.getAppDatabase(context).cryptoCurrencyDao().getById(balance.getCryptoCurrencyId());
LiveData<CryptoCurrency> currencyFromLD = CrystalDatabase.getAppDatabase(context).cryptoCurrencyDao().getLDById(balance.getCryptoCurrencyId());
currencyFromLD.observe((LifecycleOwner) this.context, new Observer<CryptoCurrency>() {
@Override
public void onChanged(@Nullable final CryptoCurrency currencyFrom) {
//Sets the name and amount of the balance in the view //Sets the name and amount of the balance in the view
String balanceString = String.format("%.2f",balance.getBalance()/Math.pow(10,currencyFrom.getPrecision())); String balanceString = String.format("%.2f",balance.getBalance()/Math.pow(10,currencyFrom.getPrecision()));
cryptoCoinName.setText(currencyFrom.getName()); cryptoCoinName.setText(currencyFrom.getName());
cryptoCoinBalance.setText(balanceString); cryptoCoinBalance.setText(balanceString);
//Observes the preferred currency of the user. If the user changes it, this will change the equivalent value //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<GeneralSetting>() { preferedCurrencySetting.observe((LifecycleOwner) context, new Observer<GeneralSetting>() {
@Override @Override
public void onChanged(@Nullable GeneralSetting generalSetting) { public void onChanged(@Nullable GeneralSetting generalSetting) {
if (generalSetting != null) { if (generalSetting != null) {
@ -102,9 +108,14 @@ public class CryptoCoinBalanceViewHolder extends RecyclerView.ViewHolder {
//Observes the equivalent value. If the equivalent value changes, this will keep the value showed correct //Observes the equivalent value. If the equivalent value changes, this will keep the value showed correct
currencyEquivalenceLiveData.observe((LifecycleOwner) context, new Observer<CryptoCurrencyEquivalence>() { currencyEquivalenceLiveData.observe((LifecycleOwner) context, new Observer<CryptoCurrencyEquivalence>() {
@Override @Override
public void onChanged(@Nullable CryptoCurrencyEquivalence cryptoCurrencyEquivalence) { public void onChanged(final @Nullable CryptoCurrencyEquivalence cryptoCurrencyEquivalence) {
if (cryptoCurrencyEquivalence != null) { if (cryptoCurrencyEquivalence != null) {
CryptoCurrency toCurrency = CrystalDatabase.getAppDatabase(context).cryptoCurrencyDao().getById(cryptoCurrencyEquivalence.getFromCurrencyId()); LiveData<CryptoCurrency> toCurrencyLD = CrystalDatabase.getAppDatabase(context).cryptoCurrencyDao().getLDById(cryptoCurrencyEquivalence.getFromCurrencyId());
toCurrencyLD.observe((LifecycleOwner) context, new Observer<CryptoCurrency>() {
@Override
public void onChanged(@Nullable CryptoCurrency toCurrency) {
double equivalentValue = 0; double equivalentValue = 0;
String equivalenceString = ""; String equivalenceString = "";
if (cryptoCurrencyEquivalence.getValue() > 0) { if (cryptoCurrencyEquivalence.getValue() > 0) {
@ -123,7 +134,6 @@ public class CryptoCoinBalanceViewHolder extends RecyclerView.ViewHolder {
cryptoCoinBalanceEquivalence.setText( cryptoCoinBalanceEquivalence.setText(
equivalenceString + " " + toCurrency.getName()); equivalenceString + " " + toCurrency.getName());
} }
}
}); });
} }
} }
@ -133,4 +143,9 @@ public class CryptoCoinBalanceViewHolder extends RecyclerView.ViewHolder {
}); });
} }
} }
});
}
});
}
}
} }