fix(ios): wait for image attachments

This commit is contained in:
Theo Browne committed 2026-08-09 20:52:50 -07:00
1 parent 79c888651f
commit e6fdb7bebc
3 files changed
+167 -15

No files matched your search

@@ -2,6 +2,7 @@ import SwiftUI
struct FeatureComposerView: View {
@State private var isManuallyExpanded = false
@State private var attachmentPreparation = FeatureAttachmentPreparationState()
@Binding private var text: String
@Binding private var selection: FeatureSelection?
@@ -117,7 +118,10 @@ struct FeatureComposerView: View {
.ignoresSafeArea()
}
.onChange(of: focused.wrappedValue) {
if !focused.wrappedValue, textIsEmpty, attachments.isEmpty {
if !focused.wrappedValue,
textIsEmpty,
attachments.isEmpty,
!attachmentPreparation.isPreparing {
isManuallyExpanded = false
}
}
@@ -188,6 +192,16 @@ struct FeatureComposerView: View {
.padding(.bottom, 4)
}
if attachmentPreparation.isPreparing {
Label(attachmentPreparation.statusLabel, systemImage: "hourglass")
.font(.caption)
.foregroundStyle(T3Colors.textSecondary)
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.horizontal, 15)
.padding(.bottom, 4)
.accessibilityIdentifier("attachment-preparing")
}
composerFooter
}
}
@@ -196,6 +210,7 @@ struct FeatureComposerView: View {
HStack(spacing: 2) {
FeatureImageAttachmentPicker(
attachments: $attachments,
preparationState: $attachmentPreparation,
isEnabled: imagesAllowed
)
@@ -291,7 +306,7 @@ struct FeatureComposerView: View {
}
private var submitButton: some View {
Button(action: showsStop ? onStop : onSend) {
Button(action: performPrimaryAction) {
Group {
if isSending {
ProgressView()
@@ -325,6 +340,7 @@ struct FeatureComposerView: View {
|| focused.wrappedValue
|| !textIsEmpty
|| !attachments.isEmpty
|| attachmentPreparation.isPreparing
}
private var showsStop: Bool {
@@ -340,15 +356,27 @@ struct FeatureComposerView: View {
}
private var canSend: Bool {
(!textIsEmpty || !attachments.isEmpty)
&& (attachments.isEmpty || imagesAllowed)
&& !isSending
FeatureComposerSubmissionEligibility.canSend(
text: text,
attachmentCount: attachments.count,
imagesAllowed: imagesAllowed,
isSending: isSending,
preparationState: attachmentPreparation
)
}
private var imagesAllowed: Bool {
DailyUXModelOptions.supportsImages(selection: selection, providers: providers)
}
private func performPrimaryAction() {
if showsStop {
onStop()
} else if canSend {
onSend()
}
}
private func runtimeModeLabel(_ mode: FeatureRuntimeMode) -> String {
switch mode {
case .approvalRequired: "Supervised"
@@ -374,6 +402,23 @@ struct FeatureComposerView: View {
}
}
enum FeatureComposerSubmissionEligibility {
static func canSend(
text: String,
attachmentCount: Int,
imagesAllowed: Bool,
isSending: Bool,
preparationState: FeatureAttachmentPreparationState
) -> Bool {
let hasText = !text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
let hasAttachments = attachmentCount > 0
return !isSending
&& !preparationState.isPreparing
&& (hasText || hasAttachments)
&& (!hasAttachments || imagesAllowed)
}
}
private struct FeatureContextMeter: View {
let usage: Double
@@ -4,23 +4,56 @@ import SwiftUI
import UniformTypeIdentifiers
import UIKit
struct FeatureAttachmentPreparationState: Equatable {
struct Operation: Hashable {
fileprivate let id: UUID
}
private var pendingItemsByOperation: [Operation: Int] = [:]
var isPreparing: Bool {
!pendingItemsByOperation.isEmpty
}
var pendingItemCount: Int {
pendingItemsByOperation.values.reduce(0, +)
}
var statusLabel: String {
pendingItemCount == 1 ? "Preparing image…" : "Preparing \(pendingItemCount) images…"
}
@discardableResult
mutating func begin(itemCount: Int, id: UUID = UUID()) -> Operation {
let operation = Operation(id: id)
pendingItemsByOperation[operation] = max(1, itemCount)
return operation
}
mutating func finish(_ operation: Operation) {
pendingItemsByOperation.removeValue(forKey: operation)
}
}
struct FeatureImageAttachmentPicker: View {
@Binding var attachments: [FeatureDraftAttachment]
@Binding var preparationState: FeatureAttachmentPreparationState
let maximumCount: Int
let isEnabled: Bool
@State private var selection: [PhotosPickerItem] = []
@State private var isLoading = false
@State private var isCameraPresented = false
@State private var isFileImporterPresented = false
@State private var errorMessage: String?
init(
attachments: Binding<[FeatureDraftAttachment]>,
preparationState: Binding<FeatureAttachmentPreparationState>,
maximumCount: Int = 8,
isEnabled: Bool = true
) {
_attachments = attachments
_preparationState = preparationState
self.maximumCount = maximumCount
self.isEnabled = isEnabled
}
@@ -48,7 +81,7 @@ struct FeatureImageAttachmentPicker: View {
Label("Files", systemImage: "folder")
}
} label: {
Image(systemName: isLoading ? "ellipsis" : "paperclip")
Image(systemName: preparationState.isPreparing ? "hourglass" : "paperclip")
.font(.system(size: 17, weight: .medium))
.foregroundStyle(T3Colors.textSecondary)
.frame(width: T3Metrics.minimumTapTarget, height: T3Metrics.minimumTapTarget)
@@ -99,11 +132,11 @@ struct FeatureImageAttachmentPicker: View {
}
private var canAdd: Bool {
isEnabled && !isLoading && remainingCount > 0
isEnabled && !preparationState.isPreparing && remainingCount > 0
}
private var attachmentAccessibilityLabel: String {
if isLoading { return "Adding attachment" }
if preparationState.isPreparing { return preparationState.statusLabel }
if remainingCount == 0 { return "Attachment limit reached" }
return "Add attachment"
}
@@ -118,10 +151,10 @@ struct FeatureImageAttachmentPicker: View {
let items = selection
guard !items.isEmpty else { return }
selection = []
isLoading = true
let operation = preparationState.begin(itemCount: items.count)
Task {
defer { isLoading = false }
defer { preparationState.finish(operation) }
for item in items.prefix(remainingCount) {
do {
guard let data = try await item.loadTransferable(type: Data.self) else {
@@ -139,10 +172,10 @@ struct FeatureImageAttachmentPicker: View {
private func loadCapturedImage(_ image: UIImage) {
isCameraPresented = false
guard canAdd else { return }
isLoading = true
let operation = preparationState.begin(itemCount: 1)
Task {
defer { isLoading = false }
defer { preparationState.finish(operation) }
do {
let data = try await Task.detached(priority: .userInitiated) {
guard let data = image.jpegData(compressionQuality: 0.94) else {
@@ -163,10 +196,10 @@ struct FeatureImageAttachmentPicker: View {
errorMessage = error.localizedDescription
case .success(let urls):
guard !urls.isEmpty, canAdd else { return }
isLoading = true
let operation = preparationState.begin(itemCount: min(urls.count, remainingCount))
Task {
defer { isLoading = false }
defer { preparationState.finish(operation) }
for url in urls.prefix(remainingCount) {
do {
let data = try await Task.detached(priority: .userInitiated) {
@@ -0,0 +1,74 @@
import Foundation
import Testing
@testable import T3Code
@Suite("Attachment preparation")
struct AttachmentPreparationTests {
@Test
func overlappingPreparationOnlyFinishesAfterEveryOperation() {
var state = FeatureAttachmentPreparationState()
let firstID = UUID(uuidString: "00000000-0000-0000-0000-000000000001")!
let secondID = UUID(uuidString: "00000000-0000-0000-0000-000000000002")!
let first = state.begin(itemCount: 2, id: firstID)
let second = state.begin(itemCount: 1, id: secondID)
#expect(state.isPreparing)
#expect(state.pendingItemCount == 3)
#expect(state.statusLabel == "Preparing 3 images…")
state.finish(first)
#expect(state.isPreparing)
#expect(state.pendingItemCount == 1)
#expect(state.statusLabel == "Preparing image…")
state.finish(second)
#expect(!state.isPreparing)
#expect(state.pendingItemCount == 0)
}
@Test
func textOnlySubmissionWaitsForSelectedImagePreparation() {
var state = FeatureAttachmentPreparationState()
let operation = state.begin(itemCount: 1)
#expect(!FeatureComposerSubmissionEligibility.canSend(
text: "Explain this screenshot",
attachmentCount: 0,
imagesAllowed: true,
isSending: false,
preparationState: state
))
state.finish(operation)
#expect(FeatureComposerSubmissionEligibility.canSend(
text: "Explain this screenshot",
attachmentCount: 1,
imagesAllowed: true,
isSending: false,
preparationState: state
))
}
@Test
func attachmentSubmissionStillRequiresImageCapableModel() {
let state = FeatureAttachmentPreparationState()
#expect(!FeatureComposerSubmissionEligibility.canSend(
text: "",
attachmentCount: 1,
imagesAllowed: false,
isSending: false,
preparationState: state
))
#expect(FeatureComposerSubmissionEligibility.canSend(
text: "Text still works",
attachmentCount: 0,
imagesAllowed: false,
isSending: false,
preparationState: state
))
}
}