// One window's database, wired into the window.//// `.databaseScene(scene)` goes on a window's root, once: the phone's stack// (`RootView.hosts`) and the Mac window's board (`ColumnsHost`). It puts the// scene in the environment for the bar's cap, seeds// it from the app's `DatabasesModel` (which the root put in the environment),// and raises the Databases screen when the scene asks for it — so the centre// cap only calls `scene.openDatabases()`.//// On the phone the shown id also rides in `@SceneStorage`, so a scene the// system restores opens on the database it was showing. A Mac window is an// AppKit window hosting this view, with no SwiftUI scene to store into; its// value lives on `MacWindowState` for the life of the window.importCottonKitimportSwiftUIextensionView {/// Make this subtree one window's database scene. Needs a/// `DatabasesModel` in the environment above it; with none it draws the/// content alone.func databaseScene(_ scene: DatabaseScene) -> someView { modifier(DatabaseSceneWiring(scene: scene)) }}privatestructDatabaseSceneWiring: ViewModifier {let scene: DatabaseScene @Environment(DatabasesModel.self) privatevar model: DatabasesModel? @Environment(Navigator.self) privatevar navigator: Navigator? #if os(iOS) @SceneStorage("cotton.shownDatabase") privatevar stored: String? #endiffunc body(content: Content) -> someView { content .environment(scene) .task {guardlet model else { return } #if os(iOS)if scene.restoresStoredSelection, scene.shownId == nil, let stored { scene.shownId = stored } #endif// Seeded from the held list alone, so Home's fan never waits// on the sync lines, the owner or the rosters (readpool).let shown = await model.refreshList()await scene.seed(from: model)await model.refreshAround(shown: shown) }// A database made or let go in another window: a choice no longer// held falls back to the opening one. One this window SHOWED that// is no longer held — the device was removed from it, or it was// left elsewhere — takes the window back to Personal with its// navigation reset, as a leave of the shown database does// (`DatabaseLeaving.leaveShown`): nothing of it stays reachable. .onChange(of: model?.held) { previous, _ inguardlet model else { return }iflet shown = scene.shownId, !model.held.isEmpty, model.database(shown) == nil,let personal = model.ordered.first(where: \.isPersonal) {// Adoption replaces Personal's identity, not its place in// this window. Keep Backup open while its new copy draws.if previous?.first(where: { $0.databaseId == shown })?.isPersonal != true { navigator?.resetForDatabaseChange() scene.reset() }Task { await scene.show(personal.databaseId, in: model) }return }Task { await scene.seed(from: model) } } #if os(iOS) .onChange(of: scene.shownId) { _, id in stored = id } #endif #if COTTON_DEV// DEVELOPER TOOL: Settings › Developer › Databases. .onReceive(NotificationCenter.default.publisher(for: .cottonDevShowDatabases)) { _ in scene.openDatabases() } #endif// The screen is handed the model and the scene, as a card's// content is drawn in the environment it was opened from. .cottonCard(isPresented: Binding(get: { scene.choosing }, set: { scene.choosing = $0 }), detents: [.tall]) {iflet model {DatabasesScreen(model: model, scene: scene) } } }}#if COTTON_DEVextensionNotification.Name {/// DEVELOPER TOOL: open the Databases screen before the centre cap can.staticlet cottonDevShowDatabases = Notification.Name("com.cottondb.dev.showDatabases")}#endif