// 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.importCottonKitimportSwiftUIpublicstructHosted<Model: AnyObject & Observable, Screen: View>: View { @Environment(\.cottonCore) privatevar core @Environment(\.cottonAnswers) privatevar answers @Environment(ToastCenter.self) privatevar toasts @State privatevar model: Model?privatelet make: @MainActor (any CottonCalling, ToastCenter) -> Modelprivatelet 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.privatelet 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.publicinit( unwired make: @escaping @MainActor (any CottonCalling, ToastCenter) -> Model, @ViewBuilder screen: @escaping (Model) -> Screen ) {self.make = makeself.screen = screenself.reread = nil }fileprivateinit( make: @escaping @MainActor (any CottonCalling, ToastCenter) -> Model, screen: @escaping (Model) -> Screen, reread: @escaping (Model, [ScreenFamily]) -> Task<Void, Never>? ) {self.make = makeself.screen = screenself.reread = reread }publicvar body: someView {iflet model { screen(model) .rereading(acrossDatabases: (model as? any Rereadable)?.readsAcrossDatabases ?? false) { families in reread?(model, families) } } elseiflet 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)iflet 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") ) } }}extensionHostedwhereModel: 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.publicinit( _ 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) }) }}