From f13e559536a5f48fbabe4695fb574267bbf35e91 Mon Sep 17 00:00:00 2001 From: Beingpax Date: Sat, 23 Aug 2025 17:16:17 +0545 Subject: [PATCH] Add support for retrying the last transcription from menu bar --- .../Services/LastTranscriptionService.swift | 44 ++++++++++++++++++- VoiceInk/Views/MenuBarView.swift | 4 ++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/VoiceInk/Services/LastTranscriptionService.swift b/VoiceInk/Services/LastTranscriptionService.swift index d6ef0cf..4ef46fa 100644 --- a/VoiceInk/Services/LastTranscriptionService.swift +++ b/VoiceInk/Services/LastTranscriptionService.swift @@ -29,7 +29,7 @@ class LastTranscriptionService: ObservableObject { return } - let success = ClipboardManager.copyToClipboard(lastTranscription.text) + let success = ClipboardManager.copyToClipboard(lastTranscription.enhancedText?.isEmpty == false ? lastTranscription.enhancedText! : lastTranscription.text) Task { @MainActor in if success { @@ -69,6 +69,46 @@ class LastTranscriptionService: ObservableObject { DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) { CursorPaster.pasteAtCursor(textToPaste + " ") } - + } + + static func retryLastTranscription(from modelContext: ModelContext, whisperState: WhisperState) { + Task { @MainActor in + guard let lastTranscription = getLastTranscription(from: modelContext), + let audioURLString = lastTranscription.audioFileURL, + let audioURL = URL(string: audioURLString), + FileManager.default.fileExists(atPath: audioURL.path) else { + NotificationManager.shared.showNotification( + title: "Cannot retry: Audio file not found", + type: .error + ) + return + } + + guard let currentModel = whisperState.currentTranscriptionModel else { + NotificationManager.shared.showNotification( + title: "No transcription model selected", + type: .error + ) + return + } + + let transcriptionService = AudioTranscriptionService(modelContext: modelContext, whisperState: whisperState) + do { + let newTranscription = try await transcriptionService.retranscribeAudio(from: audioURL, using: currentModel) + + let textToCopy = newTranscription.enhancedText?.isEmpty == false ? newTranscription.enhancedText! : newTranscription.text + ClipboardManager.copyToClipboard(textToCopy) + + NotificationManager.shared.showNotification( + title: "Copied to clipboard", + type: .success + ) + } catch { + NotificationManager.shared.showNotification( + title: "Retry failed: \(error.localizedDescription)", + type: .error + ) + } + } } } \ No newline at end of file diff --git a/VoiceInk/Views/MenuBarView.swift b/VoiceInk/Views/MenuBarView.swift index acf0105..38c26fc 100644 --- a/VoiceInk/Views/MenuBarView.swift +++ b/VoiceInk/Views/MenuBarView.swift @@ -147,6 +147,10 @@ struct MenuBarView: View { Divider() + Button("Retry Last Transcription") { + LastTranscriptionService.retryLastTranscription(from: whisperState.modelContext, whisperState: whisperState) + } + Button("Copy Last Transcription") { LastTranscriptionService.copyLastTranscription(from: whisperState.modelContext) }