Cotton code
176 lines · 7.7 KB · SwiftRaw
// COTTON CONTACTS' ROOT (PEOPLE-DESIGN.md f): the whole app, mounted by the root in place of the fan and the board// (`RootView.hosts`), as Sites and Secrets are. Four tabs at the foot, Contacts · Groups · Circle · You, each its own// stack of pages; a toast at the foot; the app switcher sees the ground, never a page.#if os(iOS)import CottonKitimport SwiftUIstruct ContactsRoot: View {    /// Cotton Contacts' own build.    static var applies: Bool { FlavourManifest.current?.flavour == "contacts" }    @State private var model: ContactsModel    @Environment(\.scenePhase) private var phase    @Environment(\.cottonSync) private var sync    @State private var name = ""    init(core: any CottonCalling) {        _model = State(initialValue: ContactsModel(core: core))    }    var body: some View {        ZStack(alignment: .bottom) {            ContactsInk.ground.ignoresSafeArea()            VStack(spacing: 0) {                // Every tab visited keeps its own stack (its place and scroll); only the one standing is seen and reached.                ZStack {                    ForEach(ContactsTab.allCases, id: \.self) { tab in                        if model.visited.contains(tab) {                            let on = model.tab == tab                            stack(tab)                                .opacity(on ? 1 : 0)                                .allowsHitTesting(on)                                .accessibilityHidden(!on)                                .zIndex(on ? 1 : 0)                        }                    }                }                if model.footShown { ContactsTabs(model: model) }            }            if let toast = model.toast {                Text(verbatim: toast)                    .font(ContactsType.small)                    .foregroundStyle(ContactsInk.onAccent)                    .multilineTextAlignment(.center)                    .padding(.horizontal, 16)                    .padding(.vertical, 10)                    .background(Capsule().fill(ContactsInk.ink))                    .padding(.horizontal, 24)                    .padding(.bottom, 72)                    .transition(.move(edge: .bottom).combined(with: .opacity))                    .allowsHitTesting(false)                    .accessibilityIdentifier("contacts-toast")            }            if phase != .active {                ContactsInk.ground.ignoresSafeArea().accessibilityHidden(true)            }        }        .tint(ContactsInk.accent)        .confirmationDialog(model.confirming?.confirm ?? "", isPresented: Binding(get: { model.confirming != nil }, set: { if !$0 { model.confirming = nil } }),                            titleVisibility: .visible, presenting: model.confirming) { act in            Button(act.words, role: act.red == true ? .destructive : nil) { Task { await model.take(act.call) } }                .accessibilityIdentifier("contacts-ask-yes")            Button(ContactsWords.line("ask.cancel"), role: .cancel) {}        }        .alert(model.naming?.title ?? "", isPresented: Binding(get: { model.naming != nil }, set: { if !$0 { model.naming = nil } })) {            TextField("", text: $name).accessibilityIdentifier("contacts-ask-text")            Button(ContactsWords.line("ask.done")) { model.named(name); name = "" }.accessibilityIdentifier("contacts-ask-yes")            Button(ContactsWords.line("ask.cancel"), role: .cancel) { model.naming = nil; name = "" }        }        .onChange(of: sync?.revision) { _, _ in model.changed() }        .onChange(of: phase) { _, now in if now == .active { model.changed() } }    }    private func stack(_ tab: ContactsTab) -> some View {        NavigationStack(path: Binding(get: { model.paths[tab] ?? [] }, set: { model.paths[tab] = $0 })) {            first(tab)                .toolbar(.hidden, for: .navigationBar)                .navigationDestination(for: ContactsRoute.self) { route in                    page(route)                        .navigationBarTitleDisplayMode(.inline)                        .toolbarBackground(ContactsInk.ground, for: .navigationBar)                }        }    }    @ViewBuilder private func first(_ tab: ContactsTab) -> some View {        switch tab {        case .contacts: ContactsHomeScreen(model: model)        case .groups: ContactsGroupsScreen(model: model)        case .circle: ContactsCircleScreen(model: model)        case .you: ContactsYouScreen(model: model)        }    }    @ViewBuilder private func page(_ route: ContactsRoute) -> some View {        switch route {        case .person(let id): ContactsPersonScreen(model: model, id: id)        case .group(let id): ContactsGroupScreen(model: model, id: id)        case .same: ContactsSameScreen(model: model, pair: nil)        case .pair(let call): ContactsSameScreen(model: model, pair: call)        case .form(let call): ContactsFormScreen(model: model, call: call)        case .pick(let call, let name): ContactsPickScreen(model: model, call: call, name: name)        case .read: ContactsReadScreen(model: model)        }    }}/// The four tabs in words at the foot, the one standing in bold accent; a tap on it goes back to its first screen.struct ContactsTabs: View {    let model: ContactsModel    var body: some View {        VStack(spacing: 0) {            ContactsRule()            HStack(spacing: 0) {                ForEach(ContactsTab.allCases, id: \.self) { tab in                    let on = model.tab == tab                    Button {                        if on { model.paths[tab] = [] } else { model.open(tab) }                    } label: {                        VStack(spacing: 3) {                            Text(verbatim: ContactsWords.line("tabs.\(tab.rawValue)"))                                .font(ContactsType.tab.weight(on ? .bold : .regular))                                .foregroundStyle(on ? ContactsInk.accent : ContactsInk.quiet)                                .lineLimit(1)                                .minimumScaleFactor(0.8)                            Capsule().fill(on ? ContactsInk.accent : .clear).frame(width: 18, height: 2)                        }                        .padding(.top, 10)                        .padding(.bottom, 4)                        .frame(maxWidth: .infinity, minHeight: 48)                        .contentShape(Rectangle())                    }                    .buttonStyle(.plain)                    .accessibilityAddTraits(on ? [.isSelected, .isButton] : .isButton)                    .accessibilityIdentifier("contacts-tab-\(tab.rawValue)")                }            }        }        .dynamicTypeSize(...DynamicTypeSize.xxxLarge)        .background(ContactsInk.ground.ignoresSafeArea(edges: .bottom))    }}/// A page's frame: scrolls, the side margins, the ground.struct ContactsPage<Content: View>: View {    var tag: String    @ViewBuilder var content: () -> Content    var body: some View {        ScrollView {            VStack(alignment: .leading, spacing: 0) { content() }                .padding(.horizontal, ContactsInk.side)                .padding(.top, 8)                .padding(.bottom, 32)        }        .scrollDismissesKeyboard(.interactively)        .background(ContactsInk.ground.ignoresSafeArea())        .accessibilityIdentifier(tag)    }}/// A read not answered, or refused: the core's sentence and Try again.struct ContactsRefused: View {    let line: String    let again: () -> Void    var body: some View {        VStack(alignment: .leading, spacing: 12) {            ContactsNote(text: line)            ContactsButton(label: ContactsWords.line("ask.try_again"), filled: false, id: "contacts-try-again", action: again)        }        .padding(.top, 20)    }}#endif