oficina / docs Public

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

Plain git — no account needed to clone.

Download

Download this file

1import Foundation
2import RastrilloApple
3import RastrilloNative
4import SwiftUI
5
6/// What the window can do with its document, from the last detail answer
7/// (spec §6 rows 1 and 10). A 401 is not here: it pauses input and leaves
8/// the access alone while the session is re-minted.
9enum DocsAccess: Equatable, Sendable {
10 case writable
11 case readOnly
12 /// A 403 or 404 from `refreshAccess`: deleted, or no longer shared.
13 case unavailable
14}
15
16/// One row of spec §6's subtitle table, in its precedence order. The first
17/// that applies wins (`init(...)`).
18enum DocsSubtitleRow: Equatable, Sendable {
19 case unavailable // 1
20 case failedHeld // 2
21 case cannotSaveOnDevice // 3
22 case tooManyUnsent // 4
23 case needsSignIn(pending: Int) // 5
24 case signedOut // 6
25 case offline(pending: Int) // 7, and 12 is `saved` with the banner
26 case retrying(pending: Int) // 8
27 case saving // 9
28 case viewOnly // 10
29 case saved // 11 and 12
30 case loading // 13
31
32 /// - `savingShown`: the status has not been `.saved` for a whole second
33 /// (the debounce, kept by the view in a `.task`, never in `body`).
34 /// - `loading`: no projection yet.
35 ///
36 /// Row 13 is checked before rows 10 and 11: the table lists it last, but
37 /// a document still loading is "saved" to the outbox, so taken in the
38 /// table's order it could never show.
39 init(access: DocsAccess, status: SyncStatus, savingShown: Bool, loading: Bool) {
40 if access == .unavailable { self = .unavailable; return }
41 switch status {
42 case .failed(_, .held): self = .failedHeld; return
43 case .failed(_, .cannotSaveOnDevice): self = .cannotSaveOnDevice; return
44 case .failed(_, .tooManyUnsent): self = .tooManyUnsent; return
45 case .needsSignIn(let pending): self = pending > 0 ? .needsSignIn(pending: pending) : .signedOut; return
46 case .offline(let pending): self = .offline(pending: pending); return
47 case .retrying(let pending): self = .retrying(pending: pending); return
48 case .saving, .saved: break
49 }
50 if savingShown { self = .saving; return }
51 if loading { self = .loading; return }
52 self = access == .readOnly ? .viewOnly : .saved
53 }
54
55 /// Red, and the words say what went wrong (rows 1–6).
56 var isNegative: Bool {
57 switch self {
58 case .unavailable, .failedHeld, .cannotSaveOnDevice, .tooManyUnsent, .needsSignIn, .signedOut: return true
59 default: return false
60 }
61 }
62 /// Only row 2 is semibold, as the kit's failed status is.
63 var isEmphasised: Bool { self == .failedHeld }
64
65 /// The kit's rule (offline, needs sign-in and failed are announced, a
66 /// count change inside one kind is not, saving never is), plus No longer
67 /// available, and View only when it arrives mid-session.
68 static func announces(from old: DocsSubtitleRow, to new: DocsSubtitleRow) -> Bool {
69 if sameKind(old, new) { return false }
70 switch new {
71 case .unavailable, .failedHeld, .cannotSaveOnDevice, .tooManyUnsent, .needsSignIn, .signedOut, .offline: return true
72 case .viewOnly: return old != .loading
73 default: return false
74 }
75 }
76 static func sameKind(_ a: DocsSubtitleRow, _ b: DocsSubtitleRow) -> Bool {
77 switch (a, b) {
78 case (.offline, .offline), (.retrying, .retrying): return true
79 case (.needsSignIn, .needsSignIn), (.needsSignIn, .signedOut), (.signedOut, .needsSignIn), (.signedOut, .signedOut): return true
80 default: return a == b
81 }
82 }
83}
84
85/// Docs' catalog read in one localisation, so a view drawn in Arabic for a
86/// snapshot says Arabic even though the process runs in English, and the
87/// kit's sync wording in the same language.
88struct DocsStrings {
89 let bundle: Bundle
90 let localization: String
91
92 init(locale: Locale) {
93 let available = DocsResources.bundle.localizations.filter { $0 != "Base" }
94 let chosen = Bundle.preferredLocalizations(from: available, forPreferences: [locale.identifier]).first
95 ?? DocsResources.bundle.preferredLocalizations.first ?? "en"
96 localization = chosen
97 bundle = DocsResources.bundle.path(forResource: chosen, ofType: "lproj").flatMap(Bundle.init(path:)) ?? DocsResources.bundle
98 }
99 func callAsFunction(_ key: String) -> String { bundle.localizedString(forKey: key, value: nil, table: nil) }
100 var sync: NativeSyncStrings { NativeSyncStrings.kit(localization: localization) }
101
102 /// `native.subtitle.joined` ("%1$@ · %2$@"), looked up as a key and then
103 /// formatted, so a right-to-left language can order the two.
104 func joined(_ first: String?, _ second: String?) -> String {
105 switch (first?.isEmpty == false ? first : nil, second) {
106 case (let first?, let second?): return String(format: self("native.subtitle.joined"), first, second)
107 case (let first?, nil): return first
108 case (nil, let second?): return second
109 case (nil, nil): return ""
110 }
111 }
112
113 /// The state half of the subtitle, nil when the row says only the team.
114 func state(_ row: DocsSubtitleRow) -> String? {
115 let sync = self.sync
116 switch row {
117 case .unavailable: return self("native.subtitle.unavailable")
118 case .failedHeld: return sync.failed(.held(.tooLarge))
119 case .cannotSaveOnDevice: return sync.failed(.cannotSaveOnDevice)
120 case .tooManyUnsent: return sync.failed(.tooManyUnsent)
121 case .needsSignIn(let pending): return sync.needsSignIn(pending)
122 case .signedOut: return self("native.subtitle.signedOut")
123 case .offline(let pending): return sync.offline(pending)
124 case .retrying: return sync.retrying
125 case .saving: return sync.saving
126 case .viewOnly: return self("native.subtitle.viewOnly")
127 case .saved: return self("native.subtitle.saved")
128 case .loading: return nil
129 }
130 }
131 func subtitle(team: String?, row: DocsSubtitleRow) -> String { joined(team, state(row)) }
132}
133
134/// The subtitle under the document's title: "<Team> · <state>" (spec §6).
135/// Its own small view, so a sync status change re-evaluates this and
136/// nothing else: the body that hosts the text view never reads the status.
137@MainActor struct DocsSubtitleView: View {
138 let model: DocsModel
139 let team: String?
140 /// Whether its changes are announced: false for a copy that is folded
141 /// away while another shows (the iPhone's large title under its bar
142 /// title), so each change is heard once.
143 var announces = true
144 var body: some View {
145 DocsSubtitleBody(status: model.syncStatus, access: model.documentAccess, loading: model.loading, draft: model.isDraft, team: team,
146 announces: announces)
147 }
148}
149
150/// The subtitle given its inputs, holding the "Saving…" debounce and the
151/// announcements. `observe` hears every row shown (tests).
152@MainActor struct DocsSubtitleBody: View {
153 let status: SyncStatus
154 let access: DocsAccess
155 let loading: Bool
156 let draft: Bool
157 let team: String?
158 var observe: ((DocsSubtitleRow) -> Void)? = nil
159 var announces = true
160 @Environment(\.locale) private var locale
161 /// The 1 s debounce for row 9, set only from the `.task` below.
162 @State private var savingShown = false
163 @State private var shown: DocsSubtitleRow?
164
165 var body: some View {
166 let row = DocsSubtitleRow(access: access, status: status, savingShown: savingShown, loading: loading)
167 DocsSubtitleLine(row: row, team: team, strings: DocsStrings(locale: locale))
168 // Not `.saved` continuously for a second shows "Saving…"; every
169 // return to `.saved` restarts the clock (spec §6, row 9).
170 .task(id: Self.unsettled(status, draft: draft)) {
171 guard Self.unsettled(status, draft: draft) else { savingShown = false; return }
172 try? await Task.sleep(for: .seconds(1))
173 if !Task.isCancelled { savingShown = true }
174 }
175 .onChange(of: row, initial: true) { old, new in
176 observe?(new)
177 defer { shown = new }
178 // Nothing is announced as the window opens, only changes.
179 guard announces, let previous = shown, previous != new, DocsSubtitleRow.announces(from: old, to: new) else { return }
180 AccessibilityNotification.Announcement(DocsStrings(locale: locale).state(new) ?? "").post()
181 }
182 }
183 static func unsettled(_ status: SyncStatus, draft: Bool) -> Bool { draft || status != .saved }
184}
185
186/// The subtitle's drawing, given its row: muted, or negative (semibold for
187/// row 2), with an eye for View only. One accessibility element, present
188/// even when quiet, that updates frequently.
189struct DocsSubtitleLine: View {
190 let row: DocsSubtitleRow
191 let team: String?
192 let strings: DocsStrings
193 var body: some View {
194 let text = strings.subtitle(team: team, row: row)
195 HStack(spacing: DocsSpacing.sp1) {
196 if row == .viewOnly {
197 Image(systemName: "eye").font(DocsType.font(9, .semibold, ios: .caption2))
198 #if os(iOS)
199 .imageScale(.small)
200 #endif
201 }
202 Text(verbatim: text.isEmpty ? " " : text)
203 .lineLimit(1)
204 .truncationMode(.tail)
205 }
206 // 11pt; on iPhone and iPad it scales with Dynamic Type, to AX5 (§9).
207 .font(DocsType.font(11, row.isEmphasised ? .semibold : .regular, ios: .caption2))
208 .foregroundStyle(row.isNegative ? OficinaPalette.negative : OficinaPalette.textMuted)
209 .accessibilityElement(children: .ignore)
210 .accessibilityLabel(Text(verbatim: text))
211 .accessibilityAddTraits(.updatesFrequently)
212 .accessibilityIdentifier("docs.document.subtitle")
213 }
214}
215