From b58d61c3397f669bde0e0f34e482227ca425f0f3 Mon Sep 17 00:00:00 2001 From: Stanislav Dmitrenko <7953703+avently@users.noreply.github.com> Date: Wed, 25 Oct 2023 04:27:58 +0800 Subject: [PATCH] android: delete files after sharing correctly (#3264) --- .../simplex/common/views/chat/ComposeView.kt | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ComposeView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ComposeView.kt index 972bc6621..84a5879b2 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ComposeView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ComposeView.kt @@ -324,8 +324,26 @@ fun ComposeView( } fun deleteUnusedFiles() { - chatModel.filesToDelete.forEach { it.delete() } - chatModel.filesToDelete.clear() + val shared = chatModel.sharedContent.value + if (shared == null) { + chatModel.filesToDelete.forEach { it.delete() } + chatModel.filesToDelete.clear() + } else { + val sharedPaths = when (shared) { + is SharedContent.Media -> shared.uris.map { it.toString() } + is SharedContent.File -> listOf(shared.uri.toString()) + is SharedContent.Text -> emptyList() + } + // When sharing a file and pasting it in SimpleX itself, the file shouldn't be deleted before sending or before leaving the chat after sharing + chatModel.filesToDelete.removeAll { file -> + if (sharedPaths.any { it.endsWith(file.name) }) { + false + } else { + file.delete() + true + } + } + } } suspend fun send(cInfo: ChatInfo, mc: MsgContent, quoted: Long?, file: CryptoFile? = null, live: Boolean = false, ttl: Int?): ChatItem? {