[DetailsActivity] Merge Transactions and Trades in the same list

This commit is contained in:
Tanguy Herbron 2018-08-21 00:19:29 +02:00
parent e80c9f4046
commit ac7fb545ec
6 changed files with 243 additions and 92 deletions

View File

@ -28,6 +28,8 @@ import com.herbron.moodl.CustomAdapters.TransactionListAdapter;
import com.herbron.moodl.R; import com.herbron.moodl.R;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List; import java.util.List;
/** /**
@ -39,7 +41,7 @@ public class TransactionsFragment extends Fragment {
private Currency currency; private Currency currency;
private View loadingFooter; private View loadingFooter;
private View view; private View view;
private ListView tradeLayout; //private ListView tradeLayout;
private ListView transactionLayout; private ListView transactionLayout;
private boolean flag_loading; private boolean flag_loading;
private List<BinanceManager> binanceManagerList; private List<BinanceManager> binanceManagerList;
@ -57,7 +59,7 @@ public class TransactionsFragment extends Fragment {
currency = getActivity().getIntent().getParcelableExtra("currency"); currency = getActivity().getIntent().getParcelableExtra("currency");
databaseManager = new DatabaseManager(getActivity().getBaseContext()); databaseManager = new DatabaseManager(getActivity().getBaseContext());
binanceManagerList = databaseManager.getBinanceAccounts(); binanceManagerList = databaseManager.getBinanceAccounts();
tradeLayout = view.findViewById(R.id.listTrades); //tradeLayout = view.findViewById(R.id.listTrades);
transactionLayout = view.findViewById(R.id.listTransactions); transactionLayout = view.findViewById(R.id.listTransactions);
flag_loading = false; flag_loading = false;
@ -65,59 +67,137 @@ public class TransactionsFragment extends Fragment {
TransactionUpdater transactionUpdater = new TransactionUpdater(); TransactionUpdater transactionUpdater = new TransactionUpdater();
transactionUpdater.execute(); transactionUpdater.execute();
TradeUpdater updater = new TradeUpdater();
updater.execute();
return view; return view;
} }
private void loadingIndicatorGenerator() private class TransactionUpdater extends AsyncTask<Void, Integer, Void> implements BinanceUpdateNotifierInterface
{ {
loadingFooter = LayoutInflater.from(getActivity().getBaseContext()).inflate(R.layout.listview_loading_indicator, null, false); private ArrayList<Object> displayedTransactions;
private int nbTradeAccountUpdated;
private int nbTransactionAccountUpdated;
Drawable drawable = ((ProgressBar) loadingFooter.findViewById(R.id.progressIndicator)).getIndeterminateDrawable(); @Override
drawable.mutate(); protected void onPreExecute() {
drawable.setColorFilter(new PorterDuffColorFilter(currency.getChartColor(), PorterDuff.Mode.SRC_IN)); super.onPreExecute();
drawable.invalidateSelf();
tradeLayout.addFooterView(loadingFooter); displayedTransactions = new ArrayList<>();
nbTradeAccountUpdated = 0;
nbTransactionAccountUpdated = 0;
} }
private void drawTradeList(ArrayList<com.herbron.moodl.DataManagers.CurrencyData.Trade> trades)
{
if(returnedTrades.size() > 20)
{
tradeLayout.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override @Override
public void onScrollStateChanged(AbsListView absListView, int i) { protected Void doInBackground(Void... voids) {
if(Looper.myLooper() == null)
{
Looper.prepare();
}
displayedTransactions.addAll(databaseManager.getCurrencyTransactionsForSymbol(currency.getSymbol()));
for(int i = 0; i < binanceManagerList.size(); i++)
{
binanceManagerList.get(i).addListener(this);
binanceManagerList.get(i).updateTrades(currency.getSymbol());
binanceManagerList.get(i).updateTransactions(currency.getSymbol());
}
if(nbTradeAccountUpdated == binanceManagerList.size())
{
drawTransactionList(displayedTransactions);
}
return null;
}
@Override
public void onBinanceTradesUpdated(List<Trade> trades) {
nbTradeAccountUpdated++;
displayedTransactions.addAll(trades);
if(nbTradeAccountUpdated == binanceManagerList.size())
{
drawTransactionList(displayedTransactions);
}
}
@Override
public void onBinanceBalanceUpdateSuccess() {
} }
@Override @Override
public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) { public void onBinanceBalanceUpdateError(int accountId, String error) {
if(firstVisibleItem+visibleItemCount == totalItemCount && totalItemCount!=0)
{
if(!flag_loading && tradeLayout.getCount() != returnedTrades.size() - 1)
{
flag_loading = true;
TradeAdder tradeAdder = new TradeAdder();
tradeAdder.execute();
} }
private void drawTransactionList(ArrayList<Object> transactions)
{
Collections.sort(transactions, new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
Long time1;
Long time2;
if(o1 instanceof Transaction)
{
time1 = ((Transaction) o1).getTimestamp();
} }
else
{
time1 = ((Trade) o1).getTime();
}
if(o2 instanceof Transaction)
{
time2 = ((Transaction) o2).getTimestamp();
}
else
{
time2 = ((Trade) o2).getTime();
}
return time2.compareTo(time1);
}
});
TransactionListAdapter transactionListAdapter = new TransactionListAdapter(getActivity(), transactions);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
transactionLayout.setAdapter(transactionListAdapter);
transactionLayout.setTextFilterEnabled(false);
} }
}); });
} }
tradeListAdapter = new TradeListAdapter(getActivity().getBaseContext(), trades);
tradeLayout.setAdapter(tradeListAdapter);
tradeLayout.setTextFilterEnabled(false);
view.findViewById(R.id.tradeLoaderIndicator).setVisibility(View.GONE);
} }
private class TradeAdder extends AsyncTask<Void, Integer, Void> /*public class TransactionUpdater extends AsyncTask<Void, Integer, Void>
{
@Override
protected Void doInBackground(Void... voids) {
if(Looper.myLooper() == null)
{
Looper.prepare();
}
//binanceManager.updateTransactions(currency.getSymbol());
final ArrayList<Transaction> transactionList = databaseManager.getCurrencyTransactionsForSymbol(currency.getSymbol());
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
drawTransactionList(transactionList);
}
});
return null;
}
}*/
/*private class TradeAdder extends AsyncTask<Void, Integer, Void>
{ {
@Override @Override
protected void onPreExecute() protected void onPreExecute()
@ -146,7 +226,7 @@ public class TransactionsFragment extends Fragment {
tradeLayout.removeFooterView(loadingFooter); tradeLayout.removeFooterView(loadingFooter);
} }
}); });
/*binanceManager.updateTrades(new BinanceManager.BinanceCallBack() { binanceManager.updateTrades(new BinanceManager.BinanceCallBack() {
@Override @Override
public void onSuccess() { public void onSuccess() {
ArrayList<com.nauk.moodl.DataManagers.CurrencyData.Trade> trades = binanceManager.getTrades(); ArrayList<com.nauk.moodl.DataManagers.CurrencyData.Trade> trades = binanceManager.getTrades();
@ -173,13 +253,13 @@ public class TransactionsFragment extends Fragment {
public void onError(String error) { public void onError(String error) {
} }
}, currency.getSymbol(), tradeListAdapter.getItem(tradeListAdapter.getCount() - 1).getId());*/ }, currency.getSymbol(), tradeListAdapter.getItem(tradeListAdapter.getCount() - 1).getId());
return null; return null;
} }
} }*/
private void drawTransactionList(ArrayList<Transaction> transactions) /*private void drawTransactionList(ArrayList<Transaction> transactions)
{ {
TransactionListAdapter transactionListAdapter = new TransactionListAdapter(getActivity(), transactions); TransactionListAdapter transactionListAdapter = new TransactionListAdapter(getActivity(), transactions);
@ -187,30 +267,6 @@ public class TransactionsFragment extends Fragment {
transactionLayout.setTextFilterEnabled(false); transactionLayout.setTextFilterEnabled(false);
} }
public class TransactionUpdater extends AsyncTask<Void, Integer, Void>
{
@Override
protected Void doInBackground(Void... voids) {
if(Looper.myLooper() == null)
{
Looper.prepare();
}
//binanceManager.updateTransactions(currency.getSymbol());
final ArrayList<Transaction> transactionList = databaseManager.getCurrencyTransactionsForSymbol(currency.getSymbol());
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
drawTransactionList(transactionList);
}
});
return null;
}
}
private class TradeUpdater extends AsyncTask<Void, Integer, Void> implements BinanceUpdateNotifierInterface private class TradeUpdater extends AsyncTask<Void, Integer, Void> implements BinanceUpdateNotifierInterface
{ {
private List<Trade> trades; private List<Trade> trades;
@ -276,7 +332,7 @@ public class TransactionsFragment extends Fragment {
trades.add(returnedTrades.get(i)); trades.add(returnedTrades.get(i));
} }
drawTradeList(trades); //drawTradeList(trades);
} }
}); });
} catch (NullPointerException e) { } catch (NullPointerException e) {
@ -294,5 +350,51 @@ public class TransactionsFragment extends Fragment {
public void onBinanceBalanceUpdateError(int accountId, String error) { public void onBinanceBalanceUpdateError(int accountId, String error) {
} }
}*/
/*private void loadingIndicatorGenerator()
{
loadingFooter = LayoutInflater.from(getActivity().getBaseContext()).inflate(R.layout.listview_loading_indicator, null, false);
Drawable drawable = ((ProgressBar) loadingFooter.findViewById(R.id.progressIndicator)).getIndeterminateDrawable();
drawable.mutate();
drawable.setColorFilter(new PorterDuffColorFilter(currency.getChartColor(), PorterDuff.Mode.SRC_IN));
drawable.invalidateSelf();
tradeLayout.addFooterView(loadingFooter);
}
private void drawTradeList(ArrayList<com.herbron.moodl.DataManagers.CurrencyData.Trade> trades)
{
if(returnedTrades.size() > 20)
{
tradeLayout.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView absListView, int i) {
}
@Override
public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if(firstVisibleItem+visibleItemCount == totalItemCount && totalItemCount!=0)
{
if(!flag_loading && tradeLayout.getCount() != returnedTrades.size() - 1)
{
flag_loading = true;
TradeAdder tradeAdder = new TradeAdder();
tradeAdder.execute();
} }
} }
}
});
}
tradeListAdapter = new TradeListAdapter(getActivity().getBaseContext(), trades);
tradeLayout.setAdapter(tradeListAdapter);
tradeLayout.setTextFilterEnabled(false);
view.findViewById(R.id.tradeLoaderIndicator).setVisibility(View.GONE);
}*/
}

View File

@ -41,7 +41,7 @@ public class TradeListAdapter extends ArrayAdapter<Trade> {
} }
TextView amountTxtView = convertView.findViewById(R.id.amountPurchased); TextView amountTxtView = convertView.findViewById(R.id.amountPurchased);
TextView purchasedPrice = convertView.findViewById(R.id.purchasedPrice); TextView purchasedPrice = convertView.findViewById(R.id.purchasePrice);
TextView tradePair = convertView.findViewById(R.id.pair); TextView tradePair = convertView.findViewById(R.id.pair);
TextView dateTxtView = convertView.findViewById(R.id.tradeDate); TextView dateTxtView = convertView.findViewById(R.id.tradeDate);
View tradeIndicator = convertView.findViewById(R.id.tradeIndicator); View tradeIndicator = convertView.findViewById(R.id.tradeIndicator);

View File

@ -5,6 +5,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -14,6 +15,7 @@ import android.widget.TextView;
import com.daimajia.swipe.SwipeLayout; import com.daimajia.swipe.SwipeLayout;
import com.herbron.moodl.Activities.RecordTransactionActivity; import com.herbron.moodl.Activities.RecordTransactionActivity;
import com.herbron.moodl.DataManagers.CurrencyData.Trade;
import com.herbron.moodl.DataManagers.CurrencyData.Transaction; import com.herbron.moodl.DataManagers.CurrencyData.Transaction;
import com.herbron.moodl.DataManagers.DatabaseManager; import com.herbron.moodl.DataManagers.DatabaseManager;
import com.herbron.moodl.DataManagers.PreferencesManager; import com.herbron.moodl.DataManagers.PreferencesManager;
@ -30,11 +32,11 @@ import static java.lang.Math.abs;
* Created by Guitoune on 24/04/2018. * Created by Guitoune on 24/04/2018.
*/ */
public class TransactionListAdapter extends ArrayAdapter<Transaction> { public class TransactionListAdapter extends ArrayAdapter<Object> {
private Context context; private Context context;
public TransactionListAdapter(Context context, ArrayList<Transaction> transactions) public TransactionListAdapter(Context context, ArrayList<Object> transactions)
{ {
super(context, android.R.layout.simple_list_item_1, transactions); super(context, android.R.layout.simple_list_item_1, transactions);
this.context = context; this.context = context;
@ -43,12 +45,51 @@ public class TransactionListAdapter extends ArrayAdapter<Transaction> {
@NonNull @NonNull
@Override @Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
final Transaction transaction = getItem(position);
if(convertView == null) if(getItem(position) instanceof Transaction)
{ {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom_transaction_row, parent, false); return generateTransactionLayout(position, parent);
} }
else
{
return generateTradeLayout(position, parent);
}
}
private View generateTradeLayout(int position, ViewGroup parent)
{
Trade trade = (Trade) getItem(position);
View convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom_trade_row, parent, false);
TextView amountTxtView = convertView.findViewById(R.id.amountPurchased);
TextView purchasedPrice = convertView.findViewById(R.id.purchasePrice);
TextView tradePair = convertView.findViewById(R.id.pair);
TextView dateTxtView = convertView.findViewById(R.id.tradeDate);
View tradeIndicator = convertView.findViewById(R.id.tradeIndicator);
amountTxtView.setText(String.valueOf(trade.getQty()));
purchasedPrice.setText(trade.getPrice());
dateTxtView.setText(getDateFromTimestamp(trade.getTime()));
tradePair.setText(trade.getSymbol() + "/" + trade.getPairSymbol());
if(trade.isBuyer())
{
tradeIndicator.setBackgroundColor(context.getResources().getColor(R.color.green));
}
else
{
tradeIndicator.setBackgroundColor(context.getResources().getColor(R.color.red));
}
return convertView;
}
private View generateTransactionLayout(int position, ViewGroup parent)
{
final Transaction transaction = (Transaction) getItem(position);
View convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom_transaction_row, parent, false);
TextView amountTxtView = convertView.findViewById(R.id.amountPurchased); TextView amountTxtView = convertView.findViewById(R.id.amountPurchased);
TextView valueTxtView = convertView.findViewById(R.id.puchasedValue); TextView valueTxtView = convertView.findViewById(R.id.puchasedValue);

View File

@ -38,7 +38,7 @@
android:textSize="15dp"/> android:textSize="15dp"/>
<TextView <TextView
android:id="@+id/purchasedPrice" android:id="@+id/purchasePrice"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:textSize="10dp"/> android:textSize="10dp"/>

View File

@ -2,6 +2,11 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="50dp" android:layout_height="50dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="49dp"
android:orientation="horizontal"> android:orientation="horizontal">
<View <View
@ -9,14 +14,9 @@
android:layout_width="5dp" android:layout_width="5dp"
android:layout_height="match_parent"/> android:layout_height="match_parent"/>
<LinearLayout <com.daimajia.swipe.SwipeLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
<com.daimajia.swipe.SwipeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="49dp"
android:id="@+id/swipeLayout"> android:id="@+id/swipeLayout">
<!-- Bottom View Start--> <!-- Bottom View Start-->
<LinearLayout <LinearLayout
@ -109,6 +109,8 @@
<!-- Surface View End --> <!-- Surface View End -->
</com.daimajia.swipe.SwipeLayout> </com.daimajia.swipe.SwipeLayout>
</LinearLayout>
<View <View
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="@dimen/separationLineSize" android:layout_height="@dimen/separationLineSize"
@ -116,5 +118,3 @@
android:background="@color/separationColor" /> android:background="@color/separationColor" />
</LinearLayout> </LinearLayout>
</LinearLayout>

View File

@ -5,7 +5,15 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"> android:orientation="vertical">
<TextView <ListView
android:id="@+id/listTransactions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:divider="@null"
android:dividerHeight="0dp"/>
<!--<TextView
android:text="@string/transaction_history" android:text="@string/transaction_history"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -65,6 +73,6 @@
android:divider="@null" android:divider="@null"
android:dividerHeight="0dp"/> android:dividerHeight="0dp"/>
</LinearLayout> </LinearLayout>-->
</LinearLayout> </LinearLayout>