From ee318266e94cb21c4f72fc7a000baf105b145aa1 Mon Sep 17 00:00:00 2001 From: Tanguy Herbron Date: Sat, 21 Apr 2018 18:40:32 +0200 Subject: [PATCH] Fix crash in details activity when started from a watchlisted coin --- .../Activities/CurrencyDetailsActivity.java | 79 +++++-- .../HomeActivityFragments/Watchlist.java | 15 ++ .../DataManagers/CurrencyData/Currency.java | 99 ++++++++- .../CurrencyData/CurrencyDataRetriever.java | 72 +++++-- .../ExchangeManager/BinanceManager.java | 4 +- .../res/layout/activity_currency_details.xml | 196 ++++++++++++------ app/src/main/res/menu/navigation_details.xml | 1 - 7 files changed, 371 insertions(+), 95 deletions(-) diff --git a/app/src/main/java/com/nauk/coinfolio/Activities/CurrencyDetailsActivity.java b/app/src/main/java/com/nauk/coinfolio/Activities/CurrencyDetailsActivity.java index aa44762..aaf212f 100644 --- a/app/src/main/java/com/nauk/coinfolio/Activities/CurrencyDetailsActivity.java +++ b/app/src/main/java/com/nauk/coinfolio/Activities/CurrencyDetailsActivity.java @@ -6,6 +6,7 @@ import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.PorterDuff; +import android.graphics.PorterDuffColorFilter; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.AsyncTask; @@ -16,12 +17,16 @@ import android.support.v4.app.NavUtils; import android.support.v4.content.ContextCompat; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; +import android.text.Html; +import android.text.method.LinkMovementMethod; +import android.util.Log; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; +import android.widget.ProgressBar; import android.widget.TextView; import android.widget.ViewFlipper; @@ -184,12 +189,12 @@ public class CurrencyDetailsActivity extends AppCompatActivity { drawTransactionList(); - updateInfoTab(); - initializeButtons(); initializeLineChart(lineChart); initializeCandleStickChart(candleStickChart); + updateInfoTab(); + updateChartTab(DAY, 1); BottomNavigationView navigation = findViewById(R.id.navigation_details); @@ -201,9 +206,54 @@ public class CurrencyDetailsActivity extends AppCompatActivity { updater.execute(); } + private void refreshInfoTab() + { + Drawable progressBarDrawable = ((ProgressBar) findViewById(R.id.percentageCoinEmited)).getProgressDrawable(); + progressBarDrawable.mutate(); + progressBarDrawable.setColorFilter(new PorterDuffColorFilter(currency.getChartColor(), PorterDuff.Mode.SRC_IN)); + progressBarDrawable.invalidateSelf(); + + ((ProgressBar) findViewById(R.id.percentageCoinEmited)) + .setProgress((int) Math.round(currency.getMinedCoinSupply() / currency.getMaxCoinSupply() * 100)); + + ((TextView) findViewById(R.id.txtViewAlgorithm)) + .setText(currency.getAlgorithm()); + ((TextView) findViewById(R.id.txtViewProofType)) + .setText(currency.getProofType()); + ((TextView) findViewById(R.id.txtViewDescription)) + .setText(Html.fromHtml(currency.getDescription())); + ((TextView) findViewById(R.id.txtViewDescription)) + .setMovementMethod(LinkMovementMethod.getInstance()); + ((TextView) findViewById(R.id.txtViewPercentageCoinEmited)) + .setText("Percentage of coin emited : " + numberConformer(currency.getMinedCoinSupply() / currency.getMaxCoinSupply() * 100) + "%"); + + if(currency.getMaxCoinSupply() == 0) + { + ((TextView) findViewById(R.id.txtViewTotalSupply)) + .setText(PlaceholderManager.getSymbolString("Infinity", getApplication())); + } + else + { + ((TextView) findViewById(R.id.txtViewTotalSupply)) + .setText(PlaceholderManager.getSymbolString(numberConformer(currency.getMaxCoinSupply()), getApplication())); + } + ((TextView) findViewById(R.id.txtViewCirculatingSupply)) + .setText(PlaceholderManager.getSymbolString(numberConformer(currency.getMinedCoinSupply()), getApplication())); + } + private void updateInfoTab() { - ((TextView) findViewById(R.id.txtViewTotalSupply)).setText(""); + currency.updateSnapshot(this, new Currency.CurrencyCallBack() { + @Override + public void onSuccess(final Currency currency) { + runOnUiThread(new Runnable() { + @Override + public void run() { + refreshInfoTab(); + } + }); + } + }); } private void setupActionBar() @@ -635,14 +685,22 @@ public class CurrencyDetailsActivity extends AppCompatActivity { str = String.format( Locale.UK, "%.4f", number).replaceAll("\\.?0*$", ""); } - int counter = 0; - for(int i = str.indexOf(".") - 1; i > 0; i--) + if(!str.equals("Infinity")) { - counter++; - if(counter == 3) + int counter = 0; + int i = str.indexOf(".") - 1; + if(i <= 0) { - str = str.substring(0, i) + " " + str.substring(i, str.length()); - counter = 0; + i = str.length() - 1; + } + for(; i > 0; i--) + { + counter++; + if(counter == 3) + { + str = str.substring(0, i) + " " + str.substring(i, str.length()); + counter = 0; + } } } @@ -716,9 +774,6 @@ public class CurrencyDetailsActivity extends AppCompatActivity { dataSet.setDrawHorizontalHighlightIndicator(false); dataSet.setHighLightColor(currency.getChartColor()); - Drawable fillDrawable = ContextCompat.getDrawable(this, R.drawable.linear_chart_gradient); - fillDrawable.setColorFilter(getColorWithAlpha(currency.getChartColor(), 0.5f), PorterDuff.Mode.SRC_ATOP); - return new LineData(dataSet); } 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 1edb555..17fb706 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 @@ -469,6 +469,20 @@ public class Watchlist extends Fragment { } } + public int getCurrencyId(String symbol) + { + int id = 0; + + try { + JSONObject jsonObject = new JSONObject(currencyDetailsList.getCoinInfosHashmap().get(symbol)); + id = jsonObject.getInt("Id"); + } catch (JSONException e) { + e.printStackTrace(); + } + + return id; + } + private class WatchlistUpdater extends AsyncTask { @Override @@ -481,6 +495,7 @@ public class Watchlist extends Fragment { protected Void doInBackground(Void... voids) { for(final Currency currency : watchlistManager.getWatchlist()) { + currency.setId(getCurrencyId(currency.getSymbol())); currency.updatePrice(getActivity(), preferencesManager.getDefaultCurrency(), new Currency.CurrencyCallBack() { @Override public void onSuccess(final Currency sucessCurrency) { 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 a4b616c..c50cf24 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 @@ -5,16 +5,13 @@ import android.os.Parcel; import android.os.Parcelable; import android.util.Log; -import com.nauk.coinfolio.R; - import org.json.JSONException; import org.json.JSONObject; +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; -import static java.sql.Types.NULL; - /** * Created by Tiji on 25/12/2017. */ @@ -34,11 +31,14 @@ public class Currency implements Parcelable { private CurrencyDataRetriever dataRetriver; private Bitmap icon; private int chartColor; - private int circulatingSupply; + private double maxCoinSupply; + private double minedCoinSupply; + private String description; + private String algorithm; + private String proofType; private int totalSupply; private double marketCapitalization; private List socialMediaLinks; - private String algorithm; //private String proofType public Currency() {} @@ -101,7 +101,7 @@ public class Currency implements Parcelable { { dataRetriver = new CurrencyDataRetriever(context); - dataRetriver.updatePrice(symbol, toSymbol, new CurrencyDataRetriever.PriceCallBack() { + dataRetriver.updatePrice(symbol, toSymbol, new CurrencyDataRetriever.CurrencyCallBack() { @Override public void onSuccess(Currency currencyInfo) { if(currencyInfo != null) @@ -110,7 +110,6 @@ public class Currency implements Parcelable { setDayFluctuation(currencyInfo.getDayFluctuation()); setDayFluctuationPercentage(currencyInfo.getDayFluctuationPercentage()); } - Log.d("coinfolio", this.toString()); callBack.onSuccess(Currency.this); } @@ -134,6 +133,32 @@ public class Currency implements Parcelable { }, CurrencyDataRetriever.MINUTES); } + private void mergeWith(Currency currency) + { + dataRetriver = currency.dataRetriver; + maxCoinSupply = currency.maxCoinSupply; + minedCoinSupply = currency.minedCoinSupply; + description = currency.description; + algorithm = currency.algorithm; + proofType = currency.proofType; + totalSupply = currency.totalSupply; + marketCapitalization = currency.marketCapitalization; + socialMediaLinks = currency.socialMediaLinks; + } + + public void updateSnapshot(android.content.Context context, final CurrencyCallBack callBack) + { + dataRetriver = new CurrencyDataRetriever(context); + dataRetriver.updateSnapshot(id, new CurrencyDataRetriever.CurrencyCallBack() { + @Override + public void onSuccess(Currency currencyInfo) { + Currency.this.mergeWith(currencyInfo); + + callBack.onSuccess(Currency.this); + } + }); + } + public void updateHistoryHours(android.content.Context context, String toSymbol, final CurrencyCallBack callBack) { dataRetriver = new CurrencyDataRetriever(context); @@ -290,6 +315,46 @@ public class Currency implements Parcelable { return icon; } + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public double getMaxCoinSupply() { + return maxCoinSupply; + } + + public void setMaxCoinSupply(double maxCoinSupply) { + this.maxCoinSupply = maxCoinSupply; + } + + public double getMinedCoinSupply() { + return minedCoinSupply; + } + + public void setMinedCoinSupply(double minedCoinSupply) { + this.minedCoinSupply = minedCoinSupply; + } + + public String getAlgorithm() { + return algorithm; + } + + public void setAlgorithm(String algorithm) { + this.algorithm = algorithm; + } + + public String getProofType() { + return proofType; + } + + public void setProofType(String proofType) { + this.proofType = proofType; + } + private void updateDayFluctuation() { if(historyMinutes != null) @@ -303,7 +368,23 @@ public class Currency implements Parcelable { @Override public String toString() { - return symbol + " " + value + " " + dayFluctuation; + Field[] fields = this.getClass().getDeclaredFields(); + String currencyString = "Currency >"; + + for(Field field : fields) + { + currencyString += "\n\t"; + + try { + currencyString += field.getName(); + currencyString += ": "; + currencyString += field.get(this); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + + return currencyString; } public interface CurrencyCallBack { 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 2441881..44691c3 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 @@ -1,6 +1,5 @@ package com.nauk.coinfolio.DataManagers.CurrencyData; -import android.provider.ContactsContract; import android.util.Log; import com.android.volley.Request; @@ -9,6 +8,7 @@ import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; +import com.fasterxml.jackson.databind.util.JSONPObject; import com.nauk.coinfolio.R; import org.json.JSONException; @@ -32,6 +32,7 @@ public class CurrencyDataRetriever { 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 String snapshotUrl = "https://www.cryptocompare.com/api/data/coinsnapshotfullbyid/?id="; private RequestQueue requestQueue; @@ -44,6 +45,29 @@ public class CurrencyDataRetriever { requestQueue = Volley.newRequestQueue(context); } + public void updateSnapshot(int id, final CurrencyCallBack callBack) + { + final String requestUrl = snapshotUrl + id; + + Log.d("coinfolio", "Update snapshot for " + id); + + StringRequest stringRequest = new StringRequest(Request.Method.GET, requestUrl, + new Response.Listener() { + @Override + public void onResponse(String response) { + callBack.onSuccess(processSnapshotResult(response)); + } + }, + new Response.ErrorListener() { + @Override + public void onErrorResponse(VolleyError error) { + + } + }); + + requestQueue.add(stringRequest); + } + public void getPriceTimestamp(final String symbolCurrencyFrom, String symbolCurrencyTo, final DataChartCallBack callBack, long timestamp) { final String requestUrl = "https://min-api.cryptocompare.com/data/pricehistorical?fsym=" + symbolCurrencyFrom + "&tsyms=" + symbolCurrencyTo + "&ts=" + timestamp; @@ -52,7 +76,6 @@ public class CurrencyDataRetriever { new Response.Listener() { @Override public void onResponse(String response) { - Log.d("coinfolio", response + " " + requestUrl); callBack.onSuccess(processPriceTimestampResult(response)); } }, @@ -95,7 +118,7 @@ public class CurrencyDataRetriever { requestQueue.add(stringRequest); } - public void updatePrice(final String symbolCurrencyFrom, String symbolCurrencyTo, final PriceCallBack callBack) + public void updatePrice(final String symbolCurrencyFrom, String symbolCurrencyTo, final CurrencyCallBack callBack) { String requestUrl = priceUrl + symbolCurrencyFrom + "&tsyms=" + symbolCurrencyTo; @@ -139,17 +162,42 @@ public class CurrencyDataRetriever { private Currency processPriceResult(String response) { Currency currency = new Currency(); - response = response.substring(response.indexOf("TYPE") - 2, response.length() - 3); + + if(response.length() > 500) + { + 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 Currency processSnapshotResult(String response) + { + Currency currency = new Currency(); try { JSONObject jsonObject = new JSONObject(response); - double open24 = jsonObject.getDouble("OPEN24HOUR"); - double value = jsonObject.getDouble("PRICE"); + jsonObject = new JSONObject(jsonObject.getString("Data")); + jsonObject = new JSONObject(jsonObject.getString("General")); - currency.setDayFluctuation(value - open24); - currency.setDayFluctuationPercentage((float) (currency.getDayFluctuation() / open24 * 100)); - - currency.setValue(value); + currency.setProofType(jsonObject.getString("ProofType")); + currency.setAlgorithm(jsonObject.getString("Algorithm")); + currency.setDescription(jsonObject.getString("Description")); + currency.setMaxCoinSupply(Double.parseDouble(jsonObject.getString("TotalCoinSupply"))); + currency.setMinedCoinSupply(Double.parseDouble(jsonObject.getString("TotalCoinsMined"))); } catch (JSONException e) { e.printStackTrace(); } @@ -225,7 +273,7 @@ public class CurrencyDataRetriever { } } - public void updatePrice(String symbolCurrencyFrom, final PriceCallBack callBack) + public void updatePrice(String symbolCurrencyFrom, final CurrencyCallBack callBack) { if(symbolCurrencyFrom.equals("USD")) { @@ -268,7 +316,7 @@ public class CurrencyDataRetriever { void onSuccess(String price); } - public interface PriceCallBack { + public interface CurrencyCallBack { void onSuccess(Currency currencyInfo); } } diff --git a/app/src/main/java/com/nauk/coinfolio/DataManagers/ExchangeManager/BinanceManager.java b/app/src/main/java/com/nauk/coinfolio/DataManagers/ExchangeManager/BinanceManager.java index 993715e..6c76f82 100644 --- a/app/src/main/java/com/nauk/coinfolio/DataManagers/ExchangeManager/BinanceManager.java +++ b/app/src/main/java/com/nauk/coinfolio/DataManagers/ExchangeManager/BinanceManager.java @@ -48,9 +48,9 @@ public class BinanceManager { for(int i = 0; i < assets.size(); i++) { - if(Double.parseDouble(assets.get(i).getFree()) > 0) + if(Double.parseDouble(assets.get(i).getFree()) > 0 || Double.parseDouble(assets.get(i).getLocked()) > 0) { - balance.add(new Currency(assets.get(i).getAsset(), Double.parseDouble(assets.get(i).getFree()))); + balance.add(new Currency(assets.get(i).getAsset(), Double.parseDouble(assets.get(i).getFree()) + Double.parseDouble(assets.get(i).getLocked()))); } } diff --git a/app/src/main/res/layout/activity_currency_details.xml b/app/src/main/res/layout/activity_currency_details.xml index 8f81ea8..171dd3c 100644 --- a/app/src/main/res/layout/activity_currency_details.xml +++ b/app/src/main/res/layout/activity_currency_details.xml @@ -368,85 +368,163 @@ - - - + android:layout_height="wrap_content"> + android:layout_height="match_parent" + android:orientation="vertical" + android:layout_margin="6dp"> - + android:layout_height="wrap_content" + android:gravity="center"/> - - - - - - - - - - - - - - - - - + android:layout_height="5dp" + style="@style/Widget.AppCompat.ProgressBar.Horizontal" + android:layout_margin="5dp" + android:background="@drawable/rounded_corners"/> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -454,7 +532,7 @@ - +