]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/NewTransactionActivity.java
remove account lines by swiping left
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / NewTransactionActivity.java
1 package net.ktnx.mobileledger;
2
3 import android.os.Bundle;
4 import android.support.design.widget.FloatingActionButton;
5 import android.support.design.widget.Snackbar;
6 import android.support.v4.app.DialogFragment;
7 import android.support.v7.app.AppCompatActivity;
8 import android.support.v7.widget.Toolbar;
9 import android.text.InputType;
10 import android.view.Menu;
11 import android.view.MenuItem;
12 import android.view.MotionEvent;
13 import android.view.View;
14 import android.widget.AutoCompleteTextView;
15 import android.widget.EditText;
16 import android.widget.TableLayout;
17 import android.widget.TableRow;
18
19 import java.util.Objects;
20
21 public class NewTransactionActivity extends AppCompatActivity {
22     private TableLayout table;
23
24     @Override
25     protected void onCreate(Bundle savedInstanceState) {
26         super.onCreate(savedInstanceState);
27         setContentView(R.layout.activity_new_transaction);
28         Toolbar toolbar = findViewById(R.id.toolbar);
29         setSupportActionBar(toolbar);
30
31         FloatingActionButton fab = findViewById(R.id.fab);
32         fab.setOnClickListener(new View.OnClickListener() {
33             @Override
34             public void onClick(View view) {
35                 Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
36                         .setAction("Action", null).show();
37             }
38         });
39         Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
40         table = findViewById(R.id.new_transaction_accounts_table);
41         for (int i = 0; i < table.getChildCount(); i++) {
42             hook_swipe_listener((TableRow)table.getChildAt(i));
43 //            Log.d("swipe", "hooked to row "+i);
44         }
45     }
46
47     private void hook_swipe_listener(final TableRow row) {
48         row.getChildAt(0).setOnTouchListener(new OnSwipeTouchListener(this) {
49             public void onSwipeLeft() {
50 //                Log.d("swipe", "LEFT" + row.getId());
51                 if (table.getChildCount() > 2) {
52                     table.removeView(row);
53 //                    Toast.makeText(NewTransactionActivity.this, "LEFT", Toast.LENGTH_LONG).show();
54                 }
55                 else {
56                     Snackbar.make(table, R.string.msg_at_least_two_accounts_are_required, Snackbar.LENGTH_LONG)
57                             .setAction("Action", null).show();
58                 }
59             }
60 //            @Override
61 //            public boolean performClick(View view, MotionEvent m) {
62 //                return true;
63 //            }
64             public boolean onTouch(View view, MotionEvent m) {
65                 return gestureDetector.onTouchEvent(m);
66             }
67         });
68     }
69
70     public boolean onCreateOptionsMenu(Menu menu) {
71         // Inflate the menu; this adds items to the action bar if it is present.
72         getMenuInflater().inflate(R.menu.new_transaction, menu);
73
74         return true;
75     }
76
77     public void pickTransactionDate(View view) {
78         DialogFragment picker = new DatePickerFragment();
79         picker.show(getSupportFragmentManager(), "datePicker");
80 //        Snackbar.make(view, "Date editing not yet ready", Snackbar.LENGTH_LONG)
81 //                .setAction("Action", null).show();
82     }
83
84     public void addTransactionAccountFromMenu(MenuItem item) {
85         final AutoCompleteTextView acc = new AutoCompleteTextView(this);
86         acc.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 9f));
87         acc.setHint(R.string.new_transaction_account_hint);
88         acc.setWidth(0);
89
90         final EditText amt = new EditText(this);
91         amt.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
92         amt.setHint(R.string.new_transaction_amount_hint);
93         amt.setWidth(0);
94         amt.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL );
95         amt.setMinWidth(64);
96         amt.setTextAlignment(EditText.TEXT_ALIGNMENT_VIEW_END);
97
98         final TableRow row = new TableRow(this);
99         row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));
100         row.addView(acc);
101         row.addView(amt);
102         table.addView(row);
103
104         acc.requestFocus();
105
106         hook_swipe_listener(row);
107     }
108
109 }