Cotton code
100 lines · 3.8 KB · SwiftRaw
// 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.import Foundation/// The one write a shot makes into Cotton. ``CoreMediaCapture`` is it.public protocol CameraShooting: Sendable {    func shoot(file url: URL, mime: String, kind: CaptureMediaKind, filename: String?) async throws -> MediaCaptured}extension CoreMediaCapture: 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.public struct FormShooter: CameraShooting {    private let capture: CoreMediaCapture    public init(core: any CottonCalling) {        capture = CoreMediaCapture(core: core)    }    public func shoot(file url: URL, mime: String, kind: CaptureMediaKind, filename: String?) async throws -> MediaCaptured {        try await capture.capture(file: url, mime: mime, kind: kind, filename: filename)    }}/// What became of one shot.public struct ShotTaken: Sendable, Equatable {    /// The photo or film's record in the personal database.    public var captured: MediaCaptured    /// Whether the camera roll took it too.    public var roll: RollOutcome}public enum RollOutcome: 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}public struct ShotPipeline: Sendable {    private let shooter: any CameraShooting    private let roll: (any CameraRoll)?    /// Asks for adding to the photo library through the capability gate;    /// true once the OS has said yes.    private let mayAddToRoll: @Sendable () async -> Bool    public init(        shooter: any CameraShooting,        roll: (any CameraRoll)? = LiveStores.cameraRoll,        mayAddToRoll: @escaping @Sendable () async -> Bool    ) {        self.shooter = shooter        self.roll = roll        self.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.    public func take(file: URL, kind: CaptureMediaKind, saveToRoll: Bool) async throws -> ShotTaken {        defer { try? FileManager.default.removeItem(at: file) }        let captured = try await shooter.shoot(            file: file,            mime: kind == .video ? "video/quicktime" : "image/jpeg",            kind: kind,            filename: file.lastPathComponent        )        return ShotTaken(captured: captured, roll: await addToRoll(file, kind: kind, saveToRoll: saveToRoll))    }    private func addToRoll(_ file: URL, kind: CaptureMediaKind, saveToRoll: Bool) async -> RollOutcome {        guard saveToRoll else { return .off }        guard let roll else { return .unreachable }        guard await mayAddToRoll() else { return .refused }        do {            try await roll.save(file: file, kind: kind)            return .saved        } catch {            return .failed        }    }}