// A PICTURE DOOR (`create.doors[].pick`; Photos' Plus, 12 Nov 2026): Choose // photos opens the system's photo picker, as many as the person picks and of // the kinds the door names, and Take a photo opens the camera, which films // too when the door takes a clip. Neither reads the library: the picker runs // out of process and hands back only what was chosen, so Photos asks for no // library permission at all. // // What comes back is written to files (`capture.media` takes a path) and // handed to the share sheet's SAME intake, as a door's files are // (`DeckDoors.bring`): a picture or a clip is its branch 2, `capture.media`, // and lands on its photo or video, exactly where a share of it lands. The // written copies are this door's and go once the intake has staged them. import CottonKit import PhotosUI import SwiftUI import UniformTypeIdentifiers extension DeckDoors { /// The photo picker's filter for the kinds a door takes. static func filter(_ kinds: [DefinitionPictureKind]) -> PHPickerFilter { let filters = kinds.map { kind -> PHPickerFilter in switch kind { case .photo: .images case .video: .videos } } return filters.count == 1 ? filters[0] : .any(of: filters) } /// How many the photo picker lets the person choose: nil is no limit. static func limit(_ pick: DefinitionCreatePick) -> Int? { pick.many ? nil : 1 } /// Whether the door's camera films as well as takes photographs. static func films(_ pick: DefinitionCreatePick) -> Bool { pick.kinds.contains(.video) } /// One chosen item written to a file: a clip as the file the picker /// hands over (a film is too big to hold in memory), a photograph as its /// bytes in the type it was taken in (a HEIC stays a HEIC). static func written(_ item: PhotosPickerItem) async -> URL? { if item.supportedContentTypes.contains(where: { $0.conforms(to: .movie) }) { return try? await item.loadTransferable(type: PickedClip.self)?.url } return await MediaStaging.stage(item)?.url } } /// A clip out of the photo picker, copied where this app may keep it until /// the intake has staged it. struct PickedClip: Transferable { let url: URL static var transferRepresentation: some TransferRepresentation { FileRepresentation(contentType: .movie) { clip in SentTransferredFile(clip.url) } importing: { received in let ext = received.file.pathExtension.isEmpty ? "mov" : received.file.pathExtension let copy = FileManager.default.temporaryDirectory.appendingPathComponent("picked-clip-\(UUID().uuidString).\(ext)") try FileManager.default.copyItem(at: received.file, to: copy) return PickedClip(url: copy) } } } extension View { /// A door's pictures (`create.doors[].pick`): while `picking` holds one, /// the photo picker or the camera, and what is chosen or taken handed to /// the share sheet's intake (`DeckDoors.bring`). func doorPictures(_ picking: Binding, for table: String?) -> some View { modifier(DoorPictures(picking: picking, table: table)) } } struct DoorPictures: ViewModifier { @Binding var picking: DefinitionCreatePick? /// The table whose door is open. let table: String? @State private var library = false @State private var limit: Int? = 1 @State private var filter: PHPickerFilter = .images @State private var chosen: [PhotosPickerItem] = [] @State private var camera = false @State private var films = false @Environment(\.shareIntake) private var shareIntake @Environment(\.cottonCore) private var core @Environment(\.cottonCapabilities) private var capabilities @Environment(ToastCenter.self) private var toasts: ToastCenter? func body(content: Content) -> some View { content .onChange(of: picking) { _, pick in guard let pick else { return } picking = nil switch pick.source { case .library: limit = DeckDoors.limit(pick) filter = DeckDoors.filter(pick.kinds) library = true case .camera: films = DeckDoors.films(pick) Task { await openCamera() } } } .photosPicker(isPresented: $library, selection: $chosen, maxSelectionCount: limit, selectionBehavior: .ordered, matching: filter, preferredItemEncoding: .current) .onChange(of: chosen) { _, items in // Cleared first, so the same pictures chosen twice in a row still land. guard !items.isEmpty else { return } chosen = [] Task { await bring(items) } } .fullScreenCover(isPresented: $camera) { CameraPicker(films: films, onTaken: { url in camera = false Task { await bring([url]) } }, onCancelled: { camera = false }) } } /// The camera, or the sentence saying why not. private func openCamera() async { if let refusal = await Camera.refusal(capabilities) { toasts?.show(refusal, tone: .error) return } camera = true } private func bring(_ items: [PhotosPickerItem]) async { var urls: [URL] = [] for item in items { if let url = await DeckDoors.written(item) { urls.append(url) } } if urls.count < items.count { toasts?.show(ShellLines.line("databases", "bring_in.unreadable"), tone: .error) } await bring(urls) } private func bring(_ urls: [URL]) async { defer { for url in urls { try? FileManager.default.removeItem(at: url) } } await DeckDoors.bring(urls, for: table, through: shareIntake, toasts: toasts, core: core) } }