Jotley / app / macOS / Views / NoteDetailPlaceholderView+EventHandlers.swift
| 1 | import SwiftUI |
| 2 | import CoreData |
| 3 | import AppKit |
| 4 | |
| 5 | extension NoteDetailPlaceholderView { |
| 6 | // MARK: Event Handlers |
| 7 | |
| 8 | func handleOnAppear() { |
| 9 | summaryGenerationError = nil |
| 10 | editableSummary = note.summary ?? "" |
| 11 | editableTranscript = note.transcription ?? "" |
| 12 | savedSummary = editableSummary |
| 13 | savedTranscript = editableTranscript |
| 14 | editableTranscriptNoteID = note.objectID |
| 15 | currentTags = note.tagNames // Initialize tags from note |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | // If this note is actively recording, always surface the transcript tab |
| 24 | // so live transcription remains visible. |
| 25 | if viewModel.recorder.isRecording && viewModel.newNoteBeingRecorded == note { |
| 26 | selectedTab = DetailTab.transcript.rawValue |
| 27 | } |
| 28 | |
| 29 | // Set per-tab markdown/rendered defaults |
| 30 | showRawMarkdownSummary = false // Rendered |
| 31 | |
| 32 | viewRefreshID = UUID() // Force a refresh of child views like DetailTabPanel |
| 33 | |
| 34 | |
| 35 | // Listen for save notification from view model |
| 36 | NotificationCenter.default.addObserver(forName: Notification.Name("JotVibeSaveCurrentNote"), object: nil, queue: .main) { _ in |
| 37 | |
| 38 | self.saveTranscriptChanges() |
| 39 | self.saveSummaryChanges() |
| 40 | } |
| 41 | |
| 42 | // Listen for force save notification when app is terminating |
| 43 | NotificationCenter.default.addObserver(forName: Notification.Name("JotleyForceAllEditorsSave"), object: nil, queue: .main) { _ in |
| 44 | |
| 45 | // Cancel any pending save timers and execute saves immediately |
| 46 | self.summarySaveTimer?.invalidate() |
| 47 | self.transcriptSaveTimer?.invalidate() |
| 48 | |
| 49 | // Force all editors to lose focus which will trigger their save handlers |
| 50 | self.isTranscriptEditorFocused = false |
| 51 | self.isSummaryEditorFocused = false |
| 52 | |
| 53 | // Give a brief moment for focus changes to propagate |
| 54 | DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) { |
| 55 | // Explicitly save all content |
| 56 | self.saveTranscriptChanges() |
| 57 | self.saveSummaryChanges() |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // Commented out periodic focus checker as it was causing focus loss while typing |
| 62 | // focusCheckTimer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { _ in |
| 63 | // // Check if any text field has focus by examining the first responder |
| 64 | // guard let window = NSApp.keyWindow, |
| 65 | // let firstResponder = window.firstResponder else { |
| 66 | // // No window or first responder, ensure all fields are marked as unfocused |
| 67 | // if self.isNotesEditorFocused || self.isSummaryEditorFocused || self.isTranscriptEditorFocused { |
| 68 | // self.isNotesEditorFocused = false |
| 69 | // self.isSummaryEditorFocused = false |
| 70 | // self.isTranscriptEditorFocused = false |
| 71 | // } |
| 72 | // return |
| 73 | // } |
| 74 | // |
| 75 | // // If first responder is not a text view, ensure our fields are unfocused |
| 76 | // if !(firstResponder is NSTextView) { |
| 77 | // if self.isNotesEditorFocused { |
| 78 | // self.isNotesEditorFocused = false |
| 79 | // } |
| 80 | // if self.isSummaryEditorFocused { |
| 81 | // self.isSummaryEditorFocused = false |
| 82 | // } |
| 83 | // if self.isTranscriptEditorFocused { |
| 84 | // self.isTranscriptEditorFocused = false |
| 85 | // } |
| 86 | // } |
| 87 | // } |
| 88 | } |
| 89 | |
| 90 | @MainActor |
| 91 | |
| 92 | |
| 93 | func handleTranscriptionPausedChanged(_ oldValue: Bool?, _ isPausedNewValue: Bool?) { |
| 94 | let vm = viewModel |
| 95 | guard vm.newNoteBeingRecorded == self.note, // Change is for the current note |
| 96 | isPausedNewValue == false, // Transcription is now active (not paused) |
| 97 | vm.recorder.isRecording == true else { // Recorder is actually running |
| 98 | return |
| 99 | } |
| 100 | |
| 101 | if selectedTab != DetailTab.transcript.rawValue { |
| 102 | selectedTab = DetailTab.transcript.rawValue |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | func handleRecordingChanged(_ oldValue: Bool?, _ isRecordingNow: Bool?) { |
| 107 | let vm = viewModel |
| 108 | guard vm.newNoteBeingRecorded == self.note, // Change is for the current note |
| 109 | isRecordingNow == true else { // Recorder just started |
| 110 | return |
| 111 | } |
| 112 | |
| 113 | if selectedTab != DetailTab.transcript.rawValue { |
| 114 | selectedTab = DetailTab.transcript.rawValue |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | func handleTranscriptFocusChanged(_ oldIsFocused: Bool, _ newIsFocused: Bool) { |
| 119 | if oldIsFocused == true && newIsFocused == false { |
| 120 | // Transcript editor lost focus, save changes |
| 121 | |
| 122 | saveTranscriptChanges() |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | func handleNoteChanged(_ oldObjectID: NSManagedObjectID, _ newObjectID: NSManagedObjectID) { |
| 127 | // Ensure editors lose focus so their state is updated before saving |
| 128 | isTranscriptEditorFocused = false |
| 129 | isSummaryEditorFocused = false |
| 130 | |
| 131 | guard oldObjectID != newObjectID else { |
| 132 | // This block handles the case where the new and old IDs are the same. |
| 133 | // This might be a refresh or no actual change. |
| 134 | // We'll ensure the current note's editable fields are synced if not focused. |
| 135 | manualTitle = note.titleOrUntitled // 'note' is the current (and effectively new) note |
| 136 | summaryGenerationError = nil |
| 137 | editableSummary = note.summary ?? "" |
| 138 | savedSummary = editableSummary |
| 139 | if !isTranscriptEditorFocused { // Sync transcript if editor not focused |
| 140 | editableTranscript = note.transcription ?? "" |
| 141 | savedTranscript = editableTranscript |
| 142 | editableTranscriptNoteID = note.objectID |
| 143 | } |
| 144 | return |
| 145 | } |
| 146 | |
| 147 | // If we are here, oldObjectID != newObjectID. A true switch has occurred. |
| 148 | // 'note' @ObservedObject has already been updated by SwiftUI to reflect the new note (associated with newObjectID). |
| 149 | // We need to save the state of 'editableTranscript' (which holds content from the old note) |
| 150 | // against the 'oldObjectID'. |
| 151 | |
| 152 | let context = viewContext |
| 153 | if editableTranscriptNoteID == oldObjectID, |
| 154 | let actualOldNote = try? context.existingObject(with: oldObjectID) as? Note, !actualOldNote.isDeleted { |
| 155 | do { |
| 156 | if !isEditingTranscript, editableTranscript != savedTranscript { |
| 157 | try actualOldNote.saveEditedTranscript(editableTranscript, original: savedTranscript, in: persistenceController) |
| 158 | } |
| 159 | if editableSummary != savedSummary { |
| 160 | try actualOldNote.saveEditedSummary(editableSummary, original: savedSummary, in: persistenceController) |
| 161 | } |
| 162 | } catch { viewModel.report(error) } |
| 163 | } |
| 164 | |
| 165 | // Now, load content for the NEW note (the 'note' object itself is already the new note) |
| 166 | manualTitle = note.titleOrUntitled |
| 167 | summaryGenerationError = nil |
| 168 | editableSummary = note.summary ?? "" |
| 169 | savedSummary = editableSummary |
| 170 | |
| 171 | // Load transcript for the new note. |
| 172 | editableTranscript = note.transcription ?? "" |
| 173 | savedTranscript = editableTranscript |
| 174 | editableTranscriptNoteID = note.objectID |
| 175 | |
| 176 | |
| 177 | |
| 178 | viewRefreshID = UUID() // Force a refresh of child views like DetailTabPanel |
| 179 | } |
| 180 | |
| 181 | func handleTabChanged(_ oldValue: Int, _ newTabRawValue: Int) { |
| 182 | if newTabRawValue != DetailTab.transcript.rawValue { isEditingTranscript = false } |
| 183 | |
| 184 | // Cancel any pending save timers for the old tab |
| 185 | if oldValue == DetailTab.summary.rawValue { |
| 186 | summarySaveTimer?.invalidate() |
| 187 | summarySaveTimer = nil |
| 188 | } else if oldValue == DetailTab.transcript.rawValue { |
| 189 | transcriptSaveTimer?.invalidate() |
| 190 | transcriptSaveTimer = nil |
| 191 | } |
| 192 | |
| 193 | // Flush in-progress edits from the previous tab, but only when the |
| 194 | // editor was actually focused. A blind save here can race the |
| 195 | // SwiftUI binding update for editableSummary/editableTranscript and |
| 196 | // clobber freshly-imported content (e.g. a transcript + |
| 197 | // auto-summary) with an empty @State buffer. |
| 198 | if oldValue == DetailTab.summary.rawValue, isSummaryEditorFocused { |
| 199 | |
| 200 | isSummaryEditorFocused = false |
| 201 | saveSummaryChanges() |
| 202 | } else if oldValue == DetailTab.transcript.rawValue, isTranscriptEditorFocused { |
| 203 | |
| 204 | isTranscriptEditorFocused = false |
| 205 | saveTranscriptChanges() |
| 206 | } |
| 207 | |
| 208 | // When switching to summary tab, ensure editableSummary is synced with the current note content |
| 209 | if newTabRawValue == DetailTab.summary.rawValue { |
| 210 | // Sync summary when switching to summary tab |
| 211 | if !isSummaryEditorFocused { |
| 212 | let currentSummaryValue = note.summary ?? "" |
| 213 | if editableSummary != currentSummaryValue { |
| 214 | |
| 215 | editableSummary = currentSummaryValue |
| 216 | savedSummary = currentSummaryValue |
| 217 | } |
| 218 | } |
| 219 | } else if newTabRawValue == DetailTab.transcript.rawValue { |
| 220 | // Sync transcript when switching to transcript tab |
| 221 | if !isTranscriptEditorFocused { |
| 222 | let currentTranscriptValue = note.transcription ?? "" |
| 223 | if editableTranscript != currentTranscriptValue { |
| 224 | |
| 225 | |
| 226 | |
| 227 | editableTranscript = currentTranscriptValue |
| 228 | savedTranscript = currentTranscriptValue |
| 229 | } |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | func handleSummaryChanged(_ oldValue: String?, _ newSummary: String?) { |
| 240 | if !isSummaryEditorFocused && editableSummary == savedSummary { |
| 241 | editableSummary = newSummary ?? "" |
| 242 | savedSummary = editableSummary |
| 243 | |
| 244 | } else { |
| 245 | |
| 246 | } |
| 247 | |
| 248 | // If the Summary tab is selected but no longer has a summary to show |
| 249 | // (and we're not actively generating one), fall back to a visible tab |
| 250 | // so the user isn't stranded on a hidden tab. |
| 251 | if selectedTab == DetailTab.summary.rawValue && !hasSummary { |
| 252 | let isGeneratingSummaryForThisNote = |
| 253 | (viewModel.isGeneratingSummary && viewModel.generatingSummaryNoteID == note.objectID) || |
| 254 | newSummary?.trimmingCharacters(in: .whitespacesAndNewlines) == "Generating summary..." |
| 255 | if !isGeneratingSummaryForThisNote { |
| 256 | selectedTab = DetailTab.transcript.rawValue |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | func handleTitleChanged(_ oldValue: String?, _ newTitle: String?) { |
| 262 | manualTitle = newTitle ?? note.titleOrUntitled |
| 263 | |
| 264 | } |
| 265 | |
| 266 | func handleSummaryFocusChanged(_ oldIsFocused: Bool, _ newIsFocused: Bool) { |
| 267 | if oldIsFocused == true && newIsFocused == false { |
| 268 | // Summary editor lost focus, save changes |
| 269 | |
| 270 | saveSummaryChanges() |
| 271 | } |
| 272 | } |
| 273 | } |