]> git.ktnx.net Git - mobile-ledger.git/commitdiff
fix duplicate accounts in template account autocompletion
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Mon, 15 Feb 2021 20:41:18 +0000 (22:41 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Thu, 18 Feb 2021 07:19:43 +0000 (07:19 +0000)
visible when multiple profiles contain accounts with the same name

app/src/main/java/net/ktnx/mobileledger/dao/AccountDAO.java
app/src/main/java/net/ktnx/mobileledger/db/AccountAutocompleteAdapter.java

index 3bdf431d3189f52a17f69c3ae8ec0ad2b28912ed..31aafbf200fdf9dc6a796c55666ced9816e33ee5 100644 (file)
@@ -19,15 +19,16 @@ package net.ktnx.mobileledger.dao;
 
 import androidx.annotation.NonNull;
 import androidx.lifecycle.LiveData;
+import androidx.room.ColumnInfo;
 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;
 
+import java.util.ArrayList;
 import java.util.List;
 
 @Dao
@@ -53,39 +54,50 @@ public abstract class AccountDAO extends BaseDAO<Account> {
 //    @Query("SELECT * FROM patterns")
 //    List<PatternWithAccounts> getPatternsWithAccounts();
 
-    @Query("SELECT *, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
+    static public List<String> unbox(List<AccountNameContainer> list) {
+        ArrayList<String> result = new ArrayList<>(list.size());
+        for (AccountNameContainer item : list) {
+            result.add(item.name);
+        }
+
+        return result;
+    }
+    @Query("SELECT name, 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);
+    public abstract LiveData<List<AccountNameContainer>> lookupInProfileByName(
+            @NonNull String profileUUID, @NonNull String term);
 
-    @Query("SELECT *, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
+    @Query("SELECT name, 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);
+    public abstract List<AccountNameContainer> lookupInProfileByNameSync(
+            @NonNull String profileUUID, @NonNull String term);
 
-    @Query("SELECT *, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
+    @Query("SELECT DISTINCT name, 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);
+    public abstract LiveData<List<AccountNameContainer>> lookupByName(@NonNull String term);
 
-    @Query("SELECT *, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
+    @Query("SELECT DISTINCT name, 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);
+    public abstract List<AccountNameContainer> lookupByNameSync(@NonNull String term);
+
+    static public class AccountNameContainer {
+        @ColumnInfo
+        public String name;
+        @ColumnInfo
+        public int ordering;
+    }
 }
index 9eabeb914ee75b31f4b035f0b68d0f9f7243ed38..6f18225d7bd7e054be463aae98c7d90823b333c0 100644 (file)
@@ -28,7 +28,7 @@ import net.ktnx.mobileledger.dao.AccountDAO;
 import java.util.ArrayList;
 import java.util.List;
 
-public class AccountAutocompleteAdapter extends ArrayAdapter<Account> {
+public class AccountAutocompleteAdapter extends ArrayAdapter<String> {
     private final AccountFilter filter = new AccountFilter();
     private final AccountDAO dao = DB.get()
                                      .getAccountDAO();
@@ -66,10 +66,10 @@ public class AccountAutocompleteAdapter extends ArrayAdapter<Account> {
                 return results;
             }
 
-            final List<Account> matches =
+            final List<String> matches = AccountDAO.unbox(
                     (profileUUID == null) ? dao.lookupByNameSync(String.valueOf(constraint))
                                           : dao.lookupInProfileByNameSync(profileUUID,
-                                                  String.valueOf(constraint));
+                                                  String.valueOf(constraint)));
             results.values = matches;
             results.count = matches.size();
 
@@ -84,7 +84,7 @@ public class AccountAutocompleteAdapter extends ArrayAdapter<Account> {
             else {
                 setNotifyOnChange(false);
                 clear();
-                addAll((List<Account>) results.values);
+                addAll((List<String>) results.values);
                 notifyDataSetChanged();
             }
         }