Skip to content

Commit 9c8cae7

Browse files
committed
update pinned messages by system messages
Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
1 parent 11b2d9e commit 9c8cae7

3 files changed

Lines changed: 22 additions & 0 deletions

File tree

app/src/main/java/com/nextcloud/talk/chat/data/ChatMessageRepository.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ interface ChatMessageRepository : LifecycleAwareManager {
4040

4141
val lastReadMessageFlow: Flow<Int>
4242

43+
val roomRefreshFlow: Flow<Unit>
44+
4345
// /**
4446
// * Used for informing the user of the underlying processing behind offline support, [String] is the key
4547
// * which is handled in a switch statement in ChatActivity.

app/src/main/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepository.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ class OfflineFirstChatRepository @Inject constructor(
9999
private val _lastReadMessageFlow:
100100
MutableSharedFlow<Int> = MutableSharedFlow()
101101

102+
override val roomRefreshFlow: Flow<Unit>
103+
get() = _roomRefreshFlow
104+
105+
private val _roomRefreshFlow: MutableSharedFlow<Unit> = MutableSharedFlow()
106+
102107
// override val generalUIFlow: Flow<String>
103108
// get() = _generalUIFlow
104109
//
@@ -729,6 +734,7 @@ class OfflineFirstChatRepository @Inject constructor(
729734
}
730735

731736
private suspend fun handleSystemMessagesThatAffectDatabase(messagesJson: List<ChatMessageJson>) {
737+
var hasPinnedMessageChange = false
732738
messagesJson.forEach { messageJson ->
733739
when (messageJson.systemMessageType) {
734740
ChatMessage.SystemMessageType.REACTION,
@@ -754,9 +760,13 @@ class OfflineFirstChatRepository @Inject constructor(
754760
chatBlocksDao.deleteChatBlocksOlderThan(internalConversationId, messageJson.id)
755761
}
756762

763+
ChatMessage.SystemMessageType.MESSAGE_PINNED,
764+
ChatMessage.SystemMessageType.MESSAGE_UNPINNED -> hasPinnedMessageChange = true
765+
757766
else -> {}
758767
}
759768
}
769+
if (hasPinnedMessageChange) _roomRefreshFlow.emit(Unit)
760770
}
761771

762772
// the parent message is always the newest state, no matter how old the system message is.

app/src/main/java/com/nextcloud/talk/chat/viewmodels/ChatViewModel.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ import kotlinx.coroutines.flow.StateFlow
7777
import kotlinx.coroutines.flow.asSharedFlow
7878
import kotlinx.coroutines.flow.catch
7979
import kotlinx.coroutines.flow.combine
80+
import kotlinx.coroutines.flow.debounce
8081
import kotlinx.coroutines.flow.distinctUntilChanged
8182
import kotlinx.coroutines.flow.distinctUntilChangedBy
8283
import kotlinx.coroutines.flow.filterNotNull
@@ -438,6 +439,7 @@ class ChatViewModel @AssistedInject constructor(
438439
observeMessages()
439440
observeMediaPlayerProgressForCompose()
440441
observePinnedMessage()
442+
observeRoomRefresh()
441443
}
442444

443445
private fun observeMediaPlayerProgressForCompose() {
@@ -569,6 +571,13 @@ class ChatViewModel @AssistedInject constructor(
569571
.launchIn(viewModelScope)
570572
}
571573

574+
private fun observeRoomRefresh() {
575+
chatRepository.roomRefreshFlow
576+
.debounce(ROOM_REFRESH_DEBOUNCE_MS)
577+
.onEach { getRoom(chatRoomToken) }
578+
.launchIn(viewModelScope)
579+
}
580+
572581
// val lastCommonReadMessageId = getLastCommonReadFlow.first()
573582

574583
// ------------------------------
@@ -1747,6 +1756,7 @@ class ChatViewModel @AssistedInject constructor(
17471756
const val HTTP_CODE_OK: Int = 200
17481757
private const val WEBSOCKET_CONNECT_TIMEOUT_MS = 3000L
17491758
private const val WEBSOCKET_POLL_INTERVAL_MS = 50L
1759+
private const val ROOM_REFRESH_DEBOUNCE_MS = 500L
17501760
}
17511761

17521762
sealed class OutOfOfficeUIState {

0 commit comments

Comments
 (0)