1 package net.ktnx.mobileledger;
3 import android.annotation.TargetApi;
4 import android.database.Cursor;
5 import android.database.MatrixCursor;
6 import android.database.sqlite.SQLiteDatabase;
7 import android.os.Build;
8 import android.os.Bundle;
9 import android.preference.PreferenceManager;
10 import android.provider.FontsContract;
11 import android.support.design.widget.FloatingActionButton;
12 import android.support.design.widget.Snackbar;
13 import android.support.v4.app.DialogFragment;
14 import android.support.v7.app.AppCompatActivity;
15 import android.support.v7.widget.Toolbar;
16 import android.text.InputType;
17 import android.util.Log;
18 import android.util.TypedValue;
19 import android.view.Menu;
20 import android.view.MenuItem;
21 import android.view.MotionEvent;
22 import android.view.View;
23 import android.widget.AutoCompleteTextView;
24 import android.widget.EditText;
25 import android.widget.FilterQueryProvider;
26 import android.widget.SimpleCursorAdapter;
27 import android.widget.TableLayout;
28 import android.widget.TableRow;
29 import android.widget.TextView;
31 import java.util.Objects;
33 public class NewTransactionActivity extends AppCompatActivity implements TaskCallback {
34 private TableLayout table;
35 private FloatingActionButton fab;
36 private TextView text_date;
37 private TextView text_descr;
38 private static SaveTransactionTask saver;
41 protected void onCreate(Bundle savedInstanceState) {
42 super.onCreate(savedInstanceState);
43 setContentView(R.layout.activity_new_transaction);
44 Toolbar toolbar = findViewById(R.id.toolbar);
45 setSupportActionBar(toolbar);
47 text_date = findViewById(R.id.new_transaction_date);
48 text_descr = findViewById(R.id.new_transaction_description);
50 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
51 text_descr.setAutofillHints("");
54 fab = findViewById(R.id.fab);
55 fab.setOnClickListener(new View.OnClickListener() {
57 public void onClick(View view) {
58 new_transaction_save_clicked(view);
61 Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
62 table = findViewById(R.id.new_transaction_accounts_table);
63 for (int i = 0; i < table.getChildCount(); i++) {
64 hook_swipe_listener((TableRow)table.getChildAt(i));
65 hook_autocompletion_adapter((TableRow)table.getChildAt(i));
66 // Log.d("swipe", "hooked to row "+i);
70 public void new_transaction_save_clicked(View view) {
71 fab.setEnabled(false);
73 saver = new SaveTransactionTask(this);
75 saver.setPref(PreferenceManager.getDefaultSharedPreferences(this));
76 LedgerTransaction tr = new LedgerTransaction(text_date.getText().toString(), text_descr.getText().toString());
78 TableLayout table = findViewById(R.id.new_transaction_accounts_table);
79 for ( int i = 0; i < table.getChildCount(); i++ ) {
80 TableRow row = (TableRow) table.getChildAt(i);
81 String acc = ((TextView) row.getChildAt(0)).getText().toString();
82 String amt = ((TextView) row.getChildAt(1)).getText().toString();
83 LedgerTransactionItem item =
85 ? new LedgerTransactionItem( acc, Float.parseFloat(amt))
86 : new LedgerTransactionItem( acc );
92 private void hook_swipe_listener(final TableRow row) {
93 row.getChildAt(0).setOnTouchListener(new OnSwipeTouchListener(this) {
94 public void onSwipeLeft() {
95 // Log.d("swipe", "LEFT" + row.getId());
96 if (table.getChildCount() > 2) {
97 table.removeView(row);
98 // Toast.makeText(NewTransactionActivity.this, "LEFT", Toast.LENGTH_LONG).show();
101 Snackbar.make(table, R.string.msg_at_least_two_accounts_are_required, Snackbar.LENGTH_LONG)
102 .setAction("Action", null).show();
106 // public boolean performClick(View view, MotionEvent m) {
109 public boolean onTouch(View view, MotionEvent m) {
110 return gestureDetector.onTouchEvent(m);
115 @TargetApi(Build.VERSION_CODES.N)
116 private void hook_autocompletion_adapter(final TableRow row) {
117 String[] from = {"name"};
118 int[] to = {android.R.id.text1};
119 SQLiteDatabase db = MobileLedgerDB.db;
121 AutoCompleteTextView acc = (AutoCompleteTextView) row.getChildAt(0);
122 SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_dropdown_item_1line, null, from, to, 0);
123 adapter.setStringConversionColumn(1);
125 FilterQueryProvider provider = new FilterQueryProvider() {
127 public Cursor runQuery(CharSequence constraint) {
128 if (constraint == null) return null;
130 String str = constraint.toString().toUpperCase();
131 Log.d("autocompletion", "Looking for "+str);
132 String[] col_names = {FontsContract.Columns._ID, "name"};
133 MatrixCursor c = new MatrixCursor(col_names);
135 Cursor matches = db.rawQuery("SELECT name FROM accounts WHERE UPPER(name) LIKE '%'||?||'%' ORDER BY name;", new String[]{str});
139 while (matches.moveToNext()) {
140 String name = matches.getString(0);
141 Log.d("autocompletion-match", name);
142 c.newRow().add(i++).add(name);
154 adapter.setFilterQueryProvider(provider);
156 acc.setAdapter(adapter);
159 public boolean onCreateOptionsMenu(Menu menu) {
160 // Inflate the menu; this adds items to the action bar if it is present.
161 getMenuInflater().inflate(R.menu.new_transaction, menu);
166 public void pickTransactionDate(View view) {
167 DialogFragment picker = new DatePickerFragment();
168 picker.show(getSupportFragmentManager(), "datePicker");
171 public int dp2px(float dp) {
172 return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics()));
175 public void addTransactionAccountFromMenu(MenuItem item) {
176 final AutoCompleteTextView acc = new AutoCompleteTextView(this);
177 acc.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 9f));
178 acc.setHint(R.string.new_transaction_account_hint);
181 final EditText amt = new EditText(this);
182 amt.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
183 amt.setHint(R.string.new_transaction_amount_hint);
185 amt.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL );
186 amt.setMinWidth(dp2px(40));
187 amt.setTextAlignment(EditText.TEXT_ALIGNMENT_VIEW_END);
189 final TableRow row = new TableRow(this);
190 row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));
197 hook_swipe_listener(row);
198 hook_autocompletion_adapter(row);
203 fab.setEnabled(true);
207 private void reset_form() {
208 text_date.setText("");
209 text_descr.setText("");
210 while(table.getChildCount() > 2) {
211 table.removeViewAt(2);
213 for( int i = 0; i < 2; i++ ) {
214 TableRow tr = (TableRow) table.getChildAt(i);
215 if ( tr == null) break;
217 ((TextView)tr.getChildAt(0)).setText("");
218 ((TextView)tr.getChildAt(1)).setText("");
221 text_descr.requestFocus();