Jotley / app · jotley.ai / macOS / Views / TabViews / TranscriptionTabView.swift
| 1 | import SwiftUI |
| 2 | import CoreData |
| 3 | |
| 4 | enum TabLoadingAnimation { |
| 5 | static let spinnerRotationDuration: Double = 1.0 |
| 6 | } |
| 7 | |
| 8 | struct TranscriptBubbleView: View { |
| 9 | @ObservedObject private var themeManager = ThemeManager.shared |
| 10 | let text: String |
| 11 | let timestampLabel: String? |
| 12 | let isInterim: Bool |
| 13 | @Environment(\.themeRefresh) private var themeRefresh |
| 14 | |
| 15 | // Simple heuristic: if text is longer than ~80 characters, it's likely multi-line |
| 16 | private var isLikelyMultiLine: Bool { |
| 17 | text.count > 80 |
| 18 | } |
| 19 | |
| 20 | private var bubbleTextColor: Color { |
| 21 | // Depend on themeRefresh so color resolves again when appearance changes. |
| 22 | _ = themeRefresh |
| 23 | return Color.themeTextPrimary |
| 24 | } |
| 25 | |
| 26 | var body: some View { |
| 27 | HStack { |
| 28 | VStack(alignment: .leading, spacing: 4) { |
| 29 | if let timestampLabel, !timestampLabel.isEmpty { |
| 30 | Text(timestampLabel) |
| 31 | .font(.caption2.monospacedDigit()) |
| 32 | .foregroundColor(Color.themeTextSecondary.opacity(0.8)) |
| 33 | } |
| 34 | |
| 35 | Text(text) |
| 36 | .foregroundColor(bubbleTextColor) |
| 37 | .lineLimit(nil) |
| 38 | .multilineTextAlignment(.leading) |
| 39 | } |
| 40 | .padding(.horizontal, 12) |
| 41 | .padding(.vertical, 8) |
| 42 | .frame(maxWidth: isLikelyMultiLine ? .infinity : nil, alignment: .leading) |
| 43 | .modifier(JotleyTranscriptCard()) |
| 44 | |
| 45 | if !isLikelyMultiLine { |
| 46 | Spacer(minLength: 0) |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | // View for the Transcript tab content |
| 52 | public struct TranscriptTabView: View { |
| 53 | @ObservedObject var note: Note |
| 54 | @Binding var editableTranscript: String |
| 55 | @FocusState.Binding var isTranscriptEditorFocused: Bool |
| 56 | /// Loading state supplied by the note coordinator. |
| 57 | var isLoading: Bool = false |
| 58 | var viewModel: NotesListViewModel? |
| 59 | @Environment(\.themeRefresh) private var themeRefresh |
| 60 | |
| 61 | private var isRecordingThisNote: Bool { |
| 62 | guard let vm = viewModel else { return false } |
| 63 | return vm.recorder.isRecording && |
| 64 | vm.recorder.isRecordingNewNote && |
| 65 | vm.newNoteBeingRecorded == note |
| 66 | } |
| 67 | |
| 68 | // Check if transcript is in loading state. Backed by the view model's |
| 69 | // per-note loading flag rather than a persisted placeholder string. |
| 70 | private var isTranscriptLoading: Bool { |
| 71 | isLoading |
| 72 | } |
| 73 | |
| 74 | // Get loading message based on note type |
| 75 | private var loadingMessage: String { "Recovering transcript…" } |
| 76 | private var loadingDetailMessage: String? { "Processing audio stored on this Mac." } |
| 77 | private func getEmptyStateMessage() -> String { |
| 78 | "Your transcript appears here as you speak. Use + to start a new transcription." |
| 79 | } |
| 80 | |
| 81 | // Check if transcript has actual content (not just whitespace) |
| 82 | private var hasActualContent: Bool { |
| 83 | !editableTranscript.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty |
| 84 | } |
| 85 | |
| 86 | public var body: some View { |
| 87 | let _ = themeRefresh |
| 88 | |
| 89 | VStack(alignment: .leading, spacing: 0) { |
| 90 | if isTranscriptLoading { |
| 91 | // Loading state with animated spinner |
| 92 | TranscriptLoadingView( |
| 93 | message: loadingMessage, |
| 94 | detailMessage: loadingDetailMessage |
| 95 | ) |
| 96 | .frame(maxWidth: .infinity, maxHeight: .infinity) |
| 97 | } else if isRecordingThisNote { |
| 98 | let liveTranscript = (viewModel?.recorder.accumulatedTranscript ?? "") |
| 99 | .trimmingCharacters(in: .whitespacesAndNewlines) |
| 100 | let transcriptToDisplay = liveTranscript.isEmpty ? editableTranscript : (viewModel?.recorder.accumulatedTranscript ?? "") |
| 101 | |
| 102 | // Live transcription while recording. |
| 103 | let liveUtterances = viewModel?.recorder.transcriptUtterances ?? [] |
| 104 | let utterancesForDisplay = liveUtterances.isEmpty ? note.transcriptUtterances : liveUtterances |
| 105 | TranscriptParagraphsView( |
| 106 | transcript: transcriptToDisplay, |
| 107 | utterances: utterancesForDisplay, |
| 108 | interimText: viewModel?.recorder.latestTranscriptDelta ?? "", |
| 109 | interimTimestamp: viewModel?.recorder.latestTranscriptTimestamp, |
| 110 | isRecordingActive: true, |
| 111 | showEllipsis: true |
| 112 | ) |
| 113 | .padding(.horizontal, 4) |
| 114 | .padding(.vertical, 4) |
| 115 | .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top) |
| 116 | } else if hasActualContent { |
| 117 | // Static transcript view - only show if there's actual content |
| 118 | TranscriptParagraphsView( |
| 119 | transcript: editableTranscript, |
| 120 | utterances: note.transcriptUtterances, |
| 121 | interimText: "", |
| 122 | isRecordingActive: false, |
| 123 | showEllipsis: false |
| 124 | ) |
| 125 | .padding(.horizontal, 4) |
| 126 | .padding(.vertical, 4) |
| 127 | .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top) |
| 128 | } else { |
| 129 | // Empty state - provide more context |
| 130 | VStack(spacing: 16) { |
| 131 | Spacer() |
| 132 | Image(systemName: "mic.slash") |
| 133 | .font(.system(size: 48)) |
| 134 | .foregroundColor(Color.themeTextSecondary.opacity(0.3)) |
| 135 | |
| 136 | Text("No transcript available") |
| 137 | .font(.headline) |
| 138 | .foregroundColor(Color.themeTextSecondary.opacity(0.7)) |
| 139 | |
| 140 | Text(getEmptyStateMessage()) |
| 141 | .font(.body) |
| 142 | .foregroundColor(Color.themeTextSecondary.opacity(0.5)) |
| 143 | .multilineTextAlignment(.center) |
| 144 | .frame(maxWidth: 400) |
| 145 | Spacer() |
| 146 | } |
| 147 | .frame(maxWidth: .infinity, maxHeight: .infinity) |
| 148 | .padding() |
| 149 | } |
| 150 | } |
| 151 | .frame(maxWidth: .infinity, alignment: .topLeading) |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | struct TranscriptParagraphsView: View { |
| 156 | let transcript: String |
| 157 | let utterances: [TranscriptUtterance] |
| 158 | let interimText: String |
| 159 | var interimTimestamp: String? = nil |
| 160 | let isRecordingActive: Bool |
| 161 | let showEllipsis: Bool |
| 162 | |
| 163 | var body: some View { |
| 164 | LazyVStack(alignment: .leading, spacing: 12) { |
| 165 | let sourceUtterances = utterances.isEmpty |
| 166 | ? TranscriptUtterance.fallbackUtterances(from: transcript) |
| 167 | : utterances |
| 168 | let utteranceList = isRecordingActive |
| 169 | ? TranscriptUtterance.coalescedForDisplay(sourceUtterances) |
| 170 | : utterances.isEmpty ? TranscriptUtterance.displayParagraphs(sourceUtterances) : sourceUtterances |
| 171 | |
| 172 | ForEach(Array(utteranceList.enumerated()), id: \.offset) { index, utterance in |
| 173 | TranscriptBubbleView( |
| 174 | text: utterance.text, |
| 175 | timestampLabel: utterance.timestampLabel, |
| 176 | isInterim: false |
| 177 | ) |
| 178 | .id(index) |
| 179 | } |
| 180 | |
| 181 | // Show interim transcript |
| 182 | if !interimText.isEmpty { |
| 183 | TranscriptBubbleView( |
| 184 | text: interimText, |
| 185 | timestampLabel: interimTimestamp, |
| 186 | isInterim: true |
| 187 | ) |
| 188 | .id("interim") |
| 189 | } |
| 190 | |
| 191 | // Show ellipsis when recording |
| 192 | if showEllipsis { |
| 193 | AnimatedEllipsis() |
| 194 | .padding(.leading, 16) |
| 195 | .padding(.top, 8) |
| 196 | .padding(.bottom, 10) |
| 197 | .id("ellipsis") |
| 198 | } |
| 199 | } |
| 200 | .padding(.horizontal, 10) |
| 201 | // Leave room for the glass rim and shadow around the first and last cards. |
| 202 | .padding(.vertical, 8) |
| 203 | .frame(maxWidth: .infinity, alignment: .leading) |
| 204 | } |
| 205 | |
| 206 | } |
| 207 | |
| 208 | // Animated ellipsis component that shows recording activity |
| 209 | struct AnimatedEllipsis: View { |
| 210 | @State private var opacity1: Double = 0.3 |
| 211 | @State private var opacity2: Double = 0.3 |
| 212 | @State private var opacity3: Double = 0.3 |
| 213 | |
| 214 | private let dotSize: CGFloat = 6 |
| 215 | |
| 216 | var body: some View { |
| 217 | HStack(spacing: 4) { |
| 218 | Circle() |
| 219 | .fill(Color.themePrimaryAccent) |
| 220 | .frame(width: dotSize, height: dotSize) |
| 221 | .fixedSize() |
| 222 | .opacity(opacity1) |
| 223 | |
| 224 | Circle() |
| 225 | .fill(Color.themePrimaryAccent) |
| 226 | .frame(width: dotSize, height: dotSize) |
| 227 | .fixedSize() |
| 228 | .opacity(opacity2) |
| 229 | |
| 230 | Circle() |
| 231 | .fill(Color.themePrimaryAccent) |
| 232 | .frame(width: dotSize, height: dotSize) |
| 233 | .fixedSize() |
| 234 | .opacity(opacity3) |
| 235 | } |
| 236 | .fixedSize() |
| 237 | .onAppear { |
| 238 | withAnimation(Animation.easeInOut(duration: 0.8).repeatForever(autoreverses: true)) { |
| 239 | opacity1 = 1.0 |
| 240 | } |
| 241 | DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { |
| 242 | withAnimation(Animation.easeInOut(duration: 0.8).repeatForever(autoreverses: true)) { |
| 243 | opacity2 = 1.0 |
| 244 | } |
| 245 | } |
| 246 | DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) { |
| 247 | withAnimation(Animation.easeInOut(duration: 0.8).repeatForever(autoreverses: true)) { |
| 248 | opacity3 = 1.0 |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | // Loading view with animated spinner for transcript imports |
| 256 | struct TranscriptLoadingView: View { |
| 257 | let message: String |
| 258 | var detailMessage: String? = nil |
| 259 | var startedAt: Date? = nil |
| 260 | var timeoutInterval: TimeInterval? = nil |
| 261 | |
| 262 | @State private var spinnerRotation: Double = 0 |
| 263 | |
| 264 | var body: some View { |
| 265 | // Use GeometryReader with explicit frame to enable Spacer centering inside ScrollView |
| 266 | GeometryReader { geometry in |
| 267 | VStack { |
| 268 | Spacer() |
| 269 | VStack(spacing: 20) { |
| 270 | // Animated spinner using @State rotation for smooth animation |
| 271 | // (avoids TimelineView(.animation) which re-evaluates body every frame) |
| 272 | ZStack { |
| 273 | Circle() |
| 274 | .stroke(Color.themeTextSecondary.opacity(0.2), lineWidth: 4) |
| 275 | .frame(width: 50, height: 50) |
| 276 | |
| 277 | Circle() |
| 278 | .trim(from: 0, to: 0.2) |
| 279 | .stroke(Color.themePrimaryAccent, lineWidth: 4) |
| 280 | .frame(width: 50, height: 50) |
| 281 | .rotationEffect(Angle(degrees: spinnerRotation)) |
| 282 | } |
| 283 | .onAppear { |
| 284 | withAnimation(Animation.linear(duration: TabLoadingAnimation.spinnerRotationDuration).repeatForever(autoreverses: false)) { |
| 285 | spinnerRotation = 360 |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | VStack(spacing: 8) { |
| 290 | Text(message) |
| 291 | .font(.headline) |
| 292 | .foregroundColor(Color.themeTextSecondary) |
| 293 | |
| 294 | if let detailMessage, !detailMessage.isEmpty { |
| 295 | Text(detailMessage) |
| 296 | .font(.subheadline) |
| 297 | .foregroundColor(Color.themeTextSecondary.opacity(0.8)) |
| 298 | .multilineTextAlignment(.center) |
| 299 | .frame(maxWidth: 420) |
| 300 | } |
| 301 | |
| 302 | if let startedAt { |
| 303 | SummaryLoadingTimerView( |
| 304 | startedAt: startedAt, |
| 305 | timeoutInterval: timeoutInterval |
| 306 | ) |
| 307 | .frame(maxWidth: 260) |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | Spacer() |
| 312 | } |
| 313 | .frame(width: geometry.size.width, height: geometry.size.height) |
| 314 | } |
| 315 | .frame(minHeight: 300) |
| 316 | } |
| 317 | } |