]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/NewTransactionActivity.java
auto-completion for the transaction description
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / NewTransactionActivity.java
index 087b87690896a42779cb2e92fe1496b5479ad519..f86780408206b050bfeee566593619d7aeabf50d 100644 (file)
@@ -6,10 +6,8 @@ import android.database.MatrixCursor;
 import android.database.sqlite.SQLiteDatabase;
 import android.os.Build;
 import android.os.Bundle;
-import android.os.Handler;
 import android.preference.PreferenceManager;
 import android.provider.FontsContract;
-import android.support.design.widget.FloatingActionButton;
 import android.support.design.widget.Snackbar;
 import android.support.v4.app.DialogFragment;
 import android.support.v7.app.AppCompatActivity;
@@ -53,51 +51,11 @@ import java.util.Objects;
 
 public class NewTransactionActivity extends AppCompatActivity implements TaskCallback {
     private TableLayout table;
-    private FloatingActionButton fab;
-    boolean fab_should_be_visible;
     private ProgressBar progress;
     private TextView text_date;
-    private TextView text_descr;
+    private AutoCompleteTextView text_descr;
     private static SaveTransactionTask saver;
-    FloatingActionButton.OnVisibilityChangedListener fab_visibility_changed_listener = new FloatingActionButton.OnVisibilityChangedListener() {
-        @Override
-        public void onShown(FloatingActionButton fab) {
-            Log.d("visuals", "FAB shown");
-            super.onShown(fab);
-            if (!fab_should_be_visible) fab.hide();
-        }
-
-        @Override
-        public void onHidden(FloatingActionButton fab) {
-            Log.d("visuals", "FAB hidden");
-            fab.setImageResource(R.drawable.ic_save_white_24dp);
-            fab.setEnabled(true);
-//            super.onHidden(fab);
-            if (fab_should_be_visible) fab.show();
-        }
-    };
-
-    private void hide_fab() {
-        hide_fab(false);
-    }
-
-    private void hide_fab(boolean force) {
-        if (!fab_should_be_visible && !force) return;
-
-        fab_should_be_visible = false;
-        fab.hide(fab_visibility_changed_listener);
-    }
-
-    private void show_fab() {
-        show_fab(false);
-    }
-
-    private void show_fab(boolean force) {
-        if (fab_should_be_visible && !force) return;
-
-        fab_should_be_visible = true;
-        fab.show(fab_visibility_changed_listener);
-    }
+    private MenuItem mSave;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
@@ -108,36 +66,43 @@ public class NewTransactionActivity extends AppCompatActivity implements TaskCal
 
         text_date = findViewById(R.id.new_transaction_date);
         text_descr = findViewById(R.id.new_transaction_description);
+        hook_autocompletion_adapter(text_descr, MobileLedgerDB.DESCRIPTION_HISTORY_TABLE, "description");
 
-        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
-            text_descr.setAutofillHints("");
-        }
-
-        fab = findViewById(R.id.fab);
-        fab.setOnClickListener(new View.OnClickListener() {
-            @Override
-            public void onClick(View view) {
-                new_transaction_save_clicked(view);
-            }
-        });
         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);
+            AutoCompleteTextView acc_name_view = (AutoCompleteTextView) row.getChildAt(0);
             TextView amount_view = (TextView) row.getChildAt(1);
             hook_swipe_listener(row);
-            hook_autocompletion_adapter(row);
+            hook_autocompletion_adapter(acc_name_view, MobileLedgerDB.ACCOUNTS_TABLE, "name");
             hook_text_change_listener(acc_name_view);
             hook_text_change_listener(amount_view);
 //            Log.d("swipe", "hooked to row "+i);
         }
     }
 
-    public void new_transaction_save_clicked(View view) {
-        fab.setEnabled(false);
+    @Override
+    public void finish() {
+        super.finish();
+        overridePendingTransition(R.anim.dummy, R.anim.slide_out_right);
+    }
+
+    @Override
+    public boolean onOptionsItemSelected(MenuItem item) {
+        switch (item.getItemId()) {
+            case android.R.id.home:
+                finish();
+                return true;
+        }
+        return super.onOptionsItemSelected(item);
+    }
+
+    public void save_transaction() {
+        if (mSave != null) mSave.setVisible(false);
+        toggle_all_editing(false);
         progress.setVisibility(View.VISIBLE);
 
         saver = new SaveTransactionTask(this);
@@ -159,6 +124,17 @@ public class NewTransactionActivity extends AppCompatActivity implements TaskCal
         }
         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() {
@@ -204,12 +180,11 @@ public class NewTransactionActivity extends AppCompatActivity implements TaskCal
     }
 
     @TargetApi(Build.VERSION_CODES.N)
-    private void hook_autocompletion_adapter(final TableRow row) {
-        String[] from = {"name"};
+    private void hook_autocompletion_adapter(final AutoCompleteTextView view, final String table, final String field) {
+        String[] from = {field};
         int[] to = {android.R.id.text1};
         SQLiteDatabase db = MobileLedgerDB.db;
 
-        AutoCompleteTextView acc = (AutoCompleteTextView) row.getChildAt(0);
         SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_dropdown_item_1line, null, from, to, 0);
         adapter.setStringConversionColumn(1);
 
@@ -220,17 +195,17 @@ public class NewTransactionActivity extends AppCompatActivity implements TaskCal
 
                 String str = constraint.toString().toUpperCase();
                 Log.d("autocompletion", "Looking for "+str);
-                String[] col_names = {FontsContract.Columns._ID, "name"};
+                String[] col_names = {FontsContract.Columns._ID, field};
                 MatrixCursor c = new MatrixCursor(col_names);
 
-                Cursor matches = db.rawQuery("SELECT name FROM accounts WHERE UPPER(name) LIKE '%'||?||'%' ORDER BY name;", new String[]{str});
+                Cursor matches = db.rawQuery(String.format("SELECT %s FROM %s WHERE UPPER(%s) LIKE '%%'||?||'%%' ORDER BY 1;", field, table, field), new String[]{str});
 
                 try {
                     int i = 0;
                     while (matches.moveToNext()) {
-                        String name = matches.getString(0);
-                        Log.d("autocompletion-match", name);
-                        c.newRow().add(i++).add(name);
+                        String match = matches.getString(0);
+                        Log.d("autocompletion", String.format("match: %s", match));
+                        c.newRow().add(i++).add(match);
                     }
                 }
                 finally {
@@ -244,12 +219,14 @@ public class NewTransactionActivity extends AppCompatActivity implements TaskCal
 
         adapter.setFilterQueryProvider(provider);
 
-        acc.setAdapter(adapter);
+        view.setAdapter(adapter);
     }
 
     public boolean onCreateOptionsMenu(Menu menu) {
         // Inflate the menu; this adds items to the action bar if it is present.
         getMenuInflater().inflate(R.menu.new_transaction, menu);
+        mSave = menu.findItem(R.id.action_submit_transaction);
+        if (mSave == null) throw new AssertionError();
 
         return true;
     }
@@ -286,7 +263,7 @@ public class NewTransactionActivity extends AppCompatActivity implements TaskCal
         if (focus) acc.requestFocus();
 
         hook_swipe_listener(row);
-        hook_autocompletion_adapter(row);
+        hook_autocompletion_adapter(acc, MobileLedgerDB.ACCOUNTS_TABLE, "name");
         hook_text_change_listener(acc);
         hook_text_change_listener(amt);
     }
@@ -295,6 +272,10 @@ public class NewTransactionActivity extends AppCompatActivity implements TaskCal
         do_add_account_row(true);
     }
 
+    public void saveTransactionFromMenu(MenuItem item) {
+        save_transaction();
+    }
+
     private void check_transaction_submittable() {
         TableLayout table = findViewById(R.id.new_transaction_accounts_table);
         int accounts = 0;
@@ -322,27 +303,19 @@ public class NewTransactionActivity extends AppCompatActivity implements TaskCal
         }
 
         if ((accounts >= 2) && (accounts_with_values >= (accounts - 1))) {
-            show_fab();
-        } else hide_fab();
+            if (mSave != null) mSave.setVisible(true);
+        } else {
+            if (mSave != null) mSave.setVisible(false);
+        }
     }
 
     @Override
     public void done() {
-        fab.setImageResource(R.drawable.ic_check_white_24dp);
         progress.setVisibility(View.INVISIBLE);
         Log.d("visuals", "hiding progress");
 
-        fab_should_be_visible = false;
-        final Handler fade_out = new Handler();
-        fade_out.postDelayed(new Runnable() {
-            @Override
-            public void run() {
-                Log.d("visuals", "hiding FAB");
-
-                hide_fab(true);
-            }
-        }, 1000);
         reset_form();
+        toggle_all_editing(true);
     }
 
     private void reset_form() {