Jotley / app · jotley.ai / JotleyWidgets / JotleyWidgetsLiveActivity.swift
| 1 | import WidgetKit |
| 2 | import SwiftUI |
| 3 | import ActivityKit |
| 4 | import Foundation |
| 5 | |
| 6 | struct RecordingActivityAttributes: ActivityAttributes { |
| 7 | struct ContentState: Codable, Hashable { |
| 8 | var elapsedSeconds: Int |
| 9 | var isRecording: Bool |
| 10 | var lastUpdatedAt: Date? |
| 11 | var themeColor: ThemeColor? = nil |
| 12 | var accent: Color { (themeColor ?? .purple).color(in: .dark) } |
| 13 | var formattedDuration: String { String(format: "%d:%02d", elapsedSeconds / 60, elapsedSeconds % 60) } |
| 14 | var timerStartDate: Date { (lastUpdatedAt ?? Date()).addingTimeInterval(-Double(elapsedSeconds)) } |
| 15 | } |
| 16 | var recordingId: String |
| 17 | var startTime: Date |
| 18 | } |
| 19 | |
| 20 | |
| 21 | struct RecordingLiveActivity: Widget { |
| 22 | var body: some WidgetConfiguration { |
| 23 | ActivityConfiguration(for: RecordingActivityAttributes.self) { context in |
| 24 | HStack(spacing: 16) { |
| 25 | Image(systemName: "waveform").font(.title2).foregroundStyle(context.state.accent) |
| 26 | VStack(alignment: .leading, spacing: 4) { |
| 27 | Text(context.state.isRecording ? "Transcribing" : "Paused").font(.headline) |
| 28 | RecordingDuration(state: context.state).font(.subheadline.monospacedDigit()) |
| 29 | } |
| 30 | Spacer() |
| 31 | RecordingControls(context: context) |
| 32 | }.padding(20).foregroundStyle(.white) |
| 33 | .activityBackgroundTint(Color(white: 0.08)) |
| 34 | .widgetURL(recordingURL("transcription", id: context.attributes.recordingId)) |
| 35 | } dynamicIsland: { context in |
| 36 | DynamicIsland { |
| 37 | DynamicIslandExpandedRegion(.leading) { |
| 38 | Label { |
| 39 | Text(context.state.isRecording ? "Transcribing" : "Paused") |
| 40 | .foregroundStyle(.white) |
| 41 | } icon: { |
| 42 | Image(systemName: "mic.fill").foregroundStyle(context.state.accent) |
| 43 | } |
| 44 | .font(.caption) |
| 45 | } |
| 46 | DynamicIslandExpandedRegion(.trailing) { |
| 47 | RecordingDuration(state: context.state).font(.headline.monospacedDigit()) |
| 48 | } |
| 49 | DynamicIslandExpandedRegion(.bottom) { |
| 50 | RecordingControls(context: context).padding(.top, 8) |
| 51 | } |
| 52 | } compactLeading: { |
| 53 | Image(systemName: "mic.fill").foregroundStyle(context.state.accent) |
| 54 | } compactTrailing: { |
| 55 | Text(context.state.isRecording ? "LIVE" : "Paused").font(.caption.bold()).foregroundStyle(.white) |
| 56 | } minimal: { |
| 57 | Image(systemName: context.state.isRecording ? "mic.fill" : "pause.fill").foregroundStyle(context.state.accent) |
| 58 | } |
| 59 | .widgetURL(recordingURL("transcription", id: context.attributes.recordingId)) |
| 60 | .keylineTint(context.state.accent) |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | private struct RecordingDuration: View { |
| 66 | let state: RecordingActivityAttributes.ContentState |
| 67 | var body: some View { |
| 68 | if state.isRecording { Text(state.timerStartDate, style: .timer) } |
| 69 | else { Text(state.formattedDuration) } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | private struct RecordingControls: View { |
| 74 | let context: ActivityViewContext<RecordingActivityAttributes> |
| 75 | var body: some View { |
| 76 | HStack(spacing: 12) { |
| 77 | Link(destination: recordingURL("pausetranscription", id: context.attributes.recordingId)) { |
| 78 | Image(systemName: context.state.isRecording ? "pause.fill" : "play.fill") |
| 79 | .frame(width: 44, height: 44).background(context.state.accent, in: Circle()) |
| 80 | }.accessibilityLabel(context.state.isRecording ? "Pause Transcription" : "Resume Transcription") |
| 81 | Link(destination: recordingURL("stoptranscription", id: context.attributes.recordingId)) { |
| 82 | Image(systemName: "stop.fill").frame(width: 44, height: 44).background(context.state.accent, in: Circle()) |
| 83 | }.accessibilityLabel("Stop Transcription") |
| 84 | }.foregroundStyle(.black) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | private func recordingURL(_ action: String, id: String) -> URL { |
| 89 | var components = URLComponents() |
| 90 | components.scheme = "jotley"; components.host = action |
| 91 | components.queryItems = [URLQueryItem(name: "id", value: id)] |
| 92 | return components.url! |
| 93 | } |
| 94 | |
| 95 | #Preview("Transcribing", as: .content, using: RecordingActivityAttributes(recordingId: UUID().uuidString, startTime: Date())) { |
| 96 | RecordingLiveActivity() |
| 97 | } contentStates: { |
| 98 | RecordingActivityAttributes.ContentState(elapsedSeconds: 125, isRecording: true, lastUpdatedAt: Date()) |
| 99 | RecordingActivityAttributes.ContentState(elapsedSeconds: 300, isRecording: false, lastUpdatedAt: Date()) |
| 100 | } |