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
This commit is contained in:
Tanguy Herbron 2018-04-18 01:37:54 +02:00
parent eab8ff14bf
commit 78adcd5485
8 changed files with 128 additions and 144 deletions

View File

@ -86,14 +86,12 @@ public class HomeActivity extends AppCompatActivity {
switch (item.getItemId()) switch (item.getItemId())
{ {
case R.id.navigation_watchlist: case R.id.navigation_watchlist:
((AppBarLayout) findViewById(R.id.app_bar)).setExpanded(false, true);
viewPager.setCurrentItem(0); viewPager.setCurrentItem(0);
break; break;
case R.id.navigation_currencies_list: case R.id.navigation_currencies_list:
viewPager.setCurrentItem(1); viewPager.setCurrentItem(1);
break; break;
case R.id.navigation_market_cap: case R.id.navigation_market_cap:
((AppBarLayout) findViewById(R.id.app_bar)).setExpanded(false, true);
viewPager.setCurrentItem(2); viewPager.setCurrentItem(2);
break; break;
} }

View File

@ -37,6 +37,7 @@ import com.nauk.coinfolio.R;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List; 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); updateAll(true);
generateSplashScreen(); generateSplashScreen();
@ -189,8 +178,6 @@ public class Summary extends Fragment {
updateAll(preferencesManager.mustUpdateSummary()); updateAll(preferencesManager.mustUpdateSummary());
updateViewButtonIcon();
displayBalance(preferencesManager.isBalanceHidden()); displayBalance(preferencesManager.isBalanceHidden());
} }
@ -243,31 +230,38 @@ public class Summary extends Fragment {
totalFluctuation = 0; totalFluctuation = 0;
} }
private void switchView()
{
if(preferencesManager.getDetailOption())
{
adaptView();
}
else
{
adaptView();
}
}
private void adaptView() private void adaptView()
{ {
currencyLayout.removeAllViews(); currencyLayout.removeAllViews();
for(int i = 0; i < balanceManager.getTotalBalance().size(); i++) final List<View> currencyView = new ArrayList<>();
{
final Currency currency = balanceManager.getTotalBalance().get(i);
if(!currency.getSymbol().equals("USD") && ((currency.getBalance() * currency.getValue()) > 0.001)) Runnable newRunnabmle = new Runnable() {
{ @Override
currencyLayout.addView(layoutGenerator.getInfoLayout(currency, preferencesManager.getDetailOption(), totalValue, preferencesManager.isBalanceHidden())); 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) private void countCoins(boolean isCoin, boolean isDetails)
@ -357,6 +351,8 @@ public class Summary extends Fragment {
{ {
float totalFluctuationPercentage = totalFluctuation / (totalValue - totalFluctuation) * 100; float totalFluctuationPercentage = totalFluctuation / (totalValue - totalFluctuation) * 100;
if(preferencesManager.isBalanceHidden()) if(preferencesManager.isBalanceHidden())
{ {
toolbarLayout.setTitle(getResources().getString(R.string.currencyPercentagePlaceholder, String.format("%.2f", totalFluctuationPercentage))); 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 @Override
protected Void doInBackground(Void... params) protected Void doInBackground(Void... params)
{ {
@ -534,22 +509,6 @@ public class Summary extends Fragment {
callBack.onSuccess(result); 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) private void displayBalance(boolean hideBalance)
{ {
updateTitle(); updateTitle();

View File

@ -27,6 +27,7 @@ import android.widget.ImageButton;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import android.support.v7.widget.Toolbar;
import com.github.mikephil.charting.charts.LineChart; import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.data.Entry;
@ -69,6 +70,7 @@ public class Watchlist extends Fragment {
private SwipeRefreshLayout refreshLayout; private SwipeRefreshLayout refreshLayout;
private long lastTimestamp; private long lastTimestamp;
private PreferencesManager preferencesManager; private PreferencesManager preferencesManager;
private Toolbar toolbar;
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
@ -91,6 +93,7 @@ public class Watchlist extends Fragment {
updateWatchlist(false); updateWatchlist(false);
} }
}); });
toolbar = view.findViewById(R.id.toolbar);
Button addWatchlistButton = view.findViewById(R.id.buttonAddWatchlist); Button addWatchlistButton = view.findViewById(R.id.buttonAddWatchlist);
addWatchlistButton.setOnClickListener(new View.OnClickListener() { addWatchlistButton.setOnClickListener(new View.OnClickListener() {
@ -225,69 +228,88 @@ public class Watchlist extends Fragment {
if(watchlistCounter >= watchlistManager.getWatchlist().size()) if(watchlistCounter >= watchlistManager.getWatchlist().size())
{ {
final List<View> watchlistViews = new ArrayList<View>();
((LinearLayout) view.findViewById(R.id.linearLayoutWatchlist)).removeAllViews(); ((LinearLayout) view.findViewById(R.id.linearLayoutWatchlist)).removeAllViews();
for(final Currency currency : watchlistManager.getWatchlist()) Runnable newRunnable = new Runnable() {
{ @Override
View card = LayoutInflater.from(getContext()).inflate(R.layout.cardview_watchlist, null); 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.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.currencyFluctuationTextView)).setText(getResources().getString(R.string.currencyDollarParenthesisPlaceholder, numberConformer(currency.getDayFluctuation())));
((TextView) card.findViewById(R.id.currencyNameTextView)).setText(currency.getName()); ((TextView) card.findViewById(R.id.currencyNameTextView)).setText(currency.getName());
((TextView) card.findViewById(R.id.currencySymbolTextView)).setText(getResources().getString(R.string.currencySymbolPlaceholder, currency.getSymbol())); ((TextView) card.findViewById(R.id.currencySymbolTextView)).setText(getResources().getString(R.string.currencySymbolPlaceholder, currency.getSymbol()));
((ImageView) card.findViewById(R.id.currencyIcon)).setImageBitmap(currency.getIcon()); ((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.currencyValueTextView)).setText(getResources().getString(R.string.currencyDollarPlaceholder, numberConformer(currency.getValue())));
Drawable arrowDrawable = ((ImageView) card.findViewById(R.id.detailsArrow)).getDrawable(); Drawable arrowDrawable = ((ImageView) card.findViewById(R.id.detailsArrow)).getDrawable();
arrowDrawable.mutate(); arrowDrawable.mutate();
arrowDrawable.setColorFilter(new PorterDuffColorFilter(currency.getChartColor(), PorterDuff.Mode.SRC_IN)); arrowDrawable.setColorFilter(new PorterDuffColorFilter(currency.getChartColor(), PorterDuff.Mode.SRC_IN));
arrowDrawable.invalidateSelf(); arrowDrawable.invalidateSelf();
updateColor(card, currency); updateColor(card, currency);
card.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); card.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
card.setOnClickListener(new View.OnClickListener() { card.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(final View view) { public void onClick(final View view) {
if(view.findViewById(R.id.collapsableLayout).getVisibility() == View.VISIBLE) if(view.findViewById(R.id.collapsableLayout).getVisibility() == View.VISIBLE)
{ {
collapseView(view); collapseView(view);
} }
else else
{ {
if (currency.getHistoryMinutes() == null) { if (currency.getHistoryMinutes() == null) {
currency.updateHistoryMinutes(getActivity(), new Currency.CurrencyCallBack() { currency.updateHistoryMinutes(getActivity(), new Currency.CurrencyCallBack() {
@Override @Override
public void onSuccess(Currency currency) { public void onSuccess(Currency currency) {
extendView(view); extendView(view);
setupLineChart(view, currency); 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()) if(refreshLayout.isRefreshing())
{ {

View File

@ -46,7 +46,7 @@ public class HomeLayoutGenerator {
this.context = context; 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); 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); updateColor(view, currency);
return view; return view;

View File

@ -14,6 +14,7 @@
android:id="@+id/toolbar" android:id="@+id/toolbar"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" android:layout_height="?attr/actionBarSize"
app:title="Test"
app:layout_collapseMode="pin"> app:layout_collapseMode="pin">
<FrameLayout <FrameLayout
@ -22,6 +23,16 @@
app:layout_collapseMode="pin" app:layout_collapseMode="pin"
android:layout_margin="10dp"> android:layout_margin="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:text="Market Capitalization"
android:textSize="18sp"
android:textColor="@color/white"
android:textStyle="bold"/>
<ImageButton <ImageButton
android:id="@+id/settings_button" android:id="@+id/settings_button"
android:layout_width="wrap_content" android:layout_width="wrap_content"

View File

@ -54,15 +54,6 @@
app:layout_collapseMode="pin" app:layout_collapseMode="pin"
android:layout_margin="10dp"> android:layout_margin="10dp">
<ImageButton
android:id="@+id/switch_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/quick_button"
android:visibility="visible"
android:layout_gravity="start"
android:contentDescription="Switch view"/>
<ImageButton <ImageButton
android:id="@+id/settings_button" android:id="@+id/settings_button"
android:layout_width="wrap_content" android:layout_width="wrap_content"

View File

@ -22,6 +22,16 @@
app:layout_collapseMode="pin" app:layout_collapseMode="pin"
android:layout_margin="10dp"> android:layout_margin="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:text="Watchlist"
android:textSize="18sp"
android:textColor="@color/white"
android:textStyle="bold"/>
<ImageButton <ImageButton
android:id="@+id/settings_button" android:id="@+id/settings_button"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -48,7 +58,8 @@
<android.support.v4.widget.NestedScrollView <android.support.v4.widget.NestedScrollView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent"
android:overScrollMode="never">
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
@ -58,7 +69,8 @@
<ScrollView <ScrollView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content"
android:overScrollMode="never">
<LinearLayout android:id="@+id/linearLayoutWatchlist" <LinearLayout android:id="@+id/linearLayoutWatchlist"
android:layout_width="match_parent" android:layout_width="match_parent"

View File

@ -35,7 +35,7 @@
<color name="green">#FF4CAF50</color> <color name="green">#FF4CAF50</color>
<color name="white">#FFFFFFFF</color> <color name="white">#FFFFFFFF</color>
<color name="cardview_background">#FFFFFFFF</color> <color name="cardview_background">#FFFFFFFF</color>
<color name="summary_background">#FBFCFF</color> <color name="summary_background">#F5F7FA</color>
<color name="decreaseCandle">#FFFF5754</color> <color name="decreaseCandle">#FFFF5754</color>
<color name="increaseCandle">#FF45B64A</color> <color name="increaseCandle">#FF45B64A</color>
</resources> </resources>