From 26d856e91f7c6431c69797b279b3cee136b3476d Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 7 Mar 2023 20:43:16 +0100 Subject: [PATCH] support chat filters --- .../android/database/dao/ChatDao.java | 24 +++++++++++++-- .../android/database/model/Account.java | 9 ++++-- .../database/model/AccountIdentifier.java | 2 +- .../android/database/model/ChatFilter.java | 2 +- .../database/model/GroupIdentifier.java | 2 +- .../android/repository/ChatRepository.java | 5 ++-- .../android/ui/model/OverviewViewModel.java | 30 +++++++++++-------- 7 files changed, 51 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/im/conversations/android/database/dao/ChatDao.java b/app/src/main/java/im/conversations/android/database/dao/ChatDao.java index 6a198f8b0..8a311de85 100644 --- a/app/src/main/java/im/conversations/android/database/dao/ChatDao.java +++ b/app/src/main/java/im/conversations/android/database/dao/ChatDao.java @@ -10,6 +10,8 @@ import com.google.common.util.concurrent.ListenableFuture; import im.conversations.android.database.entity.ChatEntity; import im.conversations.android.database.entity.MucStatusCodeEntity; import im.conversations.android.database.model.Account; +import im.conversations.android.database.model.AccountIdentifier; +import im.conversations.android.database.model.ChatFilter; import im.conversations.android.database.model.ChatIdentifier; import im.conversations.android.database.model.ChatOverviewItem; import im.conversations.android.database.model.ChatType; @@ -206,7 +208,23 @@ public abstract class ChatDao { + " vCardPhoto,(SELECT thumb_id FROM avatar WHERE avatar.address=c.address) as" + " avatar FROM CHAT c LEFT JOIN message m ON (c.id=m.chatId) LEFT OUTER JOIN" + " message m2 ON (c.id = m2.chatId AND (m.receivedAt < m2.receivedAt OR" - + " (m.receivedAt = m2.receivedAt AND m.id < m2.id))) WHERE c.archived=0 AND m2.id" - + " IS NULL ORDER by m.receivedAt DESC") - public abstract PagingSource getChatOverview(); + + " (m.receivedAt = m2.receivedAt AND m.id < m2.id))) WHERE (:accountId IS NULL OR" + + " c.accountId=:accountId) AND (:groupId IS NULL OR (c.address IN(SELECT" + + " roster.address FROM roster JOIN roster_group ON" + + " roster.id=roster_group.rosterItemId WHERE roster_group.groupId=:groupId) OR" + + " c.address IN(SELECT address FROM bookmark JOIN bookmark_group ON" + + " bookmark.id=bookmark_group.bookmarkId WHERE bookmark_group.groupId=:groupId)))" + + " AND c.archived=0 AND m2.id IS NULL ORDER by m.receivedAt DESC") + public abstract PagingSource getChatOverview( + final Long accountId, final Long groupId); + + public PagingSource getChatOverview(final ChatFilter chatFilter) { + if (chatFilter instanceof AccountIdentifier account) { + return getChatOverview(account.id, null); + } else if (chatFilter instanceof GroupIdentifier group) { + return getChatOverview(null, group.id); + } else { + return getChatOverview(null, null); + } + } } diff --git a/app/src/main/java/im/conversations/android/database/model/Account.java b/app/src/main/java/im/conversations/android/database/model/Account.java index 306aa38d7..ad0c38ba6 100644 --- a/app/src/main/java/im/conversations/android/database/model/Account.java +++ b/app/src/main/java/im/conversations/android/database/model/Account.java @@ -14,14 +14,19 @@ import java.util.UUID; import org.jxmpp.jid.BareJid; import org.jxmpp.jid.parts.Resourcepart; -public class Account extends AccountIdentifier { +public class Account { + + public final long id; + @NonNull public final BareJid address; @NonNull public final byte[] randomSeed; public Account(final long id, @NonNull final BareJid address, @NonNull byte[] randomSeed) { - super(id, address); + Preconditions.checkNotNull(address, "Account can not be instantiated without an address"); Preconditions.checkArgument( randomSeed.length == 32, "RandomSeed must have exactly 32 bytes"); + this.id = id; + this.address = address; this.randomSeed = randomSeed; } diff --git a/app/src/main/java/im/conversations/android/database/model/AccountIdentifier.java b/app/src/main/java/im/conversations/android/database/model/AccountIdentifier.java index 39c3206ad..4bb4940ff 100644 --- a/app/src/main/java/im/conversations/android/database/model/AccountIdentifier.java +++ b/app/src/main/java/im/conversations/android/database/model/AccountIdentifier.java @@ -6,7 +6,7 @@ import com.google.common.base.Objects; import com.google.common.base.Preconditions; import org.jxmpp.jid.BareJid; -public class AccountIdentifier implements ChatFilter { +public final class AccountIdentifier implements ChatFilter { public final long id; @NonNull public final BareJid address; diff --git a/app/src/main/java/im/conversations/android/database/model/ChatFilter.java b/app/src/main/java/im/conversations/android/database/model/ChatFilter.java index cc9ee1072..0e8d024e2 100644 --- a/app/src/main/java/im/conversations/android/database/model/ChatFilter.java +++ b/app/src/main/java/im/conversations/android/database/model/ChatFilter.java @@ -1,3 +1,3 @@ package im.conversations.android.database.model; -public interface ChatFilter {} +public sealed interface ChatFilter permits AccountIdentifier, GroupIdentifier {} diff --git a/app/src/main/java/im/conversations/android/database/model/GroupIdentifier.java b/app/src/main/java/im/conversations/android/database/model/GroupIdentifier.java index 11fcb8e64..e88b10787 100644 --- a/app/src/main/java/im/conversations/android/database/model/GroupIdentifier.java +++ b/app/src/main/java/im/conversations/android/database/model/GroupIdentifier.java @@ -4,7 +4,7 @@ import androidx.annotation.NonNull; import com.google.common.base.MoreObjects; import com.google.common.base.Objects; -public class GroupIdentifier implements ChatFilter { +public final class GroupIdentifier implements ChatFilter { public final long id; public final String name; diff --git a/app/src/main/java/im/conversations/android/repository/ChatRepository.java b/app/src/main/java/im/conversations/android/repository/ChatRepository.java index 01e710c53..0a50f6ecb 100644 --- a/app/src/main/java/im/conversations/android/repository/ChatRepository.java +++ b/app/src/main/java/im/conversations/android/repository/ChatRepository.java @@ -3,6 +3,7 @@ package im.conversations.android.repository; import android.content.Context; import androidx.lifecycle.LiveData; import androidx.paging.PagingSource; +import im.conversations.android.database.model.ChatFilter; import im.conversations.android.database.model.ChatOverviewItem; import im.conversations.android.database.model.GroupIdentifier; import java.util.List; @@ -17,7 +18,7 @@ public class ChatRepository extends AbstractRepository { return this.database.chatDao().getGroups(); } - public PagingSource getChatOverview() { - return this.database.chatDao().getChatOverview(); + public PagingSource getChatOverview(final ChatFilter chatFilter) { + return this.database.chatDao().getChatOverview(chatFilter); } } diff --git a/app/src/main/java/im/conversations/android/ui/model/OverviewViewModel.java b/app/src/main/java/im/conversations/android/ui/model/OverviewViewModel.java index 1b584ab37..3cd6516e9 100644 --- a/app/src/main/java/im/conversations/android/ui/model/OverviewViewModel.java +++ b/app/src/main/java/im/conversations/android/ui/model/OverviewViewModel.java @@ -5,6 +5,7 @@ import androidx.annotation.NonNull; import androidx.lifecycle.AndroidViewModel; import androidx.lifecycle.LiveData; import androidx.lifecycle.MediatorLiveData; +import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.Transformations; import androidx.lifecycle.ViewModelKt; import androidx.paging.Pager; @@ -29,11 +30,10 @@ public class OverviewViewModel extends AndroidViewModel { private final LiveData> accounts; private final LiveData> groups; private final MediatorLiveData chatFilterAvailable = new MediatorLiveData<>(); + private final MutableLiveData chatFilter = new MutableLiveData<>(null); private static final Logger LOGGER = LoggerFactory.getLogger(OverviewViewModel.class); - private ChatFilter chatFilter; - public OverviewViewModel(@NonNull Application application) { super(application); this.accountRepository = new AccountRepository(application); @@ -65,24 +65,28 @@ public class OverviewViewModel extends AndroidViewModel { } public ChatFilter getChatFilter() { - return this.chatFilter; + return this.chatFilter.getValue(); } public void setChatFilter(final ChatFilter chatFilter) { - this.chatFilter = chatFilter; + this.chatFilter.postValue(chatFilter); LOGGER.info("Setting chat filter to {}", chatFilter); } public LiveData> getChats() { - final Pager pager = - new Pager<>( - new PagingConfig(20), - () -> { - return this.chatRepository.getChatOverview(); - }); + return Transformations.switchMap( + this.chatFilter, + input -> { + final Pager pager = + new Pager<>( + new PagingConfig(20), + () -> { + return chatRepository.getChatOverview(input); + }); - LiveData> foo = PagingLiveData.getLiveData(pager); - CoroutineScope viewModelScope = ViewModelKt.getViewModelScope(this); - return PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), viewModelScope); + CoroutineScope viewModelScope = ViewModelKt.getViewModelScope(this); + return PagingLiveData.cachedIn( + PagingLiveData.getLiveData(pager), viewModelScope); + }); } }