]> git.ktnx.net Git - mobile-ledger.git/commitdiff
implement account name lookup in Templates' account fields
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 13 Feb 2021 08:10:08 +0000 (10:10 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Thu, 18 Feb 2021 07:19:43 +0000 (07:19 +0000)
app/src/main/java/net/ktnx/mobileledger/dao/AccountDAO.java
app/src/main/java/net/ktnx/mobileledger/db/AccountAutocompleteAdapter.java [new file with mode: 0644]
app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateDetailsAdapter.java
app/src/main/res/layout/template_details_account.xml

index 80bab0cc93dbfa0903cfa6f9a310a724fb55349c..3bdf431d3189f52a17f69c3ae8ec0ad2b28912ed 100644 (file)
@@ -23,6 +23,7 @@ import androidx.room.Dao;
 import androidx.room.Delete;
 import androidx.room.Insert;
 import androidx.room.Query;
+import androidx.room.RoomWarnings;
 import androidx.room.Update;
 
 import net.ktnx.mobileledger.db.Account;
@@ -51,4 +52,40 @@ public abstract class AccountDAO extends BaseDAO<Account> {
 //    @Transaction
 //    @Query("SELECT * FROM patterns")
 //    List<PatternWithAccounts> getPatternsWithAccounts();
+
+    @Query("SELECT *, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
+           "               WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
+           "               WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
+           "               ELSE 9 END AS ordering " + "FROM accounts " +
+           "WHERE profile=:profileUUID AND name_upper LIKE '%%'||:term||'%%' " +
+           "ORDER BY ordering, name_upper, rowid ")
+    @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH)
+    public abstract LiveData<List<Account>> lookupInProfileByName(@NonNull String profileUUID,
+                                                                  @NonNull String term);
+
+    @Query("SELECT *, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
+           "               WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
+           "               WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
+           "               ELSE 9 END AS ordering " + "FROM accounts " +
+           "WHERE profile=:profileUUID AND name_upper LIKE '%%'||:term||'%%' " +
+           "ORDER BY ordering, name_upper, rowid ")
+    @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH)
+    public abstract List<Account> lookupInProfileByNameSync(@NonNull String profileUUID,
+                                                            @NonNull String term);
+
+    @Query("SELECT *, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
+           "               WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
+           "               WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
+           "               ELSE 9 END AS ordering " + "FROM accounts " +
+           "WHERE name_upper LIKE '%%'||:term||'%%' " + "ORDER BY ordering, name_upper, rowid ")
+    @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH)
+    public abstract LiveData<List<Account>> lookupByName(@NonNull String term);
+
+    @Query("SELECT *, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
+           "               WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
+           "               WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
+           "               ELSE 9 END AS ordering " + "FROM accounts " +
+           "WHERE name_upper LIKE '%%'||:term||'%%' " + "ORDER BY ordering, name_upper, rowid ")
+    @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH)
+    public abstract List<Account> lookupByNameSync(@NonNull String term);
 }
diff --git a/app/src/main/java/net/ktnx/mobileledger/db/AccountAutocompleteAdapter.java b/app/src/main/java/net/ktnx/mobileledger/db/AccountAutocompleteAdapter.java
new file mode 100644 (file)
index 0000000..9eabeb9
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ * Copyright © 2021 Damyan Ivanov.
+ * This file is part of MoLe.
+ * MoLe is free software: you can distribute it and/or modify it
+ * under the term of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your opinion), any later version.
+ *
+ * MoLe is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License terms for details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package net.ktnx.mobileledger.db;
+
+import android.content.Context;
+import android.widget.ArrayAdapter;
+import android.widget.Filter;
+
+import androidx.annotation.NonNull;
+
+import net.ktnx.mobileledger.dao.AccountDAO;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class AccountAutocompleteAdapter extends ArrayAdapter<Account> {
+    private final AccountFilter filter = new AccountFilter();
+    private final AccountDAO dao = DB.get()
+                                     .getAccountDAO();
+    private String profileUUID;
+    public AccountAutocompleteAdapter(Context context) {
+        super(context, android.R.layout.simple_dropdown_item_1line, new ArrayList<>());
+    }
+    public void setProfileUUID(String profileUUID) {
+        this.profileUUID = profileUUID;
+    }
+    @NonNull
+    @Override
+    public Filter getFilter() {
+        return filter;
+    }
+    //    @NonNull
+//    @Override
+//    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
+//        View view = convertView;
+//        if (view == null) {
+//            view = LayoutInflater.from(parent.getContext())
+//                                 .inflate(android.R.layout.simple_dropdown_item_1line, parent,
+//                                         false);
+//        }
+//        Account item = getItem(position);
+//        ((TextView) view.findViewById(android.R.id.text1)).setText(item.getName());
+//        return view;
+//    }
+    class AccountFilter extends Filter {
+        @Override
+        protected FilterResults performFiltering(CharSequence constraint) {
+            FilterResults results = new FilterResults();
+            if (constraint == null) {
+                results.count = 0;
+                return results;
+            }
+
+            final List<Account> matches =
+                    (profileUUID == null) ? dao.lookupByNameSync(String.valueOf(constraint))
+                                          : dao.lookupInProfileByNameSync(profileUUID,
+                                                  String.valueOf(constraint));
+            results.values = matches;
+            results.count = matches.size();
+
+            return results;
+        }
+        @Override
+        @SuppressWarnings("unchecked")
+        protected void publishResults(CharSequence constraint, FilterResults results) {
+            if (results.values == null) {
+                notifyDataSetInvalidated();
+            }
+            else {
+                setNotifyOnChange(false);
+                clear();
+                addAll((List<Account>) results.values);
+                notifyDataSetChanged();
+            }
+        }
+    }
+}
index 4f70f8465e3e6182f5ce91c4dd485b0ed3d8f146..46bdba55280bdc182a7b4a0d6be761a5f6a0dd51 100644 (file)
@@ -22,6 +22,7 @@ import android.text.TextWatcher;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.TextView;
 
 import androidx.annotation.NonNull;
 import androidx.appcompat.app.AppCompatActivity;
@@ -32,6 +33,7 @@ import androidx.recyclerview.widget.RecyclerView;
 import net.ktnx.mobileledger.R;
 import net.ktnx.mobileledger.databinding.TemplateDetailsAccountBinding;
 import net.ktnx.mobileledger.databinding.TemplateDetailsHeaderBinding;
+import net.ktnx.mobileledger.db.AccountAutocompleteAdapter;
 import net.ktnx.mobileledger.db.TemplateBase;
 import net.ktnx.mobileledger.model.Data;
 import net.ktnx.mobileledger.model.TemplateDetailsItem;
@@ -448,6 +450,11 @@ class TemplateDetailsAdapter extends RecyclerView.Adapter<TemplateDetailsAdapter
                 }
             };
             b.templateDetailsAccountName.addTextChangedListener(accountNameWatcher);
+            b.templateDetailsAccountName.setAdapter(new AccountAutocompleteAdapter(b.getRoot()
+                                                                                    .getContext()));
+            b.templateDetailsAccountName.setOnItemClickListener(
+                    (parent, view, position, id) -> b.templateDetailsAccountName.setText(
+                            ((TextView) view).getText()));
             TextWatcher accountCommentWatcher = new TextWatcher() {
                 @Override
                 public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
index 93372b7a2492721982a2e124fa5056f4396fb786..2bed3700d314ab776ee24a688392bb428ba605d8 100644 (file)
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toBottomOf="@id/template_details_account_name_source"
+        app:endIconMode="clear_text"
         >
-        <com.google.android.material.textfield.TextInputEditText
+        <com.google.android.material.textfield.MaterialAutoCompleteTextView
+            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
             android:id="@+id/template_details_account_name"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"