From 8bec42de1a8052a38b20d44a651fbeba523534fe Mon Sep 17 00:00:00 2001 From: henry Date: Wed, 15 Nov 2017 21:47:11 -0400 Subject: [PATCH] Graphene Api with equivalent value using a name for the base asset --- .../apigenerator/GrapheneApiGenerator.java | 42 ++++++++++++++++++- .../crystalwallet/dao/BitsharesAssetDao.java | 2 +- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/cy/agorise/crystalwallet/apigenerator/GrapheneApiGenerator.java b/app/src/main/java/cy/agorise/crystalwallet/apigenerator/GrapheneApiGenerator.java index 0cb4d00..926292d 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/apigenerator/GrapheneApiGenerator.java +++ b/app/src/main/java/cy/agorise/crystalwallet/apigenerator/GrapheneApiGenerator.java @@ -559,7 +559,7 @@ public abstract class GrapheneApiGenerator { * @param context The android context of this application */ public static void getEquivalentValue(BitsharesAsset baseAsset, - List quoteAssets,Context context){ + final List quoteAssets, final Context context){ for(BitsharesAsset quoteAsset : quoteAssets){ WebSocketThread thread = new WebSocketThread(new GetLimitOrders(baseAsset.getBitsharesId(), quoteAsset.getBitsharesId(), 10, new EquivalentValueListener(baseAsset, @@ -568,6 +568,46 @@ public abstract class GrapheneApiGenerator { } } + public static void getEquivalenValue(String baseAssetName, final List quoteAssets, final Context context){ + CrystalDatabase db = CrystalDatabase.getAppDatabase(context); + final CryptoCurrencyDao cryptoCurrencyDao = db.cryptoCurrencyDao(); + final BitsharesAssetDao bitsharesAssetDao = db.bitsharesAssetDao(); + CryptoCurrency baseCurrency = cryptoCurrencyDao.getByName(baseAssetName); + BitsharesAssetInfo info = null; + if(baseCurrency != null){ + info = db.bitsharesAssetDao().getBitsharesAssetInfo(baseCurrency.getId()); + } + if(baseCurrency == null || info == null){ + ApiRequest getAssetRequest = new ApiRequest(1, new ApiRequestListener() { + @Override + public void success(Object answer, int idPetition) { + if(answer instanceof BitsharesAsset){ + BitsharesAssetInfo info = new BitsharesAssetInfo((BitsharesAsset) answer); + long cryptoCurrencyId = cryptoCurrencyDao.insertCryptoCurrency((CryptoCurrency)answer )[0]; + info.setCryptoCurrencyId(cryptoCurrencyId); + bitsharesAssetDao.insertBitsharesAssetInfo(info); + GrapheneApiGenerator.getEquivalentValue((BitsharesAsset) answer, quoteAssets, context); + } + } + + @Override + public void fail(int idPetition) { + //TODO fail asset petition, the base asset is not an asset in bitshares, or there is no connection to the server + } + }); + ArrayList names = new ArrayList<>(); + names.add(baseAssetName); + GrapheneApiGenerator.getAssetByName(names,getAssetRequest); + + }else { + BitsharesAsset baseAsset = new BitsharesAsset(baseCurrency); + baseAsset.loadInfo(info); + getEquivalentValue(baseAsset,quoteAssets,context); + } + + + } + /** * Listener of the equivalent value the answer is stored in the database, for use in conjuntion with LiveData */ diff --git a/app/src/main/java/cy/agorise/crystalwallet/dao/BitsharesAssetDao.java b/app/src/main/java/cy/agorise/crystalwallet/dao/BitsharesAssetDao.java index 96f4584..75662b8 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/dao/BitsharesAssetDao.java +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/BitsharesAssetDao.java @@ -20,7 +20,7 @@ public interface BitsharesAssetDao { LiveData> getAll(); @Query("SELECT * FROM bitshares_asset WHERE crypto_curreny_id = :cryptoCurrencyId") - LiveData getBitsharesAssetInfo(long cryptoCurrencyId); + BitsharesAssetInfo getBitsharesAssetInfo(long cryptoCurrencyId); @Query("SELECT * FROM bitshares_asset WHERE crypto_curreny_id = :cryptoCurrencyId") BitsharesAssetInfo getBitsharesAssetInfoFromCurrencyId(long cryptoCurrencyId);