| 1 | import SwiftUI |
| 2 | |
| 3 | // "Report a problem" (spec §15.26): the person's note and this call's |
| 4 | // diagnostics, with the people in it named by short peer ids, sent to the |
| 5 | // server's log under a reference the person can quote. From the call's |
| 6 | // controls, from the ⓘ beside "Copy call diagnostics", and for ten minutes |
| 7 | // after the call from the notice that it ended and from the home screen. |
| 8 | |
| 9 | /// What became of a report, from the bridge's answer (bridge.SendProblem). |
| 10 | public enum ProblemOutcome: Equatable, Sendable { |
| 11 | case sent(ref: String) |
| 12 | case tooMany |
| 13 | case gone |
| 14 | case failed |
| 15 | |
| 16 | /// `{"ref":"K7Q2M9PD"}` or `{"outcome":"too-many"|"gone"|"failed"}`. |
| 17 | public init(bridgeAnswer: String) { |
| 18 | struct Answer: Decodable { |
| 19 | let ref: String? |
| 20 | let outcome: String? |
| 21 | } |
| 22 | guard let answer = try? JSONDecoder().decode(Answer.self, from: Data(bridgeAnswer.utf8)) else { |
| 23 | self = .failed |
| 24 | return |
| 25 | } |
| 26 | if let ref = answer.ref, !ref.isEmpty { |
| 27 | self = .sent(ref: ref) |
| 28 | return |
| 29 | } |
| 30 | switch answer.outcome { |
| 31 | case "too-many": self = .tooMany |
| 32 | case "gone": self = .gone |
| 33 | default: self = .failed |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /// The words, exactly as the ruling has them. |
| 39 | public enum ProblemWords { |
| 40 | public static let title = "Report a problem" |
| 41 | public static let noteLabel = "What went wrong?" |
| 42 | public static let disclosure = "Sends this call's connection diagnostics to the Calls server log. No audio or video." |
| 43 | public static let sent = "Sent — thank you" |
| 44 | public static func reference(_ ref: String) -> String { "Reference \(ref)" } |
| 45 | public static let tooMany = "Too many reports from this call — try again in a minute." |
| 46 | public static let gone = "This call can no longer be reported." |
| 47 | public static let failed = "The report could not be sent." |
| 48 | |
| 49 | static func problem(_ outcome: ProblemOutcome) -> String? { |
| 50 | switch outcome { |
| 51 | case .sent: nil |
| 52 | case .tooMany: tooMany |
| 53 | case .gone: gone |
| 54 | case .failed: failed |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /// The sheet's state: editing, sending, sent with a reference, or a |
| 60 | /// sentence about why not (and the note kept, to send again). |
| 61 | @MainActor public final class ProblemReportModel: ObservableObject, Identifiable { |
| 62 | public enum Phase: Equatable, Sendable { |
| 63 | case editing |
| 64 | case sending |
| 65 | case sent(ref: String) |
| 66 | case refused(String) |
| 67 | } |
| 68 | |
| 69 | @Published public var note = "" { |
| 70 | didSet { |
| 71 | // The server counts characters as code points (utf8.RuneCount). |
| 72 | if note.unicodeScalars.count > QualityBounds.note { |
| 73 | note = String(String.UnicodeScalarView(note.unicodeScalars.prefix(QualityBounds.note))) |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | @Published public private(set) var phase = Phase.editing |
| 78 | private let deliver: @Sendable (String) async -> ProblemOutcome |
| 79 | |
| 80 | /// `deliver` sends the note (with whatever else the report carries) and |
| 81 | /// answers what became of it. Call supplies one; the tests supply theirs. |
| 82 | public init(deliver: @escaping @Sendable (String) async -> ProblemOutcome) { |
| 83 | self.deliver = deliver |
| 84 | } |
| 85 | |
| 86 | /// "312/1000". |
| 87 | public var counter: String { "\(note.unicodeScalars.count)/\(QualityBounds.note)" } |
| 88 | /// The counter, for VoiceOver: "312 of 1000 characters". |
| 89 | public var counterWords: String { "\(note.unicodeScalars.count) of \(QualityBounds.note) characters" } |
| 90 | public var canSend: Bool { |
| 91 | switch phase { |
| 92 | case .editing, .refused: true |
| 93 | case .sending, .sent: false |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | public func send() async { |
| 98 | guard canSend else { return } |
| 99 | phase = .sending |
| 100 | let outcome = await deliver(note) |
| 101 | switch outcome { |
| 102 | case .sent(let ref): phase = .sent(ref: ref) |
| 103 | default: phase = .refused(ProblemWords.problem(outcome) ?? ProblemWords.failed) |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /// The body a report is sent as: protocol.ProblemReport. |
| 109 | struct ProblemReportBody: Encodable { |
| 110 | let note: String |
| 111 | let diagnostics: String |
| 112 | let client: QualityClient |
| 113 | |
| 114 | /// JSON within the server's 64 KB: diagnostics are cut from the front, |
| 115 | /// keeping the newest lines, which are the ones about the problem. |
| 116 | func encoded() -> String { |
| 117 | withinProblemBody(diagnostics) { ProblemReportBody(note: note, diagnostics: $0, client: client) } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /// The automatic report's body (§15.29): protocol.AutoReport. What Report |
| 122 | /// a problem sends, scrubbed the same way, and no note, because nobody |
| 123 | /// typed one; `trigger` is the rule that fired. |
| 124 | struct AutoReportBody: Encodable { |
| 125 | let trigger: String |
| 126 | let diagnostics: String |
| 127 | let client: QualityClient |
| 128 | |
| 129 | /// Within the server's 64 KB, as a problem report is. |
| 130 | func encoded() -> String { |
| 131 | withinProblemBody(diagnostics) { AutoReportBody(trigger: trigger, diagnostics: $0, client: client) } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /// A report's JSON with its diagnostics cut from the front until the whole |
| 136 | /// is under the server's 64 KB, with a margin. |
| 137 | private func withinProblemBody<Body: Encodable>(_ diagnostics: String, _ body: (String) -> Body) -> String { |
| 138 | var diagnostics = diagnostics |
| 139 | while true { |
| 140 | let data = (try? JSONEncoder().encode(body(diagnostics))) ?? Data() |
| 141 | if data.count <= QualityBounds.problemBody - 256 || diagnostics.isEmpty { |
| 142 | return String(data: data, encoding: .utf8) ?? "{}" |
| 143 | } |
| 144 | diagnostics = "(earlier lines cut)\n" + String(diagnostics.suffix(diagnostics.count * 3 / 4)) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /// What became of an automatic report, from the bridge's answer |
| 149 | /// (bridge.SendAutoReport). |
| 150 | public enum AutoReportOutcome: Equatable, Sendable { |
| 151 | case sent(ref: String) |
| 152 | /// The server's kill switch: no more quality or automatic reports this |
| 153 | /// call. |
| 154 | case stop |
| 155 | /// Not sent ("too-many", "gone", "failed"): the call goes on as before. |
| 156 | case notSent(String) |
| 157 | |
| 158 | /// `{"ref":"K7Q2M9PD"}` or `{"outcome":"stop"|"too-many"|"gone"|"failed"}`. |
| 159 | public init(bridgeAnswer: String) { |
| 160 | struct Answer: Decodable { |
| 161 | let ref: String? |
| 162 | let outcome: String? |
| 163 | } |
| 164 | let answer = try? JSONDecoder().decode(Answer.self, from: Data(bridgeAnswer.utf8)) |
| 165 | if let ref = answer?.ref, !ref.isEmpty { |
| 166 | self = .sent(ref: ref) |
| 167 | } else if answer?.outcome == "stop" { |
| 168 | self = .stop |
| 169 | } else { |
| 170 | self = .notSent(answer?.outcome ?? "failed") |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /// The sheet. |
| 176 | public struct ReportProblemSheet: View { |
| 177 | @ObservedObject var model: ProblemReportModel |
| 178 | let done: () -> Void |
| 179 | @State private var copied = false |
| 180 | @FocusState private var noteFocused: Bool |
| 181 | |
| 182 | public init(model: ProblemReportModel, done: @escaping () -> Void) { |
| 183 | self.model = model |
| 184 | self.done = done |
| 185 | } |
| 186 | |
| 187 | public var body: some View { |
| 188 | NavigationStack { |
| 189 | Form { |
| 190 | if case .sent(let ref) = model.phase { |
| 191 | sent(ref) |
| 192 | } else { |
| 193 | compose |
| 194 | } |
| 195 | } |
| 196 | .formStyle(.grouped) |
| 197 | .navigationTitle(ProblemWords.title) |
| 198 | #if os(iOS) |
| 199 | .navigationBarTitleDisplayMode(.inline) |
| 200 | #endif |
| 201 | .toolbar { |
| 202 | if case .sent = model.phase { |
| 203 | ToolbarItem(placement: .confirmationAction) { |
| 204 | Button("Done", action: done).accessibilityIdentifier("reportProblemDone") |
| 205 | } |
| 206 | } else { |
| 207 | ToolbarItem(placement: .cancellationAction) { |
| 208 | Button("Cancel", action: done).accessibilityIdentifier("reportProblemCancel") |
| 209 | } |
| 210 | ToolbarItem(placement: .confirmationAction) { |
| 211 | Button("Send") { Task { await model.send() } } |
| 212 | .disabled(!model.canSend) |
| 213 | .accessibilityIdentifier("reportProblemSend") |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | #if os(macOS) |
| 219 | .frame(width: 440) |
| 220 | .frame(minHeight: 340) |
| 221 | #endif |
| 222 | .tint(CallsAppearance.accent) |
| 223 | } |
| 224 | |
| 225 | @ViewBuilder private var compose: some View { |
| 226 | Section { |
| 227 | TextEditor(text: $model.note) |
| 228 | .frame(minHeight: 110) |
| 229 | .focused($noteFocused) |
| 230 | .accessibilityLabel(ProblemWords.noteLabel) |
| 231 | .accessibilityHint("Optional") |
| 232 | .accessibilityIdentifier("reportProblemNote") |
| 233 | .disabled(model.phase == .sending) |
| 234 | HStack { |
| 235 | Text("Optional").foregroundStyle(.secondary) |
| 236 | Spacer() |
| 237 | Text(model.counter).monospacedDigit().foregroundStyle(.secondary) |
| 238 | .accessibilityLabel(model.counterWords) |
| 239 | .accessibilityIdentifier("reportProblemCounter") |
| 240 | } |
| 241 | .font(.footnote) |
| 242 | } header: { |
| 243 | Text(ProblemWords.noteLabel) |
| 244 | } footer: { |
| 245 | Text(ProblemWords.disclosure) |
| 246 | .fixedSize(horizontal: false, vertical: true) |
| 247 | .accessibilityIdentifier("reportProblemDisclosure") |
| 248 | } |
| 249 | if model.phase == .sending { |
| 250 | Section { ProgressView("Sending…") } |
| 251 | } |
| 252 | if case .refused(let words) = model.phase { |
| 253 | Section { |
| 254 | Label(words, systemImage: "exclamationmark.circle.fill") |
| 255 | .foregroundStyle(.red).fixedSize(horizontal: false, vertical: true) |
| 256 | .accessibilityLabel("Error: \(words)") |
| 257 | .accessibilityIdentifier("reportProblemError") |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | @ViewBuilder private func sent(_ ref: String) -> some View { |
| 263 | Section { |
| 264 | Label(ProblemWords.sent, systemImage: "checkmark.circle.fill") |
| 265 | .font(.headline) |
| 266 | .accessibilityIdentifier("reportProblemSent") |
| 267 | HStack { |
| 268 | Text(ProblemWords.reference(ref)) |
| 269 | .font(.body.monospaced()) |
| 270 | .textSelection(.enabled) |
| 271 | .accessibilityIdentifier("reportProblemReference") |
| 272 | Spacer() |
| 273 | Button(copied ? "Copied" : "Copy", systemImage: "doc.on.doc") { |
| 274 | AudioDiagnosticsReport.copy(ref) |
| 275 | copied = true |
| 276 | } |
| 277 | #if os(iOS) |
| 278 | .buttonStyle(.borderless) |
| 279 | #endif |
| 280 | .accessibilityLabel(copied ? "Reference copied" : "Copy reference") |
| 281 | .accessibilityIdentifier("reportProblemCopy") |
| 282 | } |
| 283 | } footer: { |
| 284 | Text("Quote the reference if you write to us about this call.") |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | /// "Report a problem" as a plain button, wherever it appears. |
| 290 | public struct ReportProblemButton: View { |
| 291 | let action: () -> Void |
| 292 | public init(action: @escaping () -> Void) { self.action = action } |
| 293 | public var body: some View { |
| 294 | Button(ProblemWords.title, systemImage: "exclamationmark.bubble", action: action) |
| 295 | .accessibilityIdentifier("reportProblem") |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | extension View { |
| 300 | /// The sheet, shown while `call.problemReport` is set. |
| 301 | public func reportProblemSheet(_ call: Call) -> some View { |
| 302 | modifier(ReportProblemPresenter(call: call)) |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | private struct ReportProblemPresenter: ViewModifier { |
| 307 | @ObservedObject var call: Call |
| 308 | func body(content: Content) -> some View { |
| 309 | content.sheet(item: $call.problemReport) { model in |
| 310 | ReportProblemSheet(model: model) { call.problemReport = nil } |
| 311 | #if os(iOS) |
| 312 | .presentationDetents([.medium, .large]) |
| 313 | #endif |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |