- private void hookTextChangeListener(final TextView view) {
- view.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) {
-
- }
-
- @Override
- public void afterTextChanged(Editable s) {
-// Log.d("input", "text changed");
- check_transaction_submittable();
- }
- });
-
- }
- private TableRow doAddAccountRow(boolean focus) {
- final AutoCompleteTextView acc = new AutoCompleteTextView(this);
- acc.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
- TableRow.LayoutParams.WRAP_CONTENT, 9f));
- acc.setHint(R.string.new_transaction_account_hint);
- acc.setWidth(0);
- acc.setImeOptions(EditorInfo.IME_ACTION_NEXT | EditorInfo.IME_FLAG_NO_ENTER_ACTION |
- EditorInfo.IME_FLAG_NAVIGATE_NEXT);
- acc.setSingleLine(true);
-
- final EditText amt = new EditText(this);
- amt.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
- TableRow.LayoutParams.MATCH_PARENT, 1f));
- amt.setHint(R.string.new_transaction_amount_hint);
- amt.setWidth(0);
- amt.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED |
- InputType.TYPE_NUMBER_FLAG_DECIMAL);
- amt.setMinWidth(dp2px(40));
- amt.setTextAlignment(EditText.TEXT_ALIGNMENT_VIEW_END);
- amt.setImeOptions(EditorInfo.IME_ACTION_DONE);
-
- // forward navigation support
- final TableRow last_row = (TableRow) table.getChildAt(table.getChildCount() - 1);
- final TextView last_amt = (TextView) last_row.getChildAt(1);
- last_amt.setNextFocusForwardId(acc.getId());
- last_amt.setNextFocusRightId(acc.getId());
- last_amt.setImeOptions(EditorInfo.IME_ACTION_NEXT);
- acc.setNextFocusForwardId(amt.getId());
- acc.setNextFocusRightId(amt.getId());
-
- final TableRow row = new TableRow(this);
- row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
- TableRow.LayoutParams.MATCH_PARENT));
- row.setGravity(Gravity.BOTTOM);
- row.addView(acc);
- row.addView(amt);
- table.addView(row);
-
- if (focus) acc.requestFocus();
-
- hookSwipeListener(row);
- MLDB.hookAutocompletionAdapter(this, acc, MLDB.ACCOUNTS_TABLE, "name", true, amt, null);
- hookTextChangeListener(acc);
- hookTextChangeListener(amt);
-
- return row;
- }
- public void addTransactionAccountFromMenu(MenuItem item) {
- doAddAccountRow(true);
- }
- public void resetTransactionFromMenu(MenuItem item) {
- resetForm();
- }
- 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 (fab != null) {
- fab.show();
- fab.setEnabled(true);
- }
- }
- else {
- if (fab != null) fab.hide();
- }
-
- if (single_empty_amount) {
- empty_amount.setHint(String.format("%1.2f",
- (Math.abs(running_total) > 0.005) ? -running_total : 0f));
- }
-
- }
- catch (NumberFormatException e) {
- if (fab != null) fab.hide();
- }
- catch (Exception e) {
- e.printStackTrace();
- if (fab != null) fab.hide();
- }
- }
-