diff options
author | desperateCoder <echotodevnull@gmail.com> | 2020-07-04 18:02:05 +0300 |
---|---|---|
committer | desperateCoder <echotodevnull@gmail.com> | 2020-07-04 18:02:05 +0300 |
commit | 2bc77a62229e497c472136fc2c4ef4e04b663b9b (patch) | |
tree | d72389d7a372d1445b64e9ab61925d475d61bfe3 /app/src/main | |
parent | b413966b1d98c88b02b38cf1b18003d8bb1d0782 (diff) |
#561: nicer everything.
Diffstat (limited to 'app/src/main')
3 files changed, 33 insertions, 34 deletions
diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/DataBaseAdapter.java b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/DataBaseAdapter.java index 4c0366c93..fe8492c88 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/DataBaseAdapter.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/DataBaseAdapter.java @@ -560,6 +560,10 @@ public class DataBaseAdapter { validateSearchTerm(searchTerm); return db.getUserDao().searchUserByUidOrDisplayNameForACL(accountId, notYetAssignedToACL, "%" + searchTerm.trim() + "%"); } + public List<User> searchUserByUidOrDisplayNameForACLDirectly(final long accountId, final long notYetAssignedToACL, final String searchTerm) { + validateSearchTerm(searchTerm); + return db.getUserDao().searchUserByUidOrDisplayNameForACLDirectly(accountId, notYetAssignedToACL, "%" + searchTerm.trim() + "%"); + } public LiveData<List<Label>> searchNotYetAssignedLabelsByTitle(final long accountId, final long boardId, final long notYetAssignedToLocalCardId, String searchTerm) { validateSearchTerm(searchTerm); diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/dao/UserDao.java b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/dao/UserDao.java index 6f712d07f..1d14a276a 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/dao/UserDao.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/dao/UserDao.java @@ -50,6 +50,16 @@ public interface UserDao extends GenericDao<User> { "ORDER BY u.displayname") LiveData<List<User>> searchUserByUidOrDisplayNameForACL(final long accountId, final long boardId, final String searchTerm); + @Query("SELECT u.* FROM user u WHERE accountId = :accountId " + + " AND NOT EXISTS (" + + " select 1 from accesscontrol ju" + + " where ju.userId = u.localId and ju.boardId = :boardId and status <> 3" + // not LOCAL_DELETED + " ) " + + "and ( uid LIKE :searchTerm or displayname LIKE :searchTerm or primaryKey LIKE :searchTerm ) " + + "and u.localId <> (select b.ownerId from board b where localId = :boardId)" + + "ORDER BY u.displayname") + List<User> searchUserByUidOrDisplayNameForACLDirectly(final long accountId, final long boardId, final String searchTerm); + @Query("SELECT * FROM user WHERE accountId = :accountId and uid = :uid") User getUserByUidDirectly(final long accountId, final String uid); diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/util/extrawurst/UserSearchLiveData.java b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/util/extrawurst/UserSearchLiveData.java index 560b5a078..9f5d49bf5 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/util/extrawurst/UserSearchLiveData.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/util/extrawurst/UserSearchLiveData.java @@ -1,9 +1,7 @@ package it.niedermann.nextcloud.deck.persistence.sync.adapters.db.util.extrawurst; -import androidx.lifecycle.LiveData; import androidx.lifecycle.MediatorLiveData; -import java.util.ArrayList; import java.util.List; import it.niedermann.nextcloud.deck.DeckLog; @@ -21,8 +19,6 @@ public class UserSearchLiveData extends MediatorLiveData<List<User>> implements private static final int DEBOUNCE_TIME = 300; // ms private DataBaseAdapter db; private ServerAdapter server; - private List<User> foundInDB; - private List<User> foundOnServer; long accountId; String searchTerm; long notYetAssignedInACL; @@ -37,6 +33,7 @@ public class UserSearchLiveData extends MediatorLiveData<List<User>> implements this.accountId = accountId; this.searchTerm = searchTerm; this.notYetAssignedInACL = notYetAssignedInACL; + DeckLog.info("###DeckUserSearch: UI triggered! term: " + searchTerm); new Thread(() -> debouncer.call(notYetAssignedInACL)).start(); return this; } @@ -48,7 +45,10 @@ public class UserSearchLiveData extends MediatorLiveData<List<User>> implements return; } - final String term = searchTerm; + final String term = String.copyValueOf(searchTerm.toCharArray()); + + postCurrentFromDB(term); + Account account = db.getAccountByIdDirectly(accountId); server.searchUser(term, new IResponseCallback<OcsUserList>(account) { @Override @@ -56,7 +56,6 @@ public class UserSearchLiveData extends MediatorLiveData<List<User>> implements if (response == null || response.getUsers().isEmpty()){ return; } - List<User> allFound = foundInDB == null? new ArrayList<>() : new ArrayList<>(foundInDB); for (OcsUser user : response.getUsers()) { User existingUser = db.getUserByUidDirectly(accountId, user.getId()); if (existingUser == null) { @@ -65,15 +64,15 @@ public class UserSearchLiveData extends MediatorLiveData<List<User>> implements newUser.setPrimaryKey(user.getId()); newUser.setUid(user.getId()); newUser.setDisplayname(user.getDisplayName()); - long newUserId = db.createUser(accountId, newUser); - newUser.setLocalId(newUserId); - allFound.add(newUser); + db.createUser(accountId, newUser); } } - foundOnServer = allFound; - List<User> distinctList = eliminateDuplicates(allFound); - DeckLog.info("###DeckUserSearch: posted Server value for term " + term + ":\n" + distinctList + " \n\n from Server: " + foundOnServer + " \n\n from DB: " + foundInDB); - postValue(distinctList); + if (!term.equals(searchTerm)) { + // TODO: remove log when stable + DeckLog.info("###DeckUserSearch: skip posting for term " + term + ": current searchTerm is " + searchTerm); + return; + } + postCurrentFromDB(term); } @Override @@ -82,27 +81,13 @@ public class UserSearchLiveData extends MediatorLiveData<List<User>> implements } }); - LiveData<List<User>> dbLiveData = db.searchUserByUidOrDisplayNameForACL(accountId, notYetAssignedInACL, term); - addSource(dbLiveData, changedData -> { - foundInDB = changedData; - removeSource(dbLiveData); - ArrayList<User> users = new ArrayList<>(foundInDB); - if (foundOnServer != null && !foundOnServer.isEmpty()) { - users.addAll(foundOnServer); - } - List<User> distinctList = eliminateDuplicates(users); - postValue(distinctList); - DeckLog.info("###DeckUserSearch: posted db-value for term " + term + ":\n" + distinctList + " \n\n from Server: " + foundOnServer + " \n\n from DB: " + foundInDB); - }); + } - private List<User> eliminateDuplicates(List<User> source) { - List<User> retList = new ArrayList<>(source.size()); - // should be enough like this, since the account doesn't matter here, always the same. - for (User user : source) { - if (!retList.contains(user)){ - retList.add(user); - } - } - return retList; + + private void postCurrentFromDB(String term) { + List<User> foundInDB = db.searchUserByUidOrDisplayNameForACLDirectly(accountId, notYetAssignedInACL, term); + postValue(foundInDB); + // TODO: remove log when stable + DeckLog.info("###DeckUserSearch: posting for term " + term + ": " + foundInDB); } } |