Open History to see the last commit
1import Foundation
2
3/// An operation on a tool surface that can fail recoverably and be retried unchanged.
4public protocol FeatureRecoverableOperation: Equatable, Sendable {
5 /// Headline shown on the retained failure banner, e.g. "Push failed".
6 var failureTitle: String { get }
7 /// Stable accessibility label for the Retry control, e.g. "Retry push".
8 var retryAccessibilityLabel: String { get }
9 /// Spoken confirmation once the same operation succeeds.
10 var recoveryAnnouncement: String { get }
11}
12
13/// Failure content retained for a tool surface. It survives the retry it triggers so the
14/// useful output is never blanked while recovery is in flight.
15public struct FeatureToolFailure: Identifiable, Sendable, Equatable, Hashable {
16 /// Distinct per presented failure so a repeat failure can move accessibility focus again.
17 public let id: Int
18 public var title: String
19 public var message: String
20 public var retryAccessibilityLabel: String
21 public var isRetrying: Bool
22
23 public init(
24 id: Int,
25 title: String,
26 message: String,
27 retryAccessibilityLabel: String,
28 isRetrying: Bool = false
29 ) {
30 self.id = id
31 self.title = title
32 self.message = message
33 self.retryAccessibilityLabel = retryAccessibilityLabel
34 self.isRetrying = isRetrying
35 }
36
37 /// Single spoken string so VoiceOver reads the retained content when focus lands on it.
38 public var accessibilityLabel: String {
39 isRetrying ? "\(title). \(message). Retrying." : "\(title). \(message)"
40 }
41}
42
43/// Where accessibility focus belongs after a tool surface changes recovery state.
44public enum FeatureToolRecoveryFocus: Hashable, Sendable {
45 /// The retained failure summary, read together with its content.
46 case failure
47 /// The first element of the recovered content.
48 case recoveredContent
49}
50
51/// Recovery state for one tool surface: keeps failure content visible across retries, refuses
52/// to report cancellation as a failure, and names a predictable accessibility focus target.
53public struct FeatureToolFailureState<Operation: FeatureRecoverableOperation>: Sendable, Equatable {
54 public private(set) var failure: FeatureToolFailure?
55 /// The exact operation to run again, including any input the failed attempt carried.
56 public private(set) var retryOperation: Operation?
57 /// Set once when an operation recovers, so the surface can announce it exactly once.
58 public private(set) var recoveryAnnouncement: String?
59 private var presentedFailureCount = 0
60
61 public init() {}
62
63 /// Marks an attempt as started. Retrying the failed operation keeps its content on screen
64 /// instead of blanking it; unrelated work leaves the retained failure untouched.
65 public mutating func begin(_ operation: Operation) {
66 recoveryAnnouncement = nil
67 guard failure != nil else { return }
68 failure?.isRetrying = retryOperation == operation
69 }
70
71 /// Records the outcome of a failed attempt. Cancellation is not a failure: it never creates
72 /// one and never overwrites content already on screen.
73 public mutating func recordFailure(_ operation: Operation, error: Error) {
74 guard !Self.isCancellation(error) else {
75 failure?.isRetrying = false
76 return
77 }
78 presentedFailureCount += 1
79 failure = FeatureToolFailure(
80 id: presentedFailureCount,
81 title: operation.failureTitle,
82 message: Self.message(for: error),
83 retryAccessibilityLabel: operation.retryAccessibilityLabel
84 )
85 retryOperation = operation
86 }
87
88 /// Records successful work. One completion can satisfy related retained operations, such as
89 /// an action whose explicit follow-up refresh also recovers an earlier load failure.
90 public mutating func recordSuccess(_ operations: Operation...) {
91 guard
92 failure != nil,
93 let recoveredOperation = retryOperation,
94 operations.contains(recoveredOperation)
95 else {
96 recoveryAnnouncement = nil
97 return
98 }
99 failure = nil
100 retryOperation = nil
101 recoveryAnnouncement = recoveredOperation.recoveryAnnouncement
102 }
103
104 /// Records a follow-up failure after an operation already completed. A real failure becomes
105 /// the only retryable operation; cancellation stays silent and removes the completed work.
106 public mutating func recordFollowUpFailure(
107 _ followUpOperation: Operation,
108 afterCompletionOf completedOperation: Operation,
109 error: Error
110 ) {
111 guard Self.isCancellation(error) else {
112 recordFailure(followUpOperation, error: error)
113 return
114 }
115 guard retryOperation == completedOperation else { return }
116 failure = nil
117 retryOperation = nil
118 recoveryAnnouncement = nil
119 }
120
121 /// Consumes the pending announcement so recovery is never spoken twice.
122 public mutating func takeRecoveryAnnouncement() -> String? {
123 defer { recoveryAnnouncement = nil }
124 return recoveryAnnouncement
125 }
126
127 public var focusTarget: FeatureToolRecoveryFocus {
128 failure == nil ? .recoveredContent : .failure
129 }
130
131 /// A dismissed sheet, a cancelled refresh, or a superseded request must not read as a failure.
132 public static func isCancellation(_ error: Error) -> Bool {
133 if error is CancellationError { return true }
134 let nsError = error as NSError
135 if nsError.domain == NSURLErrorDomain, nsError.code == NSURLErrorCancelled { return true }
136 if nsError.domain == NSCocoaErrorDomain, nsError.code == NSUserCancelledError { return true }
137 return false
138 }
139
140 private static func message(for error: Error) -> String {
141 let described = error.localizedDescription
142 .trimmingCharacters(in: .whitespacesAndNewlines)
143 return described.isEmpty ? "The operation could not be completed." : described
144 }
145}
146
147/// Single-flight ownership for a tool surface. A second request cannot mutate recovery state
148/// while the operation that acquired the surface is still running.
149public struct FeatureToolRunState<Operation: Equatable & Sendable>: Sendable, Equatable {
150 public private(set) var operation: Operation?
151
152 public init() {}
153
154 public var isBusy: Bool {
155 operation != nil
156 }
157
158 public mutating func begin(_ operation: Operation) -> Bool {
159 guard self.operation == nil else { return false }
160 self.operation = operation
161 return true
162 }
163
164 public mutating func finish(_ operation: Operation) {
165 guard self.operation == operation else { return }
166 self.operation = nil
167 }
168}
169
170/// The retryable work of the source control surface. `action` carries the commit message so a
171/// retry never asks for it again.
172public enum FeatureSourceControlOperation: FeatureRecoverableOperation {
173 case load
174 case action(FeatureSourceControlAction, message: String?)
175
176 public var isLoad: Bool {
177 if case .load = self { return true }
178 return false
179 }
180
181 public var failureTitle: String {
182 switch self {
183 case .load: "Repository status failed to load"
184 case .action(let action, _): "\(action.title) failed"
185 }
186 }
187
188 public var retryAccessibilityLabel: String {
189 switch self {
190 case .load: "Retry loading repository status"
191 case .action(let action, _): "Retry \(action.title.lowercased())"
192 }
193 }
194
195 public var recoveryAnnouncement: String {
196 switch self {
197 case .load: "Repository status loaded."
198 case .action(let action, _): "\(action.title) succeeded. Repository status updated."
199 }
200 }
201}
202
203public extension FeatureSourceControlAction {
204 var title: String {
205 switch self {
206 case .commit: "Commit changes"
207 case .push: "Push"
208 case .pull: "Pull latest"
209 case .createPullRequest: "Create pull request"
210 case .commitAndPush: "Commit and push"
211 case .commitPushAndCreatePullRequest: "Commit, push, and create PR"
212 }
213 }
214}