From 680840b26254905c308286089c69274bd951e014 Mon Sep 17 00:00:00 2001 From: Tanguy Herbron Date: Fri, 27 Apr 2018 16:25:51 +0200 Subject: [PATCH] Add edit transaction option in details activity --- .../Activities/CurrencyDetailsActivity.java | 11 +--- .../Activities/RecordTransactionActivity.java | 55 +++++++++++++++---- .../CurrencyData/Transaction.java | 12 +++- .../moodl/DataManagers/DatabaseManager.java | 40 +++++++++++++- .../TransactionListAdapter.java | 23 +++++++- .../res/layout/custom_transaction_row.xml | 1 + .../fragment_marketcap_homeactivity.xml | 3 +- .../layout/fragment_summary_homeactivity.xml | 28 +++++----- .../fragment_watchlist_homeactivity.xml | 2 +- app/src/main/res/values/dimens.xml | 4 +- 10 files changed, 135 insertions(+), 44 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 4478a55..db08241 100644 --- a/app/src/main/java/com/nauk/moodl/Activities/CurrencyDetailsActivity.java +++ b/app/src/main/java/com/nauk/moodl/Activities/CurrencyDetailsActivity.java @@ -21,12 +21,9 @@ 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.ViewParent; import android.view.animation.Animation; import android.view.animation.Transformation; import android.widget.AbsListView; @@ -37,8 +34,6 @@ import android.widget.ProgressBar; import android.widget.TextView; import android.widget.ViewFlipper; -import com.binance.api.client.domain.account.Trade; -import com.daimajia.swipe.SwipeLayout; import com.github.mikephil.charting.charts.BarChart; import com.github.mikephil.charting.charts.CandleStickChart; import com.github.mikephil.charting.charts.LineChart; @@ -64,11 +59,9 @@ 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; -import java.util.HashMap; import java.util.List; import java.util.Locale; @@ -142,7 +135,7 @@ public class CurrencyDetailsActivity extends AppCompatActivity { public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: - NavUtils.navigateUpFromSameTask(this); + finish(); return true; } return super.onOptionsItemSelected(item); @@ -983,7 +976,7 @@ public class CurrencyDetailsActivity extends AppCompatActivity { Looper.prepare(); } - ArrayList transactionList = databaseManager.getCurrencyTransactions(currency.getSymbol()); + ArrayList transactionList = databaseManager.getCurrencyTransactionsForSymbol(currency.getSymbol()); drawTransactionList(transactionList); return null; diff --git a/app/src/main/java/com/nauk/moodl/Activities/RecordTransactionActivity.java b/app/src/main/java/com/nauk/moodl/Activities/RecordTransactionActivity.java index 2652630..a57deec 100644 --- a/app/src/main/java/com/nauk/moodl/Activities/RecordTransactionActivity.java +++ b/app/src/main/java/com/nauk/moodl/Activities/RecordTransactionActivity.java @@ -3,12 +3,12 @@ package com.nauk.moodl.Activities; import android.content.Intent; import android.os.Bundle; import android.support.design.widget.TextInputLayout; +import android.support.v4.app.NavUtils; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; -import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.DatePicker; @@ -17,6 +17,7 @@ import android.widget.TextView; import android.widget.TimePicker; import com.nauk.moodl.DataManagers.CurrencyData.Currency; +import com.nauk.moodl.DataManagers.CurrencyData.Transaction; import com.nauk.moodl.DataManagers.DatabaseManager; import com.nauk.moodl.DataManagers.PreferencesManager; import com.nauk.moodl.R; @@ -42,6 +43,7 @@ public class RecordTransactionActivity extends AppCompatActivity { private PreferencesManager preferenceManager; private EditText purchasedPriceEditText; private Currency currency; + private int transactionId; @Override public boolean onCreateOptionsMenu(Menu menu) { @@ -63,13 +65,25 @@ public class RecordTransactionActivity extends AppCompatActivity { amount *= -1; } - databaseManager.addCurrencyToManualCurrency(symbol, amount, calendar.getTime(), purchasedPrice, fees); + if(transactionId != -1) + { + databaseManager.updateTransactionWithId(transactionId, amount, calendar.getTime(), purchasedPrice, fees); + } + else + { + databaseManager.addCurrencyToManualCurrency(symbol, amount, calendar.getTime(), purchasedPrice, fees); + } + preferenceManager.setMustUpdateSummary(true); Intent intent = new Intent(RecordTransactionActivity.this, HomeActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); startActivity(intent); finish(); break; + case android.R.id.home: + //NavUtils.navigateUpFromSameTask(this); + finish(); + break; default: break; } @@ -82,17 +96,11 @@ public class RecordTransactionActivity extends AppCompatActivity { setContentView(R.layout.activity_record_transaction); Intent intent = getIntent(); - coin = intent.getStringExtra("coin"); - symbol = intent.getStringExtra("symbol"); - - setTitle("Add " + coin + " transaction"); sdf = new SimpleDateFormat(" HH:mm dd/MM/yyyy"); calendar = Calendar.getInstance(); - currency = new Currency(coin, symbol); - databaseManager = new DatabaseManager(this); preferenceManager = new PreferencesManager(this); @@ -106,10 +114,33 @@ public class RecordTransactionActivity extends AppCompatActivity { sellButton = findViewById(R.id.sellButton); transferButton = findViewById(R.id.transfertButton); - //purchasedPrice.setText(); - purchaseDate.setText(sdf.format(calendar.getTime())); - symbolTxtView.setText(symbol); - feesTxtView.setText(String.valueOf(0)); + coin = intent.getStringExtra("coin"); + Log.d("moodl", "1" + coin); + symbol = intent.getStringExtra("symbol"); + transactionId = intent.getIntExtra("transactionId", -1); + + if(transactionId != -1) + { + setTitle("Edit " + coin + " transaction"); + + DatabaseManager databaseManager = new DatabaseManager(this); + Transaction transaction = databaseManager.getCurrencyTransactionById(transactionId); + + symbolTxtView.setText(transaction.getSymbol()); + amountTxtView.setText(String.valueOf(transaction.getAmount())); + purchaseDate.setText(sdf.format(transaction.getTimestamp())); + feesTxtView.setText(String.valueOf(transaction.getFees())); + } + else + { + setTitle("Add " + coin + " transaction"); + + purchaseDate.setText(sdf.format(calendar.getTime())); + symbolTxtView.setText(symbol); + feesTxtView.setText(String.valueOf(0)); + } + + currency = new Currency(coin, symbol); purchasedDateLayout.setOnClickListener(new View.OnClickListener() { @Override diff --git a/app/src/main/java/com/nauk/moodl/DataManagers/CurrencyData/Transaction.java b/app/src/main/java/com/nauk/moodl/DataManagers/CurrencyData/Transaction.java index cef5867..8d80245 100644 --- a/app/src/main/java/com/nauk/moodl/DataManagers/CurrencyData/Transaction.java +++ b/app/src/main/java/com/nauk/moodl/DataManagers/CurrencyData/Transaction.java @@ -11,15 +11,17 @@ public class Transaction { private double amount; private long timestamp; private double purchasedPrice; + private double fees; private boolean isMined; - public Transaction(int transactionId, String symbol, double amount, long timestamp, double purchasedPrice) + public Transaction(int transactionId, String symbol, double amount, long timestamp, double purchasedPrice, double fees) { this.transactionId = transactionId; this.symbol = symbol; this.amount = amount; this.timestamp = timestamp; this.purchasedPrice = purchasedPrice; + this.fees = fees; } public int getTransactionId() { @@ -60,4 +62,12 @@ public class Transaction { { return purchasedPrice; } + + public double getFees() { + return fees; + } + + public void setFees(double fees) { + this.fees = fees; + } } diff --git a/app/src/main/java/com/nauk/moodl/DataManagers/DatabaseManager.java b/app/src/main/java/com/nauk/moodl/DataManagers/DatabaseManager.java index 5f9bad3..43a70ee 100644 --- a/app/src/main/java/com/nauk/moodl/DataManagers/DatabaseManager.java +++ b/app/src/main/java/com/nauk/moodl/DataManagers/DatabaseManager.java @@ -162,9 +162,43 @@ public class DatabaseManager extends SQLiteOpenHelper{ return currencyList; } - public ArrayList getCurrencyTransactions(String symbol) + public void updateTransactionWithId(int transactionId, double amount, Date time, double purchasedPrice, double fees) { - String searchQuerry = "SELECT * FROM " + TABLE_MANUAL_CURRENCIES + " WHERE symbol='" + symbol.toUpperCase() + "'"; + SQLiteDatabase db = this.getWritableDatabase(); + ContentValues cv = new ContentValues(); + + cv.put(KEY_CURRENCY_BALANCE, amount); + cv.put(KEY_CURRENCY_DATE, time.getTime()); + cv.put(KEY_CURRENCY_PURCHASED_PRICE, purchasedPrice); + cv.put(KEY_CURRENCY_FEES, fees); + + db.update(TABLE_MANUAL_CURRENCIES, cv, KEY_CURRENCY_ID + "=" + transactionId, null); + + } + + public Transaction getCurrencyTransactionById(int id) + { + String searchQuerry = "SELECT * FROM " + TABLE_MANUAL_CURRENCIES + " WHERE " + KEY_CURRENCY_ID + "='" + id + "'"; + SQLiteDatabase db = this.getWritableDatabase(); + Cursor resultatList = db.rawQuery(searchQuerry, null); + + Transaction transaction = null; + + if(resultatList.moveToFirst()) + { + transaction = new Transaction(resultatList.getInt(0), resultatList.getString(1), resultatList.getDouble(3), resultatList.getLong(4), resultatList.getLong(5), resultatList.getDouble(7)); + } + + resultatList.close(); + + db.close(); + + return transaction; + } + + public ArrayList getCurrencyTransactionsForSymbol(String symbol) + { + String searchQuerry = "SELECT * FROM " + TABLE_MANUAL_CURRENCIES + " WHERE " + KEY_CURRENCY_SYMBOL + "='" + symbol.toUpperCase() + "'"; SQLiteDatabase db = this.getWritableDatabase(); Cursor resultatList = db.rawQuery(searchQuerry, null); @@ -172,7 +206,7 @@ public class DatabaseManager extends SQLiteOpenHelper{ while(resultatList.moveToNext()) { - transactionList.add(new Transaction(resultatList.getInt(0), resultatList.getString(1), resultatList.getDouble(3), resultatList.getLong(4), resultatList.getLong(5))); + transactionList.add(new Transaction(resultatList.getInt(0), resultatList.getString(1), resultatList.getDouble(3), resultatList.getLong(4), resultatList.getLong(5), resultatList.getDouble(7))); } resultatList.close(); diff --git a/app/src/main/java/com/nauk/moodl/LayoutManagers/TransactionListAdapter.java b/app/src/main/java/com/nauk/moodl/LayoutManagers/TransactionListAdapter.java index 4e1ac51..c89652e 100644 --- a/app/src/main/java/com/nauk/moodl/LayoutManagers/TransactionListAdapter.java +++ b/app/src/main/java/com/nauk/moodl/LayoutManagers/TransactionListAdapter.java @@ -1,8 +1,11 @@ package com.nauk.moodl.LayoutManagers; +import android.app.Activity; import android.content.Context; +import android.content.Intent; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -14,6 +17,8 @@ import android.widget.TextView; import com.daimajia.swipe.SwipeLayout; import com.nauk.moodl.Activities.CurrencyDetailsActivity; +import com.nauk.moodl.Activities.CurrencySelectionActivity; +import com.nauk.moodl.Activities.RecordTransactionActivity; import com.nauk.moodl.DataManagers.CurrencyData.Transaction; import com.nauk.moodl.DataManagers.DatabaseManager; import com.nauk.moodl.DataManagers.PreferencesManager; @@ -43,7 +48,7 @@ public class TransactionListAdapter extends ArrayAdapter { @NonNull @Override public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { - Transaction transaction = getItem(position); + final Transaction transaction = getItem(position); if(convertView == null) { @@ -72,6 +77,22 @@ public class TransactionListAdapter extends ArrayAdapter { } }); + LinearLayout editLayout = convertView.findViewById(R.id.editTransactionLayout); + editLayout.setTag(transaction.getTransactionId()); + + editLayout.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + String name = ((Activity) context).getTitle().toString(); + name = name.substring(1, name.indexOf("|") - 1); + Intent intent = new Intent(context, RecordTransactionActivity.class); + intent.putExtra("coin", name); + intent.putExtra("symbol", transaction.getSymbol()); + intent.putExtra("transactionId", transaction.getTransactionId()); + context.startActivity(intent); + } + }); + setupSwipeView(convertView); return convertView; diff --git a/app/src/main/res/layout/custom_transaction_row.xml b/app/src/main/res/layout/custom_transaction_row.xml index 92314cd..f10b314 100644 --- a/app/src/main/res/layout/custom_transaction_row.xml +++ b/app/src/main/res/layout/custom_transaction_row.xml @@ -18,6 +18,7 @@ - - + + + android:layout_width="wrap_content" + android:layout_height="match_parent"> + 12sp 150dp - 150dp + 172dp 16dp 16dp 6dp @@ -21,4 +21,6 @@ 8dp 56dp 70dp + + 15dp