- Equivalence values working!

This commit is contained in:
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,45 +72,50 @@ 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) {
CryptoCurrency preferredCurrency = CrystalDatabase.getAppDatabase(service).cryptoCurrencyDao().getByNameAndCryptoNet("EUR", CryptoNet.BITSHARES.name()); if (generalSetting != null) {
CryptoCurrency preferredCurrency = CrystalDatabase.getAppDatabase(service).cryptoCurrencyDao().getByNameAndCryptoNet("EUR", CryptoNet.BITSHARES.name());
if (preferredCurrency != null) { if (preferredCurrency != null) {
BitsharesAssetInfo preferredCurrencyBitsharesInfo = CrystalDatabase.getAppDatabase(service).bitsharesAssetDao().getBitsharesAssetInfoFromCurrencyId(preferredCurrency.getId()); BitsharesAssetInfo preferredCurrencyBitsharesInfo = CrystalDatabase.getAppDatabase(service).bitsharesAssetDao().getBitsharesAssetInfoFromCurrencyId(preferredCurrency.getId());
if (preferredCurrencyBitsharesInfo != null) { if (preferredCurrencyBitsharesInfo != null) {
final BitsharesAsset preferredCurrencyBitshareAsset = new BitsharesAsset(preferredCurrency); final BitsharesAsset preferredCurrencyBitshareAsset = new BitsharesAsset(preferredCurrency);
preferredCurrencyBitshareAsset.loadInfo(preferredCurrencyBitsharesInfo); preferredCurrencyBitshareAsset.loadInfo(preferredCurrencyBitsharesInfo);
//Loading "from" currencies //Loading "from" currencies
final LiveData<List<BitsharesAssetInfo>> bitsharesAssetInfo = final LiveData<List<BitsharesAssetInfo>> bitsharesAssetInfo =
CrystalDatabase.getAppDatabase(service).bitsharesAssetDao().getAll(); CrystalDatabase.getAppDatabase(service).bitsharesAssetDao().getAll();
bitsharesAssetInfo.observe(service, new Observer<List<BitsharesAssetInfo>>() { bitsharesAssetInfo.observe(service, new Observer<List<BitsharesAssetInfo>>() {
@Override @Override
public void onChanged(@Nullable List<BitsharesAssetInfo> bitsharesAssetInfos) { public void onChanged(@Nullable List<BitsharesAssetInfo> bitsharesAssetInfos) {
List<BitsharesAsset> bitsharesAssets = new ArrayList<BitsharesAsset>(); List<BitsharesAsset> bitsharesAssets = new ArrayList<BitsharesAsset>();
List<Long> currenciesIds = new ArrayList<Long>(); List<Long> currenciesIds = new ArrayList<Long>();
for (BitsharesAssetInfo bitsharesAssetInfo : bitsharesAssetInfos) { for (BitsharesAssetInfo bitsharesAssetInfo : bitsharesAssetInfos) {
currenciesIds.add(bitsharesAssetInfo.getCryptoCurrencyId()); currenciesIds.add(bitsharesAssetInfo.getCryptoCurrencyId());
}
;
List<CryptoCurrency> bitsharesCurrencies = CrystalDatabase.getAppDatabase(service).cryptoCurrencyDao().getByIds(currenciesIds);
BitsharesAsset nextAsset;
for (int i = 0; i < bitsharesCurrencies.size(); i++) {
CryptoCurrency nextCurrency = bitsharesCurrencies.get(i);
BitsharesAssetInfo nextBitsharesInfo = bitsharesAssetInfos.get(i);
nextAsset = new BitsharesAsset(nextCurrency);
nextAsset.loadInfo(nextBitsharesInfo);
bitsharesAssets.add(nextAsset);
}
if (LoadEquivalencesThread != null) {
LoadEquivalencesThread.stopLoadingEquivalences();
}
;
LoadEquivalencesThread = new EquivalencesThread(service, generalSetting.getValue(), bitsharesAssets);
LoadEquivalencesThread.start();
} }
; });
List<CryptoCurrency> bitsharesCurrencies = CrystalDatabase.getAppDatabase(service).cryptoCurrencyDao().getByIds(currenciesIds); }
BitsharesAsset nextAsset;
for (int i = 0; i < bitsharesCurrencies.size(); i++) {
CryptoCurrency nextCurrency = bitsharesCurrencies.get(i);
BitsharesAssetInfo nextBitsharesInfo = bitsharesAssetInfos.get(i);
nextAsset = new BitsharesAsset(nextCurrency);
nextAsset.loadInfo(nextBitsharesInfo);
bitsharesAssets.add(nextAsset);
}
if (LoadEquivalencesThread != null){LoadEquivalencesThread.stopLoadingEquivalences();};
LoadEquivalencesThread = new EquivalencesThread(service, preferredCurrencyBitshareAsset, bitsharesAssets);
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());
} }
} }
}); });