From d0069f4ad5a434a590053320869ef155e302ce0d Mon Sep 17 00:00:00 2001 From: Tanguy Herbron Date: Sun, 27 May 2018 01:03:59 +0200 Subject: [PATCH] Optimization - CurrencyDetailsList is now a Singleton class - CurrencyTickerList is now a Singleton class --- .../Activities/CurrencySelectionActivity.java | 50 ++++++++++++------- .../MarketCapitalization.java | 25 +++++++--- .../HomeActivityFragments/Summary.java | 22 +++++--- .../HomeActivityFragments/Watchlist.java | 49 ++++++++++++------ .../moodl/DataManagers/BalanceManager.java | 11 +++- .../CurrencyData/CurrencyDetailsList.java | 27 ++++++++-- .../CurrencyData/CurrencyTickerList.java | 25 ++++++++-- 7 files changed, 148 insertions(+), 61 deletions(-) diff --git a/app/src/main/java/com/nauk/moodl/Activities/CurrencySelectionActivity.java b/app/src/main/java/com/nauk/moodl/Activities/CurrencySelectionActivity.java index a302e41..4c9c97b 100644 --- a/app/src/main/java/com/nauk/moodl/Activities/CurrencySelectionActivity.java +++ b/app/src/main/java/com/nauk/moodl/Activities/CurrencySelectionActivity.java @@ -44,7 +44,7 @@ public class CurrencySelectionActivity extends AppCompatActivity implements Sear setContentView(R.layout.activity_add_currency); - currencyDetailsList = new CurrencyDetailsList(this); + currencyDetailsList = CurrencyDetailsList.getInstance(this); setTitle("Select a coin"); @@ -162,6 +162,23 @@ public class CurrencySelectionActivity extends AppCompatActivity implements Sear return false; } + private void detailsEvent() + { + setupAdapter(); + + setupList(); + + runOnUiThread(new Runnable() { + @Override + public void run() { + setupSearchView(); + + expand(findViewById(R.id.listContainerLayout)); + findViewById(R.id.currencyListProgressBar).setVisibility(View.GONE); + } + }); + } + private class ListLoader extends AsyncTask { @Override @@ -184,24 +201,19 @@ public class CurrencySelectionActivity extends AppCompatActivity implements Sear Looper.prepare(); } - currencyDetailsList.update(new BalanceManager.IconCallBack() { - @Override - public void onSuccess() { - setupAdapter(); - - setupList(); - - runOnUiThread(new Runnable() { - @Override - public void run() { - setupSearchView(); - - expand(findViewById(R.id.listContainerLayout)); - findViewById(R.id.currencyListProgressBar).setVisibility(View.GONE); - } - }); - } - }); + if(!currencyDetailsList.isUpToDate()) + { + currencyDetailsList.update(new BalanceManager.IconCallBack() { + @Override + public void onSuccess() { + detailsEvent(); + } + }); + } + else + { + detailsEvent(); + } return null; } diff --git a/app/src/main/java/com/nauk/moodl/Activities/HomeActivityFragments/MarketCapitalization.java b/app/src/main/java/com/nauk/moodl/Activities/HomeActivityFragments/MarketCapitalization.java index 82e838a..4f14e5f 100644 --- a/app/src/main/java/com/nauk/moodl/Activities/HomeActivityFragments/MarketCapitalization.java +++ b/app/src/main/java/com/nauk/moodl/Activities/HomeActivityFragments/MarketCapitalization.java @@ -73,14 +73,23 @@ public class MarketCapitalization extends Fragment { preferencesManager = new PreferencesManager(getContext()); marketCapManager = new MarketCapManager(getContext()); - currencyDetailsList = new CurrencyDetailsList(getContext()); - currencyDetailsList.update(new BalanceManager.IconCallBack() { - @Override - public void onSuccess() { - isDetailsUpdated = true; - countCompletedMarketCapRequest(); - } - }); + currencyDetailsList = CurrencyDetailsList.getInstance(getContext()); + + if(!currencyDetailsList.isUpToDate()) + { + currencyDetailsList.update(new BalanceManager.IconCallBack() { + @Override + public void onSuccess() { + isDetailsUpdated = true; + countCompletedMarketCapRequest(); + } + }); + } + else + { + isDetailsUpdated = true; + countCompletedMarketCapRequest(); + } defaultCurrency = preferencesManager.getDefaultCurrency(); lastTimestamp = 0; diff --git a/app/src/main/java/com/nauk/moodl/Activities/HomeActivityFragments/Summary.java b/app/src/main/java/com/nauk/moodl/Activities/HomeActivityFragments/Summary.java index ee53eea..0b86c0b 100644 --- a/app/src/main/java/com/nauk/moodl/Activities/HomeActivityFragments/Summary.java +++ b/app/src/main/java/com/nauk/moodl/Activities/HomeActivityFragments/Summary.java @@ -87,7 +87,7 @@ public class Summary extends Fragment implements HideBalanceSwitch { preferencesManager = new PreferencesManager(getActivity()); balanceManager = new BalanceManager(getActivity()); - currencyTickerList = new CurrencyTickerList(getActivity()); + currencyTickerList = CurrencyTickerList.getInstance(getActivity()); currencyLayout = fragmentView.findViewById(R.id.currencyListLayout); refreshLayout = fragmentView.findViewById(R.id.swiperefreshsummary); @@ -691,12 +691,20 @@ public class Summary extends Fragment implements HideBalanceSwitch { @Override protected Void doInBackground(Void... params) { - currencyTickerList.update(new BalanceManager.IconCallBack() { - @Override - public void onSuccess() { - countCoins(false, false, true); - } - }); + if(!currencyTickerList.isUpToDate()) + { + currencyTickerList.update(new BalanceManager.IconCallBack() { + @Override + public void onSuccess() { + countCoins(false, false, true); + } + }); + } + else + { + countCoins(false, false, true); + } + balanceManager.updateDetails(new BalanceManager.IconCallBack() { @Override public void onSuccess() diff --git a/app/src/main/java/com/nauk/moodl/Activities/HomeActivityFragments/Watchlist.java b/app/src/main/java/com/nauk/moodl/Activities/HomeActivityFragments/Watchlist.java index f6c94dd..47ead6b 100644 --- a/app/src/main/java/com/nauk/moodl/Activities/HomeActivityFragments/Watchlist.java +++ b/app/src/main/java/com/nauk/moodl/Activities/HomeActivityFragments/Watchlist.java @@ -69,12 +69,12 @@ public class Watchlist extends Fragment { view = inflater.inflate(R.layout.fragment_watchlist_homeactivity, container, false); refreshLayout = view.findViewById(R.id.swiperefreshwatchlist); - currencyDetailsList = new CurrencyDetailsList(getContext()); + currencyDetailsList = CurrencyDetailsList.getInstance(getContext()); preferencesManager = new PreferencesManager(getContext()); lastTimestamp = 0; defaultCurrency = preferencesManager.getDefaultCurrency(); - currencyTickerList = new CurrencyTickerList(getActivity()); + currencyTickerList = CurrencyTickerList.getInstance(getActivity()); tickerUpdated = false; updateTickerList(); @@ -105,13 +105,22 @@ public class Watchlist extends Fragment { AsyncTask updater = new AsyncTask() { @Override protected Void doInBackground(Void... voids) { - currencyTickerList.update(new BalanceManager.IconCallBack() { - @Override - public void onSuccess() { - tickerUpdated = true; - checkUpdatedData(); - } - }); + if(!currencyTickerList.isUpToDate()) + { + currencyTickerList.update(new BalanceManager.IconCallBack() { + @Override + public void onSuccess() { + tickerUpdated = true; + checkUpdatedData(); + } + }); + } + else + { + tickerUpdated = true; + checkUpdatedData(); + } + return null; } }; @@ -227,13 +236,21 @@ public class Watchlist extends Fragment { protected Void doInBackground(Void... voids) { watchlistManager.updateWatchlist(); - currencyDetailsList.update(new BalanceManager.IconCallBack() { - @Override - public void onSuccess() { - detailsUpdated = true; - checkUpdatedData(); - } - }); + if(!currencyDetailsList.isUpToDate()) + { + currencyDetailsList.update(new BalanceManager.IconCallBack() { + @Override + public void onSuccess() { + detailsUpdated = true; + checkUpdatedData(); + } + }); + } + else + { + detailsUpdated = true; + checkUpdatedData(); + } return null; } }; diff --git a/app/src/main/java/com/nauk/moodl/DataManagers/BalanceManager.java b/app/src/main/java/com/nauk/moodl/DataManagers/BalanceManager.java index 4fc24f2..ac647dd 100644 --- a/app/src/main/java/com/nauk/moodl/DataManagers/BalanceManager.java +++ b/app/src/main/java/com/nauk/moodl/DataManagers/BalanceManager.java @@ -61,7 +61,7 @@ public class BalanceManager { databaseManager = new DatabaseManager(context); hitBtcManagers = new ArrayList<>(); binanceManagers = new ArrayList<>(); - currencyDetailsList = new CurrencyDetailsList(context); + currencyDetailsList = CurrencyDetailsList.getInstance(context); balanceCounter = 0; } @@ -249,7 +249,14 @@ public class BalanceManager { public void updateDetails(final IconCallBack callBack) { - currencyDetailsList.update(callBack); + if(!currencyDetailsList.isUpToDate()) + { + currencyDetailsList.update(callBack); + } + else + { + callBack.onSuccess(); + } /*StringRequest strRequest = new StringRequest(Request.Method.GET, detailUrl, new Response.Listener() { @Override diff --git a/app/src/main/java/com/nauk/moodl/DataManagers/CurrencyData/CurrencyDetailsList.java b/app/src/main/java/com/nauk/moodl/DataManagers/CurrencyData/CurrencyDetailsList.java index fcd7975..f80130c 100644 --- a/app/src/main/java/com/nauk/moodl/DataManagers/CurrencyData/CurrencyDetailsList.java +++ b/app/src/main/java/com/nauk/moodl/DataManagers/CurrencyData/CurrencyDetailsList.java @@ -1,5 +1,6 @@ package com.nauk.moodl.DataManagers.CurrencyData; +import android.content.Context; import android.os.StrictMode; import android.util.Log; @@ -29,14 +30,24 @@ public class CurrencyDetailsList { final private static String DETAILURL = "https://min-api.cryptocompare.com/data/all/coinlist"; private RequestQueue requestQueue; private LinkedHashMap coinInfosHashmap; - private android.content.Context context; + private static CurrencyDetailsList INSTANCE; + private boolean upToDate; - public CurrencyDetailsList(android.content.Context context) + private CurrencyDetailsList(Context context) { - this.context = context; requestQueue = Volley.newRequestQueue(context); } + public static synchronized CurrencyDetailsList getInstance(Context context) + { + if(INSTANCE == null) + { + INSTANCE = new CurrencyDetailsList(context); + } + + return INSTANCE; + } + public void update(final BalanceManager.IconCallBack callBack) { StringRequest strRequest = new StringRequest(Request.Method.GET, DETAILURL, @@ -45,19 +56,25 @@ public class CurrencyDetailsList { public void onResponse(String response) { if (response.length() > 0) { processDetailResult(response, callBack); + } + upToDate = true; } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { - + upToDate = true; } }); requestQueue.add(strRequest); } + public boolean isUpToDate() + { + return upToDate; + } private void processDetailResult(String response, final BalanceManager.IconCallBack callBack) { @@ -87,7 +104,7 @@ public class CurrencyDetailsList { break; } } catch (JSONException e) { - Log.d(context.getResources().getString(R.string.debug), "ImageUrl not found."); + Log.d("moodl", "ImageUrl not found."); } } diff --git a/app/src/main/java/com/nauk/moodl/DataManagers/CurrencyData/CurrencyTickerList.java b/app/src/main/java/com/nauk/moodl/DataManagers/CurrencyData/CurrencyTickerList.java index 0affd0b..8e8d099 100644 --- a/app/src/main/java/com/nauk/moodl/DataManagers/CurrencyData/CurrencyTickerList.java +++ b/app/src/main/java/com/nauk/moodl/DataManagers/CurrencyData/CurrencyTickerList.java @@ -1,5 +1,6 @@ package com.nauk.moodl.DataManagers.CurrencyData; +import android.content.Context; import android.util.Log; import com.android.volley.Request; @@ -28,14 +29,29 @@ public class CurrencyTickerList { final private static String TICKERLISTURL = "https://api.coinmarketcap.com/v2/listings/"; private RequestQueue requestQueue; private List currencyTickerList; - private android.content.Context context; + private static CurrencyTickerList INSTANCE; + private boolean upToDate; - public CurrencyTickerList(android.content.Context context) + private CurrencyTickerList(Context context) { - this.context = context; requestQueue = Volley.newRequestQueue(context); } + public static synchronized CurrencyTickerList getInstance(Context context) + { + if(INSTANCE == null) + { + INSTANCE = new CurrencyTickerList(context); + } + + return INSTANCE; + } + + public boolean isUpToDate() + { + return upToDate; + } + public void update(final BalanceManager.IconCallBack callBack) { currencyTickerList = new ArrayList<>(); @@ -46,12 +62,13 @@ public class CurrencyTickerList { if (response.length() > 0) { processTickerListResult(response, callBack); } + upToDate = true; } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { - + upToDate = true; } });