- Making the service to ask for equivalent values every minute

master
Javier Varona 2017-11-08 22:42:22 -04:00
parent 3a27dcbe23
commit 598c58a83f
4 changed files with 91 additions and 0 deletions

View File

@ -22,6 +22,9 @@ public interface BitsharesAssetDao {
@Query("SELECT * FROM bitshares_asset WHERE crypto_curreny_id = :cryptoCurrencyId")
LiveData<BitsharesAssetInfo> getBitsharesAssetInfo(long cryptoCurrencyId);
@Query("SELECT * FROM bitshares_asset WHERE crypto_curreny_id = :cryptoCurrencyId")
BitsharesAssetInfo getBitsharesAssetInfoFromCurrencyId(long cryptoCurrencyId);
@Query("SELECT * FROM bitshares_asset WHERE bitshares_id = :bitsharesId")
BitsharesAssetInfo getBitsharesAssetInfoById(String bitsharesId);

View File

@ -11,6 +11,7 @@ import java.util.List;
import cy.agorise.crystalwallet.models.CryptoCoinBalance;
import cy.agorise.crystalwallet.models.CryptoNetAccount;
import cy.agorise.crystalwallet.models.CryptoNetBalance;
import cy.agorise.crystalwallet.enums.CryptoNet;
/**
* Created by Henry Varona on 10/9/2017.
@ -28,6 +29,9 @@ public interface CryptoCoinBalanceDao {
@Query("SELECT * FROM crypto_coin_balance WHERE account_id = :accountId")
LiveData<List<CryptoCoinBalance>> getBalancesFromAccount(long accountId);
@Query("SELECT * FROM crypto_coin_balance WHERE account_id IN (SELECT id FROM crypto_net_account WHERE crypto_net = 'BITSHARES')")
LiveData<List<CryptoCoinBalance>> getBalancesFromBitsharesAccount();
@Query("SELECT * FROM crypto_coin_balance WHERE account_id = :accountId AND crypto_currency_id = :assetId")
CryptoCoinBalance getBalanceFromAccount(long accountId, long assetId);

View File

@ -22,6 +22,9 @@ public interface CryptoCurrencyDao {
@Query("SELECT * FROM crypto_currency WHERE id = :id")
CryptoCurrency getById(long id);
@Query("SELECT * FROM crypto_currency WHERE name = :name AND crypto_net = :cryptoNet")
CryptoCurrency getByNameAndCryptoNet(String name,String cryptoNet);
@Query("SELECT * FROM crypto_currency WHERE id IN (:ids)")
List<CryptoCurrency> getByIds(List<Long> ids);

View File

@ -13,12 +13,20 @@ import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
import cy.agorise.crystalwallet.apigenerator.GrapheneApiGenerator;
import cy.agorise.crystalwallet.cryptonetinforequests.CryptoNetInfoRequests;
import cy.agorise.crystalwallet.dao.CrystalDatabase;
import cy.agorise.crystalwallet.enums.CryptoNet;
import cy.agorise.crystalwallet.manager.BitsharesAccountManager;
import cy.agorise.crystalwallet.models.BitsharesAsset;
import cy.agorise.crystalwallet.models.BitsharesAssetInfo;
import cy.agorise.crystalwallet.models.CryptoCoinBalance;
import cy.agorise.crystalwallet.models.CryptoCurrency;
import cy.agorise.crystalwallet.models.CryptoNetAccount;
import cy.agorise.crystalwallet.models.GeneralSetting;
import cy.agorise.crystalwallet.models.GrapheneAccount;
import cy.agorise.crystalwallet.models.GrapheneAccountInfo;
@ -33,7 +41,9 @@ public class CrystalWalletService extends LifecycleService {
private ServiceHandler mServiceHandler;
private BitsharesAccountManager bitsharesAccountManager;
private Thread LoadAccountTransactionsThread;
private Thread LoadEquivalencesThread;
private boolean keepLoadingAccountTransactions;
private boolean keepLoadingEquivalences;
private CryptoNetInfoRequests cryptoNetInfoRequests;
// Handler that receives messages from the thread
@ -52,6 +62,68 @@ public class CrystalWalletService extends LifecycleService {
}
}
public void loadEquivalentsValues(){
this.keepLoadingEquivalences = true;
final LifecycleService service = this;
//getting the preferred currency of the user
final LiveData<GeneralSetting> preferredCurrencySetting =
CrystalDatabase.getAppDatabase(service).generalSettingDao().getByName(GeneralSetting.SETTING_NAME_PREFERED_CURRENCY);
preferredCurrencySetting.observe(service, new Observer<GeneralSetting>() {
@Override
public void onChanged(@Nullable GeneralSetting generalSetting) {
CryptoCurrency preferredCurrency = CrystalDatabase.getAppDatabase(service).cryptoCurrencyDao().getByNameAndCryptoNet("EUR", CryptoNet.BITSHARES.name());
if (preferredCurrency != null) {
BitsharesAssetInfo preferredCurrencyBitsharesInfo = CrystalDatabase.getAppDatabase(service).bitsharesAssetDao().getBitsharesAssetInfoFromCurrencyId(preferredCurrency.getId());
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();
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);
}
while (keepLoadingEquivalences) {
try {
GrapheneApiGenerator.getEquivalentValue(preferredCurrencyBitshareAsset, bitsharesAssets, service);
Thread.sleep(60000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
});
}
}
}
});
}
public void loadAccountTransactions(){
this.keepLoadingAccountTransactions = true;
final CrystalWalletService thisService = this;
@ -112,6 +184,15 @@ public class CrystalWalletService extends LifecycleService {
LoadAccountTransactionsThread.start();
}
if (LoadEquivalencesThread == null) {
LoadEquivalencesThread = new Thread() {
public void run() {
loadEquivalentsValues();
}
};
LoadEquivalencesThread.start();
}
// If we get killed, after returning from here, restart
return START_STICKY;
}