From 7ec49264e6fffd86b46b6d14103c52eb266aae81 Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Sat, 4 Jul 2026 15:41:45 +0200 Subject: [PATCH] Implemented the pre-installation of speech model assets for the selected locale for the ContentViewModel view model in the Sample app target. --- .../View Models/ContentViewModel.swift | 26 +++++++++++++++++++ Apps/Attendi/Views/ContentView.swift | 3 +++ 2 files changed, 29 insertions(+) diff --git a/Apps/Attendi/View Models/ContentViewModel.swift b/Apps/Attendi/View Models/ContentViewModel.swift index 773f73f..69f5c22 100644 --- a/Apps/Attendi/View Models/ContentViewModel.swift +++ b/Apps/Attendi/View Models/ContentViewModel.swift @@ -62,6 +62,32 @@ extension ContentView { } } + /// Preinstalls the speech model assets for the supported equivalent of ``locale``, so the first transcription in that locale + /// does not have to download them mid-processing. + /// + /// Failures are ignored on purpose: the transcribing service installs any missing assets itself as a fallback when a + /// transcription starts. + func preinstallAssets() async { + guard let locale = await SpeechTranscriber.supportedLocale( + equivalentTo: locale + ) else { + return + } + + let transcriber = SpeechTranscriber( + locale: locale, + preset: .transcription + ) + + guard let request = try? await AssetInventory.assetInstallationRequest( + supporting: [transcriber] + ) else { + return + } + + try? await request.downloadAndInstall() + } + /// Returns the localized name of a locale for the locale picker. /// /// - Parameter locale: The locale to name. diff --git a/Apps/Attendi/Views/ContentView.swift b/Apps/Attendi/Views/ContentView.swift index b4dca85..d5c881d 100644 --- a/Apps/Attendi/Views/ContentView.swift +++ b/Apps/Attendi/Views/ContentView.swift @@ -61,6 +61,9 @@ struct ContentView: View { .task { await model.load() } + .task(id: model.locale) { + await model.preinstallAssets() + } } .sheet(item: $model.transcription) { transcription in NavigationStack {