fix(swift-ios): attach photos after picker dismissal
This commit is contained in:
2 files changed
+46
-17
No files matched your search
@@ -94,6 +94,7 @@ struct FeatureImageAttachmentPicker: View {
|
||||
isPresented: $isPhotoLibraryPresented,
|
||||
selection: $photoSelections,
|
||||
maxSelectionCount: max(1, remainingCount),
|
||||
selectionBehavior: .ordered,
|
||||
matching: .images,
|
||||
preferredItemEncoding: .compatible
|
||||
)
|
||||
@@ -124,8 +125,16 @@ struct FeatureImageAttachmentPicker: View {
|
||||
.onDisappear {
|
||||
sourcePresentationTask?.cancel()
|
||||
}
|
||||
.onChange(of: isPhotoLibraryPresented) { wasPresented, isPresented in
|
||||
guard wasPresented, !isPresented else { return }
|
||||
finishPhotoLibrarySelection()
|
||||
}
|
||||
.onChange(of: photoSelections) { _, selections in
|
||||
loadPhotoSelections(selections)
|
||||
// Some PhotosUI versions publish the final binding value one turn
|
||||
// after the presentation flag. This fallback still waits until the
|
||||
// picker no longer owns the binding before beginning the import.
|
||||
guard !isPhotoLibraryPresented, !selections.isEmpty else { return }
|
||||
finishPhotoLibrarySelection()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -169,21 +178,31 @@ struct FeatureImageAttachmentPicker: View {
|
||||
}
|
||||
}
|
||||
|
||||
private func loadPhotoSelections(_ selections: [PhotosPickerItem]) {
|
||||
guard !selections.isEmpty, canAdd else { return }
|
||||
let selected = Array(selections.prefix(remainingCount))
|
||||
photoSelections = []
|
||||
let firstOrdinal = attachments.count + preparationState.pendingItemCount + 1
|
||||
let operation = preparationState.begin(itemCount: selected.count)
|
||||
private func finishPhotoLibrarySelection() {
|
||||
Task { @MainActor in
|
||||
// PhotosUI owns the selection binding while its controller is on-screen.
|
||||
// Let dismissal finish before reading or mutating that binding; clearing it
|
||||
// from its change callback can tear down the picker during a selection tap.
|
||||
await Task.yield()
|
||||
guard !isPhotoLibraryPresented, !photoSelections.isEmpty, canAdd else { return }
|
||||
|
||||
let selected = Array(photoSelections.prefix(remainingCount))
|
||||
let firstOrdinal = attachments.count + preparationState.pendingItemCount + 1
|
||||
let operation = preparationState.begin(itemCount: selected.count)
|
||||
|
||||
defer {
|
||||
photoSelections = []
|
||||
preparationState.finish(operation)
|
||||
}
|
||||
|
||||
Task {
|
||||
defer { preparationState.finish(operation) }
|
||||
for (offset, item) in selected.enumerated() {
|
||||
do {
|
||||
guard let data = try await item.loadTransferable(type: Data.self) else {
|
||||
guard let photo = try await item.loadTransferable(
|
||||
type: FeatureImportedPhoto.self
|
||||
) else {
|
||||
throw FeatureImageAttachmentError.invalidImage
|
||||
}
|
||||
try await appendImage(data, ordinal: firstOrdinal + offset)
|
||||
try await appendImage(photo.data, ordinal: firstOrdinal + offset)
|
||||
} catch {
|
||||
errorMessage = error.localizedDescription
|
||||
}
|
||||
@@ -250,6 +269,16 @@ struct FeatureImageAttachmentPicker: View {
|
||||
}
|
||||
}
|
||||
|
||||
private struct FeatureImportedPhoto: Transferable {
|
||||
let data: Data
|
||||
|
||||
static var transferRepresentation: some TransferRepresentation {
|
||||
DataRepresentation(importedContentType: .image) { data in
|
||||
FeatureImportedPhoto(data: data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct FeatureAttachmentStrip: View {
|
||||
@Binding var attachments: [FeatureDraftAttachment]
|
||||
|
||||
|
||||
@@ -617,7 +617,7 @@
|
||||
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
|
||||
CODE_SIGN_ENTITLEMENTS = Extensions/Shared/T3Code.entitlements;
|
||||
CODE_SIGN_STYLE = Automatic;
|
||||
CURRENT_PROJECT_VERSION = 16;
|
||||
CURRENT_PROJECT_VERSION = 17;
|
||||
ENABLE_PREVIEWS = YES;
|
||||
GENERATE_INFOPLIST_FILE = YES;
|
||||
INFOPLIST_FILE = Resources/Info.plist;
|
||||
@@ -674,7 +674,7 @@
|
||||
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
|
||||
CODE_SIGN_ENTITLEMENTS = Extensions/Shared/T3Code.entitlements;
|
||||
CODE_SIGN_STYLE = Automatic;
|
||||
CURRENT_PROJECT_VERSION = 16;
|
||||
CURRENT_PROJECT_VERSION = 17;
|
||||
GENERATE_INFOPLIST_FILE = YES;
|
||||
INFOPLIST_FILE = Resources/Info.plist;
|
||||
INFOPLIST_KEY_CFBundleDisplayName = "T3 Code SwiftUI";
|
||||
@@ -764,7 +764,7 @@
|
||||
APPLICATION_EXTENSION_API_ONLY = YES;
|
||||
CODE_SIGN_ENTITLEMENTS = Extensions/Widgets/T3CodeWidgets.entitlements;
|
||||
CODE_SIGN_STYLE = Automatic;
|
||||
CURRENT_PROJECT_VERSION = 16;
|
||||
CURRENT_PROJECT_VERSION = 17;
|
||||
GENERATE_INFOPLIST_FILE = YES;
|
||||
INFOPLIST_FILE = Extensions/Widgets/Info.plist;
|
||||
INFOPLIST_KEY_ITSAppUsesNonExemptEncryption = NO;
|
||||
@@ -795,7 +795,7 @@
|
||||
APPLICATION_EXTENSION_API_ONLY = YES;
|
||||
CODE_SIGN_ENTITLEMENTS = Extensions/Widgets/T3CodeWidgets.entitlements;
|
||||
CODE_SIGN_STYLE = Automatic;
|
||||
CURRENT_PROJECT_VERSION = 16;
|
||||
CURRENT_PROJECT_VERSION = 17;
|
||||
GENERATE_INFOPLIST_FILE = YES;
|
||||
INFOPLIST_FILE = Extensions/Widgets/Info.plist;
|
||||
INFOPLIST_KEY_ITSAppUsesNonExemptEncryption = NO;
|
||||
@@ -828,7 +828,7 @@
|
||||
APPLICATION_EXTENSION_API_ONLY = YES;
|
||||
CODE_SIGN_ENTITLEMENTS = Extensions/Share/T3CodeShare.entitlements;
|
||||
CODE_SIGN_STYLE = Automatic;
|
||||
CURRENT_PROJECT_VERSION = 16;
|
||||
CURRENT_PROJECT_VERSION = 17;
|
||||
GENERATE_INFOPLIST_FILE = YES;
|
||||
INFOPLIST_FILE = Extensions/Share/Info.plist;
|
||||
INFOPLIST_KEY_ITSAppUsesNonExemptEncryption = NO;
|
||||
@@ -859,7 +859,7 @@
|
||||
APPLICATION_EXTENSION_API_ONLY = YES;
|
||||
CODE_SIGN_ENTITLEMENTS = Extensions/Share/T3CodeShare.entitlements;
|
||||
CODE_SIGN_STYLE = Automatic;
|
||||
CURRENT_PROJECT_VERSION = 16;
|
||||
CURRENT_PROJECT_VERSION = 17;
|
||||
GENERATE_INFOPLIST_FILE = YES;
|
||||
INFOPLIST_FILE = Extensions/Share/Info.plist;
|
||||
INFOPLIST_KEY_ITSAppUsesNonExemptEncryption = NO;
|
||||
|
||||
Reference in new issue
Block a user