Jotley / app / macOS / Views / DetailTabViewPanel.swift
| 1 | import SwiftUI |
| 2 | import CoreData |
| 3 | import AppKit |
| 4 | import Combine |
| 5 | |
| 6 | // Refactored implementation of the detail tab view panel |
| 7 | struct DetailTabPanel: View { |
| 8 | @ObservedObject var note: Note |
| 9 | @Binding var selectedTab: Int // Raw value of DetailTab |
| 10 | @Binding var isGeneratingSummary: Bool |
| 11 | @Binding var summaryGenerationError: String? |
| 12 | |
| 13 | // Bindings for editable summary and transcript |
| 14 | @Binding var editableSummary: String |
| 15 | @FocusState.Binding var isSummaryEditorFocused: Bool |
| 16 | @Binding var summaryInlineEditRequestCounter: Int |
| 17 | @Binding var editableTranscript: String |
| 18 | @FocusState.Binding var isTranscriptEditorFocused: Bool |
| 19 | |
| 20 | // Binding for raw markdown mode |
| 21 | @Binding var showRawMarkdown: Bool |
| 22 | |
| 23 | // Optional view model to pass to the transcription tab view |
| 24 | var viewModel: NotesListViewModel? |
| 25 | |
| 26 | // Per-note saved-audio loading flag, computed by the parent (which observes the |
| 27 | // view model) and threaded down so SwiftUI re-renders the transcript tab when |
| 28 | // it flips. Replaces the old persisted "Loading transcript..." placeholder. |
| 29 | var isTranscriptLoading: Bool = false |
| 30 | |
| 31 | // Optional callback for generating summary from empty state |
| 32 | var summaryLoadingStartedAt: Date? |
| 33 | @ObservedObject var streamingSummaryViewModel: StreamingSummaryViewModel |
| 34 | |
| 35 | // Create a state property to force redraw when needed |
| 36 | @State private var refreshID = UUID() |
| 37 | // Flag to prevent responding to CoreData updates during local summary generation |
| 38 | @State private var ignoreNoteChanges: Bool = false |
| 39 | |
| 40 | // Define an ID for the bottom anchor |
| 41 | |
| 42 | var body: some View { |
| 43 | // Use Group to ensure proper view switching |
| 44 | Group { |
| 45 | if selectedTab == DetailTab.summary.rawValue { |
| 46 | SummaryTabView( |
| 47 | note: note, |
| 48 | editableSummary: $editableSummary, |
| 49 | isSummaryEditorFocused: $isSummaryEditorFocused, |
| 50 | inlineEditRequestCounter: $summaryInlineEditRequestCounter, |
| 51 | isGeneratingSummary: $isGeneratingSummary, |
| 52 | summaryGenerationError: $summaryGenerationError, |
| 53 | showRawMarkdown: $showRawMarkdown, |
| 54 | summaryLoadingStartedAt: summaryLoadingStartedAt, |
| 55 | streamingSummaryViewModel: streamingSummaryViewModel |
| 56 | ) |
| 57 | .transition(.opacity) |
| 58 | } else if selectedTab == DetailTab.transcript.rawValue { |
| 59 | TranscriptTabView( |
| 60 | note: note, |
| 61 | editableTranscript: $editableTranscript, |
| 62 | isTranscriptEditorFocused: $isTranscriptEditorFocused, |
| 63 | isLoading: isTranscriptLoading, |
| 64 | viewModel: viewModel |
| 65 | ) |
| 66 | .transition(.opacity) |
| 67 | } else { |
| 68 | EmptyView() |
| 69 | } |
| 70 | } |
| 71 | .padding(.top, 6) |
| 72 | .padding(.horizontal, 4) |
| 73 | .padding(.bottom, 12) |
| 74 | .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading) |
| 75 | .onReceive(NotificationCenter.default.publisher(for: Notification.Name("SummaryGenerationComplete")) |
| 76 | .receive(on: RunLoop.main)) { notification in |
| 77 | receiveSummaryCompletion(notification) |
| 78 | } |
| 79 | .onReceive(NotificationCenter.default.publisher(for: Notification.Name("SummaryGenerationFailed")) |
| 80 | .receive(on: RunLoop.main)) { notification in |
| 81 | receiveSummaryFailure(notification) |
| 82 | } |
| 83 | .onChange(of: note.summary) { oldValue, newValue in |
| 84 | // Only process CoreData updates if we're not ignoring them during local summary generation |
| 85 | if !ignoreNoteChanges { |
| 86 | let trimmedSummary = newValue?.trimmingCharacters(in: .whitespacesAndNewlines) ?? "" |
| 87 | if trimmedSummary.isEmpty { |
| 88 | summaryGenerationError = nil |
| 89 | } |
| 90 | editableSummary = newValue ?? "" |
| 91 | refreshID = UUID() // Force a refresh when summary changes |
| 92 | } |
| 93 | } |
| 94 | .onChange(of: isGeneratingSummary) { oldValue, newValue in |
| 95 | if oldValue && !newValue { // If we just finished generating |
| 96 | // Let the notification handle updating the UI when data is truly ready |
| 97 | // But still clear the ignore flag |
| 98 | ignoreNoteChanges = false |
| 99 | } else if !oldValue && newValue { // If we just started generating |
| 100 | // Start ignoring CoreData updates until local summary generation completes |
| 101 | ignoreNoteChanges = true |
| 102 | summaryGenerationError = nil |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // SwiftUI owns these subscriptions and cancels them when this panel is removed. |
| 108 | private func receiveSummaryCompletion(_ notification: Notification) { |
| 109 | guard let userInfo = notification.userInfo, |
| 110 | let noteID = userInfo["noteID"] as? NSManagedObjectID, |
| 111 | noteID == note.objectID, !note.isDeleted else { return } |
| 112 | |
| 113 | summaryGenerationError = nil |
| 114 | editableSummary = userInfo["summary"] as? String ?? note.summary ?? "" |
| 115 | refreshID = UUID() |
| 116 | ignoreNoteChanges = false |
| 117 | // The generating task owns its loading state. A completion event must not |
| 118 | // schedule a delayed reset that could stop a newer generation for this note. |
| 119 | } |
| 120 | |
| 121 | private func receiveSummaryFailure(_ notification: Notification) { |
| 122 | guard let userInfo = notification.userInfo, |
| 123 | let noteID = userInfo["noteID"] as? NSManagedObjectID, |
| 124 | noteID == note.objectID, !note.isDeleted else { return } |
| 125 | |
| 126 | if let message = userInfo["message"] as? String, !message.isEmpty { |
| 127 | summaryGenerationError = message |
| 128 | } else if let error = userInfo["error"] as? Error { |
| 129 | summaryGenerationError = error.localizedDescription |
| 130 | } else { |
| 131 | summaryGenerationError = "Couldn’t generate a summary right now. Please try again." |
| 132 | } |
| 133 | // Failed local generation leaves the previously saved summary intact. |
| 134 | editableSummary = note.summary ?? "" |
| 135 | refreshID = UUID() |
| 136 | ignoreNoteChanges = false |
| 137 | } |
| 138 | } |