- 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>() {
@Override
public void onChanged(@Nullable GeneralSetting generalSetting) {
CryptoCurrency preferredCurrency = CrystalDatabase.getAppDatabase(service).cryptoCurrencyDao().getByNameAndCryptoNet("EUR", CryptoNet.BITSHARES.name());
public void onChanged(final @Nullable GeneralSetting generalSetting) {
if (generalSetting != null) {
CryptoCurrency preferredCurrency = CrystalDatabase.getAppDatabase(service).cryptoCurrencyDao().getByNameAndCryptoNet("EUR", CryptoNet.BITSHARES.name());
if (preferredCurrency != null) {
BitsharesAssetInfo preferredCurrencyBitsharesInfo = CrystalDatabase.getAppDatabase(service).bitsharesAssetDao().getBitsharesAssetInfoFromCurrencyId(preferredCurrency.getId());
if (preferredCurrency != null) {
BitsharesAssetInfo preferredCurrencyBitsharesInfo = CrystalDatabase.getAppDatabase(service).bitsharesAssetDao().getBitsharesAssetInfoFromCurrencyId(preferredCurrency.getId());
if (preferredCurrencyBitsharesInfo != null) {
final BitsharesAsset preferredCurrencyBitshareAsset = new BitsharesAsset(preferredCurrency);
preferredCurrencyBitshareAsset.loadInfo(preferredCurrencyBitsharesInfo);
if (preferredCurrencyBitsharesInfo != null) {
final BitsharesAsset preferredCurrencyBitshareAsset = new BitsharesAsset(preferredCurrency);
preferredCurrencyBitshareAsset.loadInfo(preferredCurrencyBitsharesInfo);
//Loading "from" currencies
final LiveData<List<BitsharesAssetInfo>> bitsharesAssetInfo =
CrystalDatabase.getAppDatabase(service).bitsharesAssetDao().getAll();
//Loading "from" currencies
final LiveData<List<BitsharesAssetInfo>> bitsharesAssetInfo =
CrystalDatabase.getAppDatabase(service).bitsharesAssetDao().getAll();
bitsharesAssetInfo.observe(service, new Observer<List<BitsharesAssetInfo>>() {
@Override
public void onChanged(@Nullable List<BitsharesAssetInfo> bitsharesAssetInfos) {
List<BitsharesAsset> bitsharesAssets = new ArrayList<BitsharesAsset>();
List<Long> currenciesIds = new ArrayList<Long>();
for (BitsharesAssetInfo bitsharesAssetInfo : bitsharesAssetInfos) {
currenciesIds.add(bitsharesAssetInfo.getCryptoCurrencyId());
bitsharesAssetInfo.observe(service, new Observer<List<BitsharesAssetInfo>>() {
@Override
public void onChanged(@Nullable List<BitsharesAssetInfo> bitsharesAssetInfos) {
List<BitsharesAsset> bitsharesAssets = new ArrayList<BitsharesAsset>();
List<Long> currenciesIds = new ArrayList<Long>();
for (BitsharesAssetInfo bitsharesAssetInfo : bitsharesAssetInfos) {
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{
private boolean keepLoadingEquivalences = true;
private LifecycleService service;
private BitsharesAsset fromAsset;
private String fromAsset;
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.fromAsset = fromAsset;
this.bitsharesAssets = bitsharesAssets;
@ -30,7 +30,7 @@ public class EquivalencesThread extends Thread{
while(this.keepLoadingEquivalences){
try {
GrapheneApiGenerator.getEquivalentValue(fromAsset, bitsharesAssets, this.service);
GrapheneApiGenerator.getEquivalenValue(fromAsset, bitsharesAssets, this.service);
Log.i("Equivalences Thread", "In loop");
Thread.sleep(1000);
} catch (InterruptedException e) {

View file

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