// What happens to one Camera tool shot, in order.//// **Cotton first, the roll second, the file last.** The shot is saved into the// personal database the moment it is taken (`camera.shot`: the photo row);// only then is it added to the camera roll, and only while the person leaves// that on; then the staged file goes. A roll that refuses or fails never// loses the shot — it is already in Cotton, and the answer says the roll did// not take it. Bursts need nothing here: each shot is one call.//// The capture surface is the only caller; a test drives this with a fixture// image, a recording shooter and a recording roll, because the simulator has// no camera.importFoundation/// The one write a shot makes into Cotton. ``CoreMediaCapture`` is it.publicprotocolCameraShooting: Sendable {func shoot(file url: URL, mime: String, kind: CaptureMediaKind, filename: String?) asyncthrows -> MediaCaptured}extensionCoreMediaCapture: CameraShooting {}/// A one-photo form's shot (`TableDefinition.onePhoto`): the picture kept/// in the form's database through `capture.media` — the row the form makes/// with it is the record.publicstructFormShooter: CameraShooting {privatelet capture: CoreMediaCapturepublicinit(core: any CottonCalling) { capture = CoreMediaCapture(core: core) }publicfunc shoot(file url: URL, mime: String, kind: CaptureMediaKind, filename: String?) asyncthrows -> MediaCaptured {tryawait capture.capture(file: url, mime: mime, kind: kind, filename: filename) }}/// What became of one shot.publicstructShotTaken: Sendable, Equatable {/// The photo or film's record in the personal database.publicvar captured: MediaCaptured/// Whether the camera roll took it too.publicvar roll: RollOutcome}publicenumRollOutcome: Sendable, Equatable {/// Added to the camera roll.case saved/// The person turned the roll off for this device.case off/// This process has no camera roll (a test, a Mac without one).case unreachable/// The OS refused adding to the photo library; the shot is in Cotton.case refused/// The roll failed to take the file; the shot is in Cotton.case failed}publicstructShotPipeline: Sendable {privatelet shooter: any CameraShootingprivatelet roll: (any CameraRoll)?/// Asks for adding to the photo library through the capability gate;/// true once the OS has said yes.privatelet mayAddToRoll: @Sendable () async -> Boolpublicinit( shooter: any CameraShooting, roll: (any CameraRoll)? = LiveStores.cameraRoll, mayAddToRoll: @escaping @Sendable () async -> Bool ) {self.shooter = shooterself.roll = rollself.mayAddToRoll = mayAddToRoll }/// One shot, staged at `file` by the capture surface; the file is this/// pipeline's from here and is gone when it returns, kept or not.publicfunc take(file: URL, kind: CaptureMediaKind, saveToRoll: Bool) asyncthrows -> ShotTaken {defer { try? FileManager.default.removeItem(at: file) }let captured = tryawait shooter.shoot( file: file, mime: kind == .video ? "video/quicktime" : "image/jpeg", kind: kind, filename: file.lastPathComponent )returnShotTaken(captured: captured, roll: await addToRoll(file, kind: kind, saveToRoll: saveToRoll)) }privatefunc addToRoll(_ file: URL, kind: CaptureMediaKind, saveToRoll: Bool) async -> RollOutcome {guard saveToRoll else { return .off }guardlet roll else { return .unreachable }guardawait mayAddToRoll() else { return .refused }do {tryawait roll.save(file: file, kind: kind)return .saved } catch {return .failed } }}