130 lines
4.8 KiB
Swift
130 lines
4.8 KiB
Swift
import Foundation
|
|
import Observation
|
|
import Recording
|
|
import Speech
|
|
|
|
extension ContentView {
|
|
|
|
/// The observable model that drives ``ContentView``.
|
|
///
|
|
/// The model owns the recording and transcribing services attached to the recording feature, along with the ``locale`` the feature
|
|
/// transcribes in, picked in the view's toolbar from the supported ``locales`` that ``load()`` fetches. It also holds the transcription
|
|
/// currently presented in the view's modal sheet: ``received(_:)`` presents the transcription of a processed recording, and
|
|
/// ``dismissed()`` clears it when the sheet closes.
|
|
@MainActor
|
|
@Observable
|
|
final class Model {
|
|
|
|
// MARK: Properties
|
|
|
|
/// The locale of the spoken language to transcribe.
|
|
var locale: Locale
|
|
|
|
/// The transcription currently presented in the modal sheet, or `nil` when none is shown.
|
|
var transcription: Transcription?
|
|
|
|
/// The locales the transcriber supports, sorted by their localized names, or empty while ``load()`` has not finished yet.
|
|
private(set) var locales: [Locale]
|
|
|
|
/// The service that captures the audio from the device's microphone.
|
|
@ObservationIgnored
|
|
let capturer: AudioCapturing
|
|
|
|
/// The service that transcribes the recorded audio into text on device.
|
|
@ObservationIgnored
|
|
let transcriber: AudioTranscribing
|
|
|
|
// MARK: Initializers
|
|
|
|
/// Creates a model set to the user's current locale, with no supported locales loaded yet, attached to the on-device recording
|
|
/// and transcribing services.
|
|
init() {
|
|
self.locale = .current
|
|
self.locales = []
|
|
self.capturer = .init()
|
|
self.transcriber = .init()
|
|
}
|
|
|
|
// MARK: Methods
|
|
|
|
/// Loads the locales the transcriber supports into ``locales``, sorted by their localized names, and aligns ``locale`` with the
|
|
/// supported equivalent of its current value — falling back to the supported equivalent of a default locale when none exists —
|
|
/// so the locale picker starts with a valid selection.
|
|
func load() async {
|
|
locales = await SpeechTranscriber.supportedLocales.sorted {
|
|
name(for: $0).localizedStandardCompare(name(for: $1)) == .orderedAscending
|
|
}
|
|
|
|
if let equivalent = await SpeechTranscriber.supportedLocale(equivalentTo: locale) {
|
|
locale = equivalent
|
|
} else if let fallback = await SpeechTranscriber.supportedLocale(equivalentTo: .byDefault) {
|
|
locale = fallback
|
|
}
|
|
}
|
|
|
|
/// 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.
|
|
/// - Returns: The name of the locale in the user's current locale, or its identifier when no name is available.
|
|
func name(
|
|
for locale: Locale
|
|
) -> String {
|
|
Locale
|
|
.current
|
|
.localizedString(
|
|
forIdentifier: locale.identifier
|
|
)?.localizedCapitalized
|
|
?? locale.identifier
|
|
}
|
|
|
|
/// Handles the transcription of a processed recording, presenting it in the modal sheet.
|
|
///
|
|
/// - Parameter transcription: The transcription of the processed recording.
|
|
func received(
|
|
_ transcription: Transcription
|
|
) {
|
|
self.transcription = transcription
|
|
}
|
|
|
|
/// Handles the dismissal of the modal sheet.
|
|
func dismissed() {
|
|
transcription = nil
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - Constants
|
|
|
|
private extension Locale {
|
|
/// The locale to fall back to when the transcriber supports no equivalent of the user's current locale.
|
|
static let byDefault: Locale = .init(identifier: "en_US")
|
|
}
|