From 07e8c0dddfa8492a9b3b906d8734406b861cd678 Mon Sep 17 00:00:00 2001 From: Beingpax Date: Fri, 10 Oct 2025 22:10:32 +0545 Subject: [PATCH] Fix immediate deletion breaking transcription process --- VoiceInk/Notifications/AppNotifications.swift | 1 + .../TranscriptionAutoCleanupService.swift | 17 +++++------------ VoiceInk/Whisper/WhisperState.swift | 5 ++++- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/VoiceInk/Notifications/AppNotifications.swift b/VoiceInk/Notifications/AppNotifications.swift index c23e055..f778b03 100644 --- a/VoiceInk/Notifications/AppNotifications.swift +++ b/VoiceInk/Notifications/AppNotifications.swift @@ -13,6 +13,7 @@ extension Notification.Name { static let promptSelectionChanged = Notification.Name("promptSelectionChanged") static let powerModeConfigurationApplied = Notification.Name("powerModeConfigurationApplied") static let transcriptionCreated = Notification.Name("transcriptionCreated") + static let transcriptionCompleted = Notification.Name("transcriptionCompleted") static let enhancementToggleChanged = Notification.Name("enhancementToggleChanged") static let openFileForTranscription = Notification.Name("openFileForTranscription") } diff --git a/VoiceInk/Services/TranscriptionAutoCleanupService.swift b/VoiceInk/Services/TranscriptionAutoCleanupService.swift index 65b4cc7..54a37ed 100644 --- a/VoiceInk/Services/TranscriptionAutoCleanupService.swift +++ b/VoiceInk/Services/TranscriptionAutoCleanupService.swift @@ -20,8 +20,8 @@ class TranscriptionAutoCleanupService { NotificationCenter.default.addObserver( self, - selector: #selector(handleTranscriptionCreated(_:)), - name: .transcriptionCreated, + selector: #selector(handleTranscriptionCompleted(_:)), + name: .transcriptionCompleted, object: nil ) @@ -35,21 +35,20 @@ class TranscriptionAutoCleanupService { } func stopMonitoring() { - NotificationCenter.default.removeObserver(self, name: .transcriptionCreated, object: nil) - + NotificationCenter.default.removeObserver(self, name: .transcriptionCompleted, object: nil) } func runManualCleanup(modelContext: ModelContext) async { await sweepOldTranscriptions(modelContext: modelContext) } - @objc private func handleTranscriptionCreated(_ notification: Notification) { + @objc private func handleTranscriptionCompleted(_ notification: Notification) { let isEnabled = UserDefaults.standard.bool(forKey: keyIsEnabled) guard isEnabled else { return } let minutes = UserDefaults.standard.integer(forKey: keyRetentionMinutes) if minutes > 0 { - // Trigger a sweep based on the retention window whenever a new item is added + // Trigger a sweep based on the retention window if let modelContext = self.modelContext { Task { [weak self] in guard let self = self else { return } @@ -65,25 +64,19 @@ class TranscriptionAutoCleanupService { return } - - - // Delete the audio file if it exists if let urlString = transcription.audioFileURL, let url = URL(string: urlString) { do { try FileManager.default.removeItem(at: url) - } catch { logger.error("Failed to delete audio file: \(error.localizedDescription)") } } - // Delete the transcription from the database modelContext.delete(transcription) do { try modelContext.save() - } catch { logger.error("Failed to save after transcription deletion: \(error.localizedDescription)") } diff --git a/VoiceInk/Whisper/WhisperState.swift b/VoiceInk/Whisper/WhisperState.swift index f6c9558..906e4de 100644 --- a/VoiceInk/Whisper/WhisperState.swift +++ b/VoiceInk/Whisper/WhisperState.swift @@ -369,7 +369,10 @@ class WhisperState: NSObject, ObservableObject { // --- Finalize and save --- try? modelContext.save() - NotificationCenter.default.post(name: .transcriptionCreated, object: transcription) + + if transcription.transcriptionStatus == TranscriptionStatus.completed.rawValue { + NotificationCenter.default.post(name: .transcriptionCompleted, object: transcription) + } if await checkCancellationAndCleanup() { return }