Add platform attachment picker abstraction
This commit is contained in:
1 parent
fbe64e3cdc
commit
88abe085d2
9 files changed
+132
-12
No files matched your search
+39
@@ -0,0 +1,39 @@
|
||||
package cn.xiaobai.openclawapp.platform
|
||||
|
||||
import android.provider.OpenableColumns
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
|
||||
@Composable
|
||||
actual fun rememberAttachmentPicker(
|
||||
onPicked: (PickedAttachment) -> Unit,
|
||||
onError: (String) -> Unit,
|
||||
): () -> Unit {
|
||||
val context = LocalContext.current
|
||||
val resolver = context.contentResolver
|
||||
val launcher = rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) { uri ->
|
||||
if (uri == null) return@rememberLauncherForActivityResult
|
||||
runCatching {
|
||||
val name = resolver.query(uri, null, null, null, null)?.use { cursor ->
|
||||
val index = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
|
||||
if (index >= 0 && cursor.moveToFirst()) cursor.getString(index) else null
|
||||
} ?: "attachment"
|
||||
val mimeType = resolver.getType(uri)
|
||||
val bytes = resolver.openInputStream(uri)?.use { it.readBytes() }
|
||||
?: error("无法读取所选文件")
|
||||
PickedAttachment(
|
||||
fileName = name,
|
||||
bytes = bytes,
|
||||
contentType = mimeType,
|
||||
)
|
||||
}.onSuccess(onPicked).onFailure {
|
||||
onError(it.message ?: "文件选择失败")
|
||||
}
|
||||
}
|
||||
return remember(launcher) {
|
||||
{ launcher.launch("*/*") }
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,7 @@ import cn.xiaobai.openclawapp.feature.config.ServerConfigScreen
|
||||
import cn.xiaobai.openclawapp.feature.sessions.SessionListScreen
|
||||
import cn.xiaobai.openclawapp.feature.settings.SettingsScreen
|
||||
import cn.xiaobai.openclawapp.feature.status.StatusScreen
|
||||
import cn.xiaobai.openclawapp.platform.rememberAttachmentPicker
|
||||
import cn.xiaobai.openclawapp.ui.OpenClawTheme
|
||||
import cn.xiaobai.openclawapp.ui.StatusPill
|
||||
import kotlinx.coroutines.delay
|
||||
@@ -80,6 +81,22 @@ fun App() {
|
||||
private fun OpenClawRoot(appState: OpenClawAppState) {
|
||||
val activeSession = appState.activeSessionId?.let(appState.repository::findSession)
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
val pickAttachment = rememberAttachmentPicker(
|
||||
onPicked = { picked ->
|
||||
val sessionId = activeSession?.id ?: return@rememberAttachmentPicker
|
||||
coroutineScope.launch {
|
||||
appState.uploadAttachmentStub(
|
||||
sessionId = sessionId,
|
||||
fileName = picked.fileName,
|
||||
bytes = picked.bytes,
|
||||
contentType = picked.contentType,
|
||||
)
|
||||
}
|
||||
},
|
||||
onError = { message ->
|
||||
appState.feedbackMessage = message
|
||||
},
|
||||
)
|
||||
|
||||
LaunchedEffect(appState.feedbackMessage) {
|
||||
if (appState.feedbackMessage != null) {
|
||||
@@ -178,13 +195,7 @@ private fun OpenClawRoot(appState: OpenClawAppState) {
|
||||
}
|
||||
},
|
||||
onSendAttachmentStub = {
|
||||
coroutineScope.launch {
|
||||
appState.uploadAttachmentStub(
|
||||
sessionId = activeSession.id,
|
||||
fileName = "demo.txt",
|
||||
bytes = "demo attachment from mobile".encodeToByteArray(),
|
||||
)
|
||||
}
|
||||
pickAttachment()
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@@ -138,9 +138,14 @@ class OpenClawAppState(
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun uploadAttachmentStub(sessionId: String, fileName: String, bytes: ByteArray) {
|
||||
suspend fun uploadAttachmentStub(
|
||||
sessionId: String,
|
||||
fileName: String,
|
||||
bytes: ByteArray,
|
||||
contentType: String? = null,
|
||||
) {
|
||||
runCatching {
|
||||
repository.uploadAttachmentStub(sessionId, fileName, bytes)
|
||||
repository.uploadAttachmentStub(sessionId, fileName, bytes, contentType)
|
||||
feedbackMessage = "附件已加入发送队列"
|
||||
}.onFailure {
|
||||
syncError = it.message ?: "附件上传失败"
|
||||
|
||||
@@ -14,5 +14,10 @@ interface OpenClawRepository {
|
||||
fun togglePinned(sessionId: String): Boolean
|
||||
fun sendMessage(sessionId: String, text: String)
|
||||
suspend fun updateDefaultAgent(agent: String)
|
||||
suspend fun uploadAttachmentStub(sessionId: String, fileName: String, bytes: ByteArray)
|
||||
suspend fun uploadAttachmentStub(
|
||||
sessionId: String,
|
||||
fileName: String,
|
||||
bytes: ByteArray,
|
||||
contentType: String? = null,
|
||||
)
|
||||
}
|
||||
+11
-2
@@ -124,7 +124,12 @@ class RemoteOpenClawRepository(
|
||||
)
|
||||
}
|
||||
|
||||
override suspend fun uploadAttachmentStub(sessionId: String, fileName: String, bytes: ByteArray) {
|
||||
override suspend fun uploadAttachmentStub(
|
||||
sessionId: String,
|
||||
fileName: String,
|
||||
bytes: ByteArray,
|
||||
contentType: String? = null,
|
||||
) {
|
||||
val currentMessages = messageStore[sessionId].orEmpty()
|
||||
val localId = "local-attachment-$sessionId-${currentMessages.size + 1}"
|
||||
val pending = ChatMessage(
|
||||
@@ -139,7 +144,11 @@ class RemoteOpenClawRepository(
|
||||
updateSessionPreview(sessionId, pending)
|
||||
|
||||
runCatching {
|
||||
val remote = api.uploadAttachment(sessionId, fileName, bytes)
|
||||
val remote = api.uploadAttachment(
|
||||
sessionId = sessionId,
|
||||
fileName = fileName,
|
||||
bytes = bytes,
|
||||
)
|
||||
val uploaded = ChatMessage(
|
||||
id = remote.attachmentId,
|
||||
role = MessageRole.User,
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package cn.xiaobai.openclawapp.platform
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
|
||||
data class PickedAttachment(
|
||||
val fileName: String,
|
||||
val bytes: ByteArray,
|
||||
val contentType: String? = null,
|
||||
)
|
||||
|
||||
@Composable
|
||||
expect fun rememberAttachmentPicker(
|
||||
onPicked: (PickedAttachment) -> Unit,
|
||||
onError: (String) -> Unit,
|
||||
): () -> Unit
|
||||
@@ -0,0 +1,12 @@
|
||||
package cn.xiaobai.openclawapp.platform
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.remember
|
||||
|
||||
@Composable
|
||||
actual fun rememberAttachmentPicker(
|
||||
onPicked: (PickedAttachment) -> Unit,
|
||||
onError: (String) -> Unit,
|
||||
): () -> Unit = remember(onPicked, onError) {
|
||||
{ onError("iOS 文件选择器还没接好") }
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package cn.xiaobai.openclawapp.platform
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.remember
|
||||
|
||||
@Composable
|
||||
actual fun rememberAttachmentPicker(
|
||||
onPicked: (PickedAttachment) -> Unit,
|
||||
onError: (String) -> Unit,
|
||||
): () -> Unit = remember(onPicked, onError) {
|
||||
{ onError("Web 文件选择器还没接好") }
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package cn.xiaobai.openclawapp.platform
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.remember
|
||||
|
||||
@Composable
|
||||
actual fun rememberAttachmentPicker(
|
||||
onPicked: (PickedAttachment) -> Unit,
|
||||
onError: (String) -> Unit,
|
||||
): () -> Unit = remember(onPicked, onError) {
|
||||
{ onError("Wasm 文件选择器还没接好") }
|
||||
}
|
||||
Reference in new issue
Block a user