move hook_autocompletion_adapter to MLDB
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 22 Dec 2018 11:24:39 +0000 (11:24 +0000)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 22 Dec 2018 11:24:39 +0000 (11:24 +0000)
will be used in other places too

app/src/main/java/net/ktnx/mobileledger/NewTransactionActivity.java
app/src/main/java/net/ktnx/mobileledger/utils/MLDB.java

index c34a80043d2bd42ccdf02d5ff0b112a38b628dc4..4e966e1fb41d3eaadbe7ab60b39e959012161b75 100644 (file)
 package net.ktnx.mobileledger;
 
 import android.annotation.SuppressLint;
-import android.annotation.TargetApi;
-import android.database.Cursor;
-import android.database.MatrixCursor;
-import android.database.sqlite.SQLiteDatabase;
-import android.os.Build;
 import android.os.Bundle;
 import android.preference.PreferenceManager;
-import android.provider.FontsContract;
 import android.support.design.widget.BaseTransientBottomBar;
 import android.support.design.widget.Snackbar;
 import android.support.v4.app.DialogFragment;
@@ -44,9 +38,7 @@ import android.view.View;
 import android.view.inputmethod.EditorInfo;
 import android.widget.AutoCompleteTextView;
 import android.widget.EditText;
-import android.widget.FilterQueryProvider;
 import android.widget.ProgressBar;
-import android.widget.SimpleCursorAdapter;
 import android.widget.TableLayout;
 import android.widget.TableRow;
 import android.widget.TextView;
@@ -96,7 +88,8 @@ public class NewTransactionActivity extends AppCompatActivity implements TaskCal
             }
         });
         text_descr = findViewById(R.id.new_transaction_description);
-        hook_autocompletion_adapter(text_descr, MLDB.DESCRIPTION_HISTORY_TABLE, "description");
+        MLDB.hook_autocompletion_adapter(this, text_descr, MLDB.DESCRIPTION_HISTORY_TABLE,
+                "description");
         hook_text_change_listener(text_descr);
 
         progress = findViewById(R.id.save_transaction_progress);
@@ -108,7 +101,7 @@ public class NewTransactionActivity extends AppCompatActivity implements TaskCal
             AutoCompleteTextView acc_name_view = (AutoCompleteTextView) row.getChildAt(0);
             TextView amount_view = (TextView) row.getChildAt(1);
             hook_swipe_listener(row);
-            hook_autocompletion_adapter(acc_name_view, MLDB.ACCOUNTS_TABLE, "name");
+            MLDB.hook_autocompletion_adapter(this, acc_name_view, MLDB.ACCOUNTS_TABLE, "name");
             hook_text_change_listener(acc_name_view);
             hook_text_change_listener(amount_view);
 //            Log.d("swipe", "hooked to row "+i);
@@ -243,56 +236,6 @@ public class NewTransactionActivity extends AppCompatActivity implements TaskCal
 
     }
 
-    @TargetApi(Build.VERSION_CODES.N)
-    private void hook_autocompletion_adapter(final AutoCompleteTextView view, final String table,
-                                             final String field) {
-        String[] from = {field};
-        int[] to = {android.R.id.text1};
-        SimpleCursorAdapter adapter =
-                new SimpleCursorAdapter(this, android.R.layout.simple_dropdown_item_1line, null,
-                        from, to, 0);
-        adapter.setStringConversionColumn(1);
-
-        FilterQueryProvider provider = new FilterQueryProvider() {
-            @Override
-            public Cursor runQuery(CharSequence constraint) {
-                if (constraint == null) return null;
-
-                String str = constraint.toString().toUpperCase();
-                Log.d("autocompletion", "Looking for " + str);
-                String[] col_names = {FontsContract.Columns._ID, field};
-                MatrixCursor c = new MatrixCursor(col_names);
-
-                try (SQLiteDatabase db = MLDB.getReadableDatabase(getApplicationContext())) {
-
-                    try (Cursor matches = db.rawQuery(String.format(
-                            "SELECT %s as a, case when %s_upper LIKE ?||'%%' then 1 " +
-                            "WHEN %s_upper LIKE '%%:'||?||'%%' then 2 " +
-                            "WHEN %s_upper LIKE '%% '||?||'%%' then 3 " + "else 9 end " +
-                            "FROM %s " + "WHERE %s_upper LIKE '%%'||?||'%%' " + "ORDER BY 2, 1;",
-                            field, field, field, field, table, field),
-                            new String[]{str, str, str, str}))
-                    {
-                        int i = 0;
-                        while (matches.moveToNext()) {
-                            String match = matches.getString(0);
-                            int order = matches.getInt(1);
-                            Log.d("autocompletion", String.format("match: %s |%d", match, order));
-                            c.newRow().add(i++).add(match);
-                        }
-                    }
-
-                    return c;
-                }
-
-            }
-        };
-
-        adapter.setFilterQueryProvider(provider);
-
-        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);
@@ -355,7 +298,7 @@ public class NewTransactionActivity extends AppCompatActivity implements TaskCal
         if (focus) acc.requestFocus();
 
         hook_swipe_listener(row);
-        hook_autocompletion_adapter(acc, MLDB.ACCOUNTS_TABLE, "name");
+        MLDB.hook_autocompletion_adapter(this, acc, MLDB.ACCOUNTS_TABLE, "name");
         hook_text_change_listener(acc);
         hook_text_change_listener(amt);
     }
index 4f627779b93fb238c9cbe23c4531ff4b19f4232c..d528a37a9ccb9e17dba0c20f21ec3c32abc910c3 100644 (file)
 
 package net.ktnx.mobileledger.utils;
 
+import android.annotation.TargetApi;
 import android.content.Context;
 import android.content.res.Resources;
 import android.database.Cursor;
+import android.database.MatrixCursor;
 import android.database.SQLException;
 import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteOpenHelper;
+import android.os.Build;
+import android.provider.FontsContract;
 import android.util.Log;
+import android.widget.AutoCompleteTextView;
+import android.widget.FilterQueryProvider;
+import android.widget.SimpleCursorAdapter;
 
 import java.io.BufferedReader;
 import java.io.IOException;
@@ -114,6 +121,56 @@ public final class MLDB {
     static public void set_option_value(Context context, String name, long value) {
         set_option_value(context, name, String.valueOf(value));
     }
+    @TargetApi(Build.VERSION_CODES.N)
+    public static void hook_autocompletion_adapter(final Context context,
+                                                   final AutoCompleteTextView view,
+                                                   final String table, final String field) {
+        String[] from = {field};
+        int[] to = {android.R.id.text1};
+        SimpleCursorAdapter adapter =
+                new SimpleCursorAdapter(context, android.R.layout.simple_dropdown_item_1line, null,
+                        from, to, 0);
+        adapter.setStringConversionColumn(1);
+
+        FilterQueryProvider provider = new FilterQueryProvider() {
+            @Override
+            public Cursor runQuery(CharSequence constraint) {
+                if (constraint == null) return null;
+
+                String str = constraint.toString().toUpperCase();
+                Log.d("autocompletion", "Looking for " + str);
+                String[] col_names = {FontsContract.Columns._ID, field};
+                MatrixCursor c = new MatrixCursor(col_names);
+
+                try (SQLiteDatabase db = MLDB.getReadableDatabase(context)) {
+
+                    try (Cursor matches = db.rawQuery(String.format(
+                            "SELECT %s as a, case when %s_upper LIKE ?||'%%' then 1 " +
+                            "WHEN %s_upper LIKE '%%:'||?||'%%' then 2 " +
+                            "WHEN %s_upper LIKE '%% '||?||'%%' then 3 " + "else 9 end " +
+                            "FROM %s " + "WHERE %s_upper LIKE '%%'||?||'%%' " + "ORDER BY 2, 1;",
+                            field, field, field, field, table, field),
+                            new String[]{str, str, str, str}))
+                    {
+                        int i = 0;
+                        while (matches.moveToNext()) {
+                            String match = matches.getString(0);
+                            int order = matches.getInt(1);
+                            Log.d("autocompletion", String.format("match: %s |%d", match, order));
+                            c.newRow().add(i++).add(match);
+                        }
+                    }
+
+                    return c;
+                }
+
+            }
+        };
+
+        adapter.setFilterQueryProvider(provider);
+
+        view.setAdapter(adapter);
+    }
 }
 
 class MobileLedgerDatabase extends SQLiteOpenHelper implements AutoCloseable {