[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.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) {

View File

@ -133,126 +133,7 @@ public class SellFragment extends CustomRecordFragment {
}
};
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.record_transaction_fragment_sell, container, false);
context = getActivity().getApplicationContext();
calendar = Calendar.getInstance();
sdf = new SimpleDateFormat("HH:mm dd/MM/yyyy", Locale.UK);
preferenceManager = new PreferencesManager(getContext());
initializeViewElements();
return view;
}
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);
sellDateEditText.setText(sdf.format(calendar.getTime()));
sellDateEditText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createDatePicker();
}
});
feesCurrencySpinner = view.findViewById(R.id.feesCurrency_editText_sell);
currencyFeeAdapter = new ArrayAdapter<String>(getSecureContext(), android.R.layout.simple_spinner_item, new ArrayList<>());
currencyFeeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
feesCurrencySpinner.setAdapter(currencyFeeAdapter);
if(fragmentPair != null)
{
updateAdapter();
}
deductHoldingsSwitch = view.findViewById(R.id.deductHoldingsSell);
saveSellButton = view.findViewById(R.id.saveSellButton);
saveSellButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isFieldCorrectlyFilled(amoutEditText, true) && isFieldCorrectlyFilled(sellPriceEditText, true) && isFieldCorrectlyFilled(totalValueEditText, true))
{
double amount = Double.parseDouble(amoutEditText.getText().toString());
double purchasePrice = Double.parseDouble(sellPriceEditText.getText().toString());
double fees;
String feeCurrency;
if(feesCurrencySpinner.getSelectedItemPosition() < 1)
{
feeCurrency = fragmentPair.getFrom();
}
else
{
feeCurrency = fragmentPair.getTo();
}
fees = getFees(feeCurrency, amount, purchasePrice);
String note = note_editText.getText().toString();
DatabaseManager databaseManager = new DatabaseManager(getContext());
preferenceManager.setMustUpdateSummary(true);
if(transactionId == -1)
{
databaseManager.addTransaction(fragmentCurrency.getSymbol()
, amount
, calendar.getTime()
, purchasePrice
, fees
, note
, fragmentPair.getFrom().equals(fragmentCurrency.getSymbol()) ? fragmentPair.getTo() : fragmentPair.getFrom()
, feeCurrency
, ""
, fragmentExchange.getName()
, "s"
, feesCurrencySpinner.getSelectedItemPosition() % 2 == 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()
, purchasePrice
, fees
, note
, fragmentPair.getFrom().equals(fragmentCurrency.getSymbol()) ? fragmentPair.getTo() : fragmentPair.getFrom()
, feeCurrency
, ""
, fragmentExchange.getName()
,"s"
, feesCurrencySpinner.getSelectedItemPosition() % 2 == 0 ? "p" : "f"
, deductHoldingsSwitch.isChecked());
}
getActivity().finish();
}
}
});
fees_editText = view.findViewById(R.id.fees_editText_sell);
fees_editText.addTextChangedListener(new TextWatcher() {
private TextWatcher feesTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
totalValueEditText.removeTextChangedListener(totalValueTextWatcher);
@ -349,11 +230,138 @@ public class SellFragment extends CustomRecordFragment {
totalValueEditText.addTextChangedListener(totalValueTextWatcher);
amoutEditText.addTextChangedListener(amountTextWatcher);
}
};
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.record_transaction_fragment_sell, container, false);
context = getActivity().getApplicationContext();
calendar = Calendar.getInstance();
sdf = new SimpleDateFormat("HH:mm dd/MM/yyyy", Locale.UK);
preferenceManager = new PreferencesManager(getContext());
initializeViewElements();
return view;
}
private void initializeViewElements()
{
totalValueEditText = view.findViewById(R.id.totalValue_editText_sell);
amoutEditText = view.findViewById(R.id.amount_editText_sell);
sellPriceEditText = view.findViewById(R.id.sellPrice_editText);
sellDateEditText = view.findViewById(R.id.sellDate_editText);
sellDateEditText.setText(sdf.format(calendar.getTime()));
sellDateEditText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createDatePicker();
}
});
feesCurrencySpinner = view.findViewById(R.id.feesCurrency_editText_sell);
currencyFeeAdapter = new ArrayAdapter<String>(getSecureContext(), android.R.layout.simple_spinner_item, new ArrayList<>());
currencyFeeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
feesCurrencySpinner.setAdapter(currencyFeeAdapter);
if(fragmentPair != null)
{
updateAdapter();
}
deductHoldingsSwitch = view.findViewById(R.id.deductHoldingsSell);
saveSellButton = view.findViewById(R.id.saveSellButton);
saveSellButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isFieldCorrectlyFilled(amoutEditText, true) && isFieldCorrectlyFilled(sellPriceEditText, true) && isFieldCorrectlyFilled(totalValueEditText, true))
{
double amount = Double.parseDouble(amoutEditText.getText().toString());
double purchasePrice = Double.parseDouble(sellPriceEditText.getText().toString());
double fees;
String feeCurrency;
if(feesCurrencySpinner.getSelectedItemPosition() < 1)
{
feeCurrency = fragmentPair.getFrom();
}
else
{
feeCurrency = fragmentPair.getTo();
}
fees = getFees(feeCurrency, amount, purchasePrice);
String note = note_editText.getText().toString();
DatabaseManager databaseManager = new DatabaseManager(getContext());
preferenceManager.setMustUpdateSummary(true);
if(transactionId == -1)
{
databaseManager.addTransaction(fragmentCurrency.getSymbol()
, amount
, calendar.getTime()
, purchasePrice
, fees
, note
, fragmentPair.getFrom().equals(fragmentCurrency.getSymbol()) ? fragmentPair.getTo() : fragmentPair.getFrom()
, feeCurrency
, ""
, fragmentExchange.getName()
, "s"
, feesCurrencySpinner.getSelectedItemPosition() % 2 == 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()
, purchasePrice
, fees
, note
, fragmentPair.getFrom().equals(fragmentCurrency.getSymbol()) ? fragmentPair.getTo() : fragmentPair.getFrom()
, feeCurrency
, ""
, fragmentExchange.getName()
,"s"
, feesCurrencySpinner.getSelectedItemPosition() % 2 == 0 ? "p" : "f"
, deductHoldingsSwitch.isChecked());
}
getActivity().finish();
}
}
});
fees_editText = view.findViewById(R.id.fees_editText_sell);
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)

View File

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