From 10c3a0a67b3dcd66ad63e7c32ece24960c11e934 Mon Sep 17 00:00:00 2001 From: Tanguy Herbron Date: Mon, 20 Aug 2018 20:32:59 +0200 Subject: [PATCH] UI fix [RecordTransactionActivity] - Fix tab color when editing a transaction - Disable tab swiping - Fix crash when editing a sell transaction --- .../Activities/RecordTransactionActivity.java | 11 +- .../SellFragment.java | 208 +++++++++--------- .../layout/activity_record_transaction.xml | 2 +- 3 files changed, 117 insertions(+), 104 deletions(-) diff --git a/app/src/main/java/com/herbron/moodl/Activities/RecordTransactionActivity.java b/app/src/main/java/com/herbron/moodl/Activities/RecordTransactionActivity.java index 5c72977..4155211 100644 --- a/app/src/main/java/com/herbron/moodl/Activities/RecordTransactionActivity.java +++ b/app/src/main/java/com/herbron/moodl/Activities/RecordTransactionActivity.java @@ -37,6 +37,7 @@ import com.herbron.moodl.Activities.RecordTransactionFragments.SellFragment; import com.herbron.moodl.CurrencyInfoUpdateNotifierInterface; import com.herbron.moodl.CustomAdapters.PairRecordListAdapter; import com.herbron.moodl.CustomLayouts.CustomRecordFragment; +import com.herbron.moodl.CustomLayouts.CustomViewPager; import com.herbron.moodl.DataManagers.CurrencyData.Currency; import com.herbron.moodl.DataManagers.CurrencyData.Transaction; import com.herbron.moodl.DataManagers.ExchangeManager.Exchange; @@ -76,7 +77,7 @@ public class RecordTransactionActivity extends AppCompatActivity implements Curr private AutoCompleteTextView pair_autoCompleteTextView; private CustomTabLayout tabLayout; - private ViewPager viewPager; + private CustomViewPager viewPager; private RecordTransactionPageAdapter pageAdapter; @@ -246,7 +247,7 @@ public class RecordTransactionActivity extends AppCompatActivity implements Curr ((BuyFragment) pageAdapter.getItem(0)).updatePair(pair); - updatePairData(); + //updatePairData(); found = true; } @@ -255,6 +256,7 @@ public class RecordTransactionActivity extends AppCompatActivity implements Curr } tabLayout.getTabAt(0).select(); + ((TextView) tabLayout.getTabAt(0).getCustomView()).setTextColor(getResources().getColor(R.color.white)); break; case "s": @@ -294,7 +296,7 @@ public class RecordTransactionActivity extends AppCompatActivity implements Curr ((SellFragment) pageAdapter.getItem(1)).updatePair(pair); - updatePairData(); + //updatePairData(); found = true; } @@ -303,9 +305,11 @@ public class RecordTransactionActivity extends AppCompatActivity implements Curr } tabLayout.getTabAt(1).select(); + ((TextView) tabLayout.getTabAt(1).getCustomView()).setTextColor(getResources().getColor(R.color.white)); break; case "t": tabLayout.getTabAt(2).select(); + ((TextView) tabLayout.getTabAt(2).getCustomView()).setTextColor(getResources().getColor(R.color.white)); break; } } @@ -330,6 +334,7 @@ public class RecordTransactionActivity extends AppCompatActivity implements Curr pageAdapter = new RecordTransactionPageAdapter(getSupportFragmentManager(), tabLayout.getTabCount()); viewPager.setAdapter(pageAdapter); viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); + viewPager.setPagingEnabled(false); tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { 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 d18ca60..eed25f9 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 @@ -133,6 +133,105 @@ public class SellFragment 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(sellPriceEditText, false)) + { + double amount = Double.parseDouble(amoutEditText.getText().toString()); + double purchasePrice = Double.parseDouble(sellPriceEditText.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) { @@ -153,10 +252,8 @@ public class SellFragment extends CustomRecordFragment { private void initializeViewElements() { totalValueEditText = view.findViewById(R.id.totalValue_editText_sell); - totalValueEditText.addTextChangedListener(totalValueTextWatcher); amoutEditText = view.findViewById(R.id.amount_editText_sell); - amoutEditText.addTextChangedListener(amountTextWatcher); sellPriceEditText = view.findViewById(R.id.sellPrice_editText); sellDateEditText = view.findViewById(R.id.sellDate_editText); @@ -252,108 +349,19 @@ public class SellFragment extends CustomRecordFragment { }); fees_editText = view.findViewById(R.id.fees_editText_sell); - 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(sellPriceEditText, false)) - { - double amount = Double.parseDouble(amoutEditText.getText().toString()); - double purchasePrice = Double.parseDouble(sellPriceEditText.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_sell); 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/res/layout/activity_record_transaction.xml b/app/src/main/res/layout/activity_record_transaction.xml index 699015d..8f111ec 100644 --- a/app/src/main/res/layout/activity_record_transaction.xml +++ b/app/src/main/res/layout/activity_record_transaction.xml @@ -126,7 +126,7 @@ app:tabMode="fixed" app:tabIndicatorColor="@color/transparent"/> -