Add dynamic trade loading
This commit is contained in:
parent
eeca570566
commit
ec73925720
@ -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,41 +898,37 @@ public class CurrencyDetailsActivity extends AppCompatActivity {
|
||||
return transColor;
|
||||
}
|
||||
|
||||
private void drawTradeList(HashMap<String, List<Trade>> trades)
|
||||
private void drawTradeList(ArrayList<com.nauk.moodl.DataManagers.CurrencyData.Trade> 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);
|
||||
@Override
|
||||
public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
|
||||
if(firstVisibleItem+visibleItemCount == totalItemCount && totalItemCount!=0)
|
||||
{
|
||||
if(!flag_loading)
|
||||
{
|
||||
flag_loading = true;
|
||||
|
||||
tradeLayout.addView(view);
|
||||
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<Void, Integer, Void>
|
||||
{
|
||||
@Override
|
||||
protected Void doInBackground(Void... voids) {
|
||||
|
||||
binanceManager.updateTrades(new BinanceManager.BinanceCallBack() {
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
ArrayList<com.nauk.moodl.DataManagers.CurrencyData.Trade> trades = binanceManager.getTrades();
|
||||
final ArrayList<com.nauk.moodl.DataManagers.CurrencyData.Trade> 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<Void, Integer, Void>
|
||||
{
|
||||
@Override
|
||||
@ -1025,12 +1098,18 @@ public class CurrencyDetailsActivity extends AppCompatActivity {
|
||||
binanceManager.updateTrades(new BinanceManager.BinanceCallBack() {
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
final HashMap<String, List<Trade>> trades = binanceManager.getTrades();
|
||||
ArrayList<com.nauk.moodl.DataManagers.CurrencyData.Trade> trades = binanceManager.getTrades();
|
||||
final ArrayList<com.nauk.moodl.DataManagers.CurrencyData.Trade> 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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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<Currency> balance;
|
||||
private HashMap<String, List<Trade>> trades;
|
||||
private ArrayList<com.nauk.moodl.DataManagers.CurrencyData.Trade> trades;
|
||||
private List<String> 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<Trade> 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<com.nauk.moodl.DataManagers.CurrencyData.Trade> updateTrades(String symbol, String pairSymbol)
|
||||
{
|
||||
List<Trade> presentTrades = new ArrayList<>();
|
||||
List<com.nauk.moodl.DataManagers.CurrencyData.Trade> 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<com.nauk.moodl.DataManagers.CurrencyData.Trade> updateTrades(String symbol, String pairSymbol, long fromId)
|
||||
{
|
||||
List<Trade> presentTrades = new ArrayList<>();
|
||||
List<com.nauk.moodl.DataManagers.CurrencyData.Trade> 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<Currency> getBalance()
|
||||
@ -102,7 +164,7 @@ public class BinanceManager {
|
||||
return balance;
|
||||
}
|
||||
|
||||
public HashMap<String, List<Trade>> getTrades()
|
||||
public ArrayList<com.nauk.moodl.DataManagers.CurrencyData.Trade> getTrades()
|
||||
{
|
||||
return trades;
|
||||
}
|
||||
|
@ -22,10 +22,10 @@ public class CurrencyListAdapter extends ArrayAdapter<Currency> {
|
||||
private ArrayList<Currency> tempCurrency, suggestions;
|
||||
private Context context;
|
||||
|
||||
public CurrencyListAdapter(Context context, ArrayList<Currency> objects) {
|
||||
super(context, android.R.layout.simple_list_item_1, objects);
|
||||
this.tempCurrency = new ArrayList<Currency>(objects);
|
||||
this.suggestions = new ArrayList<Currency>(objects);
|
||||
public CurrencyListAdapter(Context context, ArrayList<Currency> currencies) {
|
||||
super(context, android.R.layout.simple_list_item_1, currencies);
|
||||
this.tempCurrency = new ArrayList<>(currencies);
|
||||
this.suggestions = new ArrayList<>(currencies);
|
||||
|
||||
this.context = context;
|
||||
}
|
||||
|
@ -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<Trade> {
|
||||
|
||||
private Context context;
|
||||
|
||||
public TradeListAdapter(Context context, ArrayList<Trade> 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";
|
||||
}
|
||||
}
|
||||
}
|
@ -619,28 +619,26 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:elevation="@dimen/cardview_elevation" />
|
||||
|
||||
<ScrollView
|
||||
android:id="@+id/svTransactions"
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_weight="0.5">
|
||||
android:layout_weight="0.5"
|
||||
android:orientation="vertical">
|
||||
|
||||
<LinearLayout
|
||||
<ListView
|
||||
android:id="@+id/listTrades"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical">
|
||||
android:orientation="vertical"/>
|
||||
|
||||
<ProgressBar
|
||||
android:id="@+id/tradeProgressBar"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</ScrollView>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</ViewFlipper>
|
||||
|
@ -4,11 +4,6 @@
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
|
||||
<android.support.v7.widget.CardView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="@drawable/rounded_corners">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="50dp"
|
||||
@ -76,7 +71,4 @@
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
|
||||
</android.support.v7.widget.CardView>
|
||||
|
||||
</LinearLayout>
|
Loading…
Reference in New Issue
Block a user