Fix overview UI freeze | Coin list now looks like other lists

This commit is contained in:
Tanguy Herbron 2018-05-28 17:13:24 +02:00
parent aeb9c9cd18
commit 8a347ceef9
6 changed files with 370 additions and 78 deletions

View File

@ -2,18 +2,26 @@ package com.nauk.moodl.Activities.HomeActivityFragments;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.graphics.Palette;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ExpandableListView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import com.nauk.moodl.Activities.HomeActivity;
import com.nauk.moodl.DataManagers.CurrencyData.Currency;
@ -38,6 +46,8 @@ public class Overview extends Fragment {
private PreferencesManager preferenceManager;
private OverviewListAdapter overviewListAdapter;
private View loadingFooter;
boolean flag_loading;
private ListView listLayout;
@ -75,66 +85,39 @@ public class Overview extends Fragment {
}
});
flag_loading = true;
updateList();
setupDrawerButton(fragmentView);
return fragmentView;
}
private void setupDrawerButton(View view)
{
ImageButton drawerButton = view.findViewById(R.id.drawer_button);
drawerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DrawerLayout drawerLayout = getActivity().findViewById(R.id.drawer_layout);
if(drawerLayout.isDrawerOpen(GravityCompat.START))
{
drawerLayout.closeDrawers();
}
else
{
drawerLayout.openDrawer(GravityCompat.START);
}
}
});
}
private void updateList()
{
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
currencyTickerList.getCurrenciesFrom(listLayout.getCount(), preferenceManager.getDefaultCurrency(), new UpdateCallBack() {
@Override
public void onSuccess(List<Currency> currencyList)
{
for(Currency currency : currencyList)
{
String iconUrl = MoodlBox.getIconUrl(currency.getSymbol(), currencyDetailsList);
if(iconUrl != null)
{
MoodlBox.getBitmapFromURL(iconUrl, currency.getSymbol(), getResources(), getContext(), new HomeActivity.IconCallBack() {
@Override
public void onSuccess(Bitmap bitmap) {
currency.setIcon(bitmap);
updateChartColor(currency);
}
});
}
else
{
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_moodl);
icon = Bitmap.createScaledBitmap(icon, 50, 50, false);
currency.setIcon(icon);
updateChartColor(currency);
}
}
if(overviewListAdapter == null)
{
overviewListAdapter = new OverviewListAdapter(getContext(), currencyList);
listLayout.setAdapter(overviewListAdapter);
listLayout.setTextFilterEnabled(false);
}
else
{
overviewListAdapter.addAll(currencyList);
overviewListAdapter.notifyDataSetChanged();
}
flag_loading = false;
}
});
return null;
}
}.execute();
CurrencyLoader currencyLoader = new CurrencyLoader();
currencyLoader.execute();
}
private void updateChartColor(Currency currency)
@ -155,4 +138,101 @@ public class Overview extends Fragment {
{
void onSuccess(List<Currency> currencyList);
}
private void loadingIndicatorGenerator()
{
loadingFooter = LayoutInflater.from(getContext()).inflate(R.layout.listview_loading_indicator, null, false);
listLayout.addFooterView(loadingFooter);
}
private class CurrencyLoader extends AsyncTask<Void, Void, Void>
{
@Override
protected void onPreExecute() {
super.onPreExecute();
loadingIndicatorGenerator();
}
@Override
protected Void doInBackground(Void... voids) {
currencyTickerList.getCurrenciesFrom(listLayout.getCount(), preferenceManager.getDefaultCurrency(), new UpdateCallBack() {
@Override
public void onSuccess(List<Currency> currencyList)
{
IconDownloader iconDownloader = new IconDownloader();
iconDownloader.execute(currencyList);
}
});
return null;
}
}
private class IconDownloader extends AsyncTask<List<Currency>, Void, Void>
{
private int iconCounter;
@Override
protected Void doInBackground(List<Currency>... currencies) {
for(Currency currency : currencies[0])
{
String iconUrl = MoodlBox.getIconUrl(currency.getSymbol(), currencyDetailsList);
if(iconUrl != null)
{
MoodlBox.getBitmapFromURL(iconUrl, currency.getSymbol(), getResources(), getContext(), new HomeActivity.IconCallBack() {
@Override
public void onSuccess(Bitmap bitmap) {
currency.setIcon(bitmap);
updateChartColor(currency);
countIcons(currencies[0]);
}
});
}
else
{
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_moodl);
icon = Bitmap.createScaledBitmap(icon, 50, 50, false);
currency.setIcon(icon);
updateChartColor(currency);
countIcons(currencies[0]);
}
}
return null;
}
private void countIcons(List<Currency> currencyList)
{
iconCounter++;
if(iconCounter == currencyList.size())
{
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if(overviewListAdapter == null)
{
overviewListAdapter = new OverviewListAdapter(getContext(), currencyList, getActivity());
listLayout.setAdapter(overviewListAdapter);
listLayout.setTextFilterEnabled(false);
}
else
{
overviewListAdapter.addAll(currencyList);
overviewListAdapter.notifyDataSetChanged();
}
listLayout.removeFooterView(loadingFooter);
flag_loading = false;
}
});
}
}
}
}

View File

@ -276,11 +276,11 @@ public class Watchlist extends Fragment {
private void generateCards()
{
((LinearLayout) view.findViewById(R.id.linearLayoutWatchlist)).removeAllViews();
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
((LinearLayout) view.findViewById(R.id.linearLayoutWatchlist)).removeAllViews();
for(Currency currency : watchlistManager.getWatchlist())
{
((LinearLayout) view.findViewById(R.id.linearLayoutWatchlist)).addView(new CurrencyCardview(getContext(), currency, getActivity()));

View File

@ -1,16 +1,22 @@
package com.nauk.moodl.LayoutManagers;
import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.nauk.moodl.DataManagers.CurrencyData.Currency;
import com.nauk.moodl.DataManagers.CurrencyData.CurrencyCardview;
import com.nauk.moodl.DataManagers.CurrencyData.CurrencyDetailsList;
import com.nauk.moodl.PlaceholderManager;
import com.nauk.moodl.R;
@ -24,12 +30,15 @@ import static com.nauk.moodl.MoodlBox.numberConformer;
public class OverviewListAdapter extends ArrayAdapter<Currency> {
private Context context;
private Activity activity;
private CurrencyDetailsList currencyDetailsList;
public OverviewListAdapter(Context context, List<Currency> currencies)
public OverviewListAdapter(Context context, List<Currency> currencies, Activity activity)
{
super(context, android.R.layout.simple_list_item_1, currencies);
this.context = context;
super(context, android.R.layout.simple_expandable_list_item_1, currencies);
this.activity = activity;
currencyDetailsList = CurrencyDetailsList.getInstance(getContext());
}
@NonNull
@ -38,24 +47,12 @@ public class OverviewListAdapter extends ArrayAdapter<Currency> {
{
Currency currency = getItem(position);
if(convertView == null)
{
convertView = LayoutInflater.from(getContext()).inflate(R.layout.cardview_watchlist, parent, false);
}
currencyDetailsList.getCurrencyDetailsFromSymbol(currency.getSymbol());
TextView symbolTxtView = convertView.findViewById(R.id.currencySymbolTextView);
TextView nameTxtView = convertView.findViewById(R.id.currencyNameTextView);
TextView valueTxtView = convertView.findViewById(R.id.currencyValueTextView);
TextView fluctuationTxtView = convertView.findViewById(R.id.currencyFluctuationTextView);
TextView percentageTxtView = convertView.findViewById(R.id.currencyFluctuationPercentageTextView);
ImageView iconImageView = convertView.findViewById(R.id.currencyIcon);
symbolTxtView.setText(currency.getSymbol());
nameTxtView.setText(currency.getName());
valueTxtView.setText(PlaceholderManager.getValueString(numberConformer(currency.getValue()), getContext()));
fluctuationTxtView.setText(PlaceholderManager.getValueParenthesisString(numberConformer(currency.getDayFluctuation()), getContext()));
percentageTxtView.setText(PlaceholderManager.getPercentageString(numberConformer(currency.getDayFluctuationPercentage()), getContext()));
iconImageView.setImageBitmap(currency.getIcon());
CurrencyCardview currencyCardview = new CurrencyCardview(getContext(), currency, activity);
LinearLayout linearLayout = new LinearLayout(getContext());
linearLayout.addView(currencyCardview);
convertView = linearLayout;
return convertView;
}

View File

@ -0,0 +1,213 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:clickable="false"
android:focusable="false">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:background="@color/white"
app:cardCornerRadius="2dp">
<LinearLayout
android:id="@+id/mainLinear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:orientation="vertical"
android:tag="mainLinear">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:baselineAligned="false">
<LinearLayout
android:id="@+id/currencyInfoLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical"
android:layout_weight="0.92">
<LinearLayout
android:id="@+id/topLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<ImageView
android:id="@+id/currencyIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp" />
<TextView
android:id="@+id/currencyNameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="start"
android:textColor="@color/mainTextViewColor"
android:textSize="@dimen/cardViewMainText" />
<TextView
android:id="@+id/currencySymbolTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:gravity="left"
android:textColor="@color/secondaryTextViewColor"
android:textSize="@dimen/cardViewSecondaryText" />
</LinearLayout>
<LinearLayout
android:id="@+id/bottomLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/currencyValueTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="start"
android:textColor="@color/secondaryTextViewColor"
android:textSize="@dimen/cardViewMainText"
android:layout_weight="0.5"/>
<LinearLayout
android:id="@+id/secondaryLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:orientation="horizontal"
android:layout_weight="0.5">
<TextView
android:id="@+id/currencyFluctuationPercentageTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/cardViewMainText" />
<TextView
android:id="@+id/currencyFluctuationTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/cardViewSecondaryText" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/deleteCardWatchlist"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@color/decreaseCandle"
android:gravity="center"
android:layout_gravity="end"
android:visibility="gone"
android:layout_weight="0.08"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_delete_white_24dp"
android:clickable="false"
android:focusable="false"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/collapsableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:orientation="vertical">
<LinearLayout
android:id="@+id/separationLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day history"
android:textSize="@dimen/cardViewSecondaryText" />
<View
android:layout_width="match_parent"
android:layout_height="@dimen/separationLineSize"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/separationColor" />
</LinearLayout>
<FrameLayout
android:id="@+id/frameLayoutChart"
android:layout_width="match_parent"
android:layout_height="@dimen/cardViewChartSize"
android:clickable="true"
android:focusable="true"
android:visibility="visible">
<ProgressBar
android:id="@+id/progressBarLinechartWatchlist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:clickable="false"
android:focusable="false"
android:visibility="visible"/>
<FrameLayout
android:id="@+id/linearLayoutSubLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
android:visibility="gone">
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/LineChartView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ImageView
android:id="@+id/detailsArrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_keyboard_arrow_right_grey_48dp"
android:layout_gravity="center_vertical|end"/>
</FrameLayout>
</FrameLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>

View File

@ -61,7 +61,9 @@
android:layout_height="wrap_content"
android:layout_marginTop="?attr/actionBarSize"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"/>
xmlns:android="http://schemas.android.com/apk/res/android"
android:divider="@null"
android:dividerHeight="0dp"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>

View File

@ -14,6 +14,6 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Looking for more trades..."/>
android:text="Hodl..."/>
</LinearLayout>