- 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")
|
@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);
|
||||||
|
|
||||||
|
|
|
@ -69,66 +69,81 @@ 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());
|
||||||
//Sets the name and amount of the balance in the view
|
LiveData<CryptoCurrency> currencyFromLD = CrystalDatabase.getAppDatabase(context).cryptoCurrencyDao().getLDById(balance.getCryptoCurrencyId());
|
||||||
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>() {
|
currencyFromLD.observe((LifecycleOwner) this.context, new Observer<CryptoCurrency>() {
|
||||||
@Override
|
@Override
|
||||||
public void onChanged(@Nullable GeneralSetting generalSetting) {
|
public void onChanged(@Nullable final CryptoCurrency currencyFrom) {
|
||||||
if (generalSetting != null) {
|
//Sets the name and amount of the balance in the view
|
||||||
//Gets the currency object of the preferred currency
|
String balanceString = String.format("%.2f",balance.getBalance()/Math.pow(10,currencyFrom.getPrecision()));
|
||||||
LiveData<CryptoCurrency> currencyToLiveData = CrystalDatabase.getAppDatabase(context).cryptoCurrencyDao().getLiveDataByName(generalSetting.getValue());
|
cryptoCoinName.setText(currencyFrom.getName());
|
||||||
|
cryptoCoinBalance.setText(balanceString);
|
||||||
|
|
||||||
currencyToLiveData.observe((LifecycleOwner) context, new Observer<CryptoCurrency>() {
|
//Observes the preferred currency of the user. If the user changes it, this will change the equivalent value
|
||||||
@Override
|
preferedCurrencySetting.observe((LifecycleOwner) context, new Observer<GeneralSetting>() {
|
||||||
public void onChanged(@Nullable CryptoCurrency cryptoCurrency) {
|
@Override
|
||||||
if (cryptoCurrency != null) {
|
public void onChanged(@Nullable GeneralSetting generalSetting) {
|
||||||
CryptoCurrency currencyTo = cryptoCurrency;
|
if (generalSetting != null) {
|
||||||
|
//Gets the currency object of the preferred currency
|
||||||
|
LiveData<CryptoCurrency> currencyToLiveData = CrystalDatabase.getAppDatabase(context).cryptoCurrencyDao().getLiveDataByName(generalSetting.getValue());
|
||||||
|
|
||||||
//Retrieves the equivalent value of this balance using the "From" currency and the "To" currency
|
currencyToLiveData.observe((LifecycleOwner) context, new Observer<CryptoCurrency>() {
|
||||||
LiveData<CryptoCurrencyEquivalence> currencyEquivalenceLiveData = CrystalDatabase.getAppDatabase(context)
|
@Override
|
||||||
.cryptoCurrencyEquivalenceDao().getByFromTo(
|
public void onChanged(@Nullable CryptoCurrency cryptoCurrency) {
|
||||||
currencyTo.getId(),
|
if (cryptoCurrency != null) {
|
||||||
currencyFrom.getId()
|
CryptoCurrency currencyTo = cryptoCurrency;
|
||||||
|
|
||||||
);
|
//Retrieves the equivalent value of this balance using the "From" currency and the "To" currency
|
||||||
|
LiveData<CryptoCurrencyEquivalence> 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<CryptoCurrencyEquivalence>() {
|
|
||||||
@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);
|
//Observes the equivalent value. If the equivalent value changes, this will keep the value showed correct
|
||||||
cryptoCoinBalanceEquivalence.setText(
|
currencyEquivalenceLiveData.observe((LifecycleOwner) context, new Observer<CryptoCurrencyEquivalence>() {
|
||||||
equivalenceString + " " + toCurrency.getName());
|
@Override
|
||||||
}
|
public void onChanged(final @Nullable CryptoCurrencyEquivalence cryptoCurrencyEquivalence) {
|
||||||
|
if (cryptoCurrencyEquivalence != null) {
|
||||||
|
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) {
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue