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