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();
} }
currencyDetailsList.update(new BalanceManager.IconCallBack() { if(!currencyDetailsList.isUpToDate())
@Override {
public void onSuccess() { currencyDetailsList.update(new BalanceManager.IconCallBack() {
setupAdapter(); @Override
public void onSuccess() {
setupList(); detailsEvent();
}
runOnUiThread(new Runnable() { });
@Override }
public void run() { else
setupSearchView(); {
detailsEvent();
expand(findViewById(R.id.listContainerLayout)); }
findViewById(R.id.currencyListProgressBar).setVisibility(View.GONE);
}
});
}
});
return null; return null;
} }

View File

@ -73,14 +73,23 @@ 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());
currencyDetailsList.update(new BalanceManager.IconCallBack() {
@Override if(!currencyDetailsList.isUpToDate())
public void onSuccess() { {
isDetailsUpdated = true; currencyDetailsList.update(new BalanceManager.IconCallBack() {
countCompletedMarketCapRequest(); @Override
} public void onSuccess() {
}); isDetailsUpdated = true;
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);
@ -691,12 +691,20 @@ public class Summary extends Fragment implements HideBalanceSwitch {
@Override @Override
protected Void doInBackground(Void... params) protected Void doInBackground(Void... params)
{ {
currencyTickerList.update(new BalanceManager.IconCallBack() { if(!currencyTickerList.isUpToDate())
@Override {
public void onSuccess() { currencyTickerList.update(new BalanceManager.IconCallBack() {
countCoins(false, false, true); @Override
} public void onSuccess() {
}); 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,13 +105,22 @@ 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) {
currencyTickerList.update(new BalanceManager.IconCallBack() { if(!currencyTickerList.isUpToDate())
@Override {
public void onSuccess() { currencyTickerList.update(new BalanceManager.IconCallBack() {
tickerUpdated = true; @Override
checkUpdatedData(); public void onSuccess() {
} tickerUpdated = true;
}); checkUpdatedData();
}
});
}
else
{
tickerUpdated = true;
checkUpdatedData();
}
return null; return null;
} }
}; };
@ -227,13 +236,21 @@ public class Watchlist extends Fragment {
protected Void doInBackground(Void... voids) { protected Void doInBackground(Void... voids) {
watchlistManager.updateWatchlist(); watchlistManager.updateWatchlist();
currencyDetailsList.update(new BalanceManager.IconCallBack() { if(!currencyDetailsList.isUpToDate())
@Override {
public void onSuccess() { currencyDetailsList.update(new BalanceManager.IconCallBack() {
detailsUpdated = true; @Override
checkUpdatedData(); public void onSuccess() {
} detailsUpdated = true;
}); 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;
} }
@ -249,7 +249,14 @@ public class BalanceManager {
public void updateDetails(final IconCallBack callBack) 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, /*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;
} }
}); });