Cotton code
223 lines · 10.3 KB · SwiftRaw
// COTTON DATA'S ROOT (assets/design/data/DECISIONS.md, data.html, CONTRACT.md "The iPhone screens"): the whole app,// mounted by the root in place of the fan and the board (`RootView.hosts`), as Sites and Days are. First open says// what the app is, once; then four text places at the foot (Tables, Lately, Search, You). Every table, row, field and// word is the core's `data.*` answer or the `data` line set, drawn in the Stone look; the person's paper, ink, face and// size are the core's `look {app: "data"}`. Read only: no screen here edits or erases anything. As Android's `DataApp.kt`.#if os(iOS)import CottonKitimport SwiftUIstruct DataRoot: View {    /// Cotton Data's own build.    static var applies: Bool { FlavourManifest.current?.flavour == "data" }    @State private var model: DataModel    @Environment(\.scenePhase) private var phase    @Environment(\.accessibilityReduceMotion) private var still    @Environment(\.cottonSync) private var sync    /// The launch's mark, standing over the first screen until it has drawn, then fading (CONTRACT brief 1).    @State private var launching = true    init(core: any CottonCalling) {        _model = State(initialValue: DataModel(core: core))    }    var body: some View {        ZStack {            Stone.ground.ignoresSafeArea()            if model.welcomed {                DataPlaces(model: model)                    .accessibilityHidden(model.sheet != nil)            } else {                DataWelcomeScreen(model: model)            }            sheetOverlay            if let toast = model.toast {                VStack { Spacer(); DataToast(words: toast).padding(.bottom, 72) }                    .transition(.move(edge: .bottom).combined(with: .opacity))                    .allowsHitTesting(false)            }            if launching {                DataLaunchMark()                    .transition(.opacity)                    .allowsHitTesting(false)            }            // The app switcher's picture: the ground and the mark, never a row of the database.            if phase != .active {                DataLaunchMark().accessibilityHidden(true)            }        }        .lookStanding(Stone.look)        .task {            await Stone.look.read(model.core)            // The shell's splash goes on the next frame, leaving this mark where it stood, over the first screen.            FanLaunch.firstContent()            // You's shared list, read once a moment after launch on a phone with none remembered, as Days, Words and            // Story warm it: You opened first draws whole.            Task { await SettingsHome.warm(model.core) }            // The list fades in under the mark: it stands until Tables has its answer (at most three seconds; first            // open has nothing to wait for), then goes (at once with Reduce Motion).            for _ in 0..<60 where model.welcomed && !model.tablesShown { try? await Task.sleep(for: .milliseconds(50)) }            try? await Task.sleep(for: .milliseconds(still ? 0 : 200))            withAnimation(still ? nil : .easeOut(duration: 0.45)) { launching = false }        }        // Another device, a sync or a sign-in changed what every place answers: each asks again.        .onChange(of: sync?.revision) { _, _ in model.changed() }        // The home's sync line was tapped: You, where Sync stands.        .onReceive(NotificationCenter.default.publisher(for: SyncSeenOpens.notice)) { _ in model.place(.you) }        .onChange(of: phase) { _, now in if now == .active { model.changed() } }        .animation(still ? nil : .easeOut(duration: 0.22), value: model.sheet)    }    @ViewBuilder private var sheetOverlay: some View {        if let sheet = model.sheet {            let close = { withAnimation(still ? nil : .easeOut(duration: 0.2)) { model.sheet = nil } }            Group {                switch sheet {                case .link(let table, let id, let column):                    DataSheet(close: close, tag: "data-link-sheet") { DataLinkView(model: model, table: table, id: id, column: column, close: close) }                case .media(let table, let id, let column):                    DataSheet(close: close, tag: "data-media-sheet") { DataMediaView(model: model, table: table, id: id, column: column, close: close) }                }            }            .transition(still ? .opacity : .move(edge: .bottom).combined(with: .opacity))        }    }}/// The launch's mark alone on the ground, where the launch screen lays it (`LaunchObject`).struct DataLaunchMark: View {    var body: some View {        ZStack {            Stone.ground            if let object = SplashKnobs.object {                object.frame(maxWidth: .infinity, maxHeight: .infinity)            }        }        .ignoresSafeArea()        .accessibilityHidden(true)    }}/// The places and the screens stood on them, with the four tabs at the foot.struct DataPlaces: View {    @Bindable var model: DataModel    @Environment(\.accessibilityReduceMotion) private var still    /// The keyboard is up: the tabs step aside under it, as the phone's own tab bars do.    @State private var typing = false    var body: some View {        VStack(spacing: 0) {            ZStack {                // Each tab's root, made the first time it is chosen and kept, so each keeps its place.                ForEach(DataTab.allCases) { tab in                    if model.visited.contains(tab) {                        let shown = model.tab == tab && model.stack.isEmpty                        root(tab)                            .opacity(model.tab == tab ? 1 : 0)                            .allowsHitTesting(shown)                            .accessibilityHidden(!shown)                    }                }                // The screens stood on, each over the last; only the top one is reachable and heard.                ForEach(Array(model.stack.enumerated()), id: \.offset) { at, screen in                    let top = at == model.stack.count - 1                    DataScreenView(model: model, screen: screen)                        .id(screen.id)                        .background(Stone.ground)                        .allowsHitTesting(top)                        .accessibilityHidden(!top)                        .transition(still ? .opacity : .move(edge: .trailing).combined(with: .opacity))                        .gesture(top ? edgeBack : nil)                        .accessibilityAction(.escape) { model.back() }                }            }            .frame(maxWidth: .infinity, maxHeight: .infinity)            .animation(still ? nil : .easeOut(duration: 0.24), value: model.stack)            if !typing { DataTabs(model: model) }        }        .background(Stone.ground)        .onReceive(NotificationCenter.default.publisher(for: UIResponder.keyboardWillShowNotification)) { _ in typing = true }        .onReceive(NotificationCenter.default.publisher(for: UIResponder.keyboardWillHideNotification)) { _ in typing = false }    }    /// A swipe in from the leading edge goes back, as the phone's own pages do.    private var edgeBack: some Gesture {        DragGesture(minimumDistance: 20, coordinateSpace: .global)            .onEnded { end in                if end.startLocation.x < 28, end.translation.width > 80, abs(end.translation.height) < end.translation.width { model.back() }            }    }    @ViewBuilder private func root(_ tab: DataTab) -> some View {        switch tab {        case .tables: DataTablesScreen(model: model)        case .lately: DataLatelyScreen(model: model)        case .search: DataSearchScreen(model: model)        case .you: DataYouScreen(model: model)        }    }}/// One screen stood on a tab.struct DataScreenView: View {    let model: DataModel    let screen: DataScreen    var body: some View {        switch screen {        case .rows(let table): DataRowsScreen(model: model, table: table)        case .row(let table, let id, let trail): DataRowScreen(model: model, table: table, id: id, trail: trail)        case .about(let table): DataAboutScreen(model: model, table: table)        case .columns(let table): DataColumnsScreen(model: model, table: table)        case .links(let table): DataLinksScreen(model: model, table: table)        case .room: DataRoomScreen(model: model)        case .leftovers: DataLeftoversScreen(model: model)        case .assistants: DataAssistantsScreen(model: model)        case .keep: DataKeepScreen(model: model)        case .look: DataLookScreen(model: model)        }    }}/// The four places at the foot (`.tabs`): text only, the one stood on in ink and bold, with a short line under it.struct DataTabs: View {    let model: DataModel    var body: some View {        VStack(spacing: 0) {            DataRule()            HStack(spacing: 0) {                ForEach(DataTab.allCases) { tab in                    let on = model.tab == tab                    Button { model.place(tab) } label: {                        VStack(spacing: 3) {                            Text(verbatim: DataWords.line("tab.\(tab.rawValue)"))                                .font(DataType.sans(15, on ? .bold : .regular, .subheadline))                                .foregroundStyle(on ? Stone.ink : Stone.quiet)                                // One line, shrinking at the largest sizes rather than breaking a word; a long press                                // shows it large, as the phone's own tab bars do.                                .lineLimit(1)                                .minimumScaleFactor(0.5)                            Capsule().fill(on ? Stone.ink : .clear).frame(width: 18, height: 2)                        }                        .padding(.horizontal, 6)                        .frame(maxWidth: .infinity, minHeight: 44)                        .contentShape(Rectangle())                    }                    .buttonStyle(.plain)                    .accessibilityLabel(DataWords.line("a11y.\(tab.rawValue)_tab"))                    .accessibilityAddTraits(on ? [.isSelected, .isButton] : .isButton)                    .accessibilityIdentifier("data-tab-\(tab.rawValue)")                    .accessibilityShowsLargeContentViewer {                        Text(verbatim: DataWords.line("tab.\(tab.rawValue)"))                    }                }            }            .padding(.top, 6).padding(.bottom, 4)        }        .background(Stone.ground)    }}#endif