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);
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<Void, Integer, Void>
{
@Override
@ -184,24 +201,19 @@ public class CurrencySelectionActivity extends AppCompatActivity implements Sear
Looper.prepare();
}
if(!currencyDetailsList.isUpToDate())
{
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);
detailsEvent();
}
});
}
});
else
{
detailsEvent();
}
return null;
}

View File

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

View File

@ -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);
@ -690,6 +690,8 @@ public class Summary extends Fragment implements HideBalanceSwitch {
@Override
protected Void doInBackground(Void... params)
{
if(!currencyTickerList.isUpToDate())
{
currencyTickerList.update(new BalanceManager.IconCallBack() {
@Override
@ -697,6 +699,12 @@ public class Summary extends Fragment implements HideBalanceSwitch {
countCoins(false, false, true);
}
});
}
else
{
countCoins(false, false, true);
}
balanceManager.updateDetails(new BalanceManager.IconCallBack() {
@Override
public void onSuccess()

View File

@ -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,6 +105,8 @@ public class Watchlist extends Fragment {
AsyncTask<Void, Integer, Void> updater = new AsyncTask<Void, Integer, Void>() {
@Override
protected Void doInBackground(Void... voids) {
if(!currencyTickerList.isUpToDate())
{
currencyTickerList.update(new BalanceManager.IconCallBack() {
@Override
public void onSuccess() {
@ -112,6 +114,13 @@ public class Watchlist extends Fragment {
checkUpdatedData();
}
});
}
else
{
tickerUpdated = true;
checkUpdatedData();
}
return null;
}
};
@ -227,6 +236,8 @@ public class Watchlist extends Fragment {
protected Void doInBackground(Void... voids) {
watchlistManager.updateWatchlist();
if(!currencyDetailsList.isUpToDate())
{
currencyDetailsList.update(new BalanceManager.IconCallBack() {
@Override
public void onSuccess() {
@ -234,6 +245,12 @@ public class Watchlist extends Fragment {
checkUpdatedData();
}
});
}
else
{
detailsUpdated = true;
checkUpdatedData();
}
return null;
}
};

View File

@ -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;
}
@ -248,8 +248,15 @@ public class BalanceManager {
}
public void updateDetails(final IconCallBack callBack)
{
if(!currencyDetailsList.isUpToDate())
{
currencyDetailsList.update(callBack);
}
else
{
callBack.onSuccess();
}
/*StringRequest strRequest = new StringRequest(Request.Method.GET, detailUrl,
new Response.Listener<String>() {
@Override

View File

@ -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<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);
}
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.");
}
}

View File

@ -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<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);
}
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;
}
});