298 lines · 14.5 KB · SwiftRaw
// YOU, KEEP EVERYTHING ON THIS PHONE AND LOOK (data.html; Android's `You.kt`). You is the card, Data's own places// (`data.you`: Keep everything, Assistants, Leftovers, Where room goes, Look), then Sync, Privacy, Storage, Help and// About as every Cotton app has them (the shared Settings parts, organised as Days' and Words' are; Delete account last// on the card's page). Keep everything is this phone's own choice, never a write to the database. Look is the shared// Look screen fed `look {app: "data"}`; values, ids and table names stay in JetBrains Mono whatever the face.#if os(iOS)import CottonKitimport CottonWritingimport SwiftUI@MainActorextension SettingsLook { /// The shared settings parts in Stone: its quiet headings, Instrument Sans, outlined buttons, accent switches. static var data: SettingsLook { SettingsLook( ink: Stone.ink, quiet: Stone.quiet, rule: Stone.rule, ground: Stone.ground, body: DataType.body, small: DataType.line, heading: { AnyView(DataSmall(text: $0)) }, act: { words, id, enabled, tap in AnyView(Button(action: tap) { Text(verbatim: words) }.buttonStyle(DataButtonStyle()).disabled(!enabled).opacity(enabled ? 1 : 0.45) .accessibilityIdentifier(id)) }, choice: { words, on, id, enabled, tap in AnyView(DataChip(label: words, on: on, tag: id, tap: tap).disabled(!enabled)) }, danger: Stone.ink, title: DataType.title ) }}/// YOU (data.html "You"; `data.you`, then the shell's `shell.settings`).struct DataYouScreen: View { let model: DataModel @State private var you: DataYou? @State private var home = SettingsHome() @State private var seen = 0 @State private var youReads = ReadInTurn() @State private var homeReads = ReadInTurn() @Environment(\.cottonSync) private var sync var body: some View { DataPage(tag: "data-you") { if home.place != nil { DataBar(back: you?.title ?? DataWords.line("you.title")) { home.back {} } DataTitle(text: home.title(own: { _ in nil }) ?? "") } else { Spacer().frame(height: 8) DataTitle(text: you?.title ?? DataWords.line("you.title")) } page.padding(.top, 6) } // Data's own rows and the shared ones are asked side by side: neither waits on the other (owner, build 1: // You stood with its card alone while the shared list's whole answer was still coming). .task(id: "\(sync?.revision ?? 0)/\(model.revision)") { youReads.ask { if let read = try? await model.calls.you() { you = read } } homeReads.ask { await home.read(model.core) } } // A tap on the You tab brings You back to its top. .onChange(of: model.placed) { _, _ in if model.tab == .you { home.place = SyncSeenOpens.take() ? .sync : nil } } } @ViewBuilder private var page: some View { switch home.place { case nil: SettingsHomeList(home: home, look: .data, own: own) { VStack(alignment: .leading, spacing: 4) { SettingsYouCard(home: home, core: model.core, look: .data) { model.tell($0) } if let line = you?.card.line { Text(verbatim: line).font(DataType.line).foregroundStyle(Stone.quiet) .accessibilityIdentifier("data-you-card-line") } } } case .profile?: SettingsProfileHead(home: home, look: .data) SettingsProfileFoot(home: home, core: model.core, look: .data, said: { model.tell($0) }) { home.place = nil model.place(.tables) } case .own?: EmptyView() case let shared?: SettingsSharedPage(place: shared, home: home, core: model.core, look: .data, said: { model.tell($0) }) { Task { await home.read(model.core) } } } } /// Data's own places, each saying how it stands; each opens its screen over You. private var own: [SettingsOwnRow] { (you?.rows ?? []).map { row in let line = row.id == "look" ? Stone.look.answer?.line : row.valueLine return SettingsOwnRow(id: row.id, label: row.title, line: line) { switch row.id { case "keep": model.open(.keep) case "assistants": model.open(.assistants) case "leftovers": model.open(.leftovers) case "room": model.open(.room) case "look": model.open(.look) default: break } } } }}/// KEEP EVERYTHING ON THIS PHONE (data.html "The setting"; `data.keep`, `data.keep.set`): the switch, how it stands,/// what it costs in this phone's own numbers, why the database shrinks on the phone, the privacy of rows kept opened,/// Only on Wi-Fi, what happens when room runs short. The phone's free room goes with every ask.struct DataKeepScreen: View { let model: DataModel @State private var read: DataRead<DataKeep> = .reading @State private var reads = ReadInTurn() var body: some View { DataPage(tag: "data-keep") { DataBar(back: model.backWord) { model.back() } DataTitle(text: read.answer?.title ?? DataWords.line("keep.title")) if let a = read.answer { Spacer().frame(height: 8) DataToggleRow(label: a.switch, on: a.on, strong: true, tag: "data-keep-switch") { on in set(on: on) } if let progress = a.progress { DataFillProgress(progress: progress) } else if let state = a.stateLine { DataNote(text: state).accessibilityIdentifier("data-keep-state") } if let small = a.tooSmallLine { DataNote(text: small) } if !a.cost.isEmpty { DataSmall(text: a.costsTitle) ForEach(Array(a.cost.enumerated()), id: \.offset) { at, cost in DataFlow(spacing: 10, lineSpacing: 2) { Text(verbatim: cost.name).font(DataType.body).foregroundStyle(Stone.quiet).fixedSize(horizontal: false, vertical: true) Text(verbatim: cost.valueLine).font(DataType.sans(16, .bold)).foregroundStyle(Stone.ink).fixedSize(horizontal: false, vertical: true) } .frame(maxWidth: .infinity, alignment: .leading) .padding(.vertical, 11) .accessibilityElement(children: .combine) if at < a.cost.count - 1 { DataRule() } } } if let why = a.whyLine { DataNote(text: why) } DataNote(text: a.privacyLine) DataSmall(text: a.howTitle) DataToggleRow(label: a.wifi, on: a.wifiOnly, line: a.wifiLine, tag: "data-keep-wifi") { on in set(wifiOnly: on) } DataSmall(text: a.shortTitle) Text(verbatim: a.shortLine).font(DataType.body).foregroundStyle(Stone.ink).lineSpacing(4).fixedSize(horizontal: false, vertical: true) if let mail = a.allMailLine { DataNote(text: mail) } } else if let words = read.refusal { DataLead(text: words) } else { DataWaiting() } } .task(id: model.revision) { reads.ask { await load() } } // While it fills, read again every few seconds: the percent and the time left move. .task { while !Task.isCancelled { try? await Task.sleep(for: .seconds(4)) if read.answer?.state == "filling" { reads.ask { await load() } } } } } private func load() async { let next = await DataRead.of { try await model.calls.keep() } if next.answer != nil || read.answer == nil { read = next } } private func set(on: Bool? = nil, wifiOnly: Bool? = nil) { Task { do { read = .read(try await model.calls.keepSet(on: on, wifiOnly: wifiOnly)) } catch { model.tell(DataModel.words(error)) } } }}/// The fill as it runs (data.html "Filling"): the percent large in mono, the accent bar, the time left, the line.struct DataFillProgress: View { let progress: DataKeep.Progress var body: some View { VStack(alignment: .leading, spacing: 0) { HStack(alignment: .lastTextBaseline, spacing: 2) { Text(verbatim: String(progress.percent)).font(DataType.mono(56, .medium, .largeTitle)).foregroundStyle(Stone.ink) Text(verbatim: "%").font(DataType.mono(20, .regular, .title3)).foregroundStyle(Stone.quiet) } DataFillBar(percent: progress.percent, thick: true).padding(.top, 14) if let left = progress.leftLine { Text(verbatim: left).font(DataType.line).foregroundStyle(Stone.quiet) .frame(maxWidth: .infinity, alignment: .trailing).padding(.top, 8) } if let line = progress.line { DataNote(text: line) } } .padding(.top, 12) .accessibilityElement(children: .combine) .accessibilityIdentifier("data-keep-progress") }}/// LOOK (data.html "Look"; the shared Look screen fed `look {app: "data"}`): a live preview of Data's own content (a row/// line, a table line, an amount), then paper for light and dark, ink, text and size, and Reset.struct DataLookScreen: View { let model: DataModel @State private var look = LookModel.standing("data") @State private var asking = false var body: some View { DataPage(tag: "data-look") { DataBar(back: model.backWord) { model.back() } DataTitle(text: look.answer?.word("title") ?? DataWords.line("you.look")) if let answer = look.answer { LookScreen(choices: LookChoices(answer), words: LookWords(answer), skin: Self.skin, gutter: 0, choose: { pick in Task { await look.choose(pick, core: model.core); tellRefusal() } }, reset: { asking = true }) { dark in DataLookPreview(answer: answer, dark: dark) } more: { EmptyView() } .padding(.top, 10) } } .task { await look.read(model.core) } .overlay { if asking, let answer = look.answer { DataSheet(close: { asking = false }, tag: "data-look-reset") { VStack(alignment: .leading, spacing: 0) { Text(verbatim: answer.word("reset_ask")).font(DataType.sans(21, .bold, .title3)).foregroundStyle(Stone.ink) .fixedSize(horizontal: false, vertical: true) Button { asking = false Task { await look.reset(core: model.core); tellRefusal() } } label: { Text(verbatim: answer.word("reset_yes")) } .buttonStyle(DataButtonStyle(go: true)) .padding(.top, 20) .accessibilityIdentifier("data-look-reset-yes") } } } } } private func tellRefusal() { if let refused = look.refused { model.tell(refused); look.refused = nil } } @MainActor static var skin: LookSkin { LookSkin(ground: Stone.ground, ink: Stone.ink, quiet: Stone.quiet, accent: Stone.accent, onAccent: Stone.onAccent, sheet: Stone.ground, rule: Stone.rule, font: { size, weight in DataType.sans(size, weight == .bold || weight == .semibold ? .semibold : (weight == .medium ? .medium : .regular)) }, face: { id, size in id == LookFaces.own ? DataType.own(size) : Font(WritingFonts.font(LookFaces.drawn(id, own: "sans"), size: size)) }) }}/// The core's made-up row line, table line and amount (`look.preview`), in the paper of one appearance and the face/// being chosen; names and values stay in the mono.struct DataLookPreview: View { let answer: LookAnswer let dark: Bool private func colour(_ token: String, _ fallback: Color) -> Color { answer.colour(token, dark: dark) ?? fallback } var body: some View { let ink = colour("ink", Stone.ink), quiet = colour("quiet", Stone.quiet), rule = colour("rule", Stone.rule), ground = colour("ground", Stone.ground) VStack(alignment: .leading, spacing: 0) { ForEach(Array((answer.preview?.dataRows ?? []).enumerated()), id: \.offset) { at, row in if at > 0 { Rectangle().fill(rule).frame(height: 1) } HStack(alignment: .center, spacing: 10) { VStack(alignment: .leading, spacing: 2) { Text(verbatim: row.title).font(row.mono == true ? DataType.monoName : DataType.name).foregroundStyle(ink).lineLimit(row.mono == true ? 1 : nil).minimumScaleFactor(row.mono == true ? 0.5 : 1) .fixedSize(horizontal: false, vertical: true) Text(verbatim: row.line).font(DataType.line).foregroundStyle(quiet).fixedSize(horizontal: false, vertical: true) } .frame(maxWidth: .infinity, alignment: .leading) if let when = row.when { Text(verbatim: when).font(row.mono == true ? DataType.monoLine : DataType.line).foregroundStyle(quiet) } } .padding(.vertical, 10) } if let field = answer.preview?.field { Rectangle().fill(rule).frame(height: 1) VStack(alignment: .leading, spacing: 4) { HStack { Text(verbatim: field.column).font(DataType.monoLine).foregroundStyle(quiet).frame(maxWidth: .infinity, alignment: .leading) Text(verbatim: field.type).font(DataType.sans(13, .regular, .footnote)).foregroundStyle(quiet) } Text(verbatim: field.value).font(DataType.mono(19)).foregroundStyle(ink) } .padding(.vertical, 12) } } .padding(.horizontal, 14).padding(.vertical, 4) .background(RoundedRectangle(cornerRadius: 12).fill(ground)) .overlay(RoundedRectangle(cornerRadius: 12).strokeBorder(rule, lineWidth: 1)) .accessibilityElement(children: .combine) }}#endif