// THE ONE SYNCHRONOUS CAMERA DECISION (accepted, 8 Nov 2026): what a shot writes // (`newDefaults`, `onePhoto`, `shotValues`) stays here, in Swift, because the // shutter cannot wait on a core call. Every other rule about a record is the core's. // A create whose record is one photo (`TableDefinition.onePhoto`): the camera // at once, as the Camera tool opens it — no page of fields, no second tap. // The shot makes the row, its moment the record's time, and the camera // closes. A hand-built screen for the camera's own reason (`CameraScreen`); // which creates come here is the definition's shape, never a table's id. import CottonKit import CottonValues import SwiftUI struct ShotCreate: View { let definition: TableDefinition let column: ColumnDefinition let preset: [String: RecordValue] @Environment(\.records) private var store @Environment(\.cottonCore) private var core @Environment(\.cottonCapabilities) private var capabilities @Environment(\.dismiss) private var dismiss @Environment(ToastCenter.self) private var toasts @State private var camera: CameraModel? var body: some View { Group { if let camera { CameraScreen(model: camera) { dismiss() } } else { Color.black.ignoresSafeArea() } } .task { guard camera == nil, let core, let store else { return } let shot = OneShot() camera = CameraModel(shooter: FormShooter(core: core), capabilities: capabilities) { taken, moment in // One shot, one record: a second while the first is kept makes none. guard shot.take() else { return } do { _ = try await store.create( table: definition.id, data: definition.shotValues(column, key: taken.captured.key, at: moment, preset: preset)) dismiss() } catch { shot.release() toasts.show(ActionRefused.words(for: error), tone: .error) } } } } } /// Whether the create's one shot has been taken. @MainActor private final class OneShot { private var taken = false func take() -> Bool { guard !taken else { return false } taken = true return true } /// The create was refused: the next shot may try again. func release() { taken = false } } // The one-photo create: whether a new record opens the camera, and what its shot writes. extension TableDefinition { /// The photo a new record asks for, when it is all the record asks for: /// the one column the person can write on it besides its time (`date`, /// which the shot fills with its moment), and a photo. Such a create /// opens the camera, never a page of fields (owner, 17 Oct 2026: "the /// minute we say add a meal, it should just open up the camera"). Nil /// when the record asks anything else, or is not new. public func onePhoto(of record: TableRecord) -> ColumnDefinition? { let asked = columns.filter { column in column.key != date && !column.readonly && column.isShown(on: record) } guard asked.count == 1, let only = asked.first, only.type == .photo else { return nil } return only } /// Each writable column's definition default, by key: what a new record /// holds before anything is written (the value the core gives a record /// that leaves it out). public var newDefaults: [String: RecordValue] { var values: [String: RecordValue] = [:] for column in columns where !column.readonly { if let value = column.defaultValue, value != .null { values[column.key] = value } } return values } /// The values a one-photo create's shot makes its record with /// (`onePhoto`): the defaults under `preset`, the picture's key in /// `column`, and the shot's moment in the record's time column where the /// person may write it. public func shotValues(_ column: ColumnDefinition, key: String, at moment: Date, preset: [String: RecordValue]) -> [String: RecordValue] { let blank = newDefaults.merging(preset) { _, given in given } var values = blank values[column.key] = .text(key) if let time = self.column(date), !time.readonly { values[time.key] = .text(CottonDate.format(moment)) } return values } } private extension ColumnDefinition { /// Whether a shot writes this column for `record`: not hidden by the /// owner, and its `shown_when` holds. func isShown(on record: TableRecord) -> Bool { if hidden == true { return false } guard let shownWhen else { return true } return record[shownWhen.column].text.map(shownWhen.values.contains) ?? false } }