ListView for Transaction history

This commit is contained in:
Tanguy Herbron 2018-04-24 16:08:05 +02:00
parent ec73925720
commit ee8ba428d7
5 changed files with 310 additions and 169 deletions

View File

@ -11,6 +11,7 @@ import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.Looper;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView; import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.NavUtils; import android.support.v4.app.NavUtils;
@ -25,6 +26,7 @@ import android.view.LayoutInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.ViewParent;
import android.view.animation.Animation; import android.view.animation.Animation;
import android.view.animation.Transformation; import android.view.animation.Transformation;
import android.widget.AbsListView; import android.widget.AbsListView;
@ -58,6 +60,7 @@ import com.nauk.moodl.DataManagers.DatabaseManager;
import com.nauk.moodl.DataManagers.ExchangeManager.BinanceManager; import com.nauk.moodl.DataManagers.ExchangeManager.BinanceManager;
import com.nauk.moodl.DataManagers.PreferencesManager; import com.nauk.moodl.DataManagers.PreferencesManager;
import com.nauk.moodl.LayoutManagers.TradeListAdapter; import com.nauk.moodl.LayoutManagers.TradeListAdapter;
import com.nauk.moodl.LayoutManagers.TransactionListAdapter;
import com.nauk.moodl.PlaceholderManager; import com.nauk.moodl.PlaceholderManager;
import com.nauk.moodl.R; import com.nauk.moodl.R;
@ -76,7 +79,7 @@ import static java.lang.Math.abs;
public class CurrencyDetailsActivity extends AppCompatActivity { public class CurrencyDetailsActivity extends AppCompatActivity {
private ViewFlipper viewFlipper; private ViewFlipper viewFlipper;
private LinearLayout transactionLayout; private ListView transactionLayout;
private ListView tradeLayout; private ListView tradeLayout;
private DatabaseManager databaseManager; private DatabaseManager databaseManager;
//private String symbol; //private String symbol;
@ -94,6 +97,7 @@ public class CurrencyDetailsActivity extends AppCompatActivity {
private PreferencesManager preferencesManager; private PreferencesManager preferencesManager;
private BinanceManager binanceManager; private BinanceManager binanceManager;
private TradeListAdapter tradeListAdapter; private TradeListAdapter tradeListAdapter;
private TransactionListAdapter transactionListAdapter;
private boolean flag_loading; private boolean flag_loading;
private boolean isSnapshotUpdated; private boolean isSnapshotUpdated;
@ -205,12 +209,13 @@ public class CurrencyDetailsActivity extends AppCompatActivity {
setupActionBar(); setupActionBar();
drawTransactionList();
initializeButtons(); initializeButtons();
initializeLineChart(lineChart); initializeLineChart(lineChart);
initializeCandleStickChart(candleStickChart); initializeCandleStickChart(candleStickChart);
TransactionUpdater transactionUpdater = new TransactionUpdater();
transactionUpdater.execute();
updateInfoTab(); updateInfoTab();
updateChartTab(DAY, 1); updateChartTab(DAY, 1);
@ -898,6 +903,14 @@ public class CurrencyDetailsActivity extends AppCompatActivity {
return transColor; return transColor;
} }
private void drawTransactionList(ArrayList<Transaction> transactions)
{
transactionListAdapter = new TransactionListAdapter(this, transactions);
transactionLayout.setAdapter(transactionListAdapter);
transactionLayout.setTextFilterEnabled(false);
}
private void drawTradeList(ArrayList<com.nauk.moodl.DataManagers.CurrencyData.Trade> trades) private void drawTradeList(ArrayList<com.nauk.moodl.DataManagers.CurrencyData.Trade> trades)
{ {
findViewById(R.id.tradeProgressBar).setVisibility(View.GONE); findViewById(R.id.tradeProgressBar).setVisibility(View.GONE);
@ -930,43 +943,6 @@ public class CurrencyDetailsActivity extends AppCompatActivity {
tradeLayout.setTextFilterEnabled(false); tradeLayout.setTextFilterEnabled(false);
} }
private void drawTransactionList()
{
transactionLayout.removeAllViews();
List<Transaction> transactionList = databaseManager.getCurrencyTransactions(currency.getSymbol());
for(int i = 0; i < transactionList.size(); i++)
{
View view = LayoutInflater.from(this).inflate(R.layout.custom_transaction_row, null);
TextView amountTxtView = view.findViewById(R.id.amountPurchased);
TextView valueTxtView = view.findViewById(R.id.puchasedValue);
TextView dateTxtView = view.findViewById(R.id.purchaseDate);
dateTxtView.setText(getDate(transactionList.get(i).getTimestamp()));
LinearLayout deleteLayout = view.findViewById(R.id.deleteTransactionLayout);
deleteLayout.setTag(transactionList.get(i).getTransactionId());
deleteLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
preferencesManager.setMustUpdateSummary(true);
databaseManager.deleteTransactionFromId(Integer.parseInt(view.getTag().toString()));
drawTransactionList();
hasBeenModified = true;
}
});
amountTxtView.setText(String.valueOf(transactionList.get(i).getAmount()));
valueTxtView.setText(numberConformer(transactionList.get(i).getPurchasedPrice() * transactionList.get(i).getAmount()));
setupSwipeView(view);
transactionLayout.addView(view);
}
}
private static void expand(final View v) { private static void expand(final View v) {
v.measure(CardView.LayoutParams.MATCH_PARENT, CardView.LayoutParams.WRAP_CONTENT); v.measure(CardView.LayoutParams.MATCH_PARENT, CardView.LayoutParams.WRAP_CONTENT);
final int targetHeight = v.getMeasuredHeight(); final int targetHeight = v.getMeasuredHeight();
@ -995,47 +971,23 @@ public class CurrencyDetailsActivity extends AppCompatActivity {
v.startAnimation(a); v.startAnimation(a);
} }
private void setupSwipeView(View view)
public class TransactionUpdater extends AsyncTask<Void, Integer, Void>
{ {
SwipeLayout swipeLayout = view.findViewById(R.id.swipeLayout); @Override
protected Void doInBackground(Void... voids) {
//set show mode. if(Looper.myLooper() == null)
swipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown); {
Looper.prepare();
//add drag edge.(If the BottomView has 'layout_gravity' attribute, this line is unnecessary)
swipeLayout.addDrag(SwipeLayout.DragEdge.Left, view.findViewById(R.id.bottom_wrapper));
swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
@Override
public void onClose(SwipeLayout layout) {
//when the SurfaceView totally cover the BottomView.
} }
@Override ArrayList<Transaction> transactionList = databaseManager.getCurrencyTransactions(currency.getSymbol());
public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) { drawTransactionList(transactionList);
//you are swiping.
}
@Override return null;
public void onStartOpen(SwipeLayout layout) { }
}
@Override
public void onOpen(SwipeLayout layout) {
//when the BottomView totally show.
}
@Override
public void onStartClose(SwipeLayout layout) {
}
@Override
public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {
//when user's hand released.
}
});
} }
private class TradeAdder extends AsyncTask<Void, Integer, Void> private class TradeAdder extends AsyncTask<Void, Integer, Void>

View File

@ -159,13 +159,13 @@ public class DatabaseManager extends SQLiteOpenHelper{
return currencyList; return currencyList;
} }
public List<Transaction> getCurrencyTransactions(String symbol) public ArrayList<Transaction> getCurrencyTransactions(String symbol)
{ {
String searchQuerry = "SELECT * FROM " + TABLE_MANUAL_CURRENCIES + " WHERE symbol='" + symbol.toUpperCase() + "'"; String searchQuerry = "SELECT * FROM " + TABLE_MANUAL_CURRENCIES + " WHERE symbol='" + symbol.toUpperCase() + "'";
SQLiteDatabase db = this.getWritableDatabase(); SQLiteDatabase db = this.getWritableDatabase();
Cursor resultatList = db.rawQuery(searchQuerry, null); Cursor resultatList = db.rawQuery(searchQuerry, null);
List<Transaction> transactionList = new ArrayList<>(); ArrayList<Transaction> transactionList = new ArrayList<>();
while(resultatList.moveToNext()) while(resultatList.moveToNext())
{ {

View File

@ -0,0 +1,196 @@
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.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.daimajia.swipe.SwipeLayout;
import com.nauk.moodl.Activities.CurrencyDetailsActivity;
import com.nauk.moodl.DataManagers.CurrencyData.Transaction;
import com.nauk.moodl.DataManagers.DatabaseManager;
import com.nauk.moodl.DataManagers.PreferencesManager;
import com.nauk.moodl.R;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import static java.lang.Math.abs;
/**
* Created by Guitoune on 24/04/2018.
*/
public class TransactionListAdapter extends ArrayAdapter<Transaction> {
private Context context;
public TransactionListAdapter(Context context, ArrayList<Transaction> transactions)
{
super(context, android.R.layout.simple_list_item_1, transactions);
this.context = context;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Transaction transaction = getItem(position);
if(convertView == null)
{
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);
TextView dateTxtView = convertView.findViewById(R.id.purchaseDate);
amountTxtView.setText(String.valueOf(transaction.getAmount()));
valueTxtView.setText(numberConformer(transaction.getPurchasedPrice() * transaction.getAmount()));
dateTxtView.setText(getDate(transaction.getTimestamp()));
LinearLayout deleteLayout = convertView.findViewById(R.id.deleteTransactionLayout);
deleteLayout.setTag(transaction.getTransactionId());
deleteLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PreferencesManager preferencesManager = new PreferencesManager(context);
DatabaseManager databaseManager = new DatabaseManager(context);
preferencesManager.setMustUpdateSummary(true);
databaseManager.deleteTransactionFromId(Integer.parseInt(view.getTag().toString()));
collapse((View) view.getParent().getParent().getParent().getParent());
}
});
setupSwipeView(convertView);
return convertView;
}
private static void collapse(final View v) {
final int initialHeight = v.getMeasuredHeight();
Animation a = new Animation()
{
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if(interpolatedTime == 1){
v.setVisibility(View.GONE);
}else{
v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
v.requestLayout();
}
}
@Override
public boolean willChangeBounds() {
return true;
}
};
// 1dp/ms
a.setDuration((int)(initialHeight / v.getContext().getResources().getDisplayMetrics().density));
v.startAnimation(a);
}
private void setupSwipeView(View view)
{
SwipeLayout swipeLayout = view.findViewById(R.id.swipeLayout);
//set show mode.
swipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown);
//add drag edge.(If the BottomView has 'layout_gravity' attribute, this line is unnecessary)
swipeLayout.addDrag(SwipeLayout.DragEdge.Left, view.findViewById(R.id.bottom_wrapper));
swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
@Override
public void onClose(SwipeLayout layout) {
//when the SurfaceView totally cover the BottomView.
}
@Override
public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
//you are swiping.
}
@Override
public void onStartOpen(SwipeLayout layout) {
}
@Override
public void onOpen(SwipeLayout layout) {
//when the BottomView totally show.
}
@Override
public void onStartClose(SwipeLayout layout) {
}
@Override
public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {
//when user's hand released.
}
});
}
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";
}
}
private String numberConformer(double number)
{
String str;
if(abs(number) > 1)
{
str = String.format( Locale.UK, "%.2f", number).replaceAll("\\.?0*$", "");
}
else
{
str = String.format( Locale.UK, "%.4f", number).replaceAll("\\.?0*$", "");
}
if(!str.equals("Infinity"))
{
int counter = 0;
int i = str.indexOf(".");
if(i <= 0)
{
i = str.length();
}
for(i -= 1; i > 0; i--)
{
counter++;
if(counter == 3)
{
str = str.substring(0, i) + " " + str.substring(i, str.length());
counter = 0;
}
}
}
return str;
}
}

View File

@ -600,18 +600,18 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:elevation="@dimen/cardview_elevation"/> android:elevation="@dimen/cardview_elevation"/>
<ScrollView <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_weight="0.5"> android:layout_weight="0.5">
<LinearLayout <ListView
android:id="@+id/listTransactions" android:id="@+id/listTransactions"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content"
android:orientation="vertical"/> android:orientation="vertical"/>
</ScrollView> </LinearLayout>
<TextView <TextView
android:text="Trade history" android:text="Trade history"

View File

@ -4,107 +4,100 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"> android:orientation="vertical">
<android.support.v7.widget.CardView <com.daimajia.swipe.SwipeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="50dp"
android:background="@drawable/rounded_corners"> android:id="@+id/swipeLayout">
<!-- Bottom View Start-->
<LinearLayout
android:id="@+id/bottom_wrapper"
android:layout_width="100dp"
android:weightSum="1"
android:layout_height="match_parent"
android:orientation="horizontal">
<!--What you want to show-->
<com.daimajia.swipe.SwipeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/swipeLayout">
<!-- Bottom View Start-->
<LinearLayout <LinearLayout
android:id="@+id/bottom_wrapper" android:layout_width="wrap_content"
android:layout_width="100dp" android:layout_weight="0.5"
android:weightSum="1"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="horizontal"> android:layout_gravity="center"
<!--What you want to show--> android:gravity="center"
android:background="@color/green">
<LinearLayout <ImageView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_weight="0.5" android:layout_height="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center" android:layout_gravity="center"
android:gravity="center" android:background="@drawable/ic_edit_white_24dp"/>
android:background="@color/green">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/ic_edit_white_24dp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/deleteTransactionLayout"
android:layout_width="wrap_content"
android:layout_weight="0.5"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:background="@color/red">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/ic_delete_white_24dp"/>
</LinearLayout>
</LinearLayout> </LinearLayout>
<!-- Bottom View End-->
<!-- Surface View Start -->
<LinearLayout <LinearLayout
android:padding="5dp" android:id="@+id/deleteTransactionLayout"
android:background="#ffffff" android:layout_width="wrap_content"
android:layout_width="match_parent" android:layout_weight="0.5"
android:layout_height="match_parent"> android:layout_height="match_parent"
<!--What you want to show in SurfaceView--> android:layout_gravity="center"
<LinearLayout android:gravity="center"
android:layout_width="match_parent" android:background="@color/red">
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="vertical">
<TextView <ImageView
android:id="@+id/amountPurchased" android:layout_width="wrap_content"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:layout_gravity="center"
android:textSize="15dp" android:background="@drawable/ic_delete_white_24dp"/>
android:text="Amount"/>
<TextView
android:id="@+id/puchasedValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
android:text="Value"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="vertical">
<TextView
android:id="@+id/purchaseDate"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Date"
android:gravity="right"/>
</LinearLayout>
</LinearLayout> </LinearLayout>
<!-- Surface View End -->
</com.daimajia.swipe.SwipeLayout>
</android.support.v7.widget.CardView> </LinearLayout>
<!-- Bottom View End-->
<!-- Surface View Start -->
<LinearLayout
android:padding="5dp"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--What you want to show in SurfaceView-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="vertical">
<TextView
android:id="@+id/amountPurchased"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:text="Amount"/>
<TextView
android:id="@+id/puchasedValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
android:text="Value"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="vertical">
<TextView
android:id="@+id/purchaseDate"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Date"
android:gravity="right"/>
</LinearLayout>
</LinearLayout>
<!-- Surface View End -->
</com.daimajia.swipe.SwipeLayout>
</LinearLayout> </LinearLayout>