@@ -20,6 +20,7 @@ class AppState: ObservableObject {
2020
2121 private var isSetUp = false
2222 private var textCorrector : Any ?
23+ private var pendingModelSelection : ModelSize ?
2324
2425 let hotkeyManager = HotkeyManager ( )
2526 let audioRecorder = AudioRecorder ( )
@@ -51,7 +52,7 @@ class AppState: ObservableObject {
5152 // Load model
5253 do {
5354 let modelPath = modelManager. localModelPath ( for: selectedModel)
54- try transcriber. loadModel ( at: modelPath)
55+ try await transcriber. loadModel ( at: modelPath)
5556 statusMessage = " Ready "
5657 } catch {
5758 statusMessage = " Failed to load model: \( error. localizedDescription) "
@@ -153,30 +154,46 @@ class AppState: ObservableObject {
153154 isTranscribing = false
154155 }
155156
156- func switchModel( to model: ModelSize ) async {
157- guard !isSwitchingModel else { return }
157+ func switchModel( to targetModel: ModelSize ) async {
158+ if isSwitchingModel {
159+ pendingModelSelection = targetModel
160+ return
161+ }
162+
158163 isSwitchingModel = true
159- defer { isSwitchingModel = false }
164+ let previousModel = selectedModel
165+ if previousModel != targetModel {
166+ selectedModel = targetModel
167+ }
168+ defer {
169+ isSwitchingModel = false
170+ if let pending = pendingModelSelection {
171+ pendingModelSelection = nil
172+ if pending != selectedModel {
173+ Task { await self . switchModel ( to: pending) }
174+ }
175+ }
176+ }
160177
161178 transcriber. unloadModel ( )
162179
163- if !modelManager. isModelDownloaded ( model ) || !modelManager. isCoreMLDownloaded ( model ) {
164- statusMessage = " Downloading \( model . displayName) ... "
180+ if !modelManager. isModelDownloaded ( targetModel ) || !modelManager. isCoreMLDownloaded ( targetModel ) {
181+ statusMessage = " Downloading \( targetModel . displayName) ... "
165182 do {
166- try await modelManager. downloadModel ( model )
183+ try await modelManager. downloadModel ( targetModel )
167184 } catch {
168185 statusMessage = " Download failed: \( error. localizedDescription) "
169- // Revert UI selection to the model that is still loaded (none now — stay on previous)
186+ selectedModel = previousModel
170187 return
171188 }
172189 }
173190
174191 do {
175- try transcriber. loadModel ( at: modelManager. localModelPath ( for: model) )
176- selectedModel = model // Commit only after successful load
192+ try await transcriber. loadModel ( at: modelManager. localModelPath ( for: targetModel) )
177193 statusMessage = " Ready "
178194 } catch {
179195 statusMessage = " Failed to load model: \( error. localizedDescription) "
196+ selectedModel = previousModel
180197 }
181198 }
182199
0 commit comments