oficina / base Public

Clone
git clone https://amadan.net/oficina/base

Plain git — no account needed to clone.

Download

Download this file

1import SwiftUI
2
3/// What the app does for Ask beyond Base's routes: opening links (the
4/// browser, or another app), and setting Ask up (Task 12 runs Oficina ID's
5/// set-up; until then the web's Settings).
6public struct AskActions {
7 /// Set Ask up (Task 12: with Oficina ID, silently).
8 public var setUp: () -> Void
9 /// Set Ask up on the web, with the code flow: the way on when Equipo
10 /// doesn't know this person (not_linked, P2). Never retried by itself.
11 public var setUpOnWeb: () -> Void
12 public init(setUp: @escaping () -> Void, setUpOnWeb: @escaping () -> Void = {}) { self.setUp = setUp; self.setUpOnWeb = setUpOnWeb }
13
14 /// What each of Ask's not-ready states offers: its line, and its
15 /// button's text and whether it sets Ask up here or on the web.
16 public enum Way: Equatable, Sendable { case here(String), web(String) }
17 public static func note(_ phase: AskPhase) -> (lede: String, way: Way?) {
18 switch phase {
19 case .connect: ("ask.connect.lede", .here("ask.connect.button"))
20 case .pending: ("ask.pending.lede", .here("ask.pending.button"))
21 case .reconnect: ("ask.reconnect.lede", .here("ask.reconnect.button"))
22 case .notLinked: ("native.ask.not_linked", .web("native.ask.not_linked.button"))
23 default: ("ask.unavailable", nil)
24 }
25 }
26}
27
28/// Ask Base, drawn: the conversation, its status line and the composer, or
29/// in their place the state Ask is in, with the web's approved copy
30/// (chat.html `ask-body`). The window's bar, title and the Mac's sidebar
31/// are the apps' own (Tasks 13 and 14).
32public struct AskView: View {
33 @Bindable var model: AskModel
34 let layout: AskLayout
35 let timeZone: TimeZone?
36 let actions: AskActions
37 let focusOnAppear: Bool
38 @FocusState private var focused: Bool
39 @Environment(\.colorScheme) private var scheme
40
41 /// `focusOnAppear`: the field takes focus as Ask shows (the menu-bar
42 /// panel; never on the iPhone, where it would raise the keyboard).
43 public init(model: AskModel, layout: AskLayout, timeZone: TimeZone?, actions: AskActions, focusOnAppear: Bool = false) {
44 self.model = model; self.layout = layout; self.timeZone = timeZone; self.actions = actions; self.focusOnAppear = focusOnAppear
45 }
46
47 public var body: some View {
48 let p = Palette(scheme)
49 Group {
50 if model.phase.chatting {
51 VStack(spacing: 0) {
52 if model.isEmpty {
53 Spacer(minLength: 0)
54 Text(BaseText.text("ask.empty"))
55 .font(BaseFont.body(14, relativeTo: .subheadline)).foregroundStyle(p[.haze])
56 .frame(maxWidth: .infinity, alignment: .leading)
57 .padding(.horizontal, 20).padding(.bottom, 12)
58 } else {
59 AskThread(model: model, layout: layout, timeZone: timeZone)
60 }
61 foot(p)
62 }
63 } else {
64 note(p)
65 }
66 }
67 .foregroundStyle(p[.frost])
68 .onAppear { if focusOnAppear { focused = true } }
69 }
70
71 @ViewBuilder private func foot(_ p: Palette) -> some View {
72 VStack(alignment: .leading, spacing: 6) {
73 if model.phase == .norunner || model.phase == .oldrunner {
74 Text(runnerNote(model.phase == .norunner ? "ask.norunner" : "ask.oldrunner"))
75 .font(BaseFont.body(14, relativeTo: .subheadline)).foregroundStyle(p[.haze]).tint(p[.focus])
76 .accessibilityAddTraits(.isStaticText)
77 }
78 if !model.status.isEmpty {
79 Text(verbatim: model.status)
80 .font(BaseFont.body(14, relativeTo: .subheadline)).foregroundStyle(p[.haze])
81 .accessibilityAddTraits(.updatesFrequently)
82 }
83 Composer(model: model, focused: $focused)
84 }
85 .padding(.horizontal, layout == .window ? 24 : 10)
86 .padding(.top, 6)
87 .padding(.bottom, layout == .window ? 20 : 12)
88 }
89
90 private func runnerNote(_ key: String) -> AttributedString { Self.runnerNote(key, equipo: model.equipoOrigin) }
91
92 /// The web's note (norunner, oldrunner), its Markdown link to Equipo
93 /// drawn as a real link, underlined, that opens in the browser (the
94 /// environment's openURL); never as brackets. With no Equipo known yet
95 /// the words stay, without a link.
96 public static func runnerNote(_ key: String, equipo: URL?) -> AttributedString {
97 let raw = BaseText.text(key)
98 let text: String
99 if let equipo, BaseJSON.webURL(equipo.absoluteString) != nil {
100 text = raw.replacingOccurrences(of: "{equipo}", with: "<\(equipo.absoluteString)>")
101 } else {
102 // [words]({equipo}) → words
103 text = raw.replacingOccurrences(of: #"\[([^\]]*)\]\(\{equipo\}\)"#, with: "$1", options: .regularExpression)
104 }
105 let options = AttributedString.MarkdownParsingOptions(interpretedSyntax: .inlineOnlyPreservingWhitespace)
106 guard var out = try? AttributedString(markdown: text, options: options) else {
107 return AttributedString(text.replacingOccurrences(of: #"[\[\]]|\(<[^>]*>\)"#, with: "", options: .regularExpression))
108 }
109 for run in out.runs where run.link != nil { out[run.range].underlineStyle = .single }
110 return out
111 }
112
113 @ViewBuilder private func note(_ p: Palette) -> some View {
114 VStack(alignment: .leading, spacing: 14) {
115 if model.phase == .loading {
116 ProgressView().controlSize(.small)
117 } else {
118 let note = AskActions.note(model.phase)
119 lede(note.lede, p)
120 switch note.way {
121 case .here(let key)?: button(key, actions.setUp)
122 case .web(let key)?: button(key, actions.setUpOnWeb)
123 case nil: EmptyView()
124 }
125 }
126 }
127 .padding(.vertical, 22).padding(.horizontal, 20)
128 .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
129 }
130
131 private func lede(_ key: String, _ p: Palette) -> some View {
132 Text(BaseText.text(key)).font(BaseFont.body(16)).foregroundStyle(p[.haze]).fixedSize(horizontal: false, vertical: true)
133 }
134
135 private func button(_ key: String, _ action: @escaping () -> Void) -> some View {
136 Button(action: action) { Text(BaseText.text(key)) }.buttonStyle(BaseButtonStyle(.primary))
137 }
138}
139