| 1 | import Foundation |
| 2 | import Observation |
| 3 | |
| 4 | @MainActor @Observable final class TaskDetailModel { |
| 5 | let session: EquipoSession |
| 6 | let taskID: String |
| 7 | @ObservationIgnored private let openMovePage: (URL, String) -> Void |
| 8 | var confirmation: TaskConfirmation? |
| 9 | var directionNote = "" |
| 10 | var menuActions: [TaskAction] { |
| 11 | TaskAction.allCases.filter { $0 != .cancel && rules.available.contains($0) } |
| 12 | + (rules.available.contains(.cancel) ? [.cancel] : []) |
| 13 | } |
| 14 | func confirm() async { |
| 15 | guard !busy, let intent = confirmation else { return } |
| 16 | confirmation = nil |
| 17 | if let card = intent.card { |
| 18 | guard task?.revision == intent.revision else { |
| 19 | failure = EquipoStrings.text("card.changed"); await refreshAfterAction(); return |
| 20 | } |
| 21 | await sendAnswer(card, choice: "merge", note: "") |
| 22 | } |
| 23 | else { await execute(intent.action, seenRevision: intent.revision, receiptRevision: intent.receiptRevision) } |
| 24 | } |
| 25 | var loaded: ThinTask? |
| 26 | var needsResume = ResumeAction() |
| 27 | var busy = false |
| 28 | var failure: String? |
| 29 | var readFailure: String? |
| 30 | var revertedTask: ThinTask? |
| 31 | var destinations: [String] = [] |
| 32 | var moving = false |
| 33 | |
| 34 | init(session: EquipoSession, taskID: String, openMovePage: ((URL, String) -> Void)? = nil) { self.session = session; self.taskID = taskID |
| 35 | self.openMovePage = openMovePage ?? { session.settings.open(origin: $0, path: $1) } |
| 36 | } |
| 37 | var task: ThinTask? { |
| 38 | guard let listed = session.task(taskID) else { return loaded } |
| 39 | guard let loaded, loaded.revision >= listed.revision else { return listed } |
| 40 | return loaded |
| 41 | } |
| 42 | var changeFiles: ThinChangeFiles? { |
| 43 | guard let task else { return nil } |
| 44 | if let facts = ChangedFilesPresentation.facts(task) { return facts } |
| 45 | // List revisions omit detail facts. Retain only this candidate's facts |
| 46 | // while the next detail read replaces loaded; never retain task actions. |
| 47 | guard let loaded, loaded.candidateSha == task.candidateSha else { return nil } |
| 48 | return ChangedFilesPresentation.facts(loaded) |
| 49 | } |
| 50 | var rules: TaskActionRules { |
| 51 | guard let task, loaded?.revision == task.revision, readFailure == nil else { return .init() } |
| 52 | return TaskActionRules(task: task, viewer: session.me?.address ?? "", owner: session.me?.role == "owner", cards: session.cards) |
| 53 | } |
| 54 | @discardableResult func load() async -> (any Error)? { |
| 55 | do { |
| 56 | loaded = try await session.client.task(taskID); readFailure = nil |
| 57 | if let loaded { session.previews.observe([loaded]) } |
| 58 | return nil |
| 59 | } |
| 60 | catch { readFailure = StoppedWorkActions.logFailure(error); return error } |
| 61 | } |
| 62 | func beginMove() async { |
| 63 | guard !busy, rules.available.contains(.move), let task else { return } |
| 64 | if rules.moveOnWeb { |
| 65 | openMovePage(session.origin, "/tasks/\(task.id)/move") |
| 66 | return |
| 67 | } |
| 68 | busy = true; failure = nil; moving = true |
| 69 | defer { busy = false } |
| 70 | do { destinations = (try await session.client.projects().allowed ?? []).filter { $0 != task.repo }.sorted() } |
| 71 | catch { failure = TaskActionFailure.words(error) } |
| 72 | } |
| 73 | func perform(_ action: TaskAction, seenRevision: Int64? = nil, receiptRevision: Int64? = nil, repo: String? = nil) async { |
| 74 | guard !busy else { return } |
| 75 | guard let task, rules.available.contains(action) else { return } |
| 76 | if [.merge, .undoMerge, .cancel].contains(action) { |
| 77 | confirmation = TaskConfirmation(action: action, revision: seenRevision ?? task.revision, |
| 78 | receiptRevision: receiptRevision ?? rules.undoReceipt?.revision) |
| 79 | return |
| 80 | } |
| 81 | await execute(action, seenRevision: seenRevision, receiptRevision: receiptRevision, repo: repo) |
| 82 | } |
| 83 | private func execute(_ action: TaskAction, seenRevision: Int64? = nil, receiptRevision: Int64? = nil, repo: String? = nil) async { |
| 84 | guard let task, rules.available.contains(action) else { return } |
| 85 | if let seenRevision, seenRevision != task.revision { |
| 86 | failure = EquipoStrings.text("card.changed"); await load(); return |
| 87 | } |
| 88 | busy = true; failure = nil |
| 89 | defer { busy = false } |
| 90 | do { |
| 91 | let command: TaskCommand |
| 92 | switch action { |
| 93 | case .retry: |
| 94 | failure = await StoppedWorkActions.retry(client: session.client, task: taskID) |
| 95 | await refreshAfterAction(); return |
| 96 | case .recover: |
| 97 | failure = await StoppedWorkActions.recover(client: session.client, task: taskID) |
| 98 | await refreshAfterAction(); return |
| 99 | case .merge: command = .merge |
| 100 | case .cancel: command = .cancel |
| 101 | case .round: command = .round |
| 102 | case .undoMerge: |
| 103 | guard let receipt = rules.undoReceipt else { return } |
| 104 | if let receiptRevision, receiptRevision != receipt.revision { |
| 105 | failure = EquipoStrings.text("card.changed"); await refreshAfterAction(); return |
| 106 | } |
| 107 | command = .undo(receiptRevision: receipt.revision) |
| 108 | case .explore: command = .kind("explore", revision: task.revision) |
| 109 | case .change: command = .kind("change", revision: task.revision) |
| 110 | case .urgent: command = .priority("urgent", by: task.priority?.by, revision: task.revision) |
| 111 | case .normal: command = .priority("normal", by: task.priority?.by, revision: task.revision) |
| 112 | case .move: |
| 113 | guard !rules.moveOnWeb, let repo, destinations.contains(repo), repo != task.repo else { return } |
| 114 | command = .move(repo: repo, revision: task.revision) |
| 115 | } |
| 116 | let result = try await session.client.execute(task: taskID, command) |
| 117 | if action == .undoMerge { revertedTask = result } |
| 118 | if action == .move { moving = false } |
| 119 | } catch { failure = TaskActionFailure.words(error) } |
| 120 | await refreshAfterAction() |
| 121 | } |
| 122 | var decision: TaskDecision { |
| 123 | guard let task, loaded?.revision == task.revision, readFailure == nil else { return .init() } |
| 124 | return TaskDecision(task: task, cards: session.cards, mine: session.isMine(task), owner: session.me?.role == "owner") |
| 125 | } |
| 126 | func answer(_ card: ThinCard, choice: String, note: String) async { |
| 127 | if choice == "merge" { |
| 128 | guard !busy else { return } |
| 129 | if decision.card?.id == card.id, decision.card?.revision == card.revision, |
| 130 | decision.canAnswer(choice, note: "") { |
| 131 | confirmation = TaskConfirmation(action: .merge, revision: task?.revision ?? 0, card: card) |
| 132 | return |
| 133 | } |
| 134 | } |
| 135 | await sendAnswer(card, choice: choice, note: note) |
| 136 | } |
| 137 | private func sendAnswer(_ card: ThinCard, choice: String, note: String) async { |
| 138 | guard !busy else { return } |
| 139 | let note = card.kind == "direction" ? note : "" |
| 140 | guard let current = decision.card, current.id == card.id, current.revision == card.revision else { |
| 141 | failure = EquipoStrings.text("card.changed"); await refreshAfterAction(); return |
| 142 | } |
| 143 | guard decision.canAnswer(choice, note: note) else { return } |
| 144 | busy = true; failure = nil |
| 145 | defer { busy = false } |
| 146 | switch await session.answer(card, choice: choice, note: note.isEmpty ? nil : note) { |
| 147 | case .answered: |
| 148 | if card.kind == "direction" { directionNote = "" } |
| 149 | case .changed: failure = EquipoStrings.text("card.changed") |
| 150 | case .offline: failure = EquipoStrings.text("card.offline") |
| 151 | case .failed: failure = session.answerNotices[card.id] ?? EquipoStrings.text("card.failed") |
| 152 | } |
| 153 | await load() |
| 154 | } |
| 155 | private func refreshAfterAction() async { |
| 156 | await session.refresh() |
| 157 | await load() |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | struct TaskConfirmation: Identifiable { |
| 162 | let id = UUID() |
| 163 | var action: TaskAction |
| 164 | var revision: Int64 |
| 165 | var receiptRevision: Int64? = nil |
| 166 | var card: ThinCard? = nil |
| 167 | } |
| 168 | |