diff --git a/shared/src/commonMain/kotlin/su/reya/coop/repository/ChatRepository.kt b/shared/src/commonMain/kotlin/su/reya/coop/repository/ChatRepository.kt index 5647b76..6dbe60a 100644 --- a/shared/src/commonMain/kotlin/su/reya/coop/repository/ChatRepository.kt +++ b/shared/src/commonMain/kotlin/su/reya/coop/repository/ChatRepository.kt @@ -1,7 +1,9 @@ package su.reya.coop.repository import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.channels.BufferOverflow import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow @@ -33,6 +35,7 @@ class ChatRepository( private val nostr: Nostr, private val mediaRepository: MediaRepository, private val scope: CoroutineScope, + private val defaultDispatcher: CoroutineDispatcher = Dispatchers.Default, ) : ErrorHost by createErrorHost() { private val _state = MutableStateFlow(ChatState()) val state = _state.stateIn( @@ -58,7 +61,7 @@ class ChatRepository( .stateIn(scope, SharingStarted.WhileSubscribed(5000), false) init { - scope.launch { + scope.launch(defaultDispatcher) { nostr.waitUntilInitialized() // Observe message sync progress @@ -137,7 +140,7 @@ class ChatRepository( } fun refreshChatRooms() { - scope.launch { + scope.launch(defaultDispatcher) { try { val rooms = nostr.messages.getChatRooms() ?: emptySet() _state.update { currentState -> @@ -166,7 +169,7 @@ class ChatRepository( } fun chatRoomConnect(roomId: Long) { - scope.launch { + scope.launch(defaultDispatcher) { try { val room = getChatRoom(roomId) ?: throw IllegalArgumentException("Room not found") val members = room.members @@ -183,7 +186,7 @@ class ChatRepository( showError("Message cannot be empty") return } - scope.launch { + scope.launch(defaultDispatcher) { try { val room = getChatRoom(roomId) ?: throw IllegalArgumentException("Room not found") nostr.messages.sendMessage( @@ -193,7 +196,7 @@ class ChatRepository( replies = replies, onRumorCreated = { event -> updateRoomList(roomId, event) - scope.launch { _newEvents.tryEmit(event) } + scope.launch(defaultDispatcher) { _newEvents.tryEmit(event) } }, ) } catch (e: Exception) { diff --git a/shared/src/commonMain/kotlin/su/reya/coop/viewmodel/ChatScreenViewModel.kt b/shared/src/commonMain/kotlin/su/reya/coop/viewmodel/ChatScreenViewModel.kt index 44b300f..6852380 100644 --- a/shared/src/commonMain/kotlin/su/reya/coop/viewmodel/ChatScreenViewModel.kt +++ b/shared/src/commonMain/kotlin/su/reya/coop/viewmodel/ChatScreenViewModel.kt @@ -39,7 +39,7 @@ class ChatScreenViewModel( private fun loadMessages() { chatRepository.loadChatRoomMessages(id) { initialMessages -> messages.clear() - messages.addAll(initialMessages) + messages.addAll(initialMessages.distinctBy { it.id() }) loading = false } } @@ -52,7 +52,7 @@ class ChatScreenViewModel( viewModelScope.launch { chatRepository.newEvents.collect { event -> if (event.roomId() == id) { - if (event.id() !in messages.map { it.id() }) { + if (messages.none { it.id() == event.id() }) { messages.add(0, event) } } else {