Observe chatroom list
This commit is contained in:
@@ -6,6 +6,7 @@ import androidx.room3.Dao
|
||||
import androidx.room3.Query
|
||||
import androidx.room3.Transaction
|
||||
import androidx.room3.Upsert
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
interface ChatRoomDao {
|
||||
@@ -17,6 +18,10 @@ interface ChatRoomDao {
|
||||
@Query("SELECT * FROM ChatRoom WHERE userPublicKey = :userPublicKey")
|
||||
fun getChatRoomListByUserPublicKey(userPublicKey: String): List<LocalChatRoom>
|
||||
|
||||
@Transaction
|
||||
@Query("SELECT * FROM ChatRoom WHERE userPublicKey = :userPublicKey")
|
||||
fun observeChatRoomListByUserPublicKey(userPublicKey: String): Flow<List<LocalChatRoom>>
|
||||
|
||||
@Upsert
|
||||
suspend fun upsert(chatRoom: ChatRoom)
|
||||
|
||||
|
||||
@@ -31,6 +31,9 @@ class DatabaseChatRepository(
|
||||
private val scope: CoroutineScope
|
||||
): ChatRepository {
|
||||
val logger = Logger.withTag(TAG)
|
||||
override suspend fun observeChatRoomListByPublicKey(publicKey: String): Flow<List<LocalChatRoom>> {
|
||||
return database.chatRoomDao().observeChatRoomListByUserPublicKey(publicKey)
|
||||
}
|
||||
|
||||
override suspend fun getChatRoomListByPublicKey(publicKey: String): List<LocalChatRoom> {
|
||||
return database.chatRoomDao().getChatRoomListByUserPublicKey(publicKey)
|
||||
|
||||
@@ -13,6 +13,8 @@ import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.flow
|
||||
|
||||
interface ChatRepository {
|
||||
suspend fun observeChatRoomListByPublicKey(publicKey: String): Flow<List<LocalChatRoom>>
|
||||
|
||||
suspend fun getChatRoomListByPublicKey(publicKey: String): List<LocalChatRoom>
|
||||
|
||||
suspend fun getChatRoomByIdentifier(id: String): LocalChatRoom?
|
||||
@@ -39,6 +41,10 @@ interface ChatRepository {
|
||||
|
||||
companion object {
|
||||
val NO_OP_CHAT_REPOSITORY = object: ChatRepository {
|
||||
override suspend fun observeChatRoomListByPublicKey(publicKey: String): Flow<List<LocalChatRoom>> {
|
||||
return flow { }
|
||||
}
|
||||
|
||||
override suspend fun getChatRoomListByPublicKey(publicKey: String): List<LocalChatRoom> {
|
||||
return emptyList()
|
||||
}
|
||||
|
||||
@@ -64,9 +64,9 @@ class ChatRoomListViewModel(
|
||||
fun observeChatRoomFeed() {
|
||||
logger.d("observeChatRoomFeed")
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
chatRepository.getChatRoomListByPublicKey(
|
||||
chatRepository.observeChatRoomListByPublicKey(
|
||||
publicKey
|
||||
).let { chatRooms ->
|
||||
).collect { chatRooms ->
|
||||
logger.d("ChatRoomList: $chatRooms")
|
||||
chatRoomListUIState = ChatRoomListUIState.Loaded(
|
||||
chatRoomList = chatRooms
|
||||
|
||||
Reference in New Issue
Block a user