From 78adcd548515e0ad092c1fb80a2c314df2bddbd9 Mon Sep 17 00:00:00 2001 From: Tanguy Herbron Date: Wed, 18 Apr 2018 01:37:54 +0200 Subject: [PATCH] Performance improvements - New data refreshing method | avoid soft freezing the app - Update fragment titles - Update background color for a better visibility of summary and watchlist cards --- .../coinfolio/Activities/HomeActivity.java | 2 - .../HomeActivityFragments/Summary.java | 99 +++++--------- .../HomeActivityFragments/Watchlist.java | 122 +++++++++++------- .../LayoutManagers/HomeLayoutGenerator.java | 11 +- .../fragment_marketcap_homeactivity.xml | 11 ++ .../layout/fragment_summary_homeactivity.xml | 9 -- .../fragment_watchlist_homeactivity.xml | 16 ++- app/src/main/res/values/colors.xml | 2 +- 8 files changed, 128 insertions(+), 144 deletions(-) diff --git a/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivity.java b/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivity.java index a4ceee6..c19a544 100644 --- a/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivity.java +++ b/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivity.java @@ -86,14 +86,12 @@ public class HomeActivity extends AppCompatActivity { switch (item.getItemId()) { case R.id.navigation_watchlist: - ((AppBarLayout) findViewById(R.id.app_bar)).setExpanded(false, true); viewPager.setCurrentItem(0); break; case R.id.navigation_currencies_list: viewPager.setCurrentItem(1); break; case R.id.navigation_market_cap: - ((AppBarLayout) findViewById(R.id.app_bar)).setExpanded(false, true); viewPager.setCurrentItem(2); break; } diff --git a/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivityFragments/Summary.java b/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivityFragments/Summary.java index 71a91db..654fc49 100644 --- a/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivityFragments/Summary.java +++ b/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivityFragments/Summary.java @@ -37,6 +37,7 @@ import com.nauk.coinfolio.R; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; +import java.util.ArrayList; import java.util.List; /** @@ -137,18 +138,6 @@ public class Summary extends Fragment { } }); - ImageButton detailsButton = view.findViewById(R.id.switch_button); - detailsButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - preferencesManager.setDetailOption(!preferencesManager.getDetailOption()); - updateViewButtonIcon(); - switchView(); - } - }); - - //updateTitle(); - updateAll(true); generateSplashScreen(); @@ -189,8 +178,6 @@ public class Summary extends Fragment { updateAll(preferencesManager.mustUpdateSummary()); - updateViewButtonIcon(); - displayBalance(preferencesManager.isBalanceHidden()); } @@ -243,31 +230,38 @@ public class Summary extends Fragment { totalFluctuation = 0; } - private void switchView() - { - if(preferencesManager.getDetailOption()) - { - adaptView(); - } - else - { - adaptView(); - } - } - private void adaptView() { currencyLayout.removeAllViews(); - for(int i = 0; i < balanceManager.getTotalBalance().size(); i++) - { - final Currency currency = balanceManager.getTotalBalance().get(i); + final List currencyView = new ArrayList<>(); - if(!currency.getSymbol().equals("USD") && ((currency.getBalance() * currency.getValue()) > 0.001)) - { - currencyLayout.addView(layoutGenerator.getInfoLayout(currency, preferencesManager.getDetailOption(), totalValue, preferencesManager.isBalanceHidden())); + Runnable newRunnabmle = new Runnable() { + @Override + public void run() { + for(int i = 0; i < balanceManager.getTotalBalance().size(); i++) + { + final Currency currency = balanceManager.getTotalBalance().get(i); + + if(!currency.getSymbol().equals("USD") && ((currency.getBalance() * currency.getValue()) > 0.001)) + { + currencyView.add(layoutGenerator.getInfoLayout(currency, totalValue, preferencesManager.isBalanceHidden())); + } + } + + getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + for(int i = 0; i < currencyView.size(); i++) + { + currencyLayout.addView(currencyView.get(i)); + } + } + }); } - } + }; + + newRunnabmle.run(); } private void countCoins(boolean isCoin, boolean isDetails) @@ -357,6 +351,8 @@ public class Summary extends Fragment { { float totalFluctuationPercentage = totalFluctuation / (totalValue - totalFluctuation) * 100; + + if(preferencesManager.isBalanceHidden()) { toolbarLayout.setTitle(getResources().getString(R.string.currencyPercentagePlaceholder, String.format("%.2f", totalFluctuationPercentage))); @@ -446,27 +442,6 @@ public class Summary extends Fragment { } } - private void refreshCurrencyList() - { - getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - currencyLayout.removeAllViews(); - - for(int i = 0; i < balanceManager.getTotalBalance().size(); i++) - { - Currency currency = balanceManager.getTotalBalance().get(i); - - if(!currency.getSymbol().equals("USD") && (currency.getBalance() * currency.getValue()) > 0.001) { - currencyLayout.addView(layoutGenerator.getInfoLayout(currency, preferencesManager.getDetailOption(), totalValue, preferencesManager.isBalanceHidden())); - } - } - - adaptView(); - } - }); - } - @Override protected Void doInBackground(Void... params) { @@ -534,22 +509,6 @@ public class Summary extends Fragment { callBack.onSuccess(result); } - private void updateViewButtonIcon() - { - ImageButton imgButton = getActivity().findViewById(R.id.switch_button); - - imgButton.setBackgroundColor(this.getResources().getColor(R.color.buttonColor)); - - if(preferencesManager.getDetailOption()) - { - imgButton.setBackground(this.getResources().getDrawable(R.drawable.ic_unfold_less_black_24dp)); - } - else - { - imgButton.setBackground(this.getResources().getDrawable(R.drawable.ic_details_black_24dp)); - } - } - private void displayBalance(boolean hideBalance) { updateTitle(); diff --git a/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivityFragments/Watchlist.java b/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivityFragments/Watchlist.java index dff61bb..0a73f8a 100644 --- a/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivityFragments/Watchlist.java +++ b/app/src/main/java/com/nauk/coinfolio/Activities/HomeActivityFragments/Watchlist.java @@ -27,6 +27,7 @@ import android.widget.ImageButton; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; +import android.support.v7.widget.Toolbar; import com.github.mikephil.charting.charts.LineChart; import com.github.mikephil.charting.data.Entry; @@ -69,6 +70,7 @@ public class Watchlist extends Fragment { private SwipeRefreshLayout refreshLayout; private long lastTimestamp; private PreferencesManager preferencesManager; + private Toolbar toolbar; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) @@ -91,6 +93,7 @@ public class Watchlist extends Fragment { updateWatchlist(false); } }); + toolbar = view.findViewById(R.id.toolbar); Button addWatchlistButton = view.findViewById(R.id.buttonAddWatchlist); addWatchlistButton.setOnClickListener(new View.OnClickListener() { @@ -225,69 +228,88 @@ public class Watchlist extends Fragment { if(watchlistCounter >= watchlistManager.getWatchlist().size()) { + final List watchlistViews = new ArrayList(); + ((LinearLayout) view.findViewById(R.id.linearLayoutWatchlist)).removeAllViews(); - for(final Currency currency : watchlistManager.getWatchlist()) - { - View card = LayoutInflater.from(getContext()).inflate(R.layout.cardview_watchlist, null); + Runnable newRunnable = new Runnable() { + @Override + public void run() { + for(final Currency currency : watchlistManager.getWatchlist()) + { + View card = LayoutInflater.from(getContext()).inflate(R.layout.cardview_watchlist, null); - ((TextView) card.findViewById(R.id.currencyFluctuationPercentageTextView)).setText(getResources().getString(R.string.currencyPercentagePlaceholder, numberConformer(currency.getDayFluctuationPercentage()))); - ((TextView) card.findViewById(R.id.currencyFluctuationTextView)).setText(getResources().getString(R.string.currencyDollarParenthesisPlaceholder, numberConformer(currency.getDayFluctuation()))); - ((TextView) card.findViewById(R.id.currencyNameTextView)).setText(currency.getName()); - ((TextView) card.findViewById(R.id.currencySymbolTextView)).setText(getResources().getString(R.string.currencySymbolPlaceholder, currency.getSymbol())); - ((ImageView) card.findViewById(R.id.currencyIcon)).setImageBitmap(currency.getIcon()); - ((TextView) card.findViewById(R.id.currencyValueTextView)).setText(getResources().getString(R.string.currencyDollarPlaceholder, numberConformer(currency.getValue()))); + ((TextView) card.findViewById(R.id.currencyFluctuationPercentageTextView)).setText(getResources().getString(R.string.currencyPercentagePlaceholder, numberConformer(currency.getDayFluctuationPercentage()))); + ((TextView) card.findViewById(R.id.currencyFluctuationTextView)).setText(getResources().getString(R.string.currencyDollarParenthesisPlaceholder, numberConformer(currency.getDayFluctuation()))); + ((TextView) card.findViewById(R.id.currencyNameTextView)).setText(currency.getName()); + ((TextView) card.findViewById(R.id.currencySymbolTextView)).setText(getResources().getString(R.string.currencySymbolPlaceholder, currency.getSymbol())); + ((ImageView) card.findViewById(R.id.currencyIcon)).setImageBitmap(currency.getIcon()); + ((TextView) card.findViewById(R.id.currencyValueTextView)).setText(getResources().getString(R.string.currencyDollarPlaceholder, numberConformer(currency.getValue()))); - Drawable arrowDrawable = ((ImageView) card.findViewById(R.id.detailsArrow)).getDrawable(); - arrowDrawable.mutate(); - arrowDrawable.setColorFilter(new PorterDuffColorFilter(currency.getChartColor(), PorterDuff.Mode.SRC_IN)); - arrowDrawable.invalidateSelf(); + Drawable arrowDrawable = ((ImageView) card.findViewById(R.id.detailsArrow)).getDrawable(); + arrowDrawable.mutate(); + arrowDrawable.setColorFilter(new PorterDuffColorFilter(currency.getChartColor(), PorterDuff.Mode.SRC_IN)); + arrowDrawable.invalidateSelf(); - updateColor(card, currency); + updateColor(card, currency); - card.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); - card.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(final View view) { - if(view.findViewById(R.id.collapsableLayout).getVisibility() == View.VISIBLE) - { - collapseView(view); - } - else - { - if (currency.getHistoryMinutes() == null) { - currency.updateHistoryMinutes(getActivity(), new Currency.CurrencyCallBack() { - @Override - public void onSuccess(Currency currency) { - extendView(view); - setupLineChart(view, currency); + card.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); + card.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(final View view) { + if(view.findViewById(R.id.collapsableLayout).getVisibility() == View.VISIBLE) + { + collapseView(view); + } + else + { + if (currency.getHistoryMinutes() == null) { + currency.updateHistoryMinutes(getActivity(), new Currency.CurrencyCallBack() { + @Override + public void onSuccess(Currency currency) { + extendView(view); + setupLineChart(view, currency); + } + }); } - }); + else + { + extendView(view); + } + } } - else + }); + + card.findViewById(R.id.LineChartView).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + Intent intent = new Intent(getActivity(), CurrencyDetailsActivity.class); + intent.putExtra("currency", currency); + getActivity().getApplicationContext().startActivity(intent); + } + }); + + if(currency.getHistoryMinutes() != null) + { + setupLineChart(card, currency); + } + + watchlistViews.add(card); + } + + getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + for(int i = 0; i < watchlistViews.size(); i++) { - extendView(view); + ((LinearLayout) view.findViewById(R.id.linearLayoutWatchlist)).addView(watchlistViews.get(i), 0); } } - } - }); - - card.findViewById(R.id.LineChartView).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - Intent intent = new Intent(getActivity(), CurrencyDetailsActivity.class); - intent.putExtra("currency", currency); - getActivity().getApplicationContext().startActivity(intent); - } - }); - - if(currency.getHistoryMinutes() != null) - { - setupLineChart(card, currency); + }); } + }; - ((LinearLayout) view.findViewById(R.id.linearLayoutWatchlist)).addView(card, 0); - } + newRunnable.run(); if(refreshLayout.isRefreshing()) { diff --git a/app/src/main/java/com/nauk/coinfolio/LayoutManagers/HomeLayoutGenerator.java b/app/src/main/java/com/nauk/coinfolio/LayoutManagers/HomeLayoutGenerator.java index 260b488..0f28802 100644 --- a/app/src/main/java/com/nauk/coinfolio/LayoutManagers/HomeLayoutGenerator.java +++ b/app/src/main/java/com/nauk/coinfolio/LayoutManagers/HomeLayoutGenerator.java @@ -46,7 +46,7 @@ public class HomeLayoutGenerator { this.context = context; } - public View getInfoLayout(final Currency currency, boolean isExtended, float totalValue, boolean isBalanceHidden) + public View getInfoLayout(final Currency currency, float totalValue, boolean isBalanceHidden) { View view = LayoutInflater.from(context).inflate(R.layout.cardview_currency, null, true); @@ -85,15 +85,6 @@ public class HomeLayoutGenerator { } }); - if(isExtended) - { - extendView(view); - } - else - { - collapseView(view); - } - updateColor(view, currency); return view; diff --git a/app/src/main/res/layout/fragment_marketcap_homeactivity.xml b/app/src/main/res/layout/fragment_marketcap_homeactivity.xml index b94da03..17fb33f 100644 --- a/app/src/main/res/layout/fragment_marketcap_homeactivity.xml +++ b/app/src/main/res/layout/fragment_marketcap_homeactivity.xml @@ -14,6 +14,7 @@ android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" + app:title="Test" app:layout_collapseMode="pin"> + + - - + + + android:layout_height="match_parent" + android:overScrollMode="never"> + android:layout_height="wrap_content" + android:overScrollMode="never"> #FF4CAF50 #FFFFFFFF #FFFFFFFF - #FBFCFF + #F5F7FA #FFFF5754 #FF45B64A \ No newline at end of file