From 1ab0f55ef17a0129baa91c4a1c2ad902d8f485ef Mon Sep 17 00:00:00 2001 From: Avently <7953703+avently@users.noreply.github.com> Date: Thu, 15 Dec 2022 14:00:36 +0300 Subject: [PATCH] android: Optimized chats snapshotFlow --- .../chat/simplex/app/views/chat/ChatView.kt | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/apps/android/app/src/main/java/chat/simplex/app/views/chat/ChatView.kt b/apps/android/app/src/main/java/chat/simplex/app/views/chat/ChatView.kt index 3f4278bf08..bb606a23a5 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/views/chat/ChatView.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/views/chat/ChatView.kt @@ -77,16 +77,15 @@ fun ChatView(chatId: String, chatModel: ChatModel, onComposed: () -> Unit) { } } launch { - // .toList() is important for making observation working - snapshotFlow { chatModel.chats.toList() } + snapshotFlow { + runCatching { chatModel.chats.firstOrNull { chat -> chat.chatInfo.id == chatModel.chatId.value } } + .onFailure { Log.e(TAG, it.stackTraceToString()) } + .getOrNull() + } .distinctUntilChanged() - .collect { chats -> - chats.firstOrNull { chat -> chat.chatInfo.id == chatModel.chatId.value }.let { - // Only changed chatInfo is important thing. Other properties can be skipped for reducing recompositions - if (it?.chatInfo != activeChat.value?.chatInfo) { - activeChat.value = it - }} - } + // Only changed chatInfo is important thing. Other properties can be skipped for reducing recompositions + .filter { it?.chatInfo != activeChat.value?.chatInfo && it != null } + .collect { activeChat.value = it } } } val view = LocalView.current