From d79a76f13bba2b7eb5971b7c8c17d2ffe5d85e8d Mon Sep 17 00:00:00 2001 From: Tanguy Herbron Date: Mon, 20 Aug 2018 22:35:37 +0200 Subject: [PATCH] [DetailsActivity] Enable binance trade update - Add support for multiple binance accounts --- .../Transactions.java | 67 ++++++++++++------- .../moodl/DataManagers/BalanceManager.java | 5 +- .../ExchangeManager/BinanceManager.java | 2 +- .../BinanceUpdateNotifierInterface.java | 6 +- 4 files changed, 51 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/com/herbron/moodl/Activities/DetailsActivityFragments/Transactions.java b/app/src/main/java/com/herbron/moodl/Activities/DetailsActivityFragments/Transactions.java index 007e136..8d09c26 100644 --- a/app/src/main/java/com/herbron/moodl/Activities/DetailsActivityFragments/Transactions.java +++ b/app/src/main/java/com/herbron/moodl/Activities/DetailsActivityFragments/Transactions.java @@ -28,6 +28,7 @@ import com.herbron.moodl.CustomAdapters.TransactionListAdapter; import com.herbron.moodl.R; import java.util.ArrayList; +import java.util.List; /** * Created by Tiji on 13/05/2018. @@ -41,7 +42,7 @@ public class Transactions extends Fragment { private ListView tradeLayout; private ListView transactionLayout; private boolean flag_loading; - private BinanceManager binanceManager; + private List binanceManagerList; private DatabaseManager databaseManager; private TradeListAdapter tradeListAdapter; private ArrayList returnedTrades; @@ -55,7 +56,7 @@ public class Transactions extends Fragment { currency = getActivity().getIntent().getParcelableExtra("currency"); databaseManager = new DatabaseManager(getActivity().getBaseContext()); - //binanceManager = new BinanceManager(preferencesManager.getBinancePublicKey(), preferencesManager.getBinancePrivateKey()); + binanceManagerList = databaseManager.getBinanceAccounts(); tradeLayout = view.findViewById(R.id.listTrades); transactionLayout = view.findViewById(R.id.listTransactions); @@ -64,8 +65,8 @@ public class Transactions extends Fragment { TransactionUpdater transactionUpdater = new TransactionUpdater(); transactionUpdater.execute(); - /*TradeUpdater updater = new TradeUpdater(); - updater.execute();*/ + TradeUpdater updater = new TradeUpdater(); + updater.execute(); return view; } @@ -212,10 +213,16 @@ public class Transactions extends Fragment { private class TradeUpdater extends AsyncTask implements BinanceUpdateNotifierInterface { + private List trades; + private int nbResponse; + @Override protected void onPreExecute() { super.onPreExecute(); + + trades = new ArrayList<>(); + nbResponse = 0; } @Override @@ -228,7 +235,11 @@ public class Transactions extends Fragment { protected Void doInBackground(Void... params) { - binanceManager.updateTrades(currency.getSymbol()); + for(int i = 0; i < binanceManagerList.size(); i++) + { + binanceManagerList.get(i).addListener(this); + binanceManagerList.get(i).updateTrades(currency.getSymbol()); + } return null; } @@ -240,31 +251,37 @@ public class Transactions extends Fragment { } @Override - public void onBinanceTradesUpdated() { - ArrayList trades = binanceManager.getTrades(); - returnedTrades = new ArrayList<>(); + public void onBinanceTradesUpdated(List newTrades) { + trades.addAll(newTrades); - for(int i = trades.size() - 1; i >= 0 ; i--) + nbResponse++; + + if(nbResponse == binanceManagerList.size()) { - returnedTrades.add(trades.get(i)); - } + returnedTrades = new ArrayList<>(); - try { - getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - ArrayList trades = new ArrayList<>(); + for(int i = trades.size() - 1; i >= 0 ; i--) + { + returnedTrades.add(trades.get(i)); + } - for(int i = 0; i < 20 && i < returnedTrades.size(); i++) - { - trades.add(returnedTrades.get(i)); + try { + getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + ArrayList trades = new ArrayList<>(); + + for(int i = 0; i < 20 && i < returnedTrades.size(); i++) + { + trades.add(returnedTrades.get(i)); + } + + drawTradeList(trades); } - - drawTradeList(trades); - } - }); - } catch (NullPointerException e) { - Log.d("moodl", "Transactions do not need to be updated anymore"); + }); + } catch (NullPointerException e) { + Log.d("moodl", "Transactions do not need to be updated anymore"); + } } } diff --git a/app/src/main/java/com/herbron/moodl/DataManagers/BalanceManager.java b/app/src/main/java/com/herbron/moodl/DataManagers/BalanceManager.java index 0ad3f03..8693f8c 100644 --- a/app/src/main/java/com/herbron/moodl/DataManagers/BalanceManager.java +++ b/app/src/main/java/com/herbron/moodl/DataManagers/BalanceManager.java @@ -1,6 +1,7 @@ package com.herbron.moodl.DataManagers; import com.herbron.moodl.Activities.HomeActivity; +import com.herbron.moodl.DataManagers.CurrencyData.Trade; import com.herbron.moodl.DataNotifiers.BinanceUpdateNotifierInterface; import com.herbron.moodl.DataManagers.CurrencyData.Currency; import com.herbron.moodl.DataManagers.InfoAPIManagers.CryptocompareApiManager; @@ -87,7 +88,7 @@ public class BalanceManager implements BinanceUpdateNotifierInterface, HitBTCUpd for(int i = 0; i < binanceManagers.size(); i++) { - final BinanceManager binanceManager = binanceManagers.get(i); + BinanceManager binanceManager = binanceManagers.get(i); binanceManager.addListener(this); binanceManager.updateBalance(); } @@ -169,7 +170,7 @@ public class BalanceManager implements BinanceUpdateNotifierInterface, HitBTCUpd } @Override - public void onBinanceTradesUpdated() { + public void onBinanceTradesUpdated(List trades) { } diff --git a/app/src/main/java/com/herbron/moodl/DataManagers/ExchangeManager/BinanceManager.java b/app/src/main/java/com/herbron/moodl/DataManagers/ExchangeManager/BinanceManager.java index f33d9bb..d6d1142 100644 --- a/app/src/main/java/com/herbron/moodl/DataManagers/ExchangeManager/BinanceManager.java +++ b/app/src/main/java/com/herbron/moodl/DataManagers/ExchangeManager/BinanceManager.java @@ -101,7 +101,7 @@ public class BinanceManager extends Exchange { for(BinanceUpdateNotifierInterface binanceUpdateNotifierInterface : binanceUpdateNotifierInterfaceList) { - binanceUpdateNotifierInterface.onBinanceTradesUpdated(); + binanceUpdateNotifierInterface.onBinanceTradesUpdated(trades); } } diff --git a/app/src/main/java/com/herbron/moodl/DataNotifiers/BinanceUpdateNotifierInterface.java b/app/src/main/java/com/herbron/moodl/DataNotifiers/BinanceUpdateNotifierInterface.java index 25920af..05bcafd 100644 --- a/app/src/main/java/com/herbron/moodl/DataNotifiers/BinanceUpdateNotifierInterface.java +++ b/app/src/main/java/com/herbron/moodl/DataNotifiers/BinanceUpdateNotifierInterface.java @@ -1,8 +1,12 @@ package com.herbron.moodl.DataNotifiers; +import com.herbron.moodl.DataManagers.CurrencyData.Trade; + +import java.util.List; + public interface BinanceUpdateNotifierInterface { - void onBinanceTradesUpdated(); + void onBinanceTradesUpdated(List trades); void onBinanceBalanceUpdateSuccess();