diff --git a/app/src/main/java/com/herbron/moodl/Activities/HomeActivity.java b/app/src/main/java/com/herbron/moodl/Activities/HomeActivity.java index c394da6..06a2b85 100644 --- a/app/src/main/java/com/herbron/moodl/Activities/HomeActivity.java +++ b/app/src/main/java/com/herbron/moodl/Activities/HomeActivity.java @@ -158,6 +158,11 @@ public class HomeActivity extends AppCompatActivity implements BalanceUpdateInte currentFragment = fragment; } + public Fragment getHoldingsFragment() + { + return holdingsFragment; + } + @Override protected void onResume() { super.onResume(); diff --git a/app/src/main/java/com/herbron/moodl/Activities/HomeActivityFragments/Summary.java b/app/src/main/java/com/herbron/moodl/Activities/HomeActivityFragments/Summary.java index 7051169..41d3a4b 100644 --- a/app/src/main/java/com/herbron/moodl/Activities/HomeActivityFragments/Summary.java +++ b/app/src/main/java/com/herbron/moodl/Activities/HomeActivityFragments/Summary.java @@ -10,6 +10,7 @@ import android.os.Handler; import android.os.Looper; import android.support.annotation.NonNull; import android.support.design.widget.CollapsingToolbarLayout; +import android.support.design.widget.Snackbar; import android.support.v4.app.Fragment; import android.os.Bundle; import android.support.v4.view.GravityCompat; @@ -30,11 +31,13 @@ import android.widget.TextView; import com.herbron.moodl.Activities.CurrencySelectionActivity; import com.herbron.moodl.Activities.HomeActivity; import com.herbron.moodl.BalanceUpdateInterface; +import com.herbron.moodl.DataManagers.BalanceManager; import com.herbron.moodl.DataManagers.CurrencyData.Currency; import com.herbron.moodl.DataManagers.CurrencyData.CurrencyCardview; import com.herbron.moodl.DataManagers.CurrencyData.CurrencyTickerList; import com.herbron.moodl.DataManagers.PreferencesManager; import com.herbron.moodl.BalanceSwitchManagerInterface; +import com.herbron.moodl.DataNotifierInterface; import com.herbron.moodl.MoodlBox; import com.herbron.moodl.PlaceholderManager; import com.herbron.moodl.R; @@ -50,7 +53,7 @@ import static java.lang.Math.abs; * Created by Tiji on 13/04/2018. */ -public class Summary extends Fragment implements BalanceSwitchManagerInterface { +public class Summary extends Fragment implements BalanceSwitchManagerInterface, DataNotifierInterface { private LinearLayout currencyLayout; private PreferencesManager preferencesManager; @@ -84,7 +87,7 @@ public class Summary extends Fragment implements BalanceSwitchManagerInterface { View fragmentView = inflater.inflate(R.layout.fragment_summary_homeactivity, container, false); preferencesManager = new PreferencesManager(getActivity()); - balanceManager = new com.herbron.moodl.DataManagers.BalanceManager(getActivity()); + balanceManager = new BalanceManager(getContext()); currencyTickerList = CurrencyTickerList.getInstance(getActivity()); currencyLayout = fragmentView.findViewById(R.id.currencyListLayout); @@ -305,14 +308,14 @@ public class Summary extends Fragment implements BalanceSwitchManagerInterface { private void showErrorSnackbar() { - /*Snackbar.make(getActivity().findViewById(R.id.snackbar_placer), "Error while updating data", Snackbar.LENGTH_LONG) + Snackbar.make(getActivity().findViewById(R.id.content_frame), "Error while updating data", Snackbar.LENGTH_LONG) .setAction("Update", new View.OnClickListener() { @Override public void onClick(View view) { } }) - .show();*/ + .show(); } private void resetCounters() @@ -497,6 +500,87 @@ public class Summary extends Fragment implements BalanceSwitchManagerInterface { displayBalance(preferencesManager.isBalanceHidden()); } + @Override + public void onTickerListUpdated() { + + } + + @Override + public void onDetailsUpdated() { + + } + + @Override + public void onBalanceDataUpdated() { + final List balance = balanceManager.getTotalBalance(); + + if(balanceManager.getTotalBalance().size() > 0) + { + for(int i = 0; i < balanceManager.getTotalBalance().size(); i++) + { + balance.get(i).updatePrice(getActivity(), defaultCurrency, new Currency.CurrencyCallBack() { + @Override + public void onSuccess(Currency currency) { + countCoins(true, false, false); + } + }); + } + } + else + { + getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + countCoins(false, false, false); + } + }); + } + } + + @Override + public void onBalanceError(String error) + { + generateSnackBarError(error); + } + + private void generateSnackBarError(String error) + { + View view = getActivity().findViewById(R.id.content_frame); + + switch (error) + { + case "com.android.volley.AuthFailureError": + preferencesManager.disableHitBTC(); + Snackbar.make(view, "HitBTC synchronization error : Invalid keys", Snackbar.LENGTH_LONG) + .show(); + + refreshLayout.setRefreshing(false); + + updateAll(true); + break; + case "API-key format invalid.": + preferencesManager.disableBinance(); + Snackbar.make(view, "Binance synchronization error : Invalid keys", Snackbar.LENGTH_LONG) + .show(); + + updateAll(true); + break; + case "com.android.volley.NoConnectionError: java.net.UnknownHostException: Unable to resolve host \"api.hitbtc.com\": No address associated with hostname": + Snackbar.make(view, "Can't resolve host", Snackbar.LENGTH_LONG) + .show(); + break; + case "com.android.volley.TimeoutError": + break; + default: + Snackbar.make(view, "Unexpected error", Snackbar.LENGTH_LONG) + .show(); + + Log.d("moodl", error); + + updateAll(false); + } + } + private class UiHeavyLoadCalculator extends AsyncTask { @@ -667,44 +751,6 @@ public class Summary extends Fragment implements BalanceSwitchManagerInterface { private class DataUpdater extends AsyncTask { - private void generateSnackBarError(String error) - { - /*View view = getActivity().findViewById(R.id.snackbar_placer); - - switch (error) - { - case "com.android.volley.AuthFailureError": - preferencesManager.disableHitBTC(); - Snackbar.make(view, "HitBTC synchronization error : Invalid keys", Snackbar.LENGTH_LONG) - .show(); - - refreshLayout.setRefreshing(false); - - updateAll(true); - break; - case "API-key format invalid.": - preferencesManager.disableBinance(); - Snackbar.make(view, "Binance synchronization error : Invalid keys", Snackbar.LENGTH_LONG) - .show(); - - updateAll(true); - break; - case "com.android.volley.NoConnectionError: java.net.UnknownHostException: Unable to resolve host \"api.hitbtc.com\": No address associated with hostname": - Snackbar.make(view, "Can't resolve host", Snackbar.LENGTH_LONG) - .show(); - break; - case "com.android.volley.TimeoutError": - break; - default: - Snackbar.make(view, "Unexpected error", Snackbar.LENGTH_LONG) - .show(); - - Log.d("moodl", error); - - updateAll(false); - }*/ - } - @Override protected Void doInBackground(Void... params) { @@ -730,39 +776,7 @@ public class Summary extends Fragment implements BalanceSwitchManagerInterface { } }); - balanceManager.updateTotalBalance(new com.herbron.moodl.DataManagers.BalanceManager.VolleyCallBack() { - @Override - public void onSuccess() { - final List balance = balanceManager.getTotalBalance(); - - if(balanceManager.getTotalBalance().size() > 0) - { - for(int i = 0; i < balanceManager.getTotalBalance().size(); i++) - { - balance.get(i).updatePrice(getActivity(), defaultCurrency, new Currency.CurrencyCallBack() { - @Override - public void onSuccess(Currency currency) { - countCoins(true, false, false); - } - }); - } - } - else - { - getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - countCoins(false, false, false); - } - }); - } - } - - public void onError(String error) - { - generateSnackBarError(error); - } - }); + balanceManager.updateTotalBalance(); return null; } diff --git a/app/src/main/java/com/herbron/moodl/DataManagers/BalanceManager.java b/app/src/main/java/com/herbron/moodl/DataManagers/BalanceManager.java index 650da0a..398db93 100644 --- a/app/src/main/java/com/herbron/moodl/DataManagers/BalanceManager.java +++ b/app/src/main/java/com/herbron/moodl/DataManagers/BalanceManager.java @@ -4,10 +4,12 @@ import android.util.Log; import com.android.volley.RequestQueue; import com.android.volley.toolbox.Volley; +import com.herbron.moodl.Activities.HomeActivity; import com.herbron.moodl.DataManagers.CurrencyData.Currency; import com.herbron.moodl.DataManagers.CurrencyData.CurrencyDetailsList; import com.herbron.moodl.DataManagers.ExchangeManager.BinanceManager; import com.herbron.moodl.DataManagers.ExchangeManager.HitBtcManager; +import com.herbron.moodl.DataNotifierInterface; import com.herbron.moodl.R; import org.json.JSONException; @@ -49,6 +51,8 @@ public class BalanceManager { private List hitBtcManagers; private List binanceManagers; + private DataNotifierInterface dataNotifierInterface; + public BalanceManager(android.content.Context context) { this.context = context; @@ -64,6 +68,13 @@ public class BalanceManager { currencyDetailsList = CurrencyDetailsList.getInstance(context); balanceCounter = 0; + + setListener((DataNotifierInterface) ((HomeActivity) context).getHoldingsFragment()); + } + + public void setListener(DataNotifierInterface dataNotifierInterface) + { + this.dataNotifierInterface = dataNotifierInterface; } public List getBiggestCurrencies() @@ -111,7 +122,7 @@ public class BalanceManager { return totalBalance; } - public void updateTotalBalance(final VolleyCallBack callBack) + public void updateTotalBalance() { boolean isUpdated = false; @@ -128,12 +139,12 @@ public class BalanceManager { binanceManagers.get(i).updateBalance(new BinanceManager.BinanceCallBack() { @Override public void onSuccess() { - countBalances(callBack); + countBalances(); } @Override public void onError(String error) { - callBack.onError(error); + dataNotifierInterface.onBalanceError(error); } }); } @@ -148,12 +159,12 @@ public class BalanceManager { hitBtcManagers.get(i).updateGlobalBalance(new HitBtcManager.HitBtcCallBack() { @Override public void onSuccess() { - countBalances(callBack); + countBalances(); } @Override public void onError(String error) { - callBack.onError(error); + dataNotifierInterface.onBalanceError(error); } }); } @@ -161,23 +172,23 @@ public class BalanceManager { if(!isUpdated) { - refreshAllBalances(callBack); + refreshAllBalances(); } } - private void countBalances(VolleyCallBack callBack) + private void countBalances() { balanceCounter++; if(balanceCounter == hitBtcManagers.size() + binanceManagers.size()) { - refreshAllBalances(callBack); + refreshAllBalances(); balanceCounter = 0; } } - private void refreshAllBalances(final VolleyCallBack callBack) + private void refreshAllBalances() { totalBalance = new ArrayList<>(); @@ -193,7 +204,7 @@ public class BalanceManager { mergeBalanceTotal(manualBalances); - callBack.onSuccess(); + dataNotifierInterface.onBalanceDataUpdated(); } private void mergeBalanceTotal(List balance) @@ -257,23 +268,6 @@ public class BalanceManager { { callBack.onSuccess(); } - /*StringRequest strRequest = new StringRequest(Request.Method.GET, detailUrl, - new Response.Listener() { - @Override - public void onResponse(String response) { - if (response.length() > 0) { - processDetailResult(response, callBack); - } - } - }, - new Response.ErrorListener() { - @Override - public void onErrorResponse(VolleyError error) { - - } - }); - - requestQueue.add(strRequest);*/ } public String getIconUrl(String symbol) diff --git a/app/src/main/java/com/herbron/moodl/DataNotifierInterface.java b/app/src/main/java/com/herbron/moodl/DataNotifierInterface.java new file mode 100644 index 0000000..409c1e6 --- /dev/null +++ b/app/src/main/java/com/herbron/moodl/DataNotifierInterface.java @@ -0,0 +1,16 @@ +package com.herbron.moodl; + +/** + * Created by Administrator on 17/06/2018. + */ + +public interface DataNotifierInterface { + + void onTickerListUpdated(); + + void onDetailsUpdated(); + + void onBalanceDataUpdated(); + + void onBalanceError(String error); +}