oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1#if DEBUG
2import SwiftUI
3import Observation
4import EquipoShareCore
5#if os(macOS)
6import AppKit
7#endif
8
9/// Offline review fixture for the real foreground response and confirmation.
10/// It does not register with APNs or contact a live account.
11public struct NotificationFixture: View {
12 @State private var model = FixtureModel()
13 public init() {}
14 public var body: some View {
15 Group {
16 if let notice = model.dispatcher.notices.first {
17 NotificationActionView(dispatcher: model.dispatcher, notice: notice)
18 } else { ProgressView().accessibilityLabel(EquipoStrings.text("work.details.task")) }
19 }
20 .preferredColorScheme(.light)
21 .task {
22 await model.open()
23 #if os(macOS)
24 if let path = ProcessInfo.processInfo.environment["EQUIPO_FIXTURE_CAPTURE"] {
25 try? await Task.sleep(for: .seconds(2))
26 if let window = NSApp.windows.first(where: { $0.isVisible && $0.isSheet }) ?? NSApp.windows.first(where: { $0.isVisible }),
27 let view = window.contentView {
28 view.layoutSubtreeIfNeeded(); window.displayIfNeeded()
29 if let bitmap = view.bitmapImageRepForCachingDisplay(in: view.bounds) {
30 view.cacheDisplay(in: view.bounds, to: bitmap)
31 // AppKit's sheet background is composited by WindowServer
32 // and absent from an NSView snapshot. Render that backing
33 // here so the capture is opaque without screen permission.
34 let capture = NSImage(size: view.bounds.size)
35 capture.lockFocus()
36 NSColor.white.setFill(); NSBezierPath(rect: NSRect(origin: .zero, size: view.bounds.size)).fill()
37 let content = NSImage(size: view.bounds.size); content.addRepresentation(bitmap)
38 content.draw(in: NSRect(origin: .zero, size: view.bounds.size), from: .zero, operation: .sourceOver, fraction: 1)
39 capture.unlockFocus()
40 if let tiff = capture.tiffRepresentation, let opaque = NSBitmapImageRep(data: tiff),
41 let data = opaque.representation(using: .png, properties: [:]) { _ = FileManager.default.createFile(atPath: path, contents: data) }
42 }
43 }
44 }
45 #endif
46 }
47 .onDisappear { model.dispatcher.clear(); try? FileManager.default.removeItem(at: model.directory) }
48 }
49 @MainActor private final class FixtureModel {
50 let session: EquipoSession
51 let dispatcher: NotificationDispatcher
52 let selection: NotificationSelection
53 let directory = FileManager.default.temporaryDirectory.appendingPathComponent("equipo-notification-fixture-" + UUID().uuidString)
54 init() {
55 let transport = NotificationFixtureTransport()
56 let accounts = DeviceAccounts(store: MemorySecretStore())
57 var account = DeviceAccount.new(origin: transport.origin, label: "Fixture")
58 account.address = "paul@example.com"; account.teamName = "Oficina"
59 account.credential = DeviceCredential(id: "fixture", token: "fixture", audience: "equipo.person.v1", instanceId: "fixture", expiresAt: .distantFuture)
60 session = EquipoSession(account: account, accounts: accounts, transport: transport)
61 selection = accounts.notificationSelection!
62 dispatcher = NotificationDispatcher(journal: ActionJournal(directory: directory))
63 dispatcher.attach(session: session, selection: selection)
64 }
65 func open() async {
66 await session.refresh()
67 let question = ProcessInfo.processInfo.environment["EQUIPO_NOTIFICATION_FIXTURE"] == "question"
68 let data: [AnyHashable: Any] = ["equipo": ["kind": "card", "card_id": "card_merge", "task_id": "tk_fixture", "actions": ["version": 1, "binding_id": selection.id, "notification_id": "22222222-2222-4222-8222-222222222222", "card_kind": question ? "question" : "merge", "card_revision": 3]]]
69 if let response = NotificationResponse(userInfo: data, category: question ? "equipo.question.choices.v1" : "equipo.merge.v1", action: question ? "answer" : "merge", text: nil) {
70 await dispatcher.receive(response)
71 }
72 }
73 }
74}
75
76private struct NotificationFixtureTransport: EquipoTransport {
77 let underlying = TaskViewFixtureTransport()
78 var origin: URL { underlying.origin }
79 func send(_ request: APIRequest) async throws -> APIResponse {
80 if request.method == "POST" { throw EquipoAPIError.offline }
81 if request.path == "/capabilities" { return APIResponse(status: 200, data: Data(#"{"notification_actions_v1":true}"#.utf8)) }
82 if request.path.hasPrefix("/cards/") {
83 let snapshot = try await send(APIRequest("GET", "/snapshot"))
84 let data = try JSONSerialization.jsonObject(with: snapshot.data) as! [String: Any]
85 let cards = data["cards"] as! [[String: Any]]
86 return APIResponse(status: 200, data: try JSONSerialization.data(withJSONObject: cards[0]))
87 }
88 let response = try await underlying.send(request)
89 if request.path == "/snapshot", ProcessInfo.processInfo.environment["EQUIPO_NOTIFICATION_FIXTURE"] == "question",
90 var snapshot = try JSONSerialization.jsonObject(with: response.data) as? [String: Any],
91 var cards = snapshot["cards"] as? [[String: Any]], !cards.isEmpty {
92 cards[0]["kind"] = "question"; cards[0]["question"] = "Should the badge clear when you switch teams too?"
93 cards[0]["options"] = ["Clear it when switching teams", "Keep each team’s count"]
94 snapshot["cards"] = cards
95 return APIResponse(status: 200, data: try JSONSerialization.data(withJSONObject: snapshot))
96 }
97 return response
98 }
99 func lines(_ request: APIRequest) async throws -> AsyncThrowingStream<String, Error> { AsyncThrowingStream { $0.finish() } }
100}
101#endif
102