Add Android save-as flow for attachments
This commit is contained in:
1 parent
471c23cf4a
commit
606c8d51bc
8 files changed
+103
-1
No files matched your search
+37
@@ -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<AttachmentOpenPayload?>(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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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(
|
||||
|
||||
@@ -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("复制文件名")
|
||||
}
|
||||
|
||||
@@ -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
|
||||
@@ -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}") }
|
||||
}
|
||||
@@ -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}") }
|
||||
}
|
||||
+12
@@ -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}") }
|
||||
}
|
||||
Reference in new issue
Block a user