Fix API limitation problem

This commit is contained in:
Tanguy Herbron 2018-04-17 01:02:27 +02:00
parent ff98837235
commit b3f566bf31
8 changed files with 207 additions and 51 deletions

View File

@ -251,7 +251,7 @@ public class Summary extends Fragment {
{ {
final Currency currency = balanceManager.getTotalBalance().get(i); 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())); 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++) 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 @Override
public void onSuccess(Currency currency) { public void onSuccess(Currency currency) {
countCoins(true, false); countCoins(true, false);

View File

@ -236,14 +236,26 @@ public class Watchlist extends Fragment {
card.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); card.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
card.setOnClickListener(new View.OnClickListener() { card.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(final View view) {
if(view.findViewById(R.id.collapsableLayout).getVisibility() == View.VISIBLE) if(view.findViewById(R.id.collapsableLayout).getVisibility() == View.VISIBLE)
{ {
collapseView(view); collapseView(view);
} }
else else
{ {
if (currency.getHistoryMinutes() == null) {
currency.updateHistoryMinutes(getActivity(), new Currency.CurrencyCallBack() {
@Override
public void onSuccess(Currency currency) {
extendView(view); extendView(view);
setupLineChart(view, currency);
}
});
}
else
{
extendView(view);
}
} }
} }
}); });
@ -410,7 +422,7 @@ public class Watchlist extends Fragment {
protected Void doInBackground(Void... voids) { protected Void doInBackground(Void... voids) {
for(final Currency currency : watchlistManager.getWatchlist()) for(final Currency currency : watchlistManager.getWatchlist())
{ {
currency.updateHistoryMinutes(getActivity(), new Currency.CurrencyCallBack() { currency.updatePrice(getActivity(), new Currency.CurrencyCallBack() {
@Override @Override
public void onSuccess(final Currency sucessCurrency) { public void onSuccess(final Currency sucessCurrency) {
if(getIconUrl(sucessCurrency.getSymbol()) != null) if(getIconUrl(sucessCurrency.getSymbol()) != null)

View File

@ -41,6 +41,8 @@ public class Currency implements Parcelable {
private String algorithm; private String algorithm;
//private String proofType //private String proofType
public Currency() {}
public Currency(String symbol, double balance) public Currency(String symbol, double balance)
{ {
this.symbol = symbol; this.symbol = symbol;
@ -95,6 +97,26 @@ public class Currency implements Parcelable {
return url; 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) public void updateHistoryMinutes(android.content.Context context, final CurrencyCallBack callBack)
{ {
dataRetriver = new CurrencyDataRetriever(context); dataRetriver = new CurrencyDataRetriever(context);
@ -104,18 +126,6 @@ public class Currency implements Parcelable {
public void onSuccess(List<CurrencyDataChart> dataChart) { public void onSuccess(List<CurrencyDataChart> dataChart) {
setHistoryMinutes(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); callBack.onSuccess(Currency.this);
} }
@ -262,6 +272,14 @@ public class Currency implements Parcelable {
historyDays = newDataChart; historyDays = newDataChart;
} }
public void setDayFluctuationPercentage(float dayFluctuationPercentage) {
this.dayFluctuationPercentage = dayFluctuationPercentage;
}
public void setDayFluctuation(double dayFluctuation) {
this.dayFluctuation = dayFluctuation;
}
public void setIcon(Bitmap newIcon) public void setIcon(Bitmap newIcon)
{ {
icon = newIcon; icon = newIcon;
@ -282,6 +300,12 @@ public class Currency implements Parcelable {
} }
} }
@Override
public String toString()
{
return symbol + " " + value + " " + dayFluctuation;
}
public interface CurrencyCallBack { public interface CurrencyCallBack {
void onSuccess(Currency currency); void onSuccess(Currency currency);
} }

View File

@ -31,6 +31,7 @@ public class CurrencyDataRetriever {
private String minuteHistoryUrl = "https://min-api.cryptocompare.com/data/histominute"; private String minuteHistoryUrl = "https://min-api.cryptocompare.com/data/histominute";
private String hourHistoryUrl = "https://min-api.cryptocompare.com/data/histohour"; private String hourHistoryUrl = "https://min-api.cryptocompare.com/data/histohour";
private String dayHistoryUrl = "https://min-api.cryptocompare.com/data/histoday"; private String dayHistoryUrl = "https://min-api.cryptocompare.com/data/histoday";
private String priceUrl = "https://min-api.cryptocompare.com/data/pricemultifull?fsyms=";
private RequestQueue requestQueue; private RequestQueue requestQueue;
@ -94,6 +95,27 @@ public class CurrencyDataRetriever {
requestQueue.add(stringRequest); 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<String>() {
@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) private String getRequestUrl(int timeUnit, String symbolCurrencyFrom, String symbolCyrrencyTo)
{ {
String requestUrl = null; String requestUrl = null;
@ -114,6 +136,27 @@ public class CurrencyDataRetriever {
return requestUrl; 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<CurrencyDataChart> processHistoryResult(String response) private List<CurrencyDataChart> processHistoryResult(String response)
{ {
List<CurrencyDataChart> dataChart = new ArrayList<>(); List<CurrencyDataChart> 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) /*public void updateCryptocompareDetails(int id, final Currency.CurrencyCallBack callBack)
{ {
String requestUrl = getRequestUrl(timeUnit, symbolCurrencyFrom, symbolCyrrencyTo); String requestUrl = getRequestUrl(timeUnit, symbolCurrencyFrom, symbolCyrrencyTo);
@ -212,4 +267,8 @@ public class CurrencyDataRetriever {
void onSuccess(List<CurrencyDataChart> dataChart); void onSuccess(List<CurrencyDataChart> dataChart);
void onSuccess(String price); void onSuccess(String price);
} }
public interface PriceCallBack {
void onSuccess(Currency currencyInfo);
}
} }

View File

@ -6,6 +6,7 @@ import android.graphics.Color;
import android.graphics.PorterDuff; import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter; import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.support.v7.widget.CardView; import android.support.v7.widget.CardView;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -51,16 +52,26 @@ public class HomeLayoutGenerator {
view.setOnClickListener(new View.OnClickListener() { view.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(final View view) {
if(view.findViewById(R.id.collapsableLayout).getVisibility() == View.VISIBLE) if (view.findViewById(R.id.collapsableLayout).getVisibility() == View.VISIBLE) {
{
collapseView(view); collapseView(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 else
{ {
extendView(view); extendView(view);
} }
} }
}
}); });
updateCardViewInfos(view, currency, totalValue, isBalanceHidden); updateCardViewInfos(view, currency, totalValue, isBalanceHidden);
@ -74,11 +85,6 @@ public class HomeLayoutGenerator {
} }
}); });
if(currency.getHistoryMinutes() != null)
{
setupLineChart(view, currency);
}
if(isExtended) if(isExtended)
{ {
extendView(view); extendView(view);
@ -93,6 +99,39 @@ public class HomeLayoutGenerator {
return view; return view;
} }
private class ChartLoader extends AsyncTask<Void, Integer, Void>
{
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) { private static void expand(final View v) {
v.measure(CardView.LayoutParams.MATCH_PARENT, CardView.LayoutParams.WRAP_CONTENT); v.measure(CardView.LayoutParams.MATCH_PARENT, CardView.LayoutParams.WRAP_CONTENT);
final int targetHeight = v.getMeasuredHeight(); final int targetHeight = v.getMeasuredHeight();
@ -226,7 +265,7 @@ public class HomeLayoutGenerator {
private void extendView(View view) private void extendView(View view)
{ {
expand(view.findViewById(R.id.collapsableLayout)); 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) private void updateColor(View view, Currency currency)

View File

@ -45,7 +45,7 @@
android:id="@+id/progressLayoutChart" android:id="@+id/progressLayoutChart"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_weight="0.4" android:layout_weight="0.405"
android:visibility="gone" android:visibility="gone"
android:gravity="center"> android:gravity="center">
@ -122,72 +122,79 @@
<Button <Button
style="@style/Widget.AppCompat.Button.Colored" style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_weight="11.1" android:layout_weight="11.1"
android:autoSizeTextType="uniform" android:autoSizeTextType="uniform"
android:maxLines="1"
android:text="@string/button1h"/> android:text="@string/button1h"/>
<Button <Button
style="@style/Widget.AppCompat.Button.Colored" style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_weight="11.1" android:layout_weight="11.1"
android:autoSizeTextType="uniform" android:autoSizeTextType="uniform"
android:maxLines="1"
android:text="@string/button3h"/> android:text="@string/button3h"/>
<Button <Button
style="@style/Widget.AppCompat.Button.Colored" style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_weight="11.1" android:layout_weight="11.1"
android:enabled="false" android:enabled="false"
android:autoSizeTextType="uniform" android:autoSizeTextType="uniform"
android:maxLines="1"
android:text="@string/button1d"/> android:text="@string/button1d"/>
<Button <Button
style="@style/Widget.AppCompat.Button.Colored" style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_weight="11.1" android:layout_weight="11.1"
android:autoSizeTextType="uniform" android:autoSizeTextType="uniform"
android:maxLines="1"
android:text="@string/button3d"/> android:text="@string/button3d"/>
<Button <Button
style="@style/Widget.AppCompat.Button.Colored" style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_weight="11.1" android:layout_weight="11.1"
android:autoSizeTextType="uniform" android:autoSizeTextType="uniform"
android:maxLines="1"
android:text="@string/button1w"/> android:text="@string/button1w"/>
<Button <Button
style="@style/Widget.AppCompat.Button.Colored" style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_weight="11.1" android:layout_weight="11.1"
android:autoSizeTextType="uniform" android:autoSizeTextType="uniform"
android:maxLines="1"
android:text="@string/button1m"/> android:text="@string/button1m"/>
<Button <Button
style="@style/Widget.AppCompat.Button.Colored" style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_weight="11.1" android:layout_weight="11.1"
android:text="@string/button3m"/> android:text="@string/button3m"/>
<Button <Button
style="@style/Widget.AppCompat.Button.Colored" style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_weight="11.1" android:layout_weight="11.1"
android:autoSizeTextType="uniform" android:autoSizeTextType="uniform"
android:maxLines="1"
android:text="@string/button6m"/> android:text="@string/button6m"/>
<Button <Button
style="@style/Widget.AppCompat.Button.Colored" style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_weight="11.1" android:layout_weight="11.1"
android:autoSizeTextType="uniform" android:autoSizeTextType="uniform"
android:text="@string/button1y"/> android:text="@string/button1y"/>
@ -198,7 +205,7 @@
android:id="@+id/llCharts" android:id="@+id/llCharts"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_weight="0.7" android:layout_weight="0.71"
android:orientation="horizontal" android:orientation="horizontal"
android:paddingStart="4dp"> android:paddingStart="4dp">

View File

@ -181,10 +181,17 @@
android:clickable="true" android:clickable="true"
android:focusable="true"> android:focusable="true">
<ProgressBar
android:id="@+id/progressLineChart"
android:layout_width="match_parent"
android:layout_height="150dp"
android:visibility="gone"/>
<com.github.mikephil.charting.charts.LineChart <com.github.mikephil.charting.charts.LineChart
android:id="@+id/LineChartView" android:id="@+id/LineChartView"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="150dp"/> android:layout_height="150dp"
android:visibility="visible"/>
<ImageView <ImageView
android:id="@+id/detailsArrow" android:id="@+id/detailsArrow"

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="time_button_text_size">
<item>12sp</item>
<item>14sp</item>
<item>18sp</item>
</array>
</resources>