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 4a8d006..efb9976 100644 --- a/app/src/main/java/com/nauk/coinfolio/Activities/CurrencyDetailsActivity.java +++ b/app/src/main/java/com/nauk/coinfolio/Activities/CurrencyDetailsActivity.java @@ -90,6 +90,9 @@ public class CurrencyDetailsActivity extends AppCompatActivity { private PreferencesManager preferencesManager; private BinanceManager binanceManager; + private boolean isSnapshotUpdated; + private boolean isTickerUpdated; + private boolean displayLineChart; private Button lineChartButton; @@ -147,6 +150,9 @@ public class CurrencyDetailsActivity extends AppCompatActivity { databaseManager = new DatabaseManager(this); preferencesManager = new PreferencesManager(this); + isSnapshotUpdated = false; + isTickerUpdated = false; + displayLineChart = true; viewFlipper = findViewById(R.id.vfCurrencyDetails); @@ -231,6 +237,17 @@ public class CurrencyDetailsActivity extends AppCompatActivity { .setMovementMethod(LinkMovementMethod.getInstance()); ((TextView) findViewById(R.id.txtViewPercentageCoinEmited)) .setText("Percentage of coin emited : " + numberConformer(currency.getMinedCoinSupply() / currency.getMaxCoinSupply() * 100) + "%"); + if(currency.getMarketCapitalization() != 0) + { + ((TextView) findViewById(R.id.txtViewMarketCapitalization)) + .setText(PlaceholderManager.getValueString(numberConformer(currency.getMarketCapitalization()), this)); + } + + if(currency.getRank() != 0) + { + ((TextView) findViewById(R.id.txtViewRank)) + .setText(String.valueOf(currency.getRank())); + } if(currency.getMaxCoinSupply() == 0) { @@ -251,14 +268,31 @@ public class CurrencyDetailsActivity extends AppCompatActivity { currency.updateSnapshot(this, new Currency.CurrencyCallBack() { @Override public void onSuccess(final Currency currency) { - runOnUiThread(new Runnable() { - @Override - public void run() { - refreshInfoTab(); - } - }); + isSnapshotUpdated = true; + dataCounter(); } }); + + currency.updateTicker(this, preferencesManager.getDefaultCurrency(), new Currency.CurrencyCallBack() { + @Override + public void onSuccess(Currency currency) { + isTickerUpdated = true; + dataCounter(); + } + }); + } + + private void dataCounter() + { + if(isTickerUpdated && isSnapshotUpdated) + { + runOnUiThread(new Runnable() { + @Override + public void run() { + refreshInfoTab(); + } + }); + } } private void setupActionBar() diff --git a/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivity.java b/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivity.java index a4868a5..8e468af 100644 --- a/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivity.java +++ b/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivity.java @@ -44,6 +44,7 @@ import com.github.mikephil.charting.data.PieEntry; import com.github.mikephil.charting.formatter.PercentFormatter; import com.nauk.coinfolio.DataManagers.BalanceManager; import com.nauk.coinfolio.DataManagers.CurrencyData.Currency; +import com.nauk.coinfolio.DataManagers.CurrencyData.CurrencyTickerList; import com.nauk.coinfolio.DataManagers.MarketCapManager; import com.nauk.coinfolio.DataManagers.PreferencesManager; import com.nauk.coinfolio.LayoutManagers.HomeLayoutGenerator; 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 43dc4c6..2758dd7 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 @@ -32,6 +32,7 @@ import com.nauk.coinfolio.Activities.HomeActivity; import com.nauk.coinfolio.Activities.SettingsActivity; import com.nauk.coinfolio.DataManagers.BalanceManager; import com.nauk.coinfolio.DataManagers.CurrencyData.Currency; +import com.nauk.coinfolio.DataManagers.CurrencyData.CurrencyTickerList; import com.nauk.coinfolio.DataManagers.PreferencesManager; import com.nauk.coinfolio.LayoutManagers.HomeLayoutGenerator; import com.nauk.coinfolio.PlaceholderManager; @@ -62,6 +63,7 @@ public class Summary extends Fragment { private SwipeRefreshLayout refreshLayout; private Dialog loadingDialog; private String defaultCurrency; + private CurrencyTickerList currencyTickerList; private TextView toolbarSubtitle; private CollapsingToolbarLayout toolbarLayout; @@ -73,6 +75,7 @@ public class Summary extends Fragment { private int iconCounter; private float totalValue; private boolean detailsChecker; + private boolean tickersChecker; protected float totalFluctuation; private long lastTimestamp; @@ -88,10 +91,12 @@ public class Summary extends Fragment { layoutGenerator = new HomeLayoutGenerator(getActivity()); refreshLayout = fragmentView.findViewById(R.id.swiperefreshsummary); toolbarSubtitle = fragmentView.findViewById(R.id.toolbarSubtitle); + currencyTickerList = new CurrencyTickerList(getActivity()); totalValue = 0; totalFluctuation = 0; lastTimestamp = 0; + tickersChecker = false; defaultCurrency = preferencesManager.getDefaultCurrency(); @@ -277,6 +282,11 @@ public class Summary extends Fragment { { currencyLayout.addView(currencyView.get(i)); } + + if(loadingDialog.isShowing()) + { + loadingDialog.dismiss(); + } } }); } @@ -285,13 +295,18 @@ public class Summary extends Fragment { newRunnable.run(); } - private void countCoins(boolean isCoin, boolean isDetails) + private void countCoins(boolean isCoin, boolean isDetails, boolean isTickers) { if(isCoin) { coinCounter++; } + if(isTickers) + { + tickersChecker = true; + } + if(isDetails) { detailsChecker = true; @@ -299,7 +314,7 @@ public class Summary extends Fragment { if(balanceManager.getTotalBalance() != null) { - if(coinCounter == balanceManager.getTotalBalance().size() && detailsChecker) + if(coinCounter == balanceManager.getTotalBalance().size() && detailsChecker && tickersChecker) { IconDownloader iconDownloader = new IconDownloader(); iconDownloader.execute(); @@ -500,6 +515,8 @@ public class Summary extends Fragment { { final Currency localCurrency = balanceManager.getTotalBalance().get(i); + localCurrency.setTickerId(currencyTickerList.getTickerIdForSymbol(localCurrency.getSymbol())); + updateChartColor(localCurrency); loadCurrency(localCurrency); @@ -514,11 +531,6 @@ public class Summary extends Fragment { } }); - if(loadingDialog.isShowing()) - { - loadingDialog.dismiss(); - } - return null; } @@ -626,11 +638,17 @@ public class Summary extends Fragment { @Override protected Void doInBackground(Void... params) { + currencyTickerList.update(new BalanceManager.IconCallBack() { + @Override + public void onSuccess() { + countCoins(false, false, true); + } + }); balanceManager.updateDetails(new BalanceManager.IconCallBack() { @Override public void onSuccess() { - countCoins(false, true); + countCoins(false, true, false); } }); @@ -647,8 +665,7 @@ public class Summary extends Fragment { balance.get(i).updatePrice(getActivity(), defaultCurrency, new Currency.CurrencyCallBack() { @Override public void onSuccess(Currency currency) { - countCoins(true, false); - Log.d("coinfolio", "History " + currency.getSymbol()); + countCoins(true, false, false); } }); } @@ -658,7 +675,7 @@ public class Summary extends Fragment { getActivity().runOnUiThread(new Runnable() { @Override public void run() { - countCoins(false, false); + countCoins(false, false, 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 5ac1602..65e3c51 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 @@ -42,6 +42,7 @@ import com.nauk.coinfolio.DataManagers.BalanceManager; import com.nauk.coinfolio.DataManagers.CurrencyData.Currency; import com.nauk.coinfolio.DataManagers.CurrencyData.CurrencyDataChart; import com.nauk.coinfolio.DataManagers.CurrencyData.CurrencyDetailsList; +import com.nauk.coinfolio.DataManagers.CurrencyData.CurrencyTickerList; import com.nauk.coinfolio.DataManagers.PreferencesManager; import com.nauk.coinfolio.DataManagers.WatchlistManager; import com.nauk.coinfolio.PlaceholderManager; @@ -73,6 +74,9 @@ public class Watchlist extends Fragment { private long lastTimestamp; private PreferencesManager preferencesManager; private String defaultCurrency; + private CurrencyTickerList currencyTickerList; + private boolean tickerUpdated; + private boolean detailsUpdated; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) @@ -85,6 +89,15 @@ public class Watchlist extends Fragment { lastTimestamp = 0; defaultCurrency = preferencesManager.getDefaultCurrency(); + currencyTickerList = new CurrencyTickerList(getActivity()); + tickerUpdated = false; + currencyTickerList.update(new BalanceManager.IconCallBack() { + @Override + public void onSuccess() { + tickerUpdated = true; + checkUpdatedData(); + } + }); watchlistManager = new WatchlistManager(getContext()); @@ -212,14 +225,15 @@ public class Watchlist extends Fragment { } lastTimestamp = System.currentTimeMillis()/1000; + detailsUpdated = false; watchlistManager.updateWatchlist(); currencyDetailsList.update(new BalanceManager.IconCallBack() { @Override public void onSuccess() { - WatchlistUpdater watchlistUpdater = new WatchlistUpdater(); - watchlistUpdater.execute(); + detailsUpdated = true; + checkUpdatedData(); } }); } @@ -232,6 +246,15 @@ public class Watchlist extends Fragment { } } + private void checkUpdatedData() + { + if(tickerUpdated && detailsUpdated) + { + WatchlistUpdater watchlistUpdater = new WatchlistUpdater(); + watchlistUpdater.execute(); + } + } + private void generateCards() { final List watchlistViews = new ArrayList(); @@ -516,6 +539,7 @@ public class Watchlist extends Fragment { protected Void doInBackground(Void... voids) { for(final Currency currency : watchlistManager.getWatchlist()) { + currency.setTickerId(currencyTickerList.getTickerIdForSymbol(currency.getSymbol())); currency.setId(getCurrencyId(currency.getSymbol())); currency.updatePrice(getActivity(), preferencesManager.getDefaultCurrency(), new Currency.CurrencyCallBack() { @Override 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 c50cf24..1024a9c 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 @@ -19,6 +19,7 @@ import java.util.List; public class Currency implements Parcelable { private int id; + private String tickerId; private String name; private String symbol; private double value; @@ -38,6 +39,7 @@ public class Currency implements Parcelable { private String proofType; private int totalSupply; private double marketCapitalization; + private int rank; private List socialMediaLinks; //private String proofType @@ -152,7 +154,29 @@ public class Currency implements Parcelable { dataRetriver.updateSnapshot(id, new CurrencyDataRetriever.CurrencyCallBack() { @Override public void onSuccess(Currency currencyInfo) { - Currency.this.mergeWith(currencyInfo); + //Currency.this.mergeWith(currencyInfo); + + Currency.this.proofType = currencyInfo.proofType; + Currency.this.algorithm = currencyInfo.algorithm; + Currency.this.description = currencyInfo.description; + Currency.this.maxCoinSupply = currencyInfo.maxCoinSupply; + Currency.this.minedCoinSupply = currencyInfo.minedCoinSupply; + + callBack.onSuccess(Currency.this); + } + }); + } + + public void updateTicker(android.content.Context context, String toSymbol, final CurrencyCallBack callBack) + { + dataRetriver = new CurrencyDataRetriever(context); + dataRetriver.updateTickerInfos(tickerId, toSymbol, new CurrencyDataRetriever.CurrencyCallBack() { + @Override + public void onSuccess(Currency currencyInfo) { + //Currency.this.mergeWith(currencyInfo); + + Currency.this.marketCapitalization = currencyInfo.marketCapitalization; + Currency.this.rank = currencyInfo.rank; callBack.onSuccess(Currency.this); } @@ -355,6 +379,30 @@ public class Currency implements Parcelable { this.proofType = proofType; } + public double getMarketCapitalization() { + return marketCapitalization; + } + + public void setMarketCapitalization(double marketCapitalization) { + this.marketCapitalization = marketCapitalization; + } + + public int getRank() { + return rank; + } + + public void setRank(int rank) { + this.rank = rank; + } + + public String getTickerId() { + return tickerId; + } + + public void setTickerId(String tickerId) { + this.tickerId = tickerId; + } + private void updateDayFluctuation() { if(historyMinutes != null) @@ -412,6 +460,7 @@ public class Currency implements Parcelable { dest.writeList(this.historyMinutes); dest.writeParcelable(this.icon, flags); dest.writeInt(this.chartColor); + dest.writeString(this.tickerId); } protected Currency(Parcel in) { @@ -426,6 +475,7 @@ public class Currency implements Parcelable { in.readList(this.historyMinutes, CurrencyDataChart.class.getClassLoader()); this.icon = in.readParcelable(Bitmap.class.getClassLoader()); this.chartColor = in.readInt(); + this.tickerId = in.readString(); } public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { 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 44691c3..aa523b4 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 @@ -33,6 +33,7 @@ public class CurrencyDataRetriever { 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 String tickerUrl = "https://api.coinmarketcap.com/v1/ticker/"; private RequestQueue requestQueue; @@ -45,12 +46,49 @@ public class CurrencyDataRetriever { requestQueue = Volley.newRequestQueue(context); } + public void updateTickerInfos(String currencyName, final String toSymbol, final CurrencyCallBack callBack) + { + final String requestUrl = tickerUrl + currencyName + "/?convert=" + toSymbol; + + StringRequest stringRequest = new StringRequest(Request.Method.GET, requestUrl, + new Response.Listener() { + @Override + public void onResponse(String response) { + callBack.onSuccess(processTicker(response, toSymbol)); + } + }, + new Response.ErrorListener() { + @Override + public void onErrorResponse(VolleyError error) { + callBack.onSuccess(new Currency()); + } + }); + + requestQueue.add(stringRequest); + } + + private Currency processTicker(String response, String toSymbol) + { + Currency currency = new Currency(); + + response = response.substring(1, response.length()-1); + + try { + JSONObject jsonObject = new JSONObject(response); + + currency.setMarketCapitalization(jsonObject.getDouble("market_cap_" + toSymbol.toLowerCase())); + currency.setRank(jsonObject.getInt("rank")); + } catch (JSONException e) { + e.printStackTrace(); + } + + return currency; + } + 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 @@ -61,7 +99,7 @@ public class CurrencyDataRetriever { new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { - + callBack.onSuccess(new Currency()); } }); diff --git a/app/src/main/java/com/nauk/coinfolio/DataManagers/CurrencyData/CurrencyTickerList.java b/app/src/main/java/com/nauk/coinfolio/DataManagers/CurrencyData/CurrencyTickerList.java new file mode 100644 index 0000000..66db323 --- /dev/null +++ b/app/src/main/java/com/nauk/coinfolio/DataManagers/CurrencyData/CurrencyTickerList.java @@ -0,0 +1,101 @@ +package com.nauk.coinfolio.DataManagers.CurrencyData; + +import android.util.Log; + +import com.android.volley.Request; +import com.android.volley.RequestQueue; +import com.android.volley.Response; +import com.android.volley.VolleyError; +import com.android.volley.toolbox.StringRequest; +import com.android.volley.toolbox.Volley; +import com.nauk.coinfolio.DataManagers.BalanceManager; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.LinkedHashMap; +import java.util.regex.Pattern; + +/** + * Created by Guitoune on 22/04/2018. + */ + +public class CurrencyTickerList { + + final private static String TICKERLISTURL = "https://api.coinmarketcap.com/v1/ticker/?limit=0"; + private RequestQueue requestQueue; + private LinkedHashMap coinTickersHashmap; + private android.content.Context context; + + public CurrencyTickerList(android.content.Context context) + { + this.context = context; + requestQueue = Volley.newRequestQueue(context); + } + + public void update(final BalanceManager.IconCallBack callBack) + { + coinTickersHashmap = new LinkedHashMap<>(); + StringRequest strRequest = new StringRequest(Request.Method.GET, TICKERLISTURL, + new Response.Listener() { + @Override + public void onResponse(String response) { + if (response.length() > 0) { + processTickerListResult(response, callBack); + } + } + }, + new Response.ErrorListener() { + @Override + public void onErrorResponse(VolleyError error) { + + } + }); + + requestQueue.add(strRequest); + } + + public String getTickerIdForSymbol(String symbol) + { + String tickerId = null; + try { + JSONObject jsonObject = new JSONObject(coinTickersHashmap.get(symbol)); + tickerId = jsonObject.getString("id"); + } catch (JSONException | NullPointerException e) { + e.printStackTrace(); + } + + return tickerId; + } + + public void processTickerListResult(String response, BalanceManager.IconCallBack callBack) + { + response = response.substring(1, response.length() - 1); + String[] strTable = response.split(Pattern.quote("},")); + + for(int i = 0; i < strTable.length; i++) + { + strTable[i] += "}"; + try { + JSONObject jsonObject = new JSONObject(strTable[i]); + switch (jsonObject.getString("symbol")) + { + case "MIOTA": + coinTickersHashmap.put("IOT", strTable[i]); + break; + case "NANO": + coinTickersHashmap.put("XRB", strTable[i]); + break; + default: + coinTickersHashmap.put(jsonObject.getString("symbol"), strTable[i]); + break; + } + } catch (JSONException e) { + e.printStackTrace(); + } + } + + callBack.onSuccess(); + } +} diff --git a/app/src/main/res/layout/activity_currency_details.xml b/app/src/main/res/layout/activity_currency_details.xml index 5f5f6c2..d4c14fb 100644 --- a/app/src/main/res/layout/activity_currency_details.xml +++ b/app/src/main/res/layout/activity_currency_details.xml @@ -443,13 +443,13 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="4dp" - android:layout_weight="0.5" - android:orientation="vertical"> + android:orientation="horizontal"> + android:orientation="vertical" + android:layout_weight="0.5"> + android:layout_height="wrap_content" + android:text="--"/> + + + + + + + +