diff --git a/app/src/main/java/com/herbron/moodl/Activities/RecordTransactionFragments/BuyFragment.java b/app/src/main/java/com/herbron/moodl/Activities/RecordTransactionFragments/BuyFragment.java index 71c460c..15ed23b 100644 --- a/app/src/main/java/com/herbron/moodl/Activities/RecordTransactionFragments/BuyFragment.java +++ b/app/src/main/java/com/herbron/moodl/Activities/RecordTransactionFragments/BuyFragment.java @@ -10,12 +10,14 @@ import android.support.design.widget.TextInputEditText; import android.support.v7.widget.AppCompatButton; import android.text.Editable; import android.text.TextWatcher; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.DatePicker; import android.widget.Spinner; +import android.widget.Switch; import android.widget.TimePicker; import com.herbron.moodl.Activities.HomeActivity; @@ -46,6 +48,7 @@ public class BuyFragment extends CustomRecordFragment { private AppCompatButton saveBuyButton; private static Spinner feesCurrencySpinner; private static View view; + private Switch deductHoldingsSwitch; private ArrayAdapter currencyFeeAdapter; @@ -131,6 +134,105 @@ public class BuyFragment extends CustomRecordFragment { } }; + private TextWatcher feesTextWatcher = new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + totalValueEditText.removeTextChangedListener(totalValueTextWatcher); + amoutEditText.removeTextChangedListener(amountTextWatcher); + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + + if((isFieldCorrectlyFilled(amoutEditText, false) || isFieldCorrectlyFilled(totalValueEditText, false)) && isFieldCorrectlyFilled(buyPriceEditText, false)) + { + double amount = Double.parseDouble(amoutEditText.getText().toString()); + double purchasePrice = Double.parseDouble(buyPriceEditText.getText().toString()); + double fees; + double totalValue = Double.parseDouble(totalValueEditText.getText().toString()); + String feeCurrency; + + if(isAmountLastUpdated) + { + totalValue = amount * purchasePrice; + } + else + { + amount = totalValue / purchasePrice; + } + + if(fees_editText.getText().toString().equals("0") || (start == 0 && before == 1 && count == 0)) + { + if(isAmountLastUpdated) + { + totalValueEditText.setText(String.valueOf(amount * purchasePrice)); + } + else + { + amoutEditText.setText(String.valueOf(totalValue / purchasePrice)); + } + } + else + { + + if(feesCurrencySpinner.getSelectedItemPosition() < 2) + { + feeCurrency = fragmentPair.getFrom(); + } + else + { + feeCurrency = fragmentPair.getTo(); + } + + fees = getFees(feeCurrency, amount, purchasePrice); + + if(feesCurrencySpinner.getSelectedItemPosition() % 2 == 0) + { + if(isAmountLastUpdated) + { + totalValueEditText.setText(String.valueOf(totalValue + fees)); + } + else + { + amoutEditText.setText(String.valueOf(amount - (fees / purchasePrice))); + } + } + else + { + if(fragmentCurrency.getSymbol().equals(feeCurrency)) + { + if(isAmountLastUpdated) + { + totalValueEditText.setText(String.valueOf(totalValue + (fees * purchasePrice))); + } + else + { + amoutEditText.setText(String.valueOf((totalValue / purchasePrice) - fees)); + } + } + else + { + if(isAmountLastUpdated) + { + totalValueEditText.setText(String.valueOf(totalValue + fees)); + } + else + { + amoutEditText.setText(String.valueOf((totalValue - fees) / purchasePrice)); + } + } + } + } + } + } + + @Override + public void afterTextChanged(Editable s) { + totalValueEditText.addTextChangedListener(totalValueTextWatcher); + amoutEditText.addTextChangedListener(amountTextWatcher); + } + }; + @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { @@ -158,7 +260,7 @@ public class BuyFragment extends CustomRecordFragment { DatabaseManager databaseManager = new DatabaseManager(context); transaction = databaseManager.getCurrencyTransactionById(transactionId); - if(transaction.getType().equals("b")) + if(transaction.getType() == null || transaction.getType().equals("b")) { fillFields(); } @@ -179,10 +281,8 @@ public class BuyFragment extends CustomRecordFragment { private void initializeViewElements() { totalValueEditText = view.findViewById(R.id.totalValue_editText_buy); - totalValueEditText.addTextChangedListener(totalValueTextWatcher); amoutEditText = view.findViewById(R.id.amount_editText_buy); - amoutEditText.addTextChangedListener(amountTextWatcher); buyPriceEditText = view.findViewById(R.id.buyPrice_editText); buyDateEditText = view.findViewById(R.id.buyDate_editText); @@ -200,6 +300,8 @@ public class BuyFragment extends CustomRecordFragment { currencyFeeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); feesCurrencySpinner.setAdapter(currencyFeeAdapter); + deductHoldingsSwitch = view.findViewById(R.id.deductHoldingsBuy); + if(fragmentPair != null) { updateAdapter(); @@ -246,7 +348,8 @@ public class BuyFragment extends CustomRecordFragment { , "" , fragmentExchange.getName() , "b" - , feesCurrencySpinner.getSelectedItemPosition() % 2 == 0 ? "p" : "f"); + , feesCurrencySpinner.getSelectedItemPosition() % 2 == 0 ? "p" : "f" + , deductHoldingsSwitch.isChecked()); Intent intent = new Intent(getActivity(), HomeActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); @@ -265,7 +368,8 @@ public class BuyFragment extends CustomRecordFragment { , "" , fragmentExchange.getName() ,"b" - , feesCurrencySpinner.getSelectedItemPosition() % 2 == 0 ? "p" : "f"); + , feesCurrencySpinner.getSelectedItemPosition() % 2 == 0 ? "p" : "f" + , deductHoldingsSwitch.isChecked()); } getActivity().finish(); @@ -274,108 +378,19 @@ public class BuyFragment extends CustomRecordFragment { }); fees_editText = view.findViewById(R.id.fees_editText_buy); - fees_editText.addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - totalValueEditText.removeTextChangedListener(totalValueTextWatcher); - amoutEditText.removeTextChangedListener(amountTextWatcher); - } - - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - - if((isFieldCorrectlyFilled(amoutEditText, false) || isFieldCorrectlyFilled(totalValueEditText, false)) && isFieldCorrectlyFilled(buyPriceEditText, false)) - { - double amount = Double.parseDouble(amoutEditText.getText().toString()); - double purchasePrice = Double.parseDouble(buyPriceEditText.getText().toString()); - double fees; - double totalValue = Double.parseDouble(totalValueEditText.getText().toString()); - String feeCurrency; - - if(isAmountLastUpdated) - { - totalValue = amount * purchasePrice; - } - else - { - amount = totalValue / purchasePrice; - } - - if(fees_editText.getText().toString().equals("0") || (start == 0 && before == 1 && count == 0)) - { - if(isAmountLastUpdated) - { - totalValueEditText.setText(String.valueOf(amount * purchasePrice)); - } - else - { - amoutEditText.setText(String.valueOf(totalValue / purchasePrice)); - } - } - else - { - - if(feesCurrencySpinner.getSelectedItemPosition() < 2) - { - feeCurrency = fragmentPair.getFrom(); - } - else - { - feeCurrency = fragmentPair.getTo(); - } - - fees = getFees(feeCurrency, amount, purchasePrice); - - if(feesCurrencySpinner.getSelectedItemPosition() % 2 == 0) - { - if(isAmountLastUpdated) - { - totalValueEditText.setText(String.valueOf(totalValue + fees)); - } - else - { - amoutEditText.setText(String.valueOf(amount - (fees / purchasePrice))); - } - } - else - { - if(fragmentCurrency.getSymbol().equals(feeCurrency)) - { - if(isAmountLastUpdated) - { - totalValueEditText.setText(String.valueOf(totalValue + (fees * purchasePrice))); - } - else - { - amoutEditText.setText(String.valueOf((totalValue / purchasePrice) - fees)); - } - } - else - { - if(isAmountLastUpdated) - { - totalValueEditText.setText(String.valueOf(totalValue + fees)); - } - else - { - amoutEditText.setText(String.valueOf((totalValue - fees) / purchasePrice)); - } - } - } - } - } - } - - @Override - public void afterTextChanged(Editable s) { - totalValueEditText.addTextChangedListener(totalValueTextWatcher); - amoutEditText.addTextChangedListener(amountTextWatcher); - } - }); note_editText = view.findViewById(R.id.note_editText_buy); checkCallingIntent(); + + setupTextWatchers(); + } + + private void setupTextWatchers() + { + totalValueEditText.addTextChangedListener(totalValueTextWatcher); + amoutEditText.addTextChangedListener(amountTextWatcher); + fees_editText.addTextChangedListener(feesTextWatcher); } private double getFees(String feeCurrency, double amount, double purchasedPrice) diff --git a/app/src/main/java/com/herbron/moodl/Activities/RecordTransactionFragments/SellFragment.java b/app/src/main/java/com/herbron/moodl/Activities/RecordTransactionFragments/SellFragment.java index 5360546..d18ca60 100644 --- a/app/src/main/java/com/herbron/moodl/Activities/RecordTransactionFragments/SellFragment.java +++ b/app/src/main/java/com/herbron/moodl/Activities/RecordTransactionFragments/SellFragment.java @@ -16,6 +16,7 @@ import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.DatePicker; import android.widget.Spinner; +import android.widget.Switch; import android.widget.TimePicker; import com.herbron.moodl.Activities.HomeActivity; @@ -46,6 +47,7 @@ public class SellFragment extends CustomRecordFragment { private AppCompatButton saveSellButton; private static Spinner feesCurrencySpinner; private static View view; + private Switch deductHoldingsSwitch; private ArrayAdapter currencyFeeAdapter; @@ -177,6 +179,8 @@ public class SellFragment extends CustomRecordFragment { updateAdapter(); } + deductHoldingsSwitch = view.findViewById(R.id.deductHoldingsSell); + saveSellButton = view.findViewById(R.id.saveSellButton); saveSellButton.setOnClickListener(new View.OnClickListener() { @Override @@ -218,7 +222,8 @@ public class SellFragment extends CustomRecordFragment { , "" , fragmentExchange.getName() , "s" - , feesCurrencySpinner.getSelectedItemPosition() % 2 == 0 ? "p" : "f"); + , feesCurrencySpinner.getSelectedItemPosition() % 2 == 0 ? "p" : "f" + , deductHoldingsSwitch.isChecked()); Intent intent = new Intent(getActivity(), HomeActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); @@ -237,7 +242,8 @@ public class SellFragment extends CustomRecordFragment { , "" , fragmentExchange.getName() ,"s" - , feesCurrencySpinner.getSelectedItemPosition() % 2 == 0 ? "p" : "f"); + , feesCurrencySpinner.getSelectedItemPosition() % 2 == 0 ? "p" : "f" + , deductHoldingsSwitch.isChecked()); } getActivity().finish(); @@ -386,7 +392,7 @@ public class SellFragment extends CustomRecordFragment { DatabaseManager databaseManager = new DatabaseManager(context); transaction = databaseManager.getCurrencyTransactionById(transactionId); - if(transaction.getType().equals("b")) + if(transaction.getType().equals("s")) { fillFields(); } diff --git a/app/src/main/java/com/herbron/moodl/Activities/RecordTransactionFragments/TransferFragment.java b/app/src/main/java/com/herbron/moodl/Activities/RecordTransactionFragments/TransferFragment.java index 73dc8b0..d057a5b 100644 --- a/app/src/main/java/com/herbron/moodl/Activities/RecordTransactionFragments/TransferFragment.java +++ b/app/src/main/java/com/herbron/moodl/Activities/RecordTransactionFragments/TransferFragment.java @@ -9,7 +9,6 @@ import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.design.widget.TextInputEditText; -import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -18,13 +17,16 @@ import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.DatePicker; import android.widget.Spinner; +import android.widget.Switch; import android.widget.TimePicker; +import com.herbron.moodl.Activities.HomeActivity; import com.herbron.moodl.CustomLayouts.CustomRecordFragment; import com.herbron.moodl.DataManagers.CurrencyData.Currency; import com.herbron.moodl.DataManagers.CurrencyData.Transaction; import com.herbron.moodl.DataManagers.DatabaseManager; import com.herbron.moodl.DataManagers.ExchangeManager.Exchange; +import com.herbron.moodl.DataManagers.PreferencesManager; import com.herbron.moodl.R; import java.text.SimpleDateFormat; @@ -43,7 +45,10 @@ public class TransferFragment extends CustomRecordFragment { private Spinner toSpinner; private Spinner feeSpinner; + private Switch deductHoldingsSwitch; + private Button saveButton; + private int transactionId; private Transaction transaction; @@ -72,22 +77,49 @@ public class TransferFragment extends CustomRecordFragment { { if(isTransactionPossible()) { + PreferencesManager preferencesManager = new PreferencesManager(getContext()); DatabaseManager databaseManager = new DatabaseManager(getContext()); double amount = Double.valueOf(amountEditText.getText().toString()); double fees = getFees(); - databaseManager.addTransaction(fragmentCurrency.getSymbol() - , amount - , calendar.getTime() - , 0 - , fees - , noteEditText.getText().toString() - , "" - , fragmentCurrency.getSymbol() - , getDestination() - , getSource() - , "t" - , feeSpinner.getSelectedItemPosition() == 0 ? "p" : "f"); + if(transactionId == -1) + { + databaseManager.addTransaction(fragmentCurrency.getSymbol() + , amount + , calendar.getTime() + , 0 + , fees + , noteEditText.getText().toString() + , "" + , fragmentCurrency.getSymbol() + , getDestination() + , getSource() + , "t" + , feeSpinner.getSelectedItemPosition() == 0 ? "p" : "f" + , deductHoldingsSwitch.isChecked()); + + Intent intent = new Intent(getActivity(), HomeActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); + startActivity(intent); + } + else + { + databaseManager.updateTransactionWithId(transactionId + , amount + , calendar.getTime() + , 0 + , fees + , noteEditText.getText().toString() + , "" + , fragmentCurrency.getSymbol() + , getDestination() + , getSource() + ,"t" + , feeSpinner.getSelectedItemPosition() == 0 ? "p" : "f" + , deductHoldingsSwitch.isChecked()); + } + + preferencesManager.setMustUpdateSummary(true); getActivity().finish(); } @@ -146,6 +178,8 @@ public class TransferFragment extends CustomRecordFragment { saveButton = view.findViewById(R.id.saveTransferButton); saveButton.setOnClickListener(saveButtonClickListener); + deductHoldingsSwitch = view.findViewById(R.id.deductHoldingsTransfer); + checkCallingIntent(); } @@ -159,7 +193,7 @@ public class TransferFragment extends CustomRecordFragment { DatabaseManager databaseManager = new DatabaseManager(getContext()); transaction = databaseManager.getCurrencyTransactionById(transactionId); - if(transaction.getType().equals("t")) + if(transaction.getType() != null && transaction.getType().equals("t")) { fillFields(); } @@ -168,8 +202,61 @@ public class TransferFragment extends CustomRecordFragment { private void fillFields() { + setupFromSpinner(); + setupToSpinner(); amountEditText.setText(String.valueOf(transaction.getAmount())); - //Fill other fields + calendar.setTimeInMillis(transaction.getTimestamp()); + transferDateEditText.setText(sdf.format(calendar.getTime())); + feesEditText.setText(String.format(Locale.UK, "%f", transaction.getFees())); + noteEditText.setText(transaction.getNote()); + feeSpinner.setSelection(transaction.getFeeFormat().equals("p") ? 0 : 1); + } + + private void setupFromSpinner() + { + switch (transaction.getSource()) + { + case EXCHANGE_CODE: + fromSpinner.setSelection(0); + break; + case WALLET_CODE: + fromSpinner.setSelection(1); + break; + case MINING_CODE: + fromSpinner.setSelection(2); + break; + case ELSE_WALLET_CODE: + fromSpinner.setSelection(3); + break; + case AIRDROP_CODE: + fromSpinner.setSelection(4); + break; + case UNKNOWN_CODE: + fromSpinner.setSelection(5); + break; + case FORK_CODE: + fromSpinner.setSelection(6); + break; + } + } + + private void setupToSpinner() + { + switch (transaction.getDestination()) + { + case EXCHANGE_CODE: + toSpinner.setSelection(0); + break; + case WALLET_CODE: + toSpinner.setSelection(1); + break; + case ELSE_WALLET_CODE: + toSpinner.setSelection(2); + break; + case UNKNOWN_CODE: + toSpinner.setSelection(3); + break; + } } private void setupSpinnesr() diff --git a/app/src/main/java/com/herbron/moodl/DataManagers/CurrencyData/Transaction.java b/app/src/main/java/com/herbron/moodl/DataManagers/CurrencyData/Transaction.java index 9155857..4c927b6 100644 --- a/app/src/main/java/com/herbron/moodl/DataManagers/CurrencyData/Transaction.java +++ b/app/src/main/java/com/herbron/moodl/DataManagers/CurrencyData/Transaction.java @@ -19,8 +19,9 @@ public class Transaction { private String source; private String destination; private String type; + private boolean isDeducted; - public Transaction(int transactionId, String symbol, String symPair, double amount, long timestamp, double purchasedPrice, double fees, String note, String feeCurrency, String source, String destination, String type, String feeFormat) + public Transaction(int transactionId, String symbol, String symPair, double amount, long timestamp, double purchasedPrice, double fees, String note, String feeCurrency, String source, String destination, String type, String feeFormat, boolean isDeducted) { this.transactionId = transactionId; this.symbol = symbol; @@ -35,6 +36,7 @@ public class Transaction { this.destination = destination; this.type = type; this.feeFormat = feeFormat; + this.isDeducted = isDeducted; } public int getTransactionId() { @@ -143,4 +145,12 @@ public class Transaction { public void setFeeFormat(String feeFormat) { this.feeFormat = feeFormat; } + + public boolean isDeducted() { + return isDeducted; + } + + public void setDeducted(boolean deducted) { + isDeducted = deducted; + } } diff --git a/app/src/main/java/com/herbron/moodl/DataManagers/DatabaseManager.java b/app/src/main/java/com/herbron/moodl/DataManagers/DatabaseManager.java index f09b966..9e2d871 100644 --- a/app/src/main/java/com/herbron/moodl/DataManagers/DatabaseManager.java +++ b/app/src/main/java/com/herbron/moodl/DataManagers/DatabaseManager.java @@ -3,6 +3,7 @@ package com.herbron.moodl.DataManagers; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; +import android.database.DatabaseUtils; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; @@ -31,7 +32,7 @@ import java.util.Set; public class DatabaseManager extends SQLiteOpenHelper{ - private static final int DATABASE_VERSION = 16; + private static final int DATABASE_VERSION = 17; private static final String DATABASE_NAME = "mdn.db"; @@ -52,6 +53,7 @@ public class DatabaseManager extends SQLiteOpenHelper{ private static final String KEY_TRANSACTION_FEE_FORMAT = "feeFormat"; private static final String KEY_TRANSACTION_NOTES = "notes"; private static final String KEY_TRANSACTION_TYPE = "transactionType"; + private static final String KEY_TRANSACTION_DEDUCT = "deductHoldings"; private static final String KEY_EXCHANGE_ID = "idExchange"; private static final String KEY_EXCHANGE_NAME = "name"; @@ -87,10 +89,11 @@ public class DatabaseManager extends SQLiteOpenHelper{ + KEY_TRANSACTION_PAIR + " VARCHAR(4)," + KEY_TRANSACTION_FEE_CURRENCY + " VARCHAR(4)," + KEY_TRANSACTION_FEES + " REAL," - + KEY_TRANSACTION_FEE_FORMAT + " VARCHAT(1)," + + KEY_TRANSACTION_FEE_FORMAT + " VARCHAR(1)," + KEY_TRANSACTION_SOURCE + " TEXT," + KEY_TRANSACTION_DESTINATION + " TEXT," - + KEY_TRANSACTION_TYPE + " VARCHAR(1)" + + KEY_TRANSACTION_TYPE + " VARCHAR(1)," + + KEY_TRANSACTION_DEDUCT + " INTEGER" + ");"); db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_EXCHANGE_KEYS + "(" @@ -154,7 +157,7 @@ public class DatabaseManager extends SQLiteOpenHelper{ return false; } - public void updateTransactionWithId(int transactionId, double amount, Date date, double purchasedPrice, double fees, String note, String symbolFrom, String feeCurrency, String destination, String source, String type, String feeFormat) + public void updateTransactionWithId(int transactionId, double amount, Date date, double purchasedPrice, double fees, String note, String symbolFrom, String feeCurrency, String destination, String source, String type, String feeFormat, boolean deductFromHoldings) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues cv = new ContentValues(); @@ -170,12 +173,13 @@ public class DatabaseManager extends SQLiteOpenHelper{ cv.put(KEY_TRANSACTION_DESTINATION, destination); cv.put(KEY_TRANSACTION_TYPE, type); cv.put(KEY_TRANSACTION_FEE_FORMAT, feeFormat); + cv.put(KEY_TRANSACTION_DEDUCT, deductFromHoldings ? 1 : 0); db.update(TABLE_MANUAL_TRANSACTIONS, cv, KEY_TRANSACTION_ID + "=" + transactionId, null); } - public void addTransaction(String symbol, Double amount, Date date, double purchasePrice, double fees, String note, String symbolFrom, String feeCurrency, String destination, String source, String type, String feeFormat) + public void addTransaction(String symbol, Double amount, Date date, double purchasePrice, double fees, String note, String symbolFrom, String feeCurrency, String destination, String source, String type, String feeFormat, boolean deductFromHoldings) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); @@ -192,8 +196,9 @@ public class DatabaseManager extends SQLiteOpenHelper{ values.put(KEY_TRANSACTION_DESTINATION, destination); values.put(KEY_TRANSACTION_TYPE, type); values.put(KEY_TRANSACTION_FEE_FORMAT, feeFormat); + values.put(KEY_TRANSACTION_DEDUCT, deductFromHoldings ? 1 : 0); - Log.d("moodl", "Insert result " + db.insert(TABLE_MANUAL_TRANSACTIONS, null, values)); + db.insert(TABLE_MANUAL_TRANSACTIONS, null, values); db.close(); } @@ -463,19 +468,37 @@ public class DatabaseManager extends SQLiteOpenHelper{ values.put(KEY_TRANSACTION_DATE, DataCrypter.decrypt(context, rawValues.getString(KEY_TRANSACTION_DATE))); values.put(KEY_TRANSACTION_PURCHASE_PRICE, DataCrypter.decrypt(context, rawValues.getString(KEY_TRANSACTION_PURCHASE_PRICE))); values.put(KEY_TRANSACTION_FEES, DataCrypter.decrypt(context, rawValues.getString(KEY_TRANSACTION_FEES))); + values.put(KEY_TRANSACTION_NOTES, DataCrypter.decrypt(context, rawValues.getString(KEY_TRANSACTION_NOTES))); + values.put(KEY_TRANSACTION_PAIR, DataCrypter.decrypt(context, rawValues.getString(KEY_TRANSACTION_PAIR))); + values.put(KEY_TRANSACTION_FEE_CURRENCY, DataCrypter.decrypt(context, rawValues.getString(KEY_TRANSACTION_FEE_CURRENCY))); + values.put(KEY_TRANSACTION_SOURCE, DataCrypter.decrypt(context, rawValues.getString(KEY_TRANSACTION_SOURCE))); + values.put(KEY_TRANSACTION_DESTINATION, DataCrypter.decrypt(context, rawValues.getString(KEY_TRANSACTION_DESTINATION))); + values.put(KEY_TRANSACTION_TYPE, DataCrypter.decrypt(context, rawValues.getString(KEY_TRANSACTION_TYPE))); + values.put(KEY_TRANSACTION_FEE_FORMAT, DataCrypter.decrypt(context, rawValues.getString(KEY_TRANSACTION_FEE_FORMAT))); + values.put(KEY_TRANSACTION_DEDUCT, DataCrypter.decrypt(context, rawValues.getString(KEY_TRANSACTION_DEDUCT))); } else { values.put(KEY_TRANSACTION_SYMBOL, rawValues.getString(KEY_TRANSACTION_SYMBOL)); values.put(KEY_TRANSACTION_AMOUNT, rawValues.getString(KEY_TRANSACTION_AMOUNT)); values.put(KEY_TRANSACTION_DATE, rawValues.getString(KEY_TRANSACTION_DATE)); - values.put(KEY_TRANSACTION_PURCHASE_PRICE, rawValues.getString(KEY_TRANSACTION_PURCHASE_PRICE)); - values.put(KEY_TRANSACTION_FEES, rawValues.getString(KEY_TRANSACTION_FEES)); + values.put(KEY_TRANSACTION_PURCHASE_PRICE, rawValues.getDouble(KEY_TRANSACTION_PURCHASE_PRICE)); + values.put(KEY_TRANSACTION_FEES, rawValues.getDouble(KEY_TRANSACTION_FEES)); + values.put(KEY_TRANSACTION_NOTES, rawValues.getString(KEY_TRANSACTION_NOTES)); + values.put(KEY_TRANSACTION_PAIR, rawValues.getString(KEY_TRANSACTION_PAIR)); + values.put(KEY_TRANSACTION_FEE_CURRENCY, rawValues.getString(KEY_TRANSACTION_FEE_CURRENCY)); + values.put(KEY_TRANSACTION_SOURCE, rawValues.getString(KEY_TRANSACTION_SOURCE)); + values.put(KEY_TRANSACTION_DESTINATION, rawValues.getString(KEY_TRANSACTION_DESTINATION)); + values.put(KEY_TRANSACTION_TYPE, rawValues.getString(KEY_TRANSACTION_TYPE)); + values.put(KEY_TRANSACTION_FEE_FORMAT, rawValues.getString(KEY_TRANSACTION_FEE_FORMAT)); + values.put(KEY_TRANSACTION_DEDUCT, rawValues.getInt(KEY_TRANSACTION_DEDUCT)); } } catch (JSONException e) { Log.d("moodl", "Error while inserting transaction " + e.getMessage()); } + Log.d("mood", "Raw " + rawValues.toString()); + db.insert(TABLE_MANUAL_TRANSACTIONS, null, values); db.close(); } @@ -585,7 +608,7 @@ public class DatabaseManager extends SQLiteOpenHelper{ if(symbol.equals(feeSym)) { currencyList.add(new Currency(resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_SYMBOL)) - , -resultatList.getDouble(resultatList.getColumnIndex(KEY_TRANSACTION_AMOUNT)) + resultatList.getDouble(resultatList.getColumnIndex(KEY_TRANSACTION_FEES)))); + , -resultatList.getDouble(resultatList.getColumnIndex(KEY_TRANSACTION_AMOUNT)) - resultatList.getDouble(resultatList.getColumnIndex(KEY_TRANSACTION_FEES)))); } else { @@ -596,15 +619,26 @@ public class DatabaseManager extends SQLiteOpenHelper{ case "t": if(isBalanceRelated(resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_SOURCE))) && isBalanceRelated(resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_DESTINATION)))) { - currencyList.add(new Currency(resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_SYMBOL)) - , -resultatList.getDouble(resultatList.getColumnIndex(KEY_TRANSACTION_FEES)))); + if(!resultatList.isNull(resultatList.getColumnIndex(KEY_TRANSACTION_DEDUCT))) + { + if(resultatList.getInt(resultatList.getColumnIndex(KEY_TRANSACTION_DEDUCT)) == 1) + { + currencyList.add(new Currency(resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_SYMBOL)) + , -resultatList.getDouble(resultatList.getColumnIndex(KEY_TRANSACTION_AMOUNT)) - resultatList.getDouble(resultatList.getColumnIndex(KEY_TRANSACTION_FEES)))); + } + else + { + currencyList.add(new Currency(resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_SYMBOL)) + , -resultatList.getDouble(resultatList.getColumnIndex(KEY_TRANSACTION_FEES)))); + } + } } else { if(isBalanceRelated(resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_SOURCE)))) { currencyList.add(new Currency(resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_SYMBOL)) - , -resultatList.getDouble(resultatList.getColumnIndex(KEY_TRANSACTION_AMOUNT)) + resultatList.getDouble(resultatList.getColumnIndex(KEY_TRANSACTION_FEES)))); + , -resultatList.getDouble(resultatList.getColumnIndex(KEY_TRANSACTION_AMOUNT)) - resultatList.getDouble(resultatList.getColumnIndex(KEY_TRANSACTION_FEES)))); } else { @@ -656,6 +690,15 @@ public class DatabaseManager extends SQLiteOpenHelper{ if(resultatList.moveToFirst()) { + boolean deduct = false; + + if(!resultatList.isNull(resultatList.getColumnIndex(KEY_TRANSACTION_DEDUCT))) + { + deduct = resultatList.getInt(resultatList.getColumnIndex(KEY_TRANSACTION_DEDUCT)) == 1; + } + + Log.d("moodl", "> " + resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_TYPE))); + transaction = new Transaction(resultatList.getInt(resultatList.getColumnIndex(KEY_TRANSACTION_ID)) , resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_SYMBOL)) , resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_PAIR)) @@ -668,9 +711,12 @@ public class DatabaseManager extends SQLiteOpenHelper{ , resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_SOURCE)) , resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_DESTINATION)) , resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_TYPE)) - , resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_FEE_FORMAT))); + , resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_FEE_FORMAT)) + , deduct); } + Log.d("moodl", "> " + DatabaseUtils.dumpCurrentRowToString(resultatList)); + resultatList.close(); db.close(); @@ -688,6 +734,13 @@ public class DatabaseManager extends SQLiteOpenHelper{ while (resultatList.moveToNext()) { + boolean deduct = false; + + if(!resultatList.isNull(resultatList.getColumnIndex(KEY_TRANSACTION_DEDUCT))) + { + deduct = resultatList.getInt(resultatList.getColumnIndex(KEY_TRANSACTION_DEDUCT)) == 1; + } + transactionList.add(new Transaction(resultatList.getInt(resultatList.getColumnIndex(KEY_TRANSACTION_ID)) , resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_SYMBOL)) , resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_PAIR)) @@ -700,7 +753,8 @@ public class DatabaseManager extends SQLiteOpenHelper{ , resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_SOURCE)) , resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_DESTINATION)) , resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_TYPE)) - , resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_FEE_FORMAT)))); + , resultatList.getString(resultatList.getColumnIndex(KEY_TRANSACTION_FEE_FORMAT)) + , deduct)); } resultatList.close(); diff --git a/app/src/main/res/layout/record_transaction_fragment_buy.xml b/app/src/main/res/layout/record_transaction_fragment_buy.xml index 26f8d38..d883f0b 100644 --- a/app/src/main/res/layout/record_transaction_fragment_buy.xml +++ b/app/src/main/res/layout/record_transaction_fragment_buy.xml @@ -104,6 +104,16 @@ + + + + + + %1$s fixed Sell price Sell date - Date of transfert + Date of transfer This transaction is not valid + Deduct from holdings + Add to holdings