diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/ChatModel.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/ChatModel.kt index 9439412518..93f3e8cc13 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/ChatModel.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/ChatModel.kt @@ -59,7 +59,7 @@ object ChatModel { val ctrlInitInProgress = MutableStateFlow(false) val dbMigrationInProgress = MutableStateFlow(false) val incompleteInitializedDbRemoved = MutableStateFlow(false) - private val _chats = MutableStateFlow(SnapshotStateList()) + private val _chats = MutableStateFlow(ArrayList()) val chats: StateFlow> = _chats private val chatsContext = ChatsContext() // map of connections network statuses, key is agent connection id @@ -2505,16 +2505,16 @@ data class ChatItem ( } } -fun MutableStateFlow>.add(index: Int, elem: Chat) { - value = SnapshotStateList().apply { addAll(value); add(index, elem) } +fun MutableStateFlow>.add(index: Int, elem: Chat) { + value = ArrayList(value).apply { add(index, elem) } } fun MutableStateFlow>.addAndNotify(index: Int, elem: ChatItem) { value = SnapshotStateList().apply { addAll(value); add(index, elem); chatItemsChangesListener?.added(elem.id to elem.isRcvNew, index) } } -fun MutableStateFlow>.add(elem: Chat) { - value = SnapshotStateList().apply { addAll(value); add(elem) } +fun MutableStateFlow>.add(elem: Chat) { + value = ArrayList(value).apply { add(elem) } } // For some reason, Kotlin version crashes if the list is empty @@ -2529,12 +2529,20 @@ fun MutableStateFlow>.addAll(index: Int, elems: List value = SnapshotStateList().apply { addAll(value); addAll(index, elems) } } +fun MutableStateFlow>.addAll(index: Int, elems: List) { + value = ArrayList(value).apply { addAll(index, elems) } +} + fun MutableStateFlow>.addAll(elems: List) { value = SnapshotStateList().apply { addAll(value); addAll(elems) } } -fun MutableStateFlow>.removeAll(block: (Chat) -> Boolean) { - value = SnapshotStateList().apply { addAll(value); removeAll(block) } +fun MutableStateFlow>.addAll(elems: List) { + value = ArrayList(value).apply { addAll(elems) } +} + +fun MutableStateFlow>.removeAll(block: (Chat) -> Boolean) { + value = ArrayList(value).apply { removeAll(block) } } // Removes item(s) from chatItems and notifies a listener about removed item(s) @@ -2555,9 +2563,8 @@ fun MutableStateFlow>.removeAllAndNotify(block: (Cha } } -fun MutableStateFlow>.removeAt(index: Int): Chat { - val new = SnapshotStateList() - new.addAll(value) +fun MutableStateFlow>.removeAt(index: Int): Chat { + val new = ArrayList(value) val res = new.removeAt(index) value = new return res @@ -2585,8 +2592,12 @@ fun MutableStateFlow>.replaceAll(elems: List) { value = SnapshotStateList().apply { addAll(elems) } } -fun MutableStateFlow>.clear() { - value = SnapshotStateList() +fun MutableStateFlow>.replaceAll(elems: List) { + value = ArrayList(elems) +} + +fun MutableStateFlow>.clear() { + value = ArrayList() } // Removes all chatItems and notifies a listener about it