Jotley / app · jotley.ai / JotleyWidgets / JotleyWidgets.swift
| 1 | import WidgetKit |
| 2 | import SwiftUI |
| 3 | |
| 4 | // MARK: - Brand Colors |
| 5 | private extension Color { |
| 6 | static let jotleyWidgetAccent = ThemeColor.purple.color(in: .dark) |
| 7 | static let jotleyDark = Color(white: 0.08) |
| 8 | } |
| 9 | |
| 10 | // MARK: - Widget Timeline Provider |
| 11 | struct QuickRecordProvider: TimelineProvider { |
| 12 | func placeholder(in context: Context) -> QuickRecordEntry { |
| 13 | QuickRecordEntry(date: Date()) |
| 14 | } |
| 15 | |
| 16 | func getSnapshot(in context: Context, completion: @escaping (QuickRecordEntry) -> Void) { |
| 17 | completion(QuickRecordEntry(date: Date())) |
| 18 | } |
| 19 | |
| 20 | func getTimeline(in context: Context, completion: @escaping (Timeline<QuickRecordEntry>) -> Void) { |
| 21 | let entry = QuickRecordEntry(date: Date()) |
| 22 | let timeline = Timeline(entries: [entry], policy: .never) |
| 23 | completion(timeline) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | // MARK: - Widget Entry |
| 28 | struct QuickRecordEntry: TimelineEntry { |
| 29 | let date: Date |
| 30 | } |
| 31 | |
| 32 | // MARK: - Widget Views |
| 33 | private struct QuickRecordGlyph: View { |
| 34 | @Environment(\.widgetRenderingMode) private var renderingMode |
| 35 | let diameter: CGFloat |
| 36 | let fontSize: CGFloat |
| 37 | |
| 38 | var body: some View { |
| 39 | Image(systemName: "mic.fill") |
| 40 | .font(.system(size: fontSize, weight: .semibold)) |
| 41 | .foregroundStyle(renderingMode == .fullColor ? Color.black : Color.primary) |
| 42 | .frame(width: diameter, height: diameter) |
| 43 | .background { |
| 44 | if renderingMode == .fullColor { |
| 45 | Circle() |
| 46 | .fill(Color.jotleyWidgetAccent) |
| 47 | .overlay(Circle().strokeBorder(.white.opacity(0.18))) |
| 48 | .shadow(color: .black.opacity(0.3), radius: 3, y: 2) |
| 49 | } else { |
| 50 | Circle().fill(.primary.opacity(0.15)) |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | struct QuickRecordSmallView: View { |
| 57 | var body: some View { |
| 58 | VStack(spacing: 12) { |
| 59 | QuickRecordGlyph(diameter: 56, fontSize: 24) |
| 60 | .frame(width: 72, height: 72) |
| 61 | Text("Transcribe") |
| 62 | .font(.system(size: 13, weight: .semibold, design: .rounded)) |
| 63 | } |
| 64 | .frame(maxWidth: .infinity, maxHeight: .infinity) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | struct QuickRecordMediumView: View { |
| 69 | var body: some View { |
| 70 | HStack(spacing: 16) { |
| 71 | VStack(spacing: 10) { |
| 72 | QuickRecordGlyph(diameter: 52, fontSize: 22) |
| 73 | .frame(width: 64, height: 64) |
| 74 | Text("Transcribe live") |
| 75 | .font(.system(size: 11, weight: .medium, design: .rounded)) |
| 76 | .foregroundStyle(.secondary) |
| 77 | } |
| 78 | .frame(width: 100) |
| 79 | |
| 80 | VStack(alignment: .leading, spacing: 8) { |
| 81 | HStack(spacing: 6) { |
| 82 | Image(systemName: "waveform.circle.fill") |
| 83 | .font(.system(size: 18)) |
| 84 | .foregroundStyle(Color.jotleyWidgetAccent) |
| 85 | .widgetAccentable() |
| 86 | Text("Jotley") |
| 87 | .font(.system(size: 17, weight: .bold, design: .rounded)) |
| 88 | } |
| 89 | |
| 90 | Text("Show up. Leave the notes to us.") |
| 91 | .font(.system(size: 12, weight: .regular)) |
| 92 | .foregroundStyle(.secondary) |
| 93 | .lineLimit(2) |
| 94 | |
| 95 | Spacer() |
| 96 | |
| 97 | HStack(spacing: 3) { |
| 98 | ForEach(0..<12, id: \.self) { i in |
| 99 | RoundedRectangle(cornerRadius: 1) |
| 100 | .fill(Color.jotleyWidgetAccent) |
| 101 | .frame(width: 3, height: CGFloat([8, 14, 10, 18, 12, 16, 8, 14, 20, 10, 14, 8][i])) |
| 102 | } |
| 103 | } |
| 104 | .widgetAccentable() |
| 105 | } |
| 106 | .padding(.vertical, 14) |
| 107 | |
| 108 | Spacer() |
| 109 | } |
| 110 | .padding(.leading, 16) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // MARK: - Widget Configuration |
| 115 | struct QuickRecordWidget: Widget { |
| 116 | let kind: String = "QuickRecordWidget" |
| 117 | |
| 118 | var body: some WidgetConfiguration { |
| 119 | StaticConfiguration(kind: kind, provider: QuickRecordProvider()) { entry in |
| 120 | QuickRecordWidgetEntryView(entry: entry) |
| 121 | .containerBackground(Color.jotleyDark, for: .widget) |
| 122 | } |
| 123 | .configurationDisplayName("Live Transcription") |
| 124 | .description("Start on-device live transcription with one tap") |
| 125 | .supportedFamilies([.systemSmall, .systemMedium]) |
| 126 | .contentMarginsDisabled() |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // MARK: - Widget Entry View |
| 131 | struct QuickRecordWidgetEntryView: View { |
| 132 | var entry: QuickRecordProvider.Entry |
| 133 | @Environment(\.widgetFamily) var family |
| 134 | @Environment(\.widgetRenderingMode) private var renderingMode |
| 135 | |
| 136 | var body: some View { |
| 137 | Group { |
| 138 | switch family { |
| 139 | case .systemMedium: QuickRecordMediumView() |
| 140 | default: QuickRecordSmallView() |
| 141 | } |
| 142 | } |
| 143 | .foregroundStyle(renderingMode == .fullColor ? Color.white : Color.primary) |
| 144 | .widgetURL(URL(string: "jotley://starttranscription")) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // MARK: - Previews |
| 149 | #Preview("Small", as: .systemSmall) { |
| 150 | QuickRecordWidget() |
| 151 | } timeline: { |
| 152 | QuickRecordEntry(date: Date()) |
| 153 | } |
| 154 | |
| 155 | #Preview("Medium", as: .systemMedium) { |
| 156 | QuickRecordWidget() |
| 157 | } timeline: { |
| 158 | QuickRecordEntry(date: Date()) |
| 159 | } |