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;
34 * TODO: auto-fill of transaction description
35 * if Android O's implementation won't work, add a custom one
36 * TODO: nicer progress while transaction is submitted
37 * TODO: periodic and manual refresh of available accounts
38 * (now done forcibly each time the main activity is started)
39 * TODO: latest transactions, maybe with browsing further in the past?
41 * TODO: get rid of the custom session/cookie and auth code?
42 * (the last problem with the POST was the missing content-length header)
44 * TODO: nicer swiping removal with visual feedback
45 * TODO: activity with current balance
47 * TODO: update accounts/check settings upon change of backend settings
50 public class NewTransactionActivity extends AppCompatActivity implements TaskCallback {
51 private TableLayout table;
52 private FloatingActionButton fab;
53 private TextView text_date;
54 private TextView text_descr;
55 private static SaveTransactionTask saver;
58 protected void onCreate(Bundle savedInstanceState) {
59 super.onCreate(savedInstanceState);
60 setContentView(R.layout.activity_new_transaction);
61 Toolbar toolbar = findViewById(R.id.toolbar);
62 setSupportActionBar(toolbar);
64 text_date = findViewById(R.id.new_transaction_date);
65 text_descr = findViewById(R.id.new_transaction_description);
67 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
68 text_descr.setAutofillHints("");
71 fab = findViewById(R.id.fab);
72 fab.setOnClickListener(new View.OnClickListener() {
74 public void onClick(View view) {
75 new_transaction_save_clicked(view);
78 Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
79 table = findViewById(R.id.new_transaction_accounts_table);
80 for (int i = 0; i < table.getChildCount(); i++) {
81 hook_swipe_listener((TableRow)table.getChildAt(i));
82 hook_autocompletion_adapter((TableRow)table.getChildAt(i));
83 // Log.d("swipe", "hooked to row "+i);
87 public void new_transaction_save_clicked(View view) {
88 fab.setEnabled(false);
90 saver = new SaveTransactionTask(this);
92 saver.setPref(PreferenceManager.getDefaultSharedPreferences(this));
93 LedgerTransaction tr = new LedgerTransaction(text_date.getText().toString(), text_descr.getText().toString());
95 TableLayout table = findViewById(R.id.new_transaction_accounts_table);
96 for ( int i = 0; i < table.getChildCount(); i++ ) {
97 TableRow row = (TableRow) table.getChildAt(i);
98 String acc = ((TextView) row.getChildAt(0)).getText().toString();
99 String amt = ((TextView) row.getChildAt(1)).getText().toString();
100 LedgerTransactionItem item =
102 ? new LedgerTransactionItem( acc, Float.parseFloat(amt))
103 : new LedgerTransactionItem( acc );
109 private void hook_swipe_listener(final TableRow row) {
110 row.getChildAt(0).setOnTouchListener(new OnSwipeTouchListener(this) {
111 public void onSwipeLeft() {
112 // Log.d("swipe", "LEFT" + row.getId());
113 if (table.getChildCount() > 2) {
114 table.removeView(row);
115 // Toast.makeText(NewTransactionActivity.this, "LEFT", Toast.LENGTH_LONG).show();
118 Snackbar.make(table, R.string.msg_at_least_two_accounts_are_required, Snackbar.LENGTH_LONG)
119 .setAction("Action", null).show();
123 // public boolean performClick(View view, MotionEvent m) {
126 public boolean onTouch(View view, MotionEvent m) {
127 return gestureDetector.onTouchEvent(m);
132 @TargetApi(Build.VERSION_CODES.N)
133 private void hook_autocompletion_adapter(final TableRow row) {
134 String[] from = {"name"};
135 int[] to = {android.R.id.text1};
136 SQLiteDatabase db = MobileLedgerDB.db;
138 AutoCompleteTextView acc = (AutoCompleteTextView) row.getChildAt(0);
139 SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_dropdown_item_1line, null, from, to, 0);
140 adapter.setStringConversionColumn(1);
142 FilterQueryProvider provider = new FilterQueryProvider() {
144 public Cursor runQuery(CharSequence constraint) {
145 if (constraint == null) return null;
147 String str = constraint.toString().toUpperCase();
148 Log.d("autocompletion", "Looking for "+str);
149 String[] col_names = {FontsContract.Columns._ID, "name"};
150 MatrixCursor c = new MatrixCursor(col_names);
152 Cursor matches = db.rawQuery("SELECT name FROM accounts WHERE UPPER(name) LIKE '%'||?||'%' ORDER BY name;", new String[]{str});
156 while (matches.moveToNext()) {
157 String name = matches.getString(0);
158 Log.d("autocompletion-match", name);
159 c.newRow().add(i++).add(name);
171 adapter.setFilterQueryProvider(provider);
173 acc.setAdapter(adapter);
176 public boolean onCreateOptionsMenu(Menu menu) {
177 // Inflate the menu; this adds items to the action bar if it is present.
178 getMenuInflater().inflate(R.menu.new_transaction, menu);
183 public void pickTransactionDate(View view) {
184 DialogFragment picker = new DatePickerFragment();
185 picker.show(getSupportFragmentManager(), "datePicker");
188 public int dp2px(float dp) {
189 return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics()));
192 public void addTransactionAccountFromMenu(MenuItem item) {
193 final AutoCompleteTextView acc = new AutoCompleteTextView(this);
194 acc.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 9f));
195 acc.setHint(R.string.new_transaction_account_hint);
198 final EditText amt = new EditText(this);
199 amt.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
200 amt.setHint(R.string.new_transaction_amount_hint);
202 amt.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL );
203 amt.setMinWidth(dp2px(40));
204 amt.setTextAlignment(EditText.TEXT_ALIGNMENT_VIEW_END);
206 final TableRow row = new TableRow(this);
207 row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));
214 hook_swipe_listener(row);
215 hook_autocompletion_adapter(row);
220 fab.setEnabled(true);
224 private void reset_form() {
225 text_date.setText("");
226 text_descr.setText("");
227 while(table.getChildCount() > 2) {
228 table.removeViewAt(2);
230 for( int i = 0; i < 2; i++ ) {
231 TableRow tr = (TableRow) table.getChildAt(i);
232 if ( tr == null) break;
234 ((TextView)tr.getChildAt(0)).setText("");
235 ((TextView)tr.getChildAt(1)).setText("");
238 text_descr.requestFocus();