[RecordTransactionActivity]
- Fix tab color when editing a transaction
- Disable tab swiping
- Fix crash when editing a sell transaction
This commit is contained in:
Tanguy Herbron 2018-08-20 20:32:59 +02:00
parent 4c65596718
commit 10c3a0a67b
3 changed files with 117 additions and 104 deletions

View File

@ -37,6 +37,7 @@ import com.herbron.moodl.Activities.RecordTransactionFragments.SellFragment;
import com.herbron.moodl.CurrencyInfoUpdateNotifierInterface; import com.herbron.moodl.CurrencyInfoUpdateNotifierInterface;
import com.herbron.moodl.CustomAdapters.PairRecordListAdapter; import com.herbron.moodl.CustomAdapters.PairRecordListAdapter;
import com.herbron.moodl.CustomLayouts.CustomRecordFragment; 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.Currency;
import com.herbron.moodl.DataManagers.CurrencyData.Transaction; import com.herbron.moodl.DataManagers.CurrencyData.Transaction;
import com.herbron.moodl.DataManagers.ExchangeManager.Exchange; import com.herbron.moodl.DataManagers.ExchangeManager.Exchange;
@ -76,7 +77,7 @@ public class RecordTransactionActivity extends AppCompatActivity implements Curr
private AutoCompleteTextView pair_autoCompleteTextView; private AutoCompleteTextView pair_autoCompleteTextView;
private CustomTabLayout tabLayout; private CustomTabLayout tabLayout;
private ViewPager viewPager; private CustomViewPager viewPager;
private RecordTransactionPageAdapter pageAdapter; private RecordTransactionPageAdapter pageAdapter;
@ -246,7 +247,7 @@ public class RecordTransactionActivity extends AppCompatActivity implements Curr
((BuyFragment) pageAdapter.getItem(0)).updatePair(pair); ((BuyFragment) pageAdapter.getItem(0)).updatePair(pair);
updatePairData(); //updatePairData();
found = true; found = true;
} }
@ -255,6 +256,7 @@ public class RecordTransactionActivity extends AppCompatActivity implements Curr
} }
tabLayout.getTabAt(0).select(); tabLayout.getTabAt(0).select();
((TextView) tabLayout.getTabAt(0).getCustomView()).setTextColor(getResources().getColor(R.color.white));
break; break;
case "s": case "s":
@ -294,7 +296,7 @@ public class RecordTransactionActivity extends AppCompatActivity implements Curr
((SellFragment) pageAdapter.getItem(1)).updatePair(pair); ((SellFragment) pageAdapter.getItem(1)).updatePair(pair);
updatePairData(); //updatePairData();
found = true; found = true;
} }
@ -303,9 +305,11 @@ public class RecordTransactionActivity extends AppCompatActivity implements Curr
} }
tabLayout.getTabAt(1).select(); tabLayout.getTabAt(1).select();
((TextView) tabLayout.getTabAt(1).getCustomView()).setTextColor(getResources().getColor(R.color.white));
break; break;
case "t": case "t":
tabLayout.getTabAt(2).select(); tabLayout.getTabAt(2).select();
((TextView) tabLayout.getTabAt(2).getCustomView()).setTextColor(getResources().getColor(R.color.white));
break; break;
} }
} }
@ -330,6 +334,7 @@ public class RecordTransactionActivity extends AppCompatActivity implements Curr
pageAdapter = new RecordTransactionPageAdapter(getSupportFragmentManager(), tabLayout.getTabCount()); pageAdapter = new RecordTransactionPageAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(pageAdapter); viewPager.setAdapter(pageAdapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
viewPager.setPagingEnabled(false);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override @Override
public void onTabSelected(TabLayout.Tab tab) { public void onTabSelected(TabLayout.Tab tab) {

View File

@ -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 @Nullable
@Override @Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
@ -153,10 +252,8 @@ public class SellFragment extends CustomRecordFragment {
private void initializeViewElements() private void initializeViewElements()
{ {
totalValueEditText = view.findViewById(R.id.totalValue_editText_sell); totalValueEditText = view.findViewById(R.id.totalValue_editText_sell);
totalValueEditText.addTextChangedListener(totalValueTextWatcher);
amoutEditText = view.findViewById(R.id.amount_editText_sell); amoutEditText = view.findViewById(R.id.amount_editText_sell);
amoutEditText.addTextChangedListener(amountTextWatcher);
sellPriceEditText = view.findViewById(R.id.sellPrice_editText); sellPriceEditText = view.findViewById(R.id.sellPrice_editText);
sellDateEditText = view.findViewById(R.id.sellDate_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 = 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); note_editText = view.findViewById(R.id.note_editText_sell);
checkCallingIntent(); checkCallingIntent();
setupTextWatchers();
}
private void setupTextWatchers()
{
totalValueEditText.addTextChangedListener(totalValueTextWatcher);
amoutEditText.addTextChangedListener(amountTextWatcher);
fees_editText.addTextChangedListener(feesTextWatcher);
} }
private double getFees(String feeCurrency, double amount, double purchasedPrice) private double getFees(String feeCurrency, double amount, double purchasedPrice)

View File

@ -126,7 +126,7 @@
app:tabMode="fixed" app:tabMode="fixed"
app:tabIndicatorColor="@color/transparent"/> app:tabIndicatorColor="@color/transparent"/>
<android.support.v4.view.ViewPager <com.herbron.moodl.CustomLayouts.CustomViewPager
android:id="@+id/transactionsViewPager" android:id="@+id/transactionsViewPager"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" /> android:layout_height="wrap_content" />