Sources/OrgApp/ClockActivity.swift
48 lines · 1561 bytes
5 symbols in this file
1#if os(iOS)
2import ActivityKit
3import AppIntents
4import Foundation
5
6/// The running clock's Live Activity, which the app starts and the widget extension draws.
7public struct ClockActivityAttributes: ActivityAttributes {
8 public struct ContentState: Codable, Hashable, Sendable {
9 public var heading: String
10 public var startedAt: Date
11
12 public init(heading: String, startedAt: Date) {
13 self.heading = heading
14 self.startedAt = startedAt
15 }
16 }
17
18 /// The clock's file and the stamp in its `CLOCK: [start]` line, which name the clock.
19 public var path: String
20 public var start: String
21
22 public init(path: String, start: String) {
23 self.path = path
24 self.start = start
25 }
26}
27
28/// The Live Activity's Clock Out button. It runs in the app's process, which sets `handler`.
29public struct ClockOutIntent: LiveActivityIntent {
30 public static let title: LocalizedStringResource = "Clock Out"
31 public static let description = IntentDescription("Stops the running clock.")
32 public static let isDiscoverable = false
33
34 /// Clocks out; set by the app at launch.
35 @MainActor public static var handler: (@MainActor @Sendable () async -> Void)?
36
37 public init() {}
38
39 public func perform() async throws -> some IntentResult {
40 let handler = await MainActor.run { Self.handler }
41 await handler?()
42 return .result()
43 }
44}
45
46/// OrgApp's App Intents, for the app and the widget extension to include.
47public struct OrgAppIntents: AppIntentsPackage {}
48#endif