Branchwell

Jotley / app · jotley.ai / iOS / Views / RecordingModalView.swift

1import SwiftUI
2
3struct LiveTranscriptionView: View {
4 @Environment(\.colorScheme) private var colorScheme
5 @Binding var isPresented: Bool
6 var existingNote: Note? = nil
7 var isNewTranscription = false
8 // Present the same session manager explicitly for new and continued
9 // transcription, without relying on sheet environment inheritance.
10 @ObservedObject var transcriptionManager: LiveTranscriptionManager
11 @State var isFinishing = false
12 @State private var didAttemptStart = false
13 @State private(set) var hasStartedTranscription = false
14 var isResuming: Bool { existingNote != nil && !isNewTranscription }
15 private var transcriptFadeHeight: CGFloat {
16 guard UIDevice.current.userInterfaceIdiom == .pad else { return 100 }
17 return colorScheme == .light ? 136 : 120
18 }
19 var body: some View {
20 GeometryReader { geometry in
21 let compactControls = geometry.size.height < 740
22 VStack(spacing: 0) {
23 Capsule().fill(Color(white: 0.4)).frame(width: 36, height: 5).padding(.top,8)
24 header
25 VStack(spacing: compactControls ? 8 : 12) {
26 waveformSection.frame(height: compactControls ? 44 : 56)
27 ViewThatFits(in: .horizontal) {
28 HStack(spacing: 24) {
29 recordingReadout.fixedSize(horizontal: true, vertical: false)
30 transcriptionControls.fixedSize(horizontal: true, vertical: false)
31 }
32 VStack(spacing: 8) {
33 recordingReadout
34 transcriptionControls
35 }
36 }
37 }
38 .frame(maxWidth: .infinity)
39 .overlay(alignment: .bottomTrailing) {
40 if transcriptionManager.busy {
41 ProgressView()
42 .controlSize(.small)
43 .accessibilityLabel(transcriptionManager.transcriptionState == .idle ? "Preparing on-device speech" : "Saving transcript")
44 }
45 }
46 .padding(.horizontal, 20)
47 .padding(.bottom, 10)
48 ScrollViewReader { proxy in
49 ScrollView {
50 LazyVStack(alignment: .leading, spacing: 8) {
51 ForEach(transcriptionManager.liveUtterances) { utterance in
52 TranscriptBubbleView(text: utterance.text,
53 timestampText: utterance.timestampLabel,
54 allowsTextSelection: true)
55 .accessibilityIdentifier("liveTranscriptCard-\(utterance.id)")
56 }
57 Color.clear.frame(height: 1).id("liveTranscriptBottom")
58 }.frame(maxWidth: .infinity, alignment: .leading).padding()
59 }
60 .accessibilityIdentifier("liveTranscript")
61 .defaultScrollAnchor(.bottom)
62 // Fill from the top until the cards outgrow the viewport, then
63 // keep the latest words visible at the bottom as before.
64 .defaultScrollAnchor(.topLeading, for: .alignment)
65 .modifier(JotleyTranscriptEdgeFade(topFadeHeight: transcriptFadeHeight))
66 .onChange(of: transcriptionManager.liveUtterances) { _, _ in
67 proxy.scrollTo("liveTranscriptBottom", anchor: .bottom)
68 }
69 }
70 .frame(maxHeight: .infinity)
71 }
72 .frame(maxWidth: .infinity, maxHeight: .infinity)
73 }
74 .background(Color.themeBackground)
75 .presentationDetents([.large])
76 .interactiveDismissDisabled(true)
77 .task {
78 guard !didAttemptStart, !isFinishing else { return }
79 didAttemptStart = true
80 if transcriptionManager.canStart { await transcriptionManager.startTranscription(note: existingNote) }
81 }
82 .alert("Transcription Needs Attention", isPresented: Binding(
83 get: { transcriptionManager.errorMessage != nil && !transcriptionManager.busy },
84 set: { if !$0 { Task { @MainActor in transcriptionManager.errorMessage = nil } } }
85 )) {
86 Button("OK", role: .cancel) { transcriptionManager.errorMessage = nil }
87 } message: {
88 Text(transcriptionManager.errorMessage ?? "")
89 }
90 .preferredColorScheme(ThemeManager.shared.effectiveColorScheme)
91 .onChange(of: transcriptionManager.isTranscribing) { _, recording in
92 // A Live Activity stop uses the same transcription manager from outside this
93 // sheet. Close the capture page once that session has stopped.
94 if recording { hasStartedTranscription = true }
95 else if hasStartedTranscription, !transcriptionManager.busy, transcriptionManager.errorMessage == nil { isPresented = false }
96 }
97 .onAppear { transcriptionManager.captureScreenVisible = true; hasStartedTranscription = transcriptionManager.isTranscribing }
98 .onDisappear { transcriptionManager.captureScreenVisible = false }
99 .onChange(of: transcriptionManager.busy) { _, busy in
100 if hasStartedTranscription, !busy, !transcriptionManager.isTranscribing, transcriptionManager.errorMessage == nil { isPresented = false }
101 }
102 }
103 func finishAndClose() {
104 guard !isFinishing else { return }
105 isFinishing = true
106 Task {
107 await transcriptionManager.stopTranscription()
108 isPresented = false
109 }
110 }
111 func toggleTranscriptionPause() {
112 guard !isFinishing else { return }
113 switch transcriptionManager.transcriptionState {
114 case .idle: Task { await transcriptionManager.startTranscription(note: existingNote) }
115 case .listening: transcriptionManager.pauseTranscription()
116 case .paused: Task { await transcriptionManager.resumeTranscription() }
117 }
118 }
119}