From 22abd84a5986a1b259b0e89d32669cbeb977a72b Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Mon, 15 Feb 2021 22:41:18 +0200 Subject: [PATCH] fix duplicate accounts in template account autocompletion visible when multiple profiles contain accounts with the same name --- .../net/ktnx/mobileledger/dao/AccountDAO.java | 42 ++++++++++++------- .../db/AccountAutocompleteAdapter.java | 8 ++-- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/dao/AccountDAO.java b/app/src/main/java/net/ktnx/mobileledger/dao/AccountDAO.java index 3bdf431d..31aafbf2 100644 --- a/app/src/main/java/net/ktnx/mobileledger/dao/AccountDAO.java +++ b/app/src/main/java/net/ktnx/mobileledger/dao/AccountDAO.java @@ -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 { // @Query("SELECT * FROM patterns") // List getPatternsWithAccounts(); - @Query("SELECT *, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " + + static public List unbox(List list) { + ArrayList 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> lookupInProfileByName(@NonNull String profileUUID, - @NonNull String term); + public abstract LiveData> 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 lookupInProfileByNameSync(@NonNull String profileUUID, - @NonNull String term); + public abstract List 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> lookupByName(@NonNull String term); + public abstract LiveData> 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 lookupByNameSync(@NonNull String term); + public abstract List lookupByNameSync(@NonNull String term); + + static public class AccountNameContainer { + @ColumnInfo + public String name; + @ColumnInfo + public int ordering; + } } diff --git a/app/src/main/java/net/ktnx/mobileledger/db/AccountAutocompleteAdapter.java b/app/src/main/java/net/ktnx/mobileledger/db/AccountAutocompleteAdapter.java index 9eabeb91..6f18225d 100644 --- a/app/src/main/java/net/ktnx/mobileledger/db/AccountAutocompleteAdapter.java +++ b/app/src/main/java/net/ktnx/mobileledger/db/AccountAutocompleteAdapter.java @@ -28,7 +28,7 @@ import net.ktnx.mobileledger.dao.AccountDAO; import java.util.ArrayList; import java.util.List; -public class AccountAutocompleteAdapter extends ArrayAdapter { +public class AccountAutocompleteAdapter extends ArrayAdapter { private final AccountFilter filter = new AccountFilter(); private final AccountDAO dao = DB.get() .getAccountDAO(); @@ -66,10 +66,10 @@ public class AccountAutocompleteAdapter extends ArrayAdapter { return results; } - final List matches = + final List 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 { else { setNotifyOnChange(false); clear(); - addAll((List) results.values); + addAll((List) results.values); notifyDataSetChanged(); } } -- 2.39.5