Add busy state for robust recorder cleanup

This commit is contained in:
Beingpax 2025-07-11 09:05:51 +05:45
parent 720d6ae8c8
commit 57f47af062
4 changed files with 17 additions and 23 deletions

View File

@ -28,7 +28,7 @@ class HotkeyManager: ObservableObject {
// MARK: - Helper Properties // MARK: - Helper Properties
private var canProcessHotkeyAction: Bool { private var canProcessHotkeyAction: Bool {
whisperState.recordingState != .transcribing && whisperState.recordingState != .enhancing whisperState.recordingState != .transcribing && whisperState.recordingState != .enhancing && whisperState.recordingState != .busy
} }
// NSEvent monitoring for modifier keys // NSEvent monitoring for modifier keys

View File

@ -153,7 +153,7 @@ class Recorder: ObservableObject {
} }
deviceManager.isRecordingActive = false deviceManager.isRecordingActive = false
} }
private func updateAudioMeter() { private func updateAudioMeter() {
guard let recorder = recorder else { return } guard let recorder = recorder else { return }
recorder.updateMeters() recorder.updateMeters()

View File

@ -53,23 +53,32 @@ extension WhisperState {
} }
func dismissMiniRecorder() async { func dismissMiniRecorder() async {
if recordingState == .busy { return }
let wasRecording = recordingState == .recording
logger.notice("📱 Dismissing \(self.recorderType) recorder") logger.notice("📱 Dismissing \(self.recorderType) recorder")
await MainActor.run { await MainActor.run {
self.recordingState = .busy
NotificationManager.shared.dismissNotification() NotificationManager.shared.dismissNotification()
} }
if recordingState == .recording { if wasRecording {
await recorder.stopRecording() await recorder.stopRecording()
} }
// Hide recorder panel first before doing anything else
hideRecorderPanel() hideRecorderPanel()
await MainActor.run { await MainActor.run {
recordingState = .idle
isMiniRecorderVisible = false isMiniRecorderVisible = false
} }
await cleanupModelResources()
await MainActor.run {
recordingState = .idle
}
} }
func cancelRecording() async { func cancelRecording() async {

View File

@ -12,6 +12,7 @@ enum RecordingState: Equatable {
case recording case recording
case transcribing case transcribing
case enhancing case enhancing
case busy
} }
@MainActor @MainActor
@ -349,9 +350,6 @@ class WhisperState: NSObject, ObservableObject {
} }
await self.dismissMiniRecorder() await self.dismissMiniRecorder()
Task.detached(priority: .background) {
await self.cleanupModelResources()
}
} catch { } catch {
do { do {
@ -377,17 +375,7 @@ class WhisperState: NSObject, ObservableObject {
logger.error("❌ Could not create a record for the failed transcription: \(error.localizedDescription)") logger.error("❌ Could not create a record for the failed transcription: \(error.localizedDescription)")
} }
await MainActor.run {
NotificationManager.shared.showNotification(
title: "Transcription Failed",
type: .error
)
}
await self.dismissMiniRecorder() await self.dismissMiniRecorder()
Task.detached(priority: .background) {
await self.cleanupModelResources()
}
} }
} }
@ -397,7 +385,7 @@ class WhisperState: NSObject, ObservableObject {
private func checkCancellationAndCleanup() async -> Bool { private func checkCancellationAndCleanup() async -> Bool {
if shouldCancelRecording { if shouldCancelRecording {
await cleanupAndDismiss() await dismissMiniRecorder()
return true return true
} }
return false return false
@ -405,9 +393,6 @@ class WhisperState: NSObject, ObservableObject {
private func cleanupAndDismiss() async { private func cleanupAndDismiss() async {
await dismissMiniRecorder() await dismissMiniRecorder()
Task.detached(priority: .background) {
await self.cleanupModelResources()
}
} }
} }