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);
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);

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.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)

View File

@ -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<CurrencyDataChart> 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);
}

View File

@ -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<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)
{
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<CurrencyDataChart> processHistoryResult(String response)
{
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)
{
String requestUrl = getRequestUrl(timeUnit, symbolCurrencyFrom, symbolCyrrencyTo);
@ -212,4 +267,8 @@ public class CurrencyDataRetriever {
void onSuccess(List<CurrencyDataChart> dataChart);
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.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<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) {
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)

View File

@ -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 @@
<Button
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="11.1"
android:autoSizeTextType="uniform"
android:maxLines="1"
android:text="@string/button1h"/>
<Button
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="11.1"
android:autoSizeTextType="uniform"
android:maxLines="1"
android:text="@string/button3h"/>
<Button
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="11.1"
android:enabled="false"
android:autoSizeTextType="uniform"
android:maxLines="1"
android:text="@string/button1d"/>
<Button
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="11.1"
android:autoSizeTextType="uniform"
android:maxLines="1"
android:text="@string/button3d"/>
<Button
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="11.1"
android:autoSizeTextType="uniform"
android:maxLines="1"
android:text="@string/button1w"/>
<Button
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="11.1"
android:autoSizeTextType="uniform"
android:maxLines="1"
android:text="@string/button1m"/>
<Button
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="11.1"
android:text="@string/button3m"/>
<Button
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="11.1"
android:autoSizeTextType="uniform"
android:maxLines="1"
android:text="@string/button6m"/>
<Button
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="11.1"
android:autoSizeTextType="uniform"
android:text="@string/button1y"/>
@ -198,7 +205,7 @@
android:id="@+id/llCharts"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.7"
android:layout_weight="0.71"
android:orientation="horizontal"
android:paddingStart="4dp">

View File

@ -181,10 +181,17 @@
android:clickable="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
android:id="@+id/LineChartView"
android:layout_width="match_parent"
android:layout_height="150dp"/>
android:layout_height="150dp"
android:visibility="visible"/>
<ImageView
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>