+ public int dp2px(float dp) {
+ return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
+ getResources().getDisplayMetrics()));
+ }
+
+ private void do_add_account_row(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();
+
+ hook_swipe_listener(row);
+ MLDB.hook_autocompletion_adapter(this, acc, MLDB.ACCOUNTS_TABLE, "name");
+ hook_text_change_listener(acc);
+ hook_text_change_listener(amt);
+ }
+
+ public void addTransactionAccountFromMenu(MenuItem item) {
+ do_add_account_row(true);
+ }
+
+ public void resetTransactionFromMenu(MenuItem item) {
+ reset_form();
+ }
+
+ public void saveTransactionFromMenu(MenuItem item) {
+ save_transaction();
+ }
+
+ private boolean is_zero(float f) {
+ return (f < 0.005) && (f > -0.005);
+ }
+
+ // 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)))
+ {
+ do_add_account_row(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 || is_zero(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", (running_total > 0) ? -running_total : 0f));
+ }
+
+ }
+ catch (NumberFormatException e) {
+ if (mSave != null) mSave.setVisible(false);
+ }
+ catch (Exception e) {
+ e.printStackTrace();
+ if (mSave != null) mSave.setVisible(false);
+ }
+ }
+
+ @Override
+ public void done(String error) {
+ progress.setVisibility(View.INVISIBLE);
+ Log.d("visuals", "hiding progress");
+
+ if (error == null) reset_form();
+ else Snackbar.make(findViewById(R.id.new_transaction_accounts_table), error,
+ BaseTransientBottomBar.LENGTH_LONG).show();
+
+ toggle_all_editing(true);
+ check_transaction_submittable();
+ }
+
+ private void reset_form() {
+ text_date.setText("");
+ text_descr.setText("");
+
+ text_descr.requestFocus();
+
+ while (table.getChildCount() > 2) {
+ table.removeViewAt(2);
+ }
+ for (int i = 0; i < 2; i++) {
+ TableRow tr = (TableRow) table.getChildAt(i);
+ if (tr == null) break;
+
+ ((TextView) tr.getChildAt(0)).setText("");
+ ((TextView) tr.getChildAt(1)).setText("");
+ }
+ }