Fix immediate deletion breaking transcription process

This commit is contained in:
Beingpax 2025-10-10 22:10:32 +05:45
parent 40f955db36
commit 07e8c0dddf
3 changed files with 10 additions and 13 deletions

View File

@ -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")
}

View File

@ -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)")
}

View File

@ -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 }