- Equivalence values working!

master
Javier Varona 2017-11-15 23:17:34 -04:00
parent 8bec42de1a
commit 8745b32446
3 changed files with 56 additions and 42 deletions

View File

@ -72,7 +72,8 @@ public class CrystalWalletService extends LifecycleService {
preferredCurrencySetting.observe(service, new Observer<GeneralSetting>() { preferredCurrencySetting.observe(service, new Observer<GeneralSetting>() {
@Override @Override
public void onChanged(@Nullable GeneralSetting generalSetting) { public void onChanged(final @Nullable GeneralSetting generalSetting) {
if (generalSetting != null) {
CryptoCurrency preferredCurrency = CrystalDatabase.getAppDatabase(service).cryptoCurrencyDao().getByNameAndCryptoNet("EUR", CryptoNet.BITSHARES.name()); CryptoCurrency preferredCurrency = CrystalDatabase.getAppDatabase(service).cryptoCurrencyDao().getByNameAndCryptoNet("EUR", CryptoNet.BITSHARES.name());
if (preferredCurrency != null) { if (preferredCurrency != null) {
@ -106,14 +107,18 @@ public class CrystalWalletService extends LifecycleService {
bitsharesAssets.add(nextAsset); bitsharesAssets.add(nextAsset);
} }
if (LoadEquivalencesThread != null){LoadEquivalencesThread.stopLoadingEquivalences();}; if (LoadEquivalencesThread != null) {
LoadEquivalencesThread = new EquivalencesThread(service, preferredCurrencyBitshareAsset, bitsharesAssets); LoadEquivalencesThread.stopLoadingEquivalences();
}
;
LoadEquivalencesThread = new EquivalencesThread(service, generalSetting.getValue(), bitsharesAssets);
LoadEquivalencesThread.start(); LoadEquivalencesThread.start();
} }
}); });
} }
} }
} }
}
}); });
} }

View File

@ -15,10 +15,10 @@ import cy.agorise.crystalwallet.models.BitsharesAsset;
public class EquivalencesThread extends Thread{ public class EquivalencesThread extends Thread{
private boolean keepLoadingEquivalences = true; private boolean keepLoadingEquivalences = true;
private LifecycleService service; private LifecycleService service;
private BitsharesAsset fromAsset; private String fromAsset;
private List<BitsharesAsset> bitsharesAssets; private List<BitsharesAsset> bitsharesAssets;
public EquivalencesThread(LifecycleService service, BitsharesAsset fromAsset, List<BitsharesAsset> bitsharesAssets){ public EquivalencesThread(LifecycleService service, String fromAsset, List<BitsharesAsset> bitsharesAssets){
this.service = service; this.service = service;
this.fromAsset = fromAsset; this.fromAsset = fromAsset;
this.bitsharesAssets = bitsharesAssets; this.bitsharesAssets = bitsharesAssets;
@ -30,7 +30,7 @@ public class EquivalencesThread extends Thread{
while(this.keepLoadingEquivalences){ while(this.keepLoadingEquivalences){
try { try {
GrapheneApiGenerator.getEquivalentValue(fromAsset, bitsharesAssets, this.service); GrapheneApiGenerator.getEquivalenValue(fromAsset, bitsharesAssets, this.service);
Log.i("Equivalences Thread", "In loop"); Log.i("Equivalences Thread", "In loop");
Thread.sleep(1000); Thread.sleep(1000);
} catch (InterruptedException e) { } catch (InterruptedException e) {

View File

@ -70,8 +70,9 @@ public class CryptoCoinBalanceViewHolder extends RecyclerView.ViewHolder {
//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 //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()); 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 //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) this.context, new Observer<GeneralSetting>() {
@ -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 //Retrieves the equivalent value of this balance using the "From" currency and the "To" currency
LiveData<CryptoCurrencyEquivalence> currencyEquivalenceLiveData = CrystalDatabase.getAppDatabase(context) LiveData<CryptoCurrencyEquivalence> currencyEquivalenceLiveData = CrystalDatabase.getAppDatabase(context)
.cryptoCurrencyEquivalenceDao().getByFromTo( .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 //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 @Override
public void onChanged(@Nullable CryptoCurrencyEquivalence cryptoCurrencyEquivalence) { public void onChanged(@Nullable CryptoCurrencyEquivalence cryptoCurrencyEquivalence) {
if (cryptoCurrencyEquivalence != null) { if (cryptoCurrencyEquivalence != null) {
CryptoCurrency toCurrency = CrystalDatabase.getAppDatabase(context).cryptoCurrencyDao().getById(cryptoCurrencyEquivalence.getToCurrencyId()); CryptoCurrency toCurrency = CrystalDatabase.getAppDatabase(context).cryptoCurrencyDao().getById(cryptoCurrencyEquivalence.getFromCurrencyId());
cryptoCoinBalanceEquivalence.setText(cryptoCurrencyEquivalence.getValue() + " " + toCurrency.getName()); String equivalenceString = String.format(
"%.2f",
(balance.getBalance()/Math.pow(10,currencyFrom.getPrecision()))/
(cryptoCurrencyEquivalence.getValue()/Math.pow(10,toCurrency.getPrecision()))
);
cryptoCoinBalanceEquivalence.setText(
equivalenceString + " " + toCurrency.getName());
} }
} }
}); });