ios: adds file uploading
This commit is contained in:
@@ -1,9 +1,17 @@
|
||||
import Observation
|
||||
import PhotosUI
|
||||
import SwiftUI
|
||||
import UniformTypeIdentifiers
|
||||
import UIKit
|
||||
|
||||
struct SybilWorkspaceView: View {
|
||||
@Bindable var viewModel: SybilViewModel
|
||||
@FocusState private var composerFocused: Bool
|
||||
@State private var isShowingAttachmentOptions = false
|
||||
@State private var isShowingFileImporter = false
|
||||
@State private var isShowingPhotoPicker = false
|
||||
@State private var photoPickerItems: [PhotosPickerItem] = []
|
||||
@State private var isComposerDropTargeted = false
|
||||
|
||||
private var isSettingsSelected: Bool {
|
||||
if case .settings = viewModel.selectedItem {
|
||||
@@ -148,54 +156,87 @@ struct SybilWorkspaceView: View {
|
||||
}
|
||||
|
||||
private var composerBar: some View {
|
||||
HStack(alignment: .bottom, spacing: 10) {
|
||||
TextField(
|
||||
viewModel.isSearchMode ? "Search the web" : "Message Sybil",
|
||||
text: $viewModel.composer,
|
||||
axis: .vertical
|
||||
)
|
||||
.focused($composerFocused)
|
||||
.textInputAutocapitalization(.sentences)
|
||||
.autocorrectionDisabled(false)
|
||||
.lineLimit(1 ... 6)
|
||||
.submitLabel(.send)
|
||||
.onSubmit {
|
||||
Task {
|
||||
await viewModel.sendComposer()
|
||||
VStack(alignment: .leading, spacing: 10) {
|
||||
if !viewModel.isSearchMode && !viewModel.composerAttachments.isEmpty {
|
||||
SybilAttachmentListView(
|
||||
attachments: viewModel.composerAttachments,
|
||||
tone: .composer
|
||||
) { attachmentID in
|
||||
viewModel.removeComposerAttachment(id: attachmentID)
|
||||
}
|
||||
}
|
||||
.padding(.horizontal, 12)
|
||||
.padding(.vertical, 10)
|
||||
.background(
|
||||
RoundedRectangle(cornerRadius: 12)
|
||||
.fill(SybilTheme.composerGradient)
|
||||
.overlay(
|
||||
RoundedRectangle(cornerRadius: 12)
|
||||
.stroke(SybilTheme.primary.opacity(0.34), lineWidth: 1)
|
||||
)
|
||||
)
|
||||
.foregroundStyle(SybilTheme.text)
|
||||
|
||||
Button {
|
||||
Task {
|
||||
await viewModel.sendComposer()
|
||||
}
|
||||
} label: {
|
||||
Image(systemName: viewModel.isSearchMode ? "magnifyingglass" : "arrow.up")
|
||||
.font(.system(size: 17, weight: .semibold))
|
||||
.frame(width: 40, height: 40)
|
||||
.background(
|
||||
Circle()
|
||||
.fill(
|
||||
viewModel.composer.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty || viewModel.isSending
|
||||
? AnyShapeStyle(SybilTheme.surface)
|
||||
: AnyShapeStyle(SybilTheme.primaryGradient)
|
||||
HStack(alignment: .bottom, spacing: 10) {
|
||||
if !viewModel.isSearchMode {
|
||||
Button {
|
||||
isShowingAttachmentOptions = true
|
||||
} label: {
|
||||
Image(systemName: "paperclip")
|
||||
.font(.system(size: 17, weight: .semibold))
|
||||
.frame(width: 40, height: 40)
|
||||
.background(
|
||||
Circle()
|
||||
.fill(SybilTheme.surface)
|
||||
)
|
||||
)
|
||||
.foregroundStyle(viewModel.composer.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty || viewModel.isSending ? SybilTheme.textMuted : SybilTheme.text)
|
||||
.overlay(
|
||||
Circle()
|
||||
.stroke(SybilTheme.border.opacity(0.82), lineWidth: 1)
|
||||
)
|
||||
.foregroundStyle(viewModel.isSending ? SybilTheme.textMuted : SybilTheme.text)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.disabled(viewModel.isSending)
|
||||
.accessibilityLabel("Attach file")
|
||||
}
|
||||
|
||||
TextField(
|
||||
viewModel.isSearchMode ? "Search the web" : "Message Sybil",
|
||||
text: $viewModel.composer,
|
||||
axis: .vertical
|
||||
)
|
||||
.focused($composerFocused)
|
||||
.textInputAutocapitalization(.sentences)
|
||||
.autocorrectionDisabled(false)
|
||||
.lineLimit(1 ... 6)
|
||||
.submitLabel(.send)
|
||||
.onSubmit {
|
||||
Task {
|
||||
await viewModel.sendComposer()
|
||||
}
|
||||
}
|
||||
.padding(.horizontal, 12)
|
||||
.padding(.vertical, 10)
|
||||
.background(
|
||||
RoundedRectangle(cornerRadius: 12)
|
||||
.fill(SybilTheme.composerGradient)
|
||||
.overlay(
|
||||
RoundedRectangle(cornerRadius: 12)
|
||||
.stroke(SybilTheme.primary.opacity(0.34), lineWidth: 1)
|
||||
)
|
||||
)
|
||||
.foregroundStyle(SybilTheme.text)
|
||||
|
||||
Button {
|
||||
Task {
|
||||
await viewModel.sendComposer()
|
||||
}
|
||||
} label: {
|
||||
Image(systemName: viewModel.isSearchMode ? "magnifyingglass" : "arrow.up")
|
||||
.font(.system(size: 17, weight: .semibold))
|
||||
.frame(width: 40, height: 40)
|
||||
.background(
|
||||
Circle()
|
||||
.fill(
|
||||
viewModel.canSendComposer
|
||||
? AnyShapeStyle(SybilTheme.primaryGradient)
|
||||
: AnyShapeStyle(SybilTheme.surface)
|
||||
)
|
||||
)
|
||||
.foregroundStyle(viewModel.canSendComposer ? SybilTheme.text : SybilTheme.textMuted)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.disabled(!viewModel.canSendComposer)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.disabled(viewModel.composer.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty || viewModel.isSending)
|
||||
}
|
||||
.padding(.horizontal, 14)
|
||||
.padding(.vertical, 12)
|
||||
@@ -209,5 +250,151 @@ struct SybilWorkspaceView: View {
|
||||
endPoint: .bottom
|
||||
)
|
||||
)
|
||||
.overlay {
|
||||
if isComposerDropTargeted && !viewModel.isSearchMode {
|
||||
RoundedRectangle(cornerRadius: 18)
|
||||
.stroke(SybilTheme.accent.opacity(0.78), style: StrokeStyle(lineWidth: 1.5, dash: [7, 5]))
|
||||
.padding(.horizontal, 14)
|
||||
.padding(.vertical, 10)
|
||||
}
|
||||
}
|
||||
.onDrop(of: [UTType.fileURL.identifier, UTType.image.identifier], isTargeted: $isComposerDropTargeted) { providers in
|
||||
if viewModel.isSearchMode || viewModel.isSending {
|
||||
return false
|
||||
}
|
||||
|
||||
Task {
|
||||
await importAttachmentsFromItemProviders(providers)
|
||||
}
|
||||
return true
|
||||
}
|
||||
.confirmationDialog("Add attachment", isPresented: $isShowingAttachmentOptions, titleVisibility: .visible) {
|
||||
Button("Photo Library") {
|
||||
isShowingPhotoPicker = true
|
||||
}
|
||||
Button("Files") {
|
||||
isShowingFileImporter = true
|
||||
}
|
||||
if canPasteFromClipboard {
|
||||
Button("Paste from Clipboard") {
|
||||
Task {
|
||||
await pasteAttachmentsFromClipboard()
|
||||
}
|
||||
}
|
||||
}
|
||||
Button("Cancel", role: .cancel) {}
|
||||
}
|
||||
.photosPicker(
|
||||
isPresented: $isShowingPhotoPicker,
|
||||
selection: $photoPickerItems,
|
||||
maxSelectionCount: max(1, SybilChatAttachmentSupport.maxAttachmentsPerMessage - viewModel.composerAttachments.count),
|
||||
matching: .images
|
||||
)
|
||||
.fileImporter(
|
||||
isPresented: $isShowingFileImporter,
|
||||
allowedContentTypes: [.item],
|
||||
allowsMultipleSelection: true
|
||||
) { result in
|
||||
Task {
|
||||
do {
|
||||
let urls = try result.get()
|
||||
let attachments = try SybilChatAttachmentSupport.buildAttachments(from: urls)
|
||||
try await MainActor.run {
|
||||
try viewModel.appendComposerAttachments(attachments)
|
||||
}
|
||||
composerFocused = true
|
||||
} catch {
|
||||
await MainActor.run {
|
||||
viewModel.errorMessage = error.localizedDescription
|
||||
}
|
||||
SybilLog.error(SybilLog.ui, "File import failed", error: error)
|
||||
}
|
||||
}
|
||||
}
|
||||
.onChange(of: photoPickerItems) { _, items in
|
||||
guard !items.isEmpty else { return }
|
||||
Task {
|
||||
do {
|
||||
let attachments = try await loadAttachmentsFromPhotoPickerItems(items)
|
||||
try await MainActor.run {
|
||||
try viewModel.appendComposerAttachments(attachments)
|
||||
photoPickerItems = []
|
||||
}
|
||||
composerFocused = true
|
||||
} catch {
|
||||
await MainActor.run {
|
||||
viewModel.errorMessage = error.localizedDescription
|
||||
photoPickerItems = []
|
||||
}
|
||||
SybilLog.error(SybilLog.ui, "Photo import failed", error: error)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var canPasteFromClipboard: Bool {
|
||||
let pasteboard = UIPasteboard.general
|
||||
if pasteboard.hasImages {
|
||||
return true
|
||||
}
|
||||
if let url = pasteboard.url, url.isFileURL {
|
||||
return true
|
||||
}
|
||||
if let string = pasteboard.string, !string.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@MainActor
|
||||
private func importAttachmentsFromItemProviders(_ providers: [NSItemProvider]) async {
|
||||
do {
|
||||
let attachments = try await SybilChatAttachmentSupport.buildAttachments(from: providers)
|
||||
try viewModel.appendComposerAttachments(attachments)
|
||||
composerFocused = true
|
||||
} catch {
|
||||
viewModel.errorMessage = error.localizedDescription
|
||||
SybilLog.error(SybilLog.ui, "Clipboard/drop attachment import failed", error: error)
|
||||
}
|
||||
}
|
||||
|
||||
private func loadAttachmentsFromPhotoPickerItems(_ items: [PhotosPickerItem]) async throws -> [ChatAttachment] {
|
||||
var attachments: [ChatAttachment] = []
|
||||
|
||||
for item in items {
|
||||
guard let data = try await item.loadTransferable(type: Data.self) else {
|
||||
continue
|
||||
}
|
||||
|
||||
let contentType = item.supportedContentTypes.first(where: { $0.conforms(to: .image) })
|
||||
let filename = contentType?.preferredFilenameExtension.map { "photo.\($0)" } ?? "photo.jpg"
|
||||
attachments.append(try SybilChatAttachmentSupport.buildImageAttachment(data: data, filename: filename, contentType: contentType))
|
||||
}
|
||||
|
||||
return attachments
|
||||
}
|
||||
|
||||
@MainActor
|
||||
private func pasteAttachmentsFromClipboard() async {
|
||||
do {
|
||||
let pasteboard = UIPasteboard.general
|
||||
var attachments: [ChatAttachment] = []
|
||||
|
||||
if let image = pasteboard.image {
|
||||
attachments.append(try SybilChatAttachmentSupport.buildImageAttachment(image: image))
|
||||
}
|
||||
|
||||
if let url = pasteboard.url, url.isFileURL {
|
||||
attachments.append(contentsOf: try SybilChatAttachmentSupport.buildAttachments(from: [url]))
|
||||
} else if let text = pasteboard.string?.trimmingCharacters(in: .whitespacesAndNewlines), !text.isEmpty {
|
||||
attachments.append(try SybilChatAttachmentSupport.buildTextAttachment(text: text))
|
||||
}
|
||||
|
||||
try viewModel.appendComposerAttachments(attachments)
|
||||
composerFocused = true
|
||||
} catch {
|
||||
viewModel.errorMessage = error.localizedDescription
|
||||
SybilLog.error(SybilLog.ui, "Clipboard attachment import failed", error: error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user