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 04f4cee..be28b78 100644 --- a/app/src/main/java/com/nauk/moodl/Activities/CurrencyDetailsActivity.java +++ b/app/src/main/java/com/nauk/moodl/Activities/CurrencyDetailsActivity.java @@ -17,14 +17,20 @@ import android.support.v4.app.NavUtils; import android.support.v4.content.ContextCompat; import android.support.v7.app.ActionBar; 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; import android.view.View; +import android.view.animation.Animation; +import android.view.animation.Transformation; +import android.widget.AbsListView; import android.widget.Button; import android.widget.LinearLayout; +import android.widget.ListView; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.ViewFlipper; @@ -51,9 +57,11 @@ import com.nauk.moodl.DataManagers.CurrencyData.Transaction; import com.nauk.moodl.DataManagers.DatabaseManager; import com.nauk.moodl.DataManagers.ExchangeManager.BinanceManager; import com.nauk.moodl.DataManagers.PreferencesManager; +import com.nauk.moodl.LayoutManagers.TradeListAdapter; 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; @@ -69,7 +77,7 @@ public class CurrencyDetailsActivity extends AppCompatActivity { private ViewFlipper viewFlipper; private LinearLayout transactionLayout; - private LinearLayout tradeLayout; + private ListView tradeLayout; private DatabaseManager databaseManager; //private String symbol; private Currency currency; @@ -85,6 +93,8 @@ public class CurrencyDetailsActivity extends AppCompatActivity { private BarChart barChart; private PreferencesManager preferencesManager; private BinanceManager binanceManager; + private TradeListAdapter tradeListAdapter; + private boolean flag_loading; private boolean isSnapshotUpdated; private boolean isTickerUpdated; @@ -150,6 +160,7 @@ public class CurrencyDetailsActivity extends AppCompatActivity { isTickerUpdated = false; displayLineChart = true; + flag_loading = false; viewFlipper = findViewById(R.id.vfCurrencyDetails); transactionLayout = findViewById(R.id.listTransactions); @@ -887,40 +898,36 @@ public class CurrencyDetailsActivity extends AppCompatActivity { return transColor; } - private void drawTradeList(HashMap> trades) + private void drawTradeList(ArrayList trades) { findViewById(R.id.tradeProgressBar).setVisibility(View.GONE); - tradeLayout.removeAllViews(); + tradeLayout.setOnScrollListener(new AbsListView.OnScrollListener() { + @Override + public void onScrollStateChanged(AbsListView absListView, int i) { - for(String key : trades.keySet()) - { - for(int i = trades.get(key).size()-1; i >= 0; i--) - { - View view = LayoutInflater.from(this).inflate(R.layout.custom_trade_row, null); - TextView amountTxtView = view.findViewById(R.id.amountPurchased); - TextView purchasedPrice = view.findViewById(R.id.purchasedPrice); - TextView tradePair = view.findViewById(R.id.pair); - TextView dateTxtView = view.findViewById(R.id.tradeDate); - View tradeIndicator = view.findViewById(R.id.tradeIndicator); - - if(trades.get(key).get(i).isBuyer()) - { - tradeIndicator.setBackgroundColor(getColor(R.color.green)); - } - else - { - tradeIndicator.setBackgroundColor(getColor(R.color.red)); - } - - amountTxtView.setText(String.valueOf(trades.get(key).get(i).getQty())); - purchasedPrice.setText(trades.get(key).get(i).getPrice()); - dateTxtView.setText(getDate(trades.get(key).get(i).getTime())); - tradePair.setText(currency.getSymbol() + "/" + key); - - tradeLayout.addView(view); } - } + + @Override + public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) { + if(firstVisibleItem+visibleItemCount == totalItemCount && totalItemCount!=0) + { + if(!flag_loading) + { + flag_loading = true; + + expand(findViewById(R.id.tradeProgressBar)); + TradeAdder tradeAdder = new TradeAdder(); + tradeAdder.execute(); + } + } + } + }); + + tradeListAdapter = new TradeListAdapter(this, trades); + + tradeLayout.setAdapter(tradeListAdapter); + tradeLayout.setTextFilterEnabled(false); } private void drawTransactionList() @@ -960,6 +967,34 @@ public class CurrencyDetailsActivity extends AppCompatActivity { } } + private static void expand(final View v) { + v.measure(CardView.LayoutParams.MATCH_PARENT, CardView.LayoutParams.WRAP_CONTENT); + final int targetHeight = v.getMeasuredHeight(); + + // Older versions of android (pre API 21) cancel animations for views with a height of 0. + v.getLayoutParams().height = 1; + v.setVisibility(View.VISIBLE); + Animation a = new Animation() + { + @Override + protected void applyTransformation(float interpolatedTime, Transformation t) { + v.getLayoutParams().height = interpolatedTime == 1 + ? CardView.LayoutParams.WRAP_CONTENT + : (int)(targetHeight * interpolatedTime); + v.requestLayout(); + } + + @Override + public boolean willChangeBounds() { + return true; + } + }; + + // 1dp/ms + a.setDuration((int)(targetHeight / v.getContext().getResources().getDisplayMetrics().density)); + v.startAnimation(a); + } + private void setupSwipeView(View view) { SwipeLayout swipeLayout = view.findViewById(R.id.swipeLayout); @@ -1003,6 +1038,44 @@ public class CurrencyDetailsActivity extends AppCompatActivity { }); } + private class TradeAdder extends AsyncTask + { + @Override + protected Void doInBackground(Void... voids) { + + binanceManager.updateTrades(new BinanceManager.BinanceCallBack() { + @Override + public void onSuccess() { + ArrayList trades = binanceManager.getTrades(); + final ArrayList returnedTrades = new ArrayList<>(); + + for(int i = trades.size() - 1; i > 0 ; i--) + { + returnedTrades.add(trades.get(i)); + } + + runOnUiThread(new Runnable() { + @Override + public void run() { + tradeListAdapter.addAll(returnedTrades); + tradeListAdapter.notifyDataSetChanged(); + flag_loading = false; + + findViewById(R.id.tradeProgressBar).setVisibility(View.GONE); + } + }); + } + + @Override + public void onError(String error) { + + } + }, currency.getSymbol(), tradeListAdapter.getItem(tradeListAdapter.getCount() - 1).getId()); + + return null; + } + } + private class TradeUpdater extends AsyncTask { @Override @@ -1025,12 +1098,18 @@ public class CurrencyDetailsActivity extends AppCompatActivity { binanceManager.updateTrades(new BinanceManager.BinanceCallBack() { @Override public void onSuccess() { - final HashMap> trades = binanceManager.getTrades(); + ArrayList trades = binanceManager.getTrades(); + final ArrayList returnedTrades = new ArrayList<>(); + + for(int i = trades.size() - 1; i > 0 ; i--) + { + returnedTrades.add(trades.get(i)); + } runOnUiThread(new Runnable() { @Override public void run() { - drawTradeList(trades); + drawTradeList(returnedTrades); } }); } diff --git a/app/src/main/java/com/nauk/moodl/DataManagers/CurrencyData/Trade.java b/app/src/main/java/com/nauk/moodl/DataManagers/CurrencyData/Trade.java new file mode 100644 index 0000000..725a4a3 --- /dev/null +++ b/app/src/main/java/com/nauk/moodl/DataManagers/CurrencyData/Trade.java @@ -0,0 +1,43 @@ +package com.nauk.moodl.DataManagers.CurrencyData; + +/** + * Created by Guitoune on 24/04/2018. + */ + +public class Trade extends com.binance.api.client.domain.account.Trade { + + private String symbol; + private String pairSymbol; + + public Trade(String symbol, String pairSymbol, com.binance.api.client.domain.account.Trade biTrade) + { + this.symbol = symbol; + this.pairSymbol = pairSymbol; + setId(biTrade.getId()); + setPrice(biTrade.getPrice()); + setQty(biTrade.getQty()); + setCommission(biTrade.getCommission()); + setCommissionAsset(biTrade.getCommissionAsset()); + setTime(biTrade.getTime()); + setBuyer(biTrade.isBuyer()); + setMaker(biTrade.isMaker()); + setBestMatch(biTrade.isBestMatch()); + setOrderId(biTrade.getOrderId()); + } + + public String getSymbol() { + return symbol; + } + + public void setSymbol(String symbol) { + this.symbol = symbol; + } + + public String getPairSymbol() { + return pairSymbol; + } + + public void setPairSymbol(String pairSymbol) { + this.pairSymbol = pairSymbol; + } +} 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 d8182c9..e5ede47 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 @@ -1,5 +1,7 @@ package com.nauk.moodl.DataManagers.ExchangeManager; +import android.util.Log; + import com.binance.api.client.BinanceApiClientFactory; import com.binance.api.client.BinanceApiRestClient; import com.binance.api.client.domain.account.Account; @@ -22,12 +24,25 @@ public class BinanceManager { private String privateKey; private List balance; - private HashMap> trades; + private ArrayList trades; + private List pairSymbolList; public BinanceManager(String publicKey, String privateKey) { this.publicKey = publicKey; this.privateKey = privateKey; + + createPairSymbolList(); + } + + private void createPairSymbolList() + { + pairSymbolList = new ArrayList<>(); + + pairSymbolList.add("BTC"); + pairSymbolList.add("ETH"); + pairSymbolList.add("BNB"); + pairSymbolList.add("USDT"); } public void updateBalance(BinanceCallBack callBack) @@ -57,20 +72,34 @@ public class BinanceManager { public void updateTrades(BinanceCallBack callBack, String symbol) { - trades = new HashMap<>(); + trades = new ArrayList<>(); - trades.put("BTC", updateTrades(null, symbol, "BTC")); - trades.put("ETH", updateTrades(null, symbol, "ETH")); - - trades.put("USDT", updateTrades(null, symbol, "USDT")); + for(int i = 0; i < pairSymbolList.size(); i++) + { + trades.addAll(updateTrades(symbol, pairSymbolList.get(i))); + } callBack.onSuccess(); } - public List updateTrades(BinanceCallBack callBack, String symbol, String pairSymbol) + public void updateTrades(BinanceCallBack callBack, String symbol, long fromId) + { + trades = new ArrayList<>(); + + for(int i = 0; i < pairSymbolList.size(); i++) + { + trades.addAll(updateTrades(symbol, pairSymbolList.get(i), fromId)); + } + + callBack.onSuccess(); + } + + + public List updateTrades(String symbol, String pairSymbol) { List presentTrades = new ArrayList<>(); + List customTrades = new ArrayList<>(); BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance(publicKey, privateKey); BinanceApiRestClient client = factory.newRestClient(); @@ -79,9 +108,10 @@ public class BinanceManager { try { presentTrades = client.getMyTrades(symbol + pairSymbol, 20); + } catch (BinanceApiException e) { try { - presentTrades = client.getMyTrades(pairSymbol + symbol); + presentTrades = client.getMyTrades(pairSymbol + symbol, 20); } catch (BinanceApiException f) { f.printStackTrace(); @@ -89,12 +119,44 @@ public class BinanceManager { } } - if(callBack != null) + for(int i = 0; i < presentTrades.size(); i++) { - callBack.onSuccess(); + customTrades.add(new com.nauk.moodl.DataManagers.CurrencyData.Trade(symbol, pairSymbol, presentTrades.get(i))); } - return presentTrades; + return customTrades; + } + + + public List updateTrades(String symbol, String pairSymbol, long fromId) + { + List presentTrades = new ArrayList<>(); + List customTrades = new ArrayList<>(); + BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance(publicKey, privateKey); + BinanceApiRestClient client = factory.newRestClient(); + + if(!symbol.equals(pairSymbol)) + { + try { + presentTrades = client.getMyTrades(symbol + pairSymbol, 20, fromId, System.currentTimeMillis(), System.currentTimeMillis()); + + + } catch (BinanceApiException e) { + try { + presentTrades = client.getMyTrades(pairSymbol + symbol, 20, fromId, System.currentTimeMillis(), System.currentTimeMillis()); + + } catch (BinanceApiException f) { + f.printStackTrace(); + } + } + } + + for(int i = 0; i < presentTrades.size(); i++) + { + customTrades.add(new com.nauk.moodl.DataManagers.CurrencyData.Trade(symbol, pairSymbol, presentTrades.get(i))); + } + + return customTrades; } public List getBalance() @@ -102,7 +164,7 @@ public class BinanceManager { return balance; } - public HashMap> getTrades() + public ArrayList getTrades() { return trades; } diff --git a/app/src/main/java/com/nauk/moodl/LayoutManagers/CurrencyListAdapter.java b/app/src/main/java/com/nauk/moodl/LayoutManagers/CurrencyListAdapter.java index b25d4b7..7fa3814 100644 --- a/app/src/main/java/com/nauk/moodl/LayoutManagers/CurrencyListAdapter.java +++ b/app/src/main/java/com/nauk/moodl/LayoutManagers/CurrencyListAdapter.java @@ -22,10 +22,10 @@ public class CurrencyListAdapter extends ArrayAdapter { private ArrayList tempCurrency, suggestions; private Context context; - public CurrencyListAdapter(Context context, ArrayList objects) { - super(context, android.R.layout.simple_list_item_1, objects); - this.tempCurrency = new ArrayList(objects); - this.suggestions = new ArrayList(objects); + public CurrencyListAdapter(Context context, ArrayList currencies) { + super(context, android.R.layout.simple_list_item_1, currencies); + this.tempCurrency = new ArrayList<>(currencies); + this.suggestions = new ArrayList<>(currencies); this.context = context; } diff --git a/app/src/main/java/com/nauk/moodl/LayoutManagers/TradeListAdapter.java b/app/src/main/java/com/nauk/moodl/LayoutManagers/TradeListAdapter.java new file mode 100644 index 0000000..8ac95c7 --- /dev/null +++ b/app/src/main/java/com/nauk/moodl/LayoutManagers/TradeListAdapter.java @@ -0,0 +1,78 @@ +package com.nauk.moodl.LayoutManagers; + +import android.content.Context; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.TextView; + +import com.nauk.moodl.DataManagers.CurrencyData.Trade; +import com.nauk.moodl.R; + +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.Locale; + +/** + * Created by Guitoune on 24/04/2018. + */ + +public class TradeListAdapter extends ArrayAdapter { + + private Context context; + + public TradeListAdapter(Context context, ArrayList trades) + { + super(context, android.R.layout.simple_list_item_1, trades); + this.context = context; + } + + @NonNull + @Override + public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { + Trade trade = getItem(position); + + if(convertView == null) + { + 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.purchasedPrice); + 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(getDate(trade.getTime())); + tradePair.setText(trade.getSymbol() + "/" + trade.getPairSymbol()); + + if(trade.isBuyer()) + { + tradeIndicator.setBackgroundColor(context.getColor(R.color.green)); + } + else + { + tradeIndicator.setBackgroundColor(context.getColor(R.color.red)); + } + + return convertView; + } + + private String getDate(long timeStamp){ + + try{ + SimpleDateFormat sdf = new SimpleDateFormat(" HH:mm dd/MM/yyyy", Locale.getDefault()); + Date netDate = (new Date(timeStamp)); + return sdf.format(netDate); + } + catch(Exception ex){ + return "xx"; + } + } +} diff --git a/app/src/main/res/layout/activity_currency_details.xml b/app/src/main/res/layout/activity_currency_details.xml index 0ceb76c..7f88788 100644 --- a/app/src/main/res/layout/activity_currency_details.xml +++ b/app/src/main/res/layout/activity_currency_details.xml @@ -619,27 +619,25 @@ android:layout_height="wrap_content" android:elevation="@dimen/cardview_elevation" /> - + android:layout_weight="0.5" + android:orientation="vertical"> - + android:orientation="vertical"/> - + - - - + diff --git a/app/src/main/res/layout/custom_trade_row.xml b/app/src/main/res/layout/custom_trade_row.xml index 0678451..b311d94 100644 --- a/app/src/main/res/layout/custom_trade_row.xml +++ b/app/src/main/res/layout/custom_trade_row.xml @@ -4,79 +4,71 @@ android:layout_height="match_parent" android:orientation="vertical"> - + android:layout_height="50dp" + android:orientation="horizontal"> + + - - + android:layout_height="match_parent" + android:id="@+id/mainLayout"> - + android:layout_height="match_parent"> + - - + android:layout_height="match_parent" + android:layout_weight="0.5" + android:orientation="vertical"> - + - + - - + + - + - + - - - - + \ No newline at end of file