Optimization

- CurrencyDetailsList is now a Singleton class
- CurrencyTickerList is now a Singleton class
This commit is contained in:
Tanguy Herbron 2018-05-27 01:03:59 +02:00
parent fcb288e0ed
commit d0069f4ad5
7 changed files with 148 additions and 61 deletions

View File

@ -44,7 +44,7 @@ public class CurrencySelectionActivity extends AppCompatActivity implements Sear
setContentView(R.layout.activity_add_currency); setContentView(R.layout.activity_add_currency);
currencyDetailsList = new CurrencyDetailsList(this); currencyDetailsList = CurrencyDetailsList.getInstance(this);
setTitle("Select a coin"); setTitle("Select a coin");
@ -162,6 +162,23 @@ public class CurrencySelectionActivity extends AppCompatActivity implements Sear
return false; 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<Void, Integer, Void> private class ListLoader extends AsyncTask<Void, Integer, Void>
{ {
@Override @Override
@ -184,24 +201,19 @@ public class CurrencySelectionActivity extends AppCompatActivity implements Sear
Looper.prepare(); Looper.prepare();
} }
if(!currencyDetailsList.isUpToDate())
{
currencyDetailsList.update(new BalanceManager.IconCallBack() { currencyDetailsList.update(new BalanceManager.IconCallBack() {
@Override @Override
public void onSuccess() { public void onSuccess() {
setupAdapter(); detailsEvent();
setupList();
runOnUiThread(new Runnable() {
@Override
public void run() {
setupSearchView();
expand(findViewById(R.id.listContainerLayout));
findViewById(R.id.currencyListProgressBar).setVisibility(View.GONE);
} }
}); });
} }
}); else
{
detailsEvent();
}
return null; return null;
} }

View File

@ -73,7 +73,10 @@ public class MarketCapitalization extends Fragment {
preferencesManager = new PreferencesManager(getContext()); preferencesManager = new PreferencesManager(getContext());
marketCapManager = new MarketCapManager(getContext()); marketCapManager = new MarketCapManager(getContext());
currencyDetailsList = new CurrencyDetailsList(getContext()); currencyDetailsList = CurrencyDetailsList.getInstance(getContext());
if(!currencyDetailsList.isUpToDate())
{
currencyDetailsList.update(new BalanceManager.IconCallBack() { currencyDetailsList.update(new BalanceManager.IconCallBack() {
@Override @Override
public void onSuccess() { public void onSuccess() {
@ -81,6 +84,12 @@ public class MarketCapitalization extends Fragment {
countCompletedMarketCapRequest(); countCompletedMarketCapRequest();
} }
}); });
}
else
{
isDetailsUpdated = true;
countCompletedMarketCapRequest();
}
defaultCurrency = preferencesManager.getDefaultCurrency(); defaultCurrency = preferencesManager.getDefaultCurrency();
lastTimestamp = 0; lastTimestamp = 0;

View File

@ -87,7 +87,7 @@ public class Summary extends Fragment implements HideBalanceSwitch {
preferencesManager = new PreferencesManager(getActivity()); preferencesManager = new PreferencesManager(getActivity());
balanceManager = new BalanceManager(getActivity()); balanceManager = new BalanceManager(getActivity());
currencyTickerList = new CurrencyTickerList(getActivity()); currencyTickerList = CurrencyTickerList.getInstance(getActivity());
currencyLayout = fragmentView.findViewById(R.id.currencyListLayout); currencyLayout = fragmentView.findViewById(R.id.currencyListLayout);
refreshLayout = fragmentView.findViewById(R.id.swiperefreshsummary); refreshLayout = fragmentView.findViewById(R.id.swiperefreshsummary);
@ -690,6 +690,8 @@ public class Summary extends Fragment implements HideBalanceSwitch {
@Override @Override
protected Void doInBackground(Void... params) protected Void doInBackground(Void... params)
{
if(!currencyTickerList.isUpToDate())
{ {
currencyTickerList.update(new BalanceManager.IconCallBack() { currencyTickerList.update(new BalanceManager.IconCallBack() {
@Override @Override
@ -697,6 +699,12 @@ public class Summary extends Fragment implements HideBalanceSwitch {
countCoins(false, false, true); countCoins(false, false, true);
} }
}); });
}
else
{
countCoins(false, false, true);
}
balanceManager.updateDetails(new BalanceManager.IconCallBack() { balanceManager.updateDetails(new BalanceManager.IconCallBack() {
@Override @Override
public void onSuccess() public void onSuccess()

View File

@ -69,12 +69,12 @@ public class Watchlist extends Fragment {
view = inflater.inflate(R.layout.fragment_watchlist_homeactivity, container, false); view = inflater.inflate(R.layout.fragment_watchlist_homeactivity, container, false);
refreshLayout = view.findViewById(R.id.swiperefreshwatchlist); refreshLayout = view.findViewById(R.id.swiperefreshwatchlist);
currencyDetailsList = new CurrencyDetailsList(getContext()); currencyDetailsList = CurrencyDetailsList.getInstance(getContext());
preferencesManager = new PreferencesManager(getContext()); preferencesManager = new PreferencesManager(getContext());
lastTimestamp = 0; lastTimestamp = 0;
defaultCurrency = preferencesManager.getDefaultCurrency(); defaultCurrency = preferencesManager.getDefaultCurrency();
currencyTickerList = new CurrencyTickerList(getActivity()); currencyTickerList = CurrencyTickerList.getInstance(getActivity());
tickerUpdated = false; tickerUpdated = false;
updateTickerList(); updateTickerList();
@ -105,6 +105,8 @@ public class Watchlist extends Fragment {
AsyncTask<Void, Integer, Void> updater = new AsyncTask<Void, Integer, Void>() { AsyncTask<Void, Integer, Void> updater = new AsyncTask<Void, Integer, Void>() {
@Override @Override
protected Void doInBackground(Void... voids) { protected Void doInBackground(Void... voids) {
if(!currencyTickerList.isUpToDate())
{
currencyTickerList.update(new BalanceManager.IconCallBack() { currencyTickerList.update(new BalanceManager.IconCallBack() {
@Override @Override
public void onSuccess() { public void onSuccess() {
@ -112,6 +114,13 @@ public class Watchlist extends Fragment {
checkUpdatedData(); checkUpdatedData();
} }
}); });
}
else
{
tickerUpdated = true;
checkUpdatedData();
}
return null; return null;
} }
}; };
@ -227,6 +236,8 @@ public class Watchlist extends Fragment {
protected Void doInBackground(Void... voids) { protected Void doInBackground(Void... voids) {
watchlistManager.updateWatchlist(); watchlistManager.updateWatchlist();
if(!currencyDetailsList.isUpToDate())
{
currencyDetailsList.update(new BalanceManager.IconCallBack() { currencyDetailsList.update(new BalanceManager.IconCallBack() {
@Override @Override
public void onSuccess() { public void onSuccess() {
@ -234,6 +245,12 @@ public class Watchlist extends Fragment {
checkUpdatedData(); checkUpdatedData();
} }
}); });
}
else
{
detailsUpdated = true;
checkUpdatedData();
}
return null; return null;
} }
}; };

View File

@ -61,7 +61,7 @@ public class BalanceManager {
databaseManager = new DatabaseManager(context); databaseManager = new DatabaseManager(context);
hitBtcManagers = new ArrayList<>(); hitBtcManagers = new ArrayList<>();
binanceManagers = new ArrayList<>(); binanceManagers = new ArrayList<>();
currencyDetailsList = new CurrencyDetailsList(context); currencyDetailsList = CurrencyDetailsList.getInstance(context);
balanceCounter = 0; balanceCounter = 0;
} }
@ -248,8 +248,15 @@ public class BalanceManager {
} }
public void updateDetails(final IconCallBack callBack) public void updateDetails(final IconCallBack callBack)
{
if(!currencyDetailsList.isUpToDate())
{ {
currencyDetailsList.update(callBack); currencyDetailsList.update(callBack);
}
else
{
callBack.onSuccess();
}
/*StringRequest strRequest = new StringRequest(Request.Method.GET, detailUrl, /*StringRequest strRequest = new StringRequest(Request.Method.GET, detailUrl,
new Response.Listener<String>() { new Response.Listener<String>() {
@Override @Override

View File

@ -1,5 +1,6 @@
package com.nauk.moodl.DataManagers.CurrencyData; package com.nauk.moodl.DataManagers.CurrencyData;
import android.content.Context;
import android.os.StrictMode; import android.os.StrictMode;
import android.util.Log; import android.util.Log;
@ -29,14 +30,24 @@ public class CurrencyDetailsList {
final private static String DETAILURL = "https://min-api.cryptocompare.com/data/all/coinlist"; final private static String DETAILURL = "https://min-api.cryptocompare.com/data/all/coinlist";
private RequestQueue requestQueue; private RequestQueue requestQueue;
private LinkedHashMap<String, String> coinInfosHashmap; private LinkedHashMap<String, String> 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); 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) public void update(final BalanceManager.IconCallBack callBack)
{ {
StringRequest strRequest = new StringRequest(Request.Method.GET, DETAILURL, StringRequest strRequest = new StringRequest(Request.Method.GET, DETAILURL,
@ -45,19 +56,25 @@ public class CurrencyDetailsList {
public void onResponse(String response) { public void onResponse(String response) {
if (response.length() > 0) { if (response.length() > 0) {
processDetailResult(response, callBack); processDetailResult(response, callBack);
} }
upToDate = true;
} }
}, },
new Response.ErrorListener() { new Response.ErrorListener() {
@Override @Override
public void onErrorResponse(VolleyError error) { public void onErrorResponse(VolleyError error) {
upToDate = true;
} }
}); });
requestQueue.add(strRequest); requestQueue.add(strRequest);
} }
public boolean isUpToDate()
{
return upToDate;
}
private void processDetailResult(String response, final BalanceManager.IconCallBack callBack) private void processDetailResult(String response, final BalanceManager.IconCallBack callBack)
{ {
@ -87,7 +104,7 @@ public class CurrencyDetailsList {
break; break;
} }
} catch (JSONException e) { } catch (JSONException e) {
Log.d(context.getResources().getString(R.string.debug), "ImageUrl not found."); Log.d("moodl", "ImageUrl not found.");
} }
} }

View File

@ -1,5 +1,6 @@
package com.nauk.moodl.DataManagers.CurrencyData; package com.nauk.moodl.DataManagers.CurrencyData;
import android.content.Context;
import android.util.Log; import android.util.Log;
import com.android.volley.Request; import com.android.volley.Request;
@ -28,14 +29,29 @@ public class CurrencyTickerList {
final private static String TICKERLISTURL = "https://api.coinmarketcap.com/v2/listings/"; final private static String TICKERLISTURL = "https://api.coinmarketcap.com/v2/listings/";
private RequestQueue requestQueue; private RequestQueue requestQueue;
private List<Currency> currencyTickerList; private List<Currency> 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); 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) public void update(final BalanceManager.IconCallBack callBack)
{ {
currencyTickerList = new ArrayList<>(); currencyTickerList = new ArrayList<>();
@ -46,12 +62,13 @@ public class CurrencyTickerList {
if (response.length() > 0) { if (response.length() > 0) {
processTickerListResult(response, callBack); processTickerListResult(response, callBack);
} }
upToDate = true;
} }
}, },
new Response.ErrorListener() { new Response.ErrorListener() {
@Override @Override
public void onErrorResponse(VolleyError error) { public void onErrorResponse(VolleyError error) {
upToDate = true;
} }
}); });