From 606c8d51bc9600d8d7319e662713b62784258c83 Mon Sep 17 00:00:00 2001 From: OpenClaw Date: Sat, 7 Mar 2026 09:53:08 +0800 Subject: [PATCH] Add Android save-as flow for attachments --- .../platform/AttachmentSaver.android.kt | 37 +++++++++++++++++++ .../kotlin/cn/xiaobai/openclawapp/App.kt | 14 +++++++ .../openclawapp/feature/chat/ChatScreen.kt | 6 +++ .../openclawapp/platform/AttachmentSaver.kt | 9 +++++ .../platform/AttachmentSaver.ios.kt | 12 ++++++ .../platform/AttachmentSaver.js.kt | 12 ++++++ .../platform/AttachmentSaver.wasmJs.kt | 12 ++++++ docs/MVP_PROGRESS.md | 2 +- 8 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 composeApp/src/androidMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentSaver.android.kt create mode 100644 composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentSaver.kt create mode 100644 composeApp/src/iosMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentSaver.ios.kt create mode 100644 composeApp/src/jsMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentSaver.js.kt create mode 100644 composeApp/src/wasmJsMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentSaver.wasmJs.kt diff --git a/composeApp/src/androidMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentSaver.android.kt b/composeApp/src/androidMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentSaver.android.kt new file mode 100644 index 0000000..ac2f8d9 --- /dev/null +++ b/composeApp/src/androidMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentSaver.android.kt @@ -0,0 +1,37 @@ +package cn.xiaobai.openclawapp.platform + +import androidx.activity.compose.rememberLauncherForActivityResult +import androidx.activity.result.contract.ActivityResultContracts +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.platform.LocalContext +import cn.xiaobai.openclawapp.data.AttachmentOpenPayload + +@Composable +actual fun rememberAttachmentSaver( + onResult: (String) -> Unit, +): (AttachmentOpenPayload) -> Unit { + val context = LocalContext.current + var pending by remember { mutableStateOf(null) } + val launcher = rememberLauncherForActivityResult(ActivityResultContracts.CreateDocument("*/*")) { uri -> + val payload = pending + if (uri == null || payload == null) return@rememberLauncherForActivityResult + runCatching { + context.contentResolver.openOutputStream(uri)?.use { it.write(payload.bytes) } + ?: error("无法写入目标文件") + onResult("已保存附件:${payload.fileName}") + }.onFailure { + onResult(it.message ?: "保存附件失败") + } + pending = null + } + return remember(launcher) { + { payload -> + pending = payload + launcher.launch(payload.fileName) + } + } +} diff --git a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/App.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/App.kt index 1ae99cb..58afe76 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/App.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/App.kt @@ -28,6 +28,7 @@ import cn.xiaobai.openclawapp.feature.settings.SettingsScreen import cn.xiaobai.openclawapp.feature.status.StatusScreen import cn.xiaobai.openclawapp.platform.rememberAttachmentOpener import cn.xiaobai.openclawapp.platform.rememberAttachmentPicker +import cn.xiaobai.openclawapp.platform.rememberAttachmentSaver import cn.xiaobai.openclawapp.platform.rememberClipboardSetter import cn.xiaobai.openclawapp.ui.OpenClawTheme import cn.xiaobai.openclawapp.ui.StatusPill @@ -105,6 +106,9 @@ private fun OpenClawRoot(appState: OpenClawAppState) { val openAttachment = rememberAttachmentOpener { message -> appState.feedbackMessage = message } + val saveAttachment = rememberAttachmentSaver { message -> + appState.feedbackMessage = message + } LaunchedEffect(appState.feedbackMessage) { if (appState.feedbackMessage != null) { @@ -228,6 +232,16 @@ private fun OpenClawRoot(appState: OpenClawAppState) { } } }, + onDownloadAttachment = { messageId -> + coroutineScope.launch { + val payload = appState.repository.ensureAttachmentOpenPayload(activeSession.id, messageId) + if (payload != null) { + saveAttachment(payload) + } else { + appState.feedbackMessage = "当前附件暂无可下载内容" + } + } + }, ) appState.selectedTab == MainTab.Sessions -> SessionListScreen( diff --git a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/feature/chat/ChatScreen.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/feature/chat/ChatScreen.kt index e3c90bf..1e461a1 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/feature/chat/ChatScreen.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/feature/chat/ChatScreen.kt @@ -57,6 +57,7 @@ fun ChatScreen( onRetryMessage: (String) -> Unit, onCopyAttachmentName: (String) -> Unit, onOpenAttachment: (String) -> Unit, + onDownloadAttachment: (String) -> Unit, ) { var input by remember { mutableStateOf("") } val listState = rememberLazyListState() @@ -185,6 +186,7 @@ fun ChatScreen( onRetryMessage = onRetryMessage, onCopyAttachmentName = onCopyAttachmentName, onOpenAttachment = onOpenAttachment, + onDownloadAttachment = onDownloadAttachment, ) } } @@ -247,6 +249,7 @@ private fun MessageBubble( onRetryMessage: (String) -> Unit, onCopyAttachmentName: (String) -> Unit, onOpenAttachment: (String) -> Unit, + onDownloadAttachment: (String) -> Unit, ) { val alignment = when (message.role) { MessageRole.User -> Alignment.CenterEnd @@ -382,6 +385,9 @@ private fun MessageBubble( TextButton(onClick = { onOpenAttachment(message.id) }) { Text("打开") } + TextButton(onClick = { onDownloadAttachment(message.id) }) { + Text("下载") + } TextButton(onClick = { onCopyAttachmentName(message.text) }) { Text("复制文件名") } diff --git a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentSaver.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentSaver.kt new file mode 100644 index 0000000..7e50236 --- /dev/null +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentSaver.kt @@ -0,0 +1,9 @@ +package cn.xiaobai.openclawapp.platform + +import androidx.compose.runtime.Composable +import cn.xiaobai.openclawapp.data.AttachmentOpenPayload + +@Composable +expect fun rememberAttachmentSaver( + onResult: (String) -> Unit = {}, +): (AttachmentOpenPayload) -> Unit diff --git a/composeApp/src/iosMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentSaver.ios.kt b/composeApp/src/iosMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentSaver.ios.kt new file mode 100644 index 0000000..f4cd688 --- /dev/null +++ b/composeApp/src/iosMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentSaver.ios.kt @@ -0,0 +1,12 @@ +package cn.xiaobai.openclawapp.platform + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.remember +import cn.xiaobai.openclawapp.data.AttachmentOpenPayload + +@Composable +actual fun rememberAttachmentSaver( + onResult: (String) -> Unit, +): (AttachmentOpenPayload) -> Unit = remember(onResult) { + { payload -> onResult("iOS 附件下载还没接好:${payload.fileName}") } +} diff --git a/composeApp/src/jsMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentSaver.js.kt b/composeApp/src/jsMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentSaver.js.kt new file mode 100644 index 0000000..2b2b594 --- /dev/null +++ b/composeApp/src/jsMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentSaver.js.kt @@ -0,0 +1,12 @@ +package cn.xiaobai.openclawapp.platform + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.remember +import cn.xiaobai.openclawapp.data.AttachmentOpenPayload + +@Composable +actual fun rememberAttachmentSaver( + onResult: (String) -> Unit, +): (AttachmentOpenPayload) -> Unit = remember(onResult) { + { payload -> onResult("Web 附件下载还没接好:${payload.fileName}") } +} diff --git a/composeApp/src/wasmJsMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentSaver.wasmJs.kt b/composeApp/src/wasmJsMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentSaver.wasmJs.kt new file mode 100644 index 0000000..05e55c3 --- /dev/null +++ b/composeApp/src/wasmJsMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentSaver.wasmJs.kt @@ -0,0 +1,12 @@ +package cn.xiaobai.openclawapp.platform + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.remember +import cn.xiaobai.openclawapp.data.AttachmentOpenPayload + +@Composable +actual fun rememberAttachmentSaver( + onResult: (String) -> Unit, +): (AttachmentOpenPayload) -> Unit = remember(onResult) { + { payload -> onResult("Wasm 附件下载还没接好:${payload.fileName}") } +} diff --git a/docs/MVP_PROGRESS.md b/docs/MVP_PROGRESS.md index 21eff98..f024de4 100644 --- a/docs/MVP_PROGRESS.md +++ b/docs/MVP_PROGRESS.md @@ -195,7 +195,7 @@ Android/iOS App - [x] 查看附件详情 - [x] 点击附件打开(Android,优先本地缓存;无缓存时经 BFF 回源读取) - [x] BFF 附件 overlay 落盘,重启后仍可读取最近附件 -- [ ] 下载附件 +- [x] 下载附件(Android,系统另存为) - [ ] iOS / Web 剪贴板 - [ ] iOS / Web 附件打开