From 4d430cfd25aa2024600f37c1837f90fefea2e529 Mon Sep 17 00:00:00 2001 From: Avently <7953703+avently@users.noreply.github.com> Date: Wed, 27 Dec 2023 21:37:10 +0700 Subject: [PATCH] android, desktop: fix terminal items crash --- .../chat/simplex/common/model/ChatModel.kt | 8 ++++---- .../chat/simplex/common/views/TerminalView.kt | 18 +++--------------- 2 files changed, 7 insertions(+), 19 deletions(-) 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 2305862b68..708bbb9073 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 @@ -58,7 +58,7 @@ object ChatModel { val chatItemStatuses = mutableMapOf() val groupMembers = mutableStateListOf() - val terminalItems = mutableStateListOf() + val terminalItems = mutableStateOf>(listOf()) val userAddress = mutableStateOf(null) // Allows to temporary save servers that are being edited on multiple screens val userSMPServersUnsaved = mutableStateOf<(List)?>(null) @@ -620,10 +620,10 @@ object ChatModel { } fun addTerminalItem(item: TerminalItem) { - if (terminalItems.size >= 500) { - terminalItems.removeAt(0) + if (terminalItems.value.size >= 500) { + terminalItems.value = terminalItems.value.subList(1, terminalItems.value.size) } - terminalItems.add(item) + terminalItems.value += item } val connectedToRemote: Boolean @Composable get() = currentRemoteHost.value != null || remoteCtrlSession.value?.active == true diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/TerminalView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/TerminalView.kt index 9887bfd243..4f01d4a39f 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/TerminalView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/TerminalView.kt @@ -34,7 +34,6 @@ fun TerminalView(chatModel: ChatModel, close: () -> Unit) { close() }) TerminalLayout( - remember { chatModel.terminalItems }, composeState, sendCommand = { sendCommand(chatModel, composeState) }, close @@ -63,7 +62,6 @@ private fun sendCommand(chatModel: ChatModel, composeState: MutableState, composeState: MutableState, sendCommand: () -> Unit, close: () -> Unit @@ -111,7 +109,7 @@ fun TerminalLayout( .fillMaxWidth(), color = MaterialTheme.colors.background ) { - TerminalLog(terminalItems) + TerminalLog() } } } @@ -120,22 +118,13 @@ fun TerminalLayout( private var lazyListState = 0 to 0 @Composable -fun TerminalLog(terminalItems: List) { +fun TerminalLog() { val listState = rememberLazyListState(lazyListState.first, lazyListState.second) DisposableEffect(Unit) { onDispose { lazyListState = listState.firstVisibleItemIndex to listState.firstVisibleItemScrollOffset } } val reversedTerminalItems by remember { - derivedStateOf { - // Such logic prevents concurrent modification - val res = ArrayList() - var i = 0 - while (i < terminalItems.size) { - res.add(terminalItems[i]) - i++ - } - res.asReversed() - } + derivedStateOf { chatModel.terminalItems.value.asReversed() } } val clipboard = LocalClipboardManager.current LazyColumn(state = listState, reverseLayout = true) { @@ -175,7 +164,6 @@ fun TerminalLog(terminalItems: List) { fun PreviewTerminalLayout() { SimpleXTheme { TerminalLayout( - terminalItems = TerminalItem.sampleData, composeState = remember { mutableStateOf(ComposeState(useLinkPreviews = false)) }, sendCommand = {}, close = {}