Cotton code
98 lines · 4.3 KB · SwiftRaw
// The one host: a pushed screen's model, owned for the life of the push.//// A screen is a value SwiftUI re-makes on every redraw, so it cannot own the// `@Observable` that holds its answer — something above it must, and die with// the push rather than with the app. Fifty-four `*Host: View` structs did that// by hand, each an `@State` model, an `init(core:toasts:…)` and a// `.rereading { model.reread($0) }`; this is that shape written once.//// **The model is built in `.task`, never in `init`.** `@Environment` is not// readable in a View's `init`, and a `View.init` runs on every evaluation —// `CapabilityGateSweepTests` holds the microphones to exactly this rule after a// recogniser built in a host's `init` gave the audio session back process-wide// on every redraw. One `ground`-coloured frame before the model exists is the// cost, and the skeleton that follows was always the first thing drawn.//// **A host with no core draws a page saying so.** `cottonCore` is optional in// the environment for a reason written on its key; the answer here is loud and// visible, never a skeleton that waits for ever or a refusal that blames the// database.//// **The model is seeded before its first frame.** A model that is// `AnswerSeeded` — every `ScreenRead`, and the hand-written list and detail// models — is handed the session's `AnswerCache` the moment it is built, so a// screen the person has been to before draws its last answer at once and// re-reads behind it. This is the one wire, so no screen seeds itself.import CottonKitimport SwiftUIpublic struct Hosted<Model: AnyObject & Observable, Screen: View>: View {    @Environment(\.cottonCore) private var core    @Environment(\.cottonAnswers) private var answers    @Environment(ToastCenter.self) private var toasts    @State private var model: Model?    private let make: @MainActor (any CottonCalling, ToastCenter) -> Model    private let screen: (Model) -> Screen    /// `nil` for a model with no read to repeat — a form, a settings page over    /// the defaults — so `.rereading` has nothing to call and calls nothing.    private let reread: ((Model, [ScreenFamily]) -> Task<Void, Never>?)?    /// A model with no read to repeat. The wired form, below, is the default    /// for anything `Rereadable`; this one says in its label that the screen    /// is off the wire on purpose.    public init(        unwired make: @escaping @MainActor (any CottonCalling, ToastCenter) -> Model,        @ViewBuilder screen: @escaping (Model) -> Screen    ) {        self.make = make        self.screen = screen        self.reread = nil    }    fileprivate init(        make: @escaping @MainActor (any CottonCalling, ToastCenter) -> Model,        screen: @escaping (Model) -> Screen,        reread: @escaping (Model, [ScreenFamily]) -> Task<Void, Never>?    ) {        self.make = make        self.screen = screen        self.reread = reread    }    public var body: some View {        if let model {            screen(model)                .rereading(acrossDatabases: (model as? any Rereadable)?.readsAcrossDatabases ?? false) { families in                    reread?(model, families)                }        } else if let core {            // The page's own ground while its model is made: the paper on            // the phone, never a white flash (291 row A).            PageGroundFill()                .ignoresSafeArea()                .task {                    let made = make(core, toasts)                    if let answers, let seeded = made as? AnswerSeeded { seeded.attach(answers) }                    model = made                }        } else {            QuietState(                title: ShellLines.line("chrome", "hosted.no_database"),                message: ShellLines.line("chrome", "hosted.this_screen_was_opened")            )        }    }}extension Hosted where Model: Rereadable {    /// The usual host: build the model from the core, hand it to the screen,    /// and read again whenever a fold names one of its families.    public init(        _ make: @escaping @MainActor (any CottonCalling, ToastCenter) -> Model,        @ViewBuilder screen: @escaping (Model) -> Screen    ) {        self.init(make: make, screen: screen, reread: { model, families in model.reread(families) })    }}