+ text_date = findViewById(R.id.new_transaction_date);
+ text_descr = findViewById(R.id.new_transaction_description);
+
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ text_descr.setAutofillHints("");
+ }
+
+ progress = findViewById(R.id.save_transaction_progress);
+
+ Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
+ table = findViewById(R.id.new_transaction_accounts_table);
+ for (int i = 0; i < table.getChildCount(); i++) {
+ TableRow row = (TableRow) table.getChildAt(i);
+ TextView acc_name_view = (TextView) row.getChildAt(0);
+ TextView amount_view = (TextView) row.getChildAt(1);
+ hook_swipe_listener(row);
+ hook_autocompletion_adapter(row);
+ hook_text_change_listener(acc_name_view);
+ hook_text_change_listener(amount_view);
+// Log.d("swipe", "hooked to row "+i);
+ }
+ }
+
+ public void save_transaction() {
+ mSave.setVisible(false);
+ toggle_all_editing(false);
+ progress.setVisibility(View.VISIBLE);
+
+ saver = new SaveTransactionTask(this);
+
+ saver.setPref(PreferenceManager.getDefaultSharedPreferences(this));
+ LedgerTransaction tr = new LedgerTransaction(text_date.getText().toString(), text_descr.getText().toString());
+
+ TableLayout table = findViewById(R.id.new_transaction_accounts_table);
+ for ( int i = 0; i < table.getChildCount(); i++ ) {
+ TableRow row = (TableRow) table.getChildAt(i);
+ String acc = ((TextView) row.getChildAt(0)).getText().toString();
+ String amt = ((TextView) row.getChildAt(1)).getText().toString();
+ LedgerTransactionItem item =
+ amt.length() > 0
+ ? new LedgerTransactionItem( acc, Float.parseFloat(amt))
+ : new LedgerTransactionItem( acc );
+
+ tr.add_item(item);
+ }
+ saver.execute(tr);
+ }
+
+ private void toggle_all_editing(boolean enabled) {
+ TableLayout table = findViewById(R.id.new_transaction_accounts_table);
+ for (int i = 0; i < table.getChildCount(); i++) {
+ TableRow row = (TableRow) table.getChildAt(i);
+ for (int j = 0; j < row.getChildCount(); j++) {
+ row.getChildAt(j).setEnabled(enabled);
+ }
+ }
+ }
+
+ private void hook_swipe_listener(final TableRow row) {
+ row.getChildAt(0).setOnTouchListener(new OnSwipeTouchListener(this) {
+ public void onSwipeLeft() {
+// Log.d("swipe", "LEFT" + row.getId());
+ if (table.getChildCount() > 2) {
+ table.removeView(row);
+// Toast.makeText(NewTransactionActivity.this, "LEFT", Toast.LENGTH_LONG).show();
+ }
+ else {
+ Snackbar.make(table, R.string.msg_at_least_two_accounts_are_required, Snackbar.LENGTH_LONG)
+ .setAction("Action", null).show();
+ }
+ }
+// @Override
+// public boolean performClick(View view, MotionEvent m) {
+// return true;
+// }
+ public boolean onTouch(View view, MotionEvent m) {
+ return gestureDetector.onTouchEvent(m);
+ }
+ });
+ }
+
+ private void hook_text_change_listener(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) {
+
+ }
+