- Changing main thread db queries to be executed in a room observer way
This commit is contained in:
parent
303011e419
commit
62edb37143
2 changed files with 67 additions and 49 deletions
|
@ -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<CryptoCurrency> getLDById(long id);
|
||||
|
||||
@Query("SELECT * FROM crypto_currency WHERE name = :name AND crypto_net = :cryptoNet")
|
||||
CryptoCurrency getByNameAndCryptoNet(String name,String cryptoNet);
|
||||
|
||||
|
|
|
@ -69,16 +69,22 @@ public class CryptoCoinBalanceViewHolder extends RecyclerView.ViewHolder {
|
|||
this.clear();
|
||||
} else {
|
||||
//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
|
||||
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
|
||||
String balanceString = String.format("%.2f",balance.getBalance()/Math.pow(10,currencyFrom.getPrecision()));
|
||||
cryptoCoinName.setText(currencyFrom.getName());
|
||||
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<GeneralSetting>() {
|
||||
preferedCurrencySetting.observe((LifecycleOwner) context, new Observer<GeneralSetting>() {
|
||||
@Override
|
||||
public void onChanged(@Nullable GeneralSetting generalSetting) {
|
||||
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
|
||||
currencyEquivalenceLiveData.observe((LifecycleOwner) context, new Observer<CryptoCurrencyEquivalence>() {
|
||||
@Override
|
||||
public void onChanged(@Nullable CryptoCurrencyEquivalence cryptoCurrencyEquivalence) {
|
||||
public void onChanged(final @Nullable CryptoCurrencyEquivalence cryptoCurrencyEquivalence) {
|
||||
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;
|
||||
String equivalenceString = "";
|
||||
if (cryptoCurrencyEquivalence.getValue() > 0) {
|
||||
|
@ -123,7 +134,6 @@ public class CryptoCoinBalanceViewHolder extends RecyclerView.ViewHolder {
|
|||
cryptoCoinBalanceEquivalence.setText(
|
||||
equivalenceString + " " + toCurrency.getName());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -133,4 +143,9 @@ public class CryptoCoinBalanceViewHolder extends RecyclerView.ViewHolder {
|
|||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue