From 6d3e5b7bfd0baeeacb7f38dcdc719c567790140b Mon Sep 17 00:00:00 2001 From: Tanguy Herbron Date: Sun, 29 Apr 2018 19:59:06 +0200 Subject: [PATCH] Fix Binance trade synchronization --- .../Activities/CurrencyDetailsActivity.java | 77 ++++++++++++++----- .../ExchangeManager/BinanceManager.java | 18 +++-- .../ExchangeManager/HitBtcManager.java | 17 ++++ .../res/layout/activity_currency_details.xml | 2 +- 4 files changed, 86 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/com/nauk/moodl/Activities/CurrencyDetailsActivity.java b/app/src/main/java/com/nauk/moodl/Activities/CurrencyDetailsActivity.java index 7814258..961ee89 100644 --- a/app/src/main/java/com/nauk/moodl/Activities/CurrencyDetailsActivity.java +++ b/app/src/main/java/com/nauk/moodl/Activities/CurrencyDetailsActivity.java @@ -24,6 +24,7 @@ import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.CardView; import android.text.Html; import android.text.method.LinkMovementMethod; +import android.util.Log; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.MotionEvent; @@ -54,6 +55,7 @@ import com.github.mikephil.charting.highlight.Highlight; import com.github.mikephil.charting.listener.OnChartValueSelectedListener; import com.nauk.moodl.DataManagers.CurrencyData.Currency; import com.nauk.moodl.DataManagers.CurrencyData.CurrencyDataChart; +import com.nauk.moodl.DataManagers.CurrencyData.Trade; import com.nauk.moodl.DataManagers.CurrencyData.Transaction; import com.nauk.moodl.DataManagers.DatabaseManager; import com.nauk.moodl.DataManagers.ExchangeManager.BinanceManager; @@ -63,6 +65,7 @@ import com.nauk.moodl.LayoutManagers.TransactionListAdapter; import com.nauk.moodl.PlaceholderManager; import com.nauk.moodl.R; +import java.lang.reflect.Array; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; @@ -105,6 +108,7 @@ public class CurrencyDetailsActivity extends AppCompatActivity { private Button lineChartButton; private Button candleStickChartButton; + private ArrayList returnedTrades; private View loadingFooter; @@ -918,26 +922,29 @@ public class CurrencyDetailsActivity extends AppCompatActivity { private void drawTradeList(ArrayList trades) { - tradeLayout.setOnScrollListener(new AbsListView.OnScrollListener() { - @Override - public void onScrollStateChanged(AbsListView absListView, int i) { + 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) + @Override + public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) { + if(firstVisibleItem+visibleItemCount == totalItemCount && totalItemCount!=0) { - flag_loading = true; + if(!flag_loading && tradeLayout.getCount() != returnedTrades.size() - 1) + { + flag_loading = true; - TradeAdder tradeAdder = new TradeAdder(); - tradeAdder.execute(); + TradeAdder tradeAdder = new TradeAdder(); + tradeAdder.execute(); + } } } - } - }); + }); + } tradeListAdapter = new TradeListAdapter(this, trades); @@ -987,8 +994,13 @@ public class CurrencyDetailsActivity extends AppCompatActivity { Looper.prepare(); } - ArrayList transactionList = databaseManager.getCurrencyTransactionsForSymbol(currency.getSymbol()); - drawTransactionList(transactionList); + final ArrayList transactionList = databaseManager.getCurrencyTransactionsForSymbol(currency.getSymbol()); + runOnUiThread(new Runnable() { + @Override + public void run() { + drawTransactionList(transactionList); + } + }); return null; } @@ -1019,7 +1031,23 @@ public class CurrencyDetailsActivity extends AppCompatActivity { @Override protected Void doInBackground(Void... voids) { - binanceManager.updateTrades(new BinanceManager.BinanceCallBack() { + final ArrayList trades = new ArrayList<>(); + for(int i = tradeLayout.getCount(); i < tradeLayout.getCount() + 20 && i < returnedTrades.size(); i++) + { + trades.add(returnedTrades.get(i)); + } + + runOnUiThread(new Runnable() { + @Override + public void run() { + tradeListAdapter.addAll(trades); + tradeListAdapter.notifyDataSetChanged(); + flag_loading = false; + + tradeLayout.removeFooterView(loadingFooter); + } + }); + /*binanceManager.updateTrades(new BinanceManager.BinanceCallBack() { @Override public void onSuccess() { ArrayList trades = binanceManager.getTrades(); @@ -1046,7 +1074,7 @@ public class CurrencyDetailsActivity extends AppCompatActivity { public void onError(String error) { } - }, currency.getSymbol(), tradeListAdapter.getItem(tradeListAdapter.getCount() - 1).getId()); + }, currency.getSymbol(), tradeListAdapter.getItem(tradeListAdapter.getCount() - 1).getId());*/ return null; } @@ -1073,9 +1101,9 @@ public class CurrencyDetailsActivity extends AppCompatActivity { @Override public void onSuccess() { ArrayList trades = binanceManager.getTrades(); - final ArrayList returnedTrades = new ArrayList<>(); + returnedTrades = new ArrayList<>(); - for(int i = trades.size() - 1; i > 0 ; i--) + for(int i = trades.size() - 1; i >= 0 ; i--) { returnedTrades.add(trades.get(i)); } @@ -1083,7 +1111,14 @@ public class CurrencyDetailsActivity extends AppCompatActivity { runOnUiThread(new Runnable() { @Override public void run() { - drawTradeList(returnedTrades); + ArrayList trades = new ArrayList<>(); + + for(int i = 0; i < 20 && i < returnedTrades.size(); i++) + { + trades.add(returnedTrades.get(i)); + } + + drawTradeList(trades); } }); } diff --git a/app/src/main/java/com/nauk/moodl/DataManagers/ExchangeManager/BinanceManager.java b/app/src/main/java/com/nauk/moodl/DataManagers/ExchangeManager/BinanceManager.java index e5ede47..bfc28b3 100644 --- a/app/src/main/java/com/nauk/moodl/DataManagers/ExchangeManager/BinanceManager.java +++ b/app/src/main/java/com/nauk/moodl/DataManagers/ExchangeManager/BinanceManager.java @@ -77,7 +77,10 @@ public class BinanceManager { for(int i = 0; i < pairSymbolList.size(); i++) { - trades.addAll(updateTrades(symbol, pairSymbolList.get(i))); + if(!pairSymbolList.get(i).equals(symbol)) + { + trades.addAll(updateTrades(symbol, pairSymbolList.get(i))); + } } callBack.onSuccess(); @@ -89,7 +92,10 @@ public class BinanceManager { for(int i = 0; i < pairSymbolList.size(); i++) { - trades.addAll(updateTrades(symbol, pairSymbolList.get(i), fromId)); + if(!pairSymbolList.get(i).equals(symbol)) + { + trades.addAll(updateTrades(symbol, pairSymbolList.get(i), fromId)); + } } callBack.onSuccess(); @@ -106,16 +112,16 @@ public class BinanceManager { if(!symbol.equals(pairSymbol)) { try { - presentTrades = client.getMyTrades(symbol + pairSymbol, 20); + presentTrades = client.getMyTrades(symbol + pairSymbol); } catch (BinanceApiException e) { - try { - presentTrades = client.getMyTrades(pairSymbol + symbol, 20); + /*try { + presentTrades = client.getMyTrades(pairSymbol + symbol); } catch (BinanceApiException f) { f.printStackTrace(); - } + }*/ } } diff --git a/app/src/main/java/com/nauk/moodl/DataManagers/ExchangeManager/HitBtcManager.java b/app/src/main/java/com/nauk/moodl/DataManagers/ExchangeManager/HitBtcManager.java index a63a62c..f29deae 100644 --- a/app/src/main/java/com/nauk/moodl/DataManagers/ExchangeManager/HitBtcManager.java +++ b/app/src/main/java/com/nauk/moodl/DataManagers/ExchangeManager/HitBtcManager.java @@ -31,7 +31,9 @@ public class HitBtcManager { private String publicKey; private String privateKey; final private String hitBalanceUrl = "https://api.hitbtc.com/api/2/trading/balance"; + final private String tradeHistoryUrl = "https://api.hitbtc.com/api/2/history/trades?"; private RequestQueue requestQueue; + private List pairSymbolList; private List balance; private android.content.Context context; @@ -45,6 +47,21 @@ public class HitBtcManager { this.privateKey = privateKey; } + private void createPairSymbolList() + { + pairSymbolList = new ArrayList<>(); + + pairSymbolList.add("BTC"); + pairSymbolList.add("ETH"); + pairSymbolList.add("BNB"); + pairSymbolList.add("USDT"); + } + + public void updateTrades(final HitBtcCallBack callBack, String symbol, String pairSymbol) + { + + } + public void updateBalance(final HitBtcCallBack callBack) { JsonArrayRequest arrayRequest = new JsonArrayRequest(Request.Method.GET, hitBalanceUrl diff --git a/app/src/main/res/layout/activity_currency_details.xml b/app/src/main/res/layout/activity_currency_details.xml index 754b82e..1049560 100644 --- a/app/src/main/res/layout/activity_currency_details.xml +++ b/app/src/main/res/layout/activity_currency_details.xml @@ -647,7 +647,7 @@