- public void saveTransactionFromMenu(MenuItem item) {
- saveTransaction();
- }
- // rules:
- // 1) at least two account names
- // 2) each amount must have account name
- // 3) amounts must balance to 0, or
- // 3a) there must be exactly one empty amount
- // 4) empty accounts with empty amounts are ignored
- // 5) a row with an empty account name or empty amount is guaranteed to exist
- @SuppressLint("DefaultLocale")
- private void check_transaction_submittable() {
- TableLayout table = findViewById(R.id.new_transaction_accounts_table);
- int accounts = 0;
- int accounts_with_values = 0;
- int amounts = 0;
- int amounts_with_accounts = 0;
- int empty_rows = 0;
- TextView empty_amount = null;
- boolean single_empty_amount = false;
- boolean single_empty_amount_has_account = false;
- float running_total = 0f;
- boolean have_description =
- !((TextView) findViewById(R.id.new_transaction_description)).getText().toString()
- .isEmpty();
-
- try {
- for (int i = 0; i < table.getChildCount(); i++) {
- TableRow row = (TableRow) table.getChildAt(i);
-
- TextView acc_name_v = (TextView) row.getChildAt(0);
- TextView amount_v = (TextView) row.getChildAt(1);
- String amt = String.valueOf(amount_v.getText());
- String acc_name = String.valueOf(acc_name_v.getText());
- acc_name = acc_name.trim();
-
- if (!acc_name.isEmpty()) {
- accounts++;
-
- if (!amt.isEmpty()) {
- accounts_with_values++;
- }
- }
- else empty_rows++;
-
- if (amt.isEmpty()) {
- amount_v.setHint(String.format("%1.2f", 0f));
- if (empty_amount == null) {
- empty_amount = amount_v;
- single_empty_amount = true;
- single_empty_amount_has_account = !acc_name.isEmpty();
- }
- else if (!acc_name.isEmpty()) single_empty_amount = false;
- }
- else {
- amounts++;
- if (!acc_name.isEmpty()) amounts_with_accounts++;
- running_total += Float.valueOf(amt);
- }
- }
-
- if ((empty_rows == 0) &&
- ((table.getChildCount() == accounts) || (table.getChildCount() == amounts)))
- {
- doAddAccountRow(false);
- }
-
- Log.d("submittable", String.format("accounts=%d, accounts_with_values=%s, " +
- "amounts_with_accounts=%d, amounts=%d, running_total=%1.2f, " +
- "single_empty_with_acc=%s", accounts,
- accounts_with_values, amounts_with_accounts, amounts, running_total,
- (single_empty_amount && single_empty_amount_has_account) ? "true" : "false"));
-
- if (have_description && (accounts >= 2) && (accounts_with_values >= (accounts - 1)) &&
- (amounts_with_accounts == amounts) &&
- (single_empty_amount && single_empty_amount_has_account || isZero(running_total)))
- {
- if (mSave != null) mSave.setVisible(true);
- }
- else if (mSave != null) mSave.setVisible(false);
-
- if (single_empty_amount) {
- empty_amount.setHint(String.format("%1.2f",
- (Math.abs(running_total) > 0.005) ? -running_total : 0f));
- }
-
- }
- catch (NumberFormatException e) {
- if (mSave != null) mSave.setVisible(false);
- }
- catch (Exception e) {
- e.printStackTrace();
- if (mSave != null) mSave.setVisible(false);
- }
- }
-