// 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. import CottonKit import SwiftUI extension View { /// 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) -> some View { modifier(DatabaseSceneWiring(scene: scene)) } } private struct DatabaseSceneWiring: ViewModifier { let scene: DatabaseScene @Environment(DatabasesModel.self) private var model: DatabasesModel? @Environment(Navigator.self) private var navigator: Navigator? #if os(iOS) @SceneStorage("cotton.shownDatabase") private var stored: String? #endif func body(content: Content) -> some View { content .environment(scene) .task { guard let 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, _ in guard let model else { return } if let 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]) { if let model { DatabasesScreen(model: model, scene: scene) } } } } #if COTTON_DEV extension Notification.Name { /// DEVELOPER TOOL: open the Databases screen before the centre cap can. static let cottonDevShowDatabases = Notification.Name("com.cottondb.dev.showDatabases") } #endif