From f32270cc4b022a89220214212f0f41fb66092e6d Mon Sep 17 00:00:00 2001 From: Tanguy Herbron Date: Thu, 16 Aug 2018 02:24:29 +0200 Subject: [PATCH] RecordTransactionActivity fixs - Dynamic amount when filling the total value - Fix fee calcul when using the pair percentage option --- .../BuyFragment.java | 135 ++++++++++-------- .../TransactionListAdapter.java | 2 +- .../CurrencyData/Transaction.java | 12 +- 3 files changed, 80 insertions(+), 69 deletions(-) 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 4b57e86..e7ec16f 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 @@ -20,7 +20,6 @@ import android.widget.Spinner; import android.widget.TimePicker; import com.herbron.moodl.Activities.HomeActivity; -import com.herbron.moodl.Activities.RecordTransactionActivity; import com.herbron.moodl.CustomLayouts.CustomRecordFragment; import com.herbron.moodl.DataManagers.CurrencyData.Currency; import com.herbron.moodl.DataManagers.CurrencyData.Transaction; @@ -65,6 +64,70 @@ public class BuyFragment extends CustomRecordFragment { private int transactionId; private Transaction transaction; + private TextWatcher amountTextWatcher = new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + totalValueEditText.removeTextChangedListener(totalValueTextWatcher); + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + if(isFieldCorrectlyFilled(buyPriceEditText, false) && isFieldCorrectlyFilled(amoutEditText, false)) + { + if(Double.parseDouble(amoutEditText.getText().toString()) > 0) + { + Double totalValue = Double.parseDouble(buyPriceEditText.getText().toString()) * Double.parseDouble(s.toString()); + totalValueEditText.setText(String.format("%f", totalValue)); + } + else + { + totalValueEditText.setText("0"); + } + } + else + { + totalValueEditText.setText(""); + } + } + + @Override + public void afterTextChanged(Editable s) { + totalValueEditText.addTextChangedListener(totalValueTextWatcher); + } + }; + + private TextWatcher totalValueTextWatcher = new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + amoutEditText.removeTextChangedListener(amountTextWatcher); + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + if(isFieldCorrectlyFilled(buyPriceEditText, false) && isFieldCorrectlyFilled(totalValueEditText, false)) + { + if(Double.parseDouble(totalValueEditText.getText().toString()) > 0) + { + Double amount = Double.parseDouble(s.toString()) / Double.parseDouble(buyPriceEditText.getText().toString()); + amoutEditText.setText(String.format("%f", amount)); + } + else + { + amoutEditText.setText("0"); + } + } + else + { + amoutEditText.setText(""); + } + } + + @Override + public void afterTextChanged(Editable s) { + amoutEditText.addTextChangedListener(amountTextWatcher); + } + }; + @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { @@ -102,75 +165,21 @@ public class BuyFragment extends CustomRecordFragment { private void fillFields() { amoutEditText.setText(String.valueOf(transaction.getAmount())); - buyPriceEditText.setText(String.valueOf(transaction.getPurchasePrice())); + buyPriceEditText.setText(String.valueOf(transaction.getPrice())); calendar.setTimeInMillis(transaction.getTimestamp()); buyDateEditText.setText(sdf.format(calendar.getTime())); - totalValueEditText.setText(String.valueOf(transaction.getAmount() * transaction.getPurchasePrice())); + totalValueEditText.setText(String.valueOf(transaction.getAmount() * transaction.getPrice())); fees_editText.setText(String.valueOf(transaction.getFees())); note_editText.setText(transaction.getNote()); - - Log.d("moodl", "> " + fragmentPair); - - /*if(transaction.getSymbol().equals(fragmentPair.getFrom())) - { - if(transaction.getFeeFormat().equals("p")) - { - feesCurrencySpinner.setSelection(0); - } - else - { - feesCurrencySpinner.setSelection(1); - } - } - else - { - if(transaction.getFeeFormat().equals("p")) - { - feesCurrencySpinner.setSelection(2); - } - else - { - feesCurrencySpinner.setSelection(3); - } - }*/ } private void initializeViewElements() { totalValueEditText = view.findViewById(R.id.totalValue_editText); + totalValueEditText.addTextChangedListener(totalValueTextWatcher); + amoutEditText = view.findViewById(R.id.amount_editText); - amoutEditText.addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - - } - - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - - if(isFieldCorrectlyFilled(buyPriceEditText, false) && isFieldCorrectlyFilled(amoutEditText, false)) - { - if(Double.parseDouble(amoutEditText.getText().toString()) > 0) - { - Double totalValue = Double.parseDouble(buyPriceEditText.getText().toString()) * Double.parseDouble(s.toString()); - totalValueEditText.setText(String.format("%f", totalValue)); - } - else - { - totalValueEditText.setText("0"); - } - } - else - { - totalValueEditText.setText(""); - } - } - - @Override - public void afterTextChanged(Editable s) { - - } - }); + amoutEditText.addTextChangedListener(amountTextWatcher); buyPriceEditText = view.findViewById(R.id.buyPrice_editText); buyDateEditText = view.findViewById(R.id.buyDate_editText); @@ -281,11 +290,13 @@ public class BuyFragment extends CustomRecordFragment { { if(fragmentCurrency.getSymbol().equals(feeCurrency)) { - fees = amount * fees / 100; + fees = (100 * amount) / (100 + fees); } else { - fees = purchasedPrice * fees / 100; + double base = (100 * purchasedPrice * amount) / (100 + fees); + + fees = purchasedPrice * amount - base; } } } diff --git a/app/src/main/java/com/herbron/moodl/CustomAdapters/TransactionListAdapter.java b/app/src/main/java/com/herbron/moodl/CustomAdapters/TransactionListAdapter.java index f196abc..df972de 100644 --- a/app/src/main/java/com/herbron/moodl/CustomAdapters/TransactionListAdapter.java +++ b/app/src/main/java/com/herbron/moodl/CustomAdapters/TransactionListAdapter.java @@ -55,7 +55,7 @@ public class TransactionListAdapter extends ArrayAdapter { TextView dateTxtView = convertView.findViewById(R.id.purchaseDate); amountTxtView.setText(String.valueOf(transaction.getAmount())); - valueTxtView.setText(numberConformer(transaction.getPurchasePrice() * transaction.getAmount())); + valueTxtView.setText(numberConformer(transaction.getPrice() * transaction.getAmount())); dateTxtView.setText(getDateFromTimestamp(transaction.getTimestamp())); LinearLayout deleteLayout = convertView.findViewById(R.id.deleteTransactionLayout); 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 29aee2c..9155857 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 @@ -10,7 +10,7 @@ public class Transaction { private String symbol; private double amount; private long timestamp; - private double purchasePrice; + private double price; private double fees; private String feeCurrency; private String feeFormat; @@ -27,7 +27,7 @@ public class Transaction { this.symPair = symPair; this.amount = amount; this.timestamp = timestamp; - this.purchasePrice = purchasedPrice; + this.price = purchasedPrice; this.fees = fees; this.note = note; this.feeCurrency = feeCurrency; @@ -66,14 +66,14 @@ public class Transaction { this.amount = amount; } - public void setPurchasePrice(double purchasedPrice) + public void setPrice(double purchasedPrice) { - this.purchasePrice = purchasedPrice; + this.price = purchasedPrice; } - public double getPurchasePrice() + public double getPrice() { - return purchasePrice; + return price; } public double getFees() {