diff --git a/app/src/main/java/com/herbron/moodl/Activities/DetailsActivityFragments/TransactionsFragment.java b/app/src/main/java/com/herbron/moodl/Activities/DetailsActivityFragments/TransactionsFragment.java index 192fb4f..2641b70 100644 --- a/app/src/main/java/com/herbron/moodl/Activities/DetailsActivityFragments/TransactionsFragment.java +++ b/app/src/main/java/com/herbron/moodl/Activities/DetailsActivityFragments/TransactionsFragment.java @@ -28,6 +28,8 @@ import com.herbron.moodl.CustomAdapters.TransactionListAdapter; import com.herbron.moodl.R; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; import java.util.List; /** @@ -39,7 +41,7 @@ public class TransactionsFragment extends Fragment { private Currency currency; private View loadingFooter; private View view; - private ListView tradeLayout; + //private ListView tradeLayout; private ListView transactionLayout; private boolean flag_loading; private List binanceManagerList; @@ -57,7 +59,7 @@ public class TransactionsFragment extends Fragment { currency = getActivity().getIntent().getParcelableExtra("currency"); databaseManager = new DatabaseManager(getActivity().getBaseContext()); binanceManagerList = databaseManager.getBinanceAccounts(); - tradeLayout = view.findViewById(R.id.listTrades); + //tradeLayout = view.findViewById(R.id.listTrades); transactionLayout = view.findViewById(R.id.listTransactions); flag_loading = false; @@ -65,59 +67,137 @@ public class TransactionsFragment extends Fragment { TransactionUpdater transactionUpdater = new TransactionUpdater(); transactionUpdater.execute(); - TradeUpdater updater = new TradeUpdater(); - updater.execute(); - return view; } - private void loadingIndicatorGenerator() + private class TransactionUpdater extends AsyncTask implements BinanceUpdateNotifierInterface { - loadingFooter = LayoutInflater.from(getActivity().getBaseContext()).inflate(R.layout.listview_loading_indicator, null, false); + private ArrayList displayedTransactions; + private int nbTradeAccountUpdated; + private int nbTransactionAccountUpdated; - Drawable drawable = ((ProgressBar) loadingFooter.findViewById(R.id.progressIndicator)).getIndeterminateDrawable(); - drawable.mutate(); - drawable.setColorFilter(new PorterDuffColorFilter(currency.getChartColor(), PorterDuff.Mode.SRC_IN)); - drawable.invalidateSelf(); + @Override + protected void onPreExecute() { + super.onPreExecute(); - tradeLayout.addFooterView(loadingFooter); - } + displayedTransactions = new ArrayList<>(); + nbTradeAccountUpdated = 0; + nbTransactionAccountUpdated = 0; + } - private void drawTradeList(ArrayList trades) - { - if(returnedTrades.size() > 20) + @Override + 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 trades) { + nbTradeAccountUpdated++; + + displayedTransactions.addAll(trades); + + if(nbTradeAccountUpdated == binanceManagerList.size()) + { + drawTransactionList(displayedTransactions); + } + } + + @Override + public void onBinanceBalanceUpdateSuccess() { + + } + + @Override + public void onBinanceBalanceUpdateError(int accountId, String error) { + + } + + private void drawTransactionList(ArrayList transactions) { - tradeLayout.setOnScrollListener(new AbsListView.OnScrollListener() { + Collections.sort(transactions, new Comparator() { @Override - public void onScrollStateChanged(AbsListView absListView, int i) { + public int compare(Object o1, Object o2) { + Long time1; + Long time2; - } - - @Override - public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) { - if(firstVisibleItem+visibleItemCount == totalItemCount && totalItemCount!=0) + if(o1 instanceof Transaction) { - if(!flag_loading && tradeLayout.getCount() != returnedTrades.size() - 1) - { - flag_loading = true; - - TradeAdder tradeAdder = new TradeAdder(); - tradeAdder.execute(); - } + 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 + /*public class TransactionUpdater extends AsyncTask + { + @Override + protected Void doInBackground(Void... voids) { + + if(Looper.myLooper() == null) + { + Looper.prepare(); + } + + //binanceManager.updateTransactions(currency.getSymbol()); + + final ArrayList transactionList = databaseManager.getCurrencyTransactionsForSymbol(currency.getSymbol()); + getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + drawTransactionList(transactionList); + } + }); + + return null; + } + }*/ + + /*private class TradeAdder extends AsyncTask { @Override protected void onPreExecute() @@ -146,7 +226,7 @@ public class TransactionsFragment extends Fragment { tradeLayout.removeFooterView(loadingFooter); } }); - /*binanceManager.updateTrades(new BinanceManager.BinanceCallBack() { + binanceManager.updateTrades(new BinanceManager.BinanceCallBack() { @Override public void onSuccess() { ArrayList trades = binanceManager.getTrades(); @@ -173,13 +253,13 @@ public class TransactionsFragment extends Fragment { public void onError(String error) { } - }, currency.getSymbol(), tradeListAdapter.getItem(tradeListAdapter.getCount() - 1).getId());*/ + }, currency.getSymbol(), tradeListAdapter.getItem(tradeListAdapter.getCount() - 1).getId()); return null; } - } + }*/ - private void drawTransactionList(ArrayList transactions) + /*private void drawTransactionList(ArrayList transactions) { TransactionListAdapter transactionListAdapter = new TransactionListAdapter(getActivity(), transactions); @@ -187,30 +267,6 @@ public class TransactionsFragment extends Fragment { transactionLayout.setTextFilterEnabled(false); } - public class TransactionUpdater extends AsyncTask - { - @Override - protected Void doInBackground(Void... voids) { - - if(Looper.myLooper() == null) - { - Looper.prepare(); - } - - //binanceManager.updateTransactions(currency.getSymbol()); - - final ArrayList transactionList = databaseManager.getCurrencyTransactionsForSymbol(currency.getSymbol()); - getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - drawTransactionList(transactionList); - } - }); - - return null; - } - } - private class TradeUpdater extends AsyncTask implements BinanceUpdateNotifierInterface { private List trades; @@ -276,7 +332,7 @@ public class TransactionsFragment extends Fragment { trades.add(returnedTrades.get(i)); } - drawTradeList(trades); + //drawTradeList(trades); } }); } catch (NullPointerException e) { @@ -294,5 +350,51 @@ public class TransactionsFragment extends Fragment { 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 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); + }*/ } diff --git a/app/src/main/java/com/herbron/moodl/CustomAdapters/TradeListAdapter.java b/app/src/main/java/com/herbron/moodl/CustomAdapters/TradeListAdapter.java index dc84a75..f113531 100644 --- a/app/src/main/java/com/herbron/moodl/CustomAdapters/TradeListAdapter.java +++ b/app/src/main/java/com/herbron/moodl/CustomAdapters/TradeListAdapter.java @@ -41,7 +41,7 @@ public class TradeListAdapter extends ArrayAdapter { } 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 dateTxtView = convertView.findViewById(R.id.tradeDate); View tradeIndicator = convertView.findViewById(R.id.tradeIndicator); diff --git a/app/src/main/java/com/herbron/moodl/CustomAdapters/TransactionListAdapter.java b/app/src/main/java/com/herbron/moodl/CustomAdapters/TransactionListAdapter.java index b1864e3..562e93c 100644 --- a/app/src/main/java/com/herbron/moodl/CustomAdapters/TransactionListAdapter.java +++ b/app/src/main/java/com/herbron/moodl/CustomAdapters/TransactionListAdapter.java @@ -5,6 +5,7 @@ import android.content.Context; import android.content.Intent; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -14,6 +15,7 @@ import android.widget.TextView; import com.daimajia.swipe.SwipeLayout; 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.DatabaseManager; import com.herbron.moodl.DataManagers.PreferencesManager; @@ -30,11 +32,11 @@ import static java.lang.Math.abs; * Created by Guitoune on 24/04/2018. */ -public class TransactionListAdapter extends ArrayAdapter { +public class TransactionListAdapter extends ArrayAdapter { private Context context; - public TransactionListAdapter(Context context, ArrayList transactions) + public TransactionListAdapter(Context context, ArrayList transactions) { super(context, android.R.layout.simple_list_item_1, transactions); this.context = context; @@ -43,12 +45,51 @@ public class TransactionListAdapter extends ArrayAdapter { @NonNull @Override 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 valueTxtView = convertView.findViewById(R.id.puchasedValue); diff --git a/app/src/main/res/layout/custom_trade_row.xml b/app/src/main/res/layout/custom_trade_row.xml index cbab56d..3298b26 100644 --- a/app/src/main/res/layout/custom_trade_row.xml +++ b/app/src/main/res/layout/custom_trade_row.xml @@ -38,7 +38,7 @@ android:textSize="15dp"/> diff --git a/app/src/main/res/layout/custom_transaction_row.xml b/app/src/main/res/layout/custom_transaction_row.xml index 7c96aaf..67e3c1b 100644 --- a/app/src/main/res/layout/custom_transaction_row.xml +++ b/app/src/main/res/layout/custom_transaction_row.xml @@ -2,21 +2,21 @@ - - + android:orientation="vertical"> + android:layout_height="49dp" + android:orientation="horizontal"> - + + - - + + \ No newline at end of file diff --git a/app/src/main/res/layout/detailsactivity_fragment_transactions.xml b/app/src/main/res/layout/detailsactivity_fragment_transactions.xml index 2d2560f..b2967aa 100644 --- a/app/src/main/res/layout/detailsactivity_fragment_transactions.xml +++ b/app/src/main/res/layout/detailsactivity_fragment_transactions.xml @@ -5,7 +5,15 @@ android:layout_height="match_parent" android:orientation="vertical"> - + + \ No newline at end of file