From b3f566bf313ddc6fb40136008fe241808d171e60 Mon Sep 17 00:00:00 2001 From: Tanguy Herbron Date: Tue, 17 Apr 2018 01:02:27 +0200 Subject: [PATCH] Fix API limitation problem --- .../HomeActivityFragments/Summary.java | 4 +- .../HomeActivityFragments/Watchlist.java | 18 ++++- .../DataManagers/CurrencyData/Currency.java | 48 ++++++++++---- .../CurrencyData/CurrencyDataRetriever.java | 59 +++++++++++++++++ .../LayoutManagers/HomeLayoutGenerator.java | 65 +++++++++++++++---- .../res/layout/activity_currency_details.xml | 47 ++++++++------ app/src/main/res/layout/cardview_currency.xml | 9 ++- app/src/main/res/values/text_size_arrays.xml | 8 +++ 8 files changed, 207 insertions(+), 51 deletions(-) create mode 100644 app/src/main/res/values/text_size_arrays.xml diff --git a/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivityFragments/Summary.java b/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivityFragments/Summary.java index 37520cc..a51f335 100644 --- a/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivityFragments/Summary.java +++ b/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivityFragments/Summary.java @@ -251,7 +251,7 @@ public class Summary extends Fragment { { final Currency currency = balanceManager.getTotalBalance().get(i); - if(!currency.getSymbol().equals("USD") && ((currency.getBalance() * currency.getValue()) > 0.001 || currency.getHistoryMinutes() == null)) + if(!currency.getSymbol().equals("USD") && ((currency.getBalance() * currency.getValue()) > 0.001)) { currencyLayout.addView(layoutGenerator.getInfoLayout(currency, preferencesManager.getDetailOption(), totalValue, preferencesManager.isBalanceHidden())); } @@ -629,7 +629,7 @@ public class Summary extends Fragment { { for(int i = 0; i < balanceManager.getTotalBalance().size(); i++) { - balance.get(i).updateHistoryMinutes(getActivity(), new Currency.CurrencyCallBack() { + balance.get(i).updatePrice(getActivity(), new Currency.CurrencyCallBack() { @Override public void onSuccess(Currency currency) { countCoins(true, false); diff --git a/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivityFragments/Watchlist.java b/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivityFragments/Watchlist.java index 8994fda..7f66a78 100644 --- a/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivityFragments/Watchlist.java +++ b/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivityFragments/Watchlist.java @@ -236,14 +236,26 @@ public class Watchlist extends Fragment { card.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); card.setOnClickListener(new View.OnClickListener() { @Override - public void onClick(View view) { + public void onClick(final View view) { if(view.findViewById(R.id.collapsableLayout).getVisibility() == View.VISIBLE) { collapseView(view); } else { - extendView(view); + if (currency.getHistoryMinutes() == null) { + currency.updateHistoryMinutes(getActivity(), new Currency.CurrencyCallBack() { + @Override + public void onSuccess(Currency currency) { + extendView(view); + setupLineChart(view, currency); + } + }); + } + else + { + extendView(view); + } } } }); @@ -410,7 +422,7 @@ public class Watchlist extends Fragment { protected Void doInBackground(Void... voids) { for(final Currency currency : watchlistManager.getWatchlist()) { - currency.updateHistoryMinutes(getActivity(), new Currency.CurrencyCallBack() { + currency.updatePrice(getActivity(), new Currency.CurrencyCallBack() { @Override public void onSuccess(final Currency sucessCurrency) { if(getIconUrl(sucessCurrency.getSymbol()) != null) diff --git a/app/src/main/java/com/nauk/coinfolio/DataManagers/CurrencyData/Currency.java b/app/src/main/java/com/nauk/coinfolio/DataManagers/CurrencyData/Currency.java index 3c2fdd9..1fcf88c 100644 --- a/app/src/main/java/com/nauk/coinfolio/DataManagers/CurrencyData/Currency.java +++ b/app/src/main/java/com/nauk/coinfolio/DataManagers/CurrencyData/Currency.java @@ -41,6 +41,8 @@ public class Currency implements Parcelable { private String algorithm; //private String proofType + public Currency() {} + public Currency(String symbol, double balance) { this.symbol = symbol; @@ -95,6 +97,26 @@ public class Currency implements Parcelable { return url; } + public void updatePrice(android.content.Context context, final CurrencyCallBack callBack) + { + dataRetriver = new CurrencyDataRetriever(context); + + dataRetriver.updatePrice(symbol, new CurrencyDataRetriever.PriceCallBack() { + @Override + public void onSuccess(Currency currencyInfo) { + if(currencyInfo != null) + { + setValue(currencyInfo.getValue()); + setDayFluctuation(currencyInfo.getDayFluctuation()); + setDayFluctuationPercentage(currencyInfo.getDayFluctuationPercentage()); + } + Log.d("coinfolio", this.toString()); + + callBack.onSuccess(Currency.this); + } + }); + } + public void updateHistoryMinutes(android.content.Context context, final CurrencyCallBack callBack) { dataRetriver = new CurrencyDataRetriever(context); @@ -104,18 +126,6 @@ public class Currency implements Parcelable { public void onSuccess(List dataChart) { setHistoryMinutes(dataChart); - if(dataChart != null) - { - Log.d("coinfolio", "Success for : " + symbol); - setValue(dataChart.get(dataChart.size() - 1).getClose()); - updateDayFluctuation(); - } - else - { - Log.d("coinfolio", "Error for : " + symbol); - value = NULL; - } - callBack.onSuccess(Currency.this); } @@ -262,6 +272,14 @@ public class Currency implements Parcelable { historyDays = newDataChart; } + public void setDayFluctuationPercentage(float dayFluctuationPercentage) { + this.dayFluctuationPercentage = dayFluctuationPercentage; + } + + public void setDayFluctuation(double dayFluctuation) { + this.dayFluctuation = dayFluctuation; + } + public void setIcon(Bitmap newIcon) { icon = newIcon; @@ -282,6 +300,12 @@ public class Currency implements Parcelable { } } + @Override + public String toString() + { + return symbol + " " + value + " " + dayFluctuation; + } + public interface CurrencyCallBack { void onSuccess(Currency currency); } diff --git a/app/src/main/java/com/nauk/coinfolio/DataManagers/CurrencyData/CurrencyDataRetriever.java b/app/src/main/java/com/nauk/coinfolio/DataManagers/CurrencyData/CurrencyDataRetriever.java index c18e916..85f2860 100644 --- a/app/src/main/java/com/nauk/coinfolio/DataManagers/CurrencyData/CurrencyDataRetriever.java +++ b/app/src/main/java/com/nauk/coinfolio/DataManagers/CurrencyData/CurrencyDataRetriever.java @@ -31,6 +31,7 @@ public class CurrencyDataRetriever { private String minuteHistoryUrl = "https://min-api.cryptocompare.com/data/histominute"; private String hourHistoryUrl = "https://min-api.cryptocompare.com/data/histohour"; private String dayHistoryUrl = "https://min-api.cryptocompare.com/data/histoday"; + private String priceUrl = "https://min-api.cryptocompare.com/data/pricemultifull?fsyms="; private RequestQueue requestQueue; @@ -94,6 +95,27 @@ public class CurrencyDataRetriever { requestQueue.add(stringRequest); } + private void updatePrice(final String symbolCurrencyFrom, String symbolCurrencyTo, final PriceCallBack callBack) + { + String requestUrl = priceUrl + symbolCurrencyFrom + "&tsyms=" + symbolCurrencyTo; + + StringRequest stringRequest = new StringRequest(Request.Method.GET, requestUrl, + new Response.Listener() { + @Override + public void onResponse(String response) { + callBack.onSuccess(processPriceResult(response)); + } + }, + new Response.ErrorListener() { + @Override + public void onErrorResponse(VolleyError error) { + + } + }); + + requestQueue.add(stringRequest); + } + private String getRequestUrl(int timeUnit, String symbolCurrencyFrom, String symbolCyrrencyTo) { String requestUrl = null; @@ -114,6 +136,27 @@ public class CurrencyDataRetriever { return requestUrl; } + private Currency processPriceResult(String response) + { + Currency currency = new Currency(); + response = response.substring(response.indexOf("TYPE") - 2, response.length() - 3); + + try { + JSONObject jsonObject = new JSONObject(response); + double open24 = jsonObject.getDouble("OPEN24HOUR"); + double value = jsonObject.getDouble("PRICE"); + + currency.setDayFluctuation(value - open24); + currency.setDayFluctuationPercentage((float) (currency.getDayFluctuation() / open24 * 100)); + + currency.setValue(value); + } catch (JSONException e) { + e.printStackTrace(); + } + + return currency; + } + private List processHistoryResult(String response) { List dataChart = new ArrayList<>(); @@ -182,6 +225,18 @@ public class CurrencyDataRetriever { } } + public void updatePrice(String symbolCurrencyFrom, final PriceCallBack callBack) + { + if(symbolCurrencyFrom.equals("USD")) + { + callBack.onSuccess(null); + } + else + { + updatePrice(symbolCurrencyFrom, "USD", callBack); + } + } + /*public void updateCryptocompareDetails(int id, final Currency.CurrencyCallBack callBack) { String requestUrl = getRequestUrl(timeUnit, symbolCurrencyFrom, symbolCyrrencyTo); @@ -212,4 +267,8 @@ public class CurrencyDataRetriever { void onSuccess(List dataChart); void onSuccess(String price); } + + public interface PriceCallBack { + void onSuccess(Currency currencyInfo); + } } diff --git a/app/src/main/java/com/nauk/coinfolio/LayoutManagers/HomeLayoutGenerator.java b/app/src/main/java/com/nauk/coinfolio/LayoutManagers/HomeLayoutGenerator.java index c075295..260b488 100644 --- a/app/src/main/java/com/nauk/coinfolio/LayoutManagers/HomeLayoutGenerator.java +++ b/app/src/main/java/com/nauk/coinfolio/LayoutManagers/HomeLayoutGenerator.java @@ -6,6 +6,7 @@ import android.graphics.Color; import android.graphics.PorterDuff; import android.graphics.PorterDuffColorFilter; import android.graphics.drawable.Drawable; +import android.os.AsyncTask; import android.support.v7.widget.CardView; import android.util.Log; import android.view.LayoutInflater; @@ -51,14 +52,24 @@ public class HomeLayoutGenerator { view.setOnClickListener(new View.OnClickListener() { @Override - public void onClick(View view) { - if(view.findViewById(R.id.collapsableLayout).getVisibility() == View.VISIBLE) - { + public void onClick(final View view) { + if (view.findViewById(R.id.collapsableLayout).getVisibility() == View.VISIBLE) { collapseView(view); - } - else - { - extendView(view); + } else { + if (currency.getHistoryMinutes() == null) { + currency.updateHistoryMinutes(context, new Currency.CurrencyCallBack() { + @Override + public void onSuccess(Currency currency) { + //setupLineChart(view, currency); + ChartLoader chartLoader = new ChartLoader(view, currency); + chartLoader.execute(); + } + }); + } + else + { + extendView(view); + } } } }); @@ -74,11 +85,6 @@ public class HomeLayoutGenerator { } }); - if(currency.getHistoryMinutes() != null) - { - setupLineChart(view, currency); - } - if(isExtended) { extendView(view); @@ -93,6 +99,39 @@ public class HomeLayoutGenerator { return view; } + private class ChartLoader extends AsyncTask + { + private View view; + private Currency currency; + + ChartLoader(View view, Currency currency) + { + this.view = view; + this.currency = currency; + } + + @Override + protected void onPreExecute() { + super.onPreExecute(); + extendView(view); + } + + @Override + protected Void doInBackground(Void... voids) { + setupLineChart(view, currency); + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); + + view.findViewById(R.id.progressLineChart).setVisibility(View.GONE); + view.findViewById(R.id.LineChartView).setVisibility(View.VISIBLE); + view.findViewById(R.id.LineChartView).invalidate(); + } + } + private static void expand(final View v) { v.measure(CardView.LayoutParams.MATCH_PARENT, CardView.LayoutParams.WRAP_CONTENT); final int targetHeight = v.getMeasuredHeight(); @@ -226,7 +265,7 @@ public class HomeLayoutGenerator { private void extendView(View view) { expand(view.findViewById(R.id.collapsableLayout)); - view.findViewById(R.id.LineChartView).invalidate(); + //view.findViewById(R.id.LineChartView).invalidate(); } private void updateColor(View view, Currency currency) diff --git a/app/src/main/res/layout/activity_currency_details.xml b/app/src/main/res/layout/activity_currency_details.xml index 31d4521..8f81ea8 100644 --- a/app/src/main/res/layout/activity_currency_details.xml +++ b/app/src/main/res/layout/activity_currency_details.xml @@ -45,7 +45,7 @@ android:id="@+id/progressLayoutChart" android:layout_width="match_parent" android:layout_height="match_parent" - android:layout_weight="0.4" + android:layout_weight="0.405" android:visibility="gone" android:gravity="center"> @@ -122,72 +122,79 @@