2026-07-04 10:32:59 +02:00
|
|
|
import Foundation
|
2026-07-05 13:43:49 +02:00
|
|
|
import Notifying
|
2026-07-04 10:32:59 +02:00
|
|
|
import Observation
|
|
|
|
|
import Recording
|
|
|
|
|
|
|
|
|
|
extension ContentView {
|
|
|
|
|
|
|
|
|
|
/// The observable model that drives ``ContentView``.
|
|
|
|
|
///
|
2026-07-05 22:06:45 +02:00
|
|
|
/// The model owns the recording, transcribing, and reporting services attached to the recording feature, along with the ``locale`` the feature
|
2026-07-05 21:04:29 +02:00
|
|
|
/// transcribes in, picked in the view's toolbar from the supported ``locales`` that ``load(identifier:)`` fetches — restoring the
|
|
|
|
|
/// locale the view persisted across launches, when one exists. It also holds the transcription
|
2026-07-04 12:59:18 +02:00
|
|
|
/// currently presented in the view's modal sheet: ``received(_:)`` presents the transcription of a processed recording, and
|
2026-07-04 10:32:59 +02:00
|
|
|
/// ``dismissed()`` clears it when the sheet closes.
|
2026-07-05 11:51:39 +02:00
|
|
|
///
|
2026-07-05 13:43:49 +02:00
|
|
|
/// Every picked locale kicks off ``preinstall()``, a cancellable task that preinstalls the locale's speech model assets through the
|
|
|
|
|
/// preinstalling service — cancelling any download still in flight for a previously picked locale. The service's started, cancelled, and
|
|
|
|
|
/// failed download events are mapped to the transient, self-dismissing notifications of the ``notifier``.
|
2026-07-05 16:29:28 +02:00
|
|
|
///
|
|
|
|
|
/// Every collaborator is injected at initialization behind its protocol, defaulting to the on-device services: the app runs the model
|
|
|
|
|
/// against the real backends, while its unit tests and previews attach mocks and dummies without touching this type.
|
2026-07-04 10:32:59 +02:00
|
|
|
@MainActor
|
|
|
|
|
@Observable
|
|
|
|
|
final class Model {
|
|
|
|
|
|
|
|
|
|
// MARK: Properties
|
|
|
|
|
|
2026-07-04 12:11:24 +02:00
|
|
|
/// The locale of the spoken language to transcribe.
|
2026-07-04 12:59:18 +02:00
|
|
|
var locale: Locale
|
2026-07-05 13:43:49 +02:00
|
|
|
|
2026-07-04 10:32:59 +02:00
|
|
|
/// The transcription currently presented in the modal sheet, or `nil` when none is shown.
|
|
|
|
|
var transcription: Transcription?
|
|
|
|
|
|
2026-07-05 21:04:29 +02:00
|
|
|
/// The locales the transcriber supports, sorted by their localized names, or empty while ``load(identifier:)`` has not finished yet.
|
2026-07-04 12:59:18 +02:00
|
|
|
private(set) var locales: [Locale]
|
|
|
|
|
|
2026-07-05 15:45:09 +02:00
|
|
|
/// The service that captures the audio from a microphone.
|
2026-07-04 10:32:59 +02:00
|
|
|
@ObservationIgnored
|
2026-07-05 15:45:09 +02:00
|
|
|
let capturer: any Capturing
|
2026-07-04 10:32:59 +02:00
|
|
|
|
2026-07-06 23:05:24 +02:00
|
|
|
/// Whether the reporting surfaces left over from an earlier run have been discarded, so the discard runs only once per launch.
|
|
|
|
|
@ObservationIgnored
|
|
|
|
|
private var didReset = false
|
|
|
|
|
|
2026-07-05 11:51:39 +02:00
|
|
|
/// The locale whose speech model assets are preinstalled or being preinstalled, or `nil` when none are.
|
|
|
|
|
@ObservationIgnored
|
|
|
|
|
private var localePreinstalling: Locale?
|
|
|
|
|
|
2026-07-05 13:43:49 +02:00
|
|
|
/// The notifier that owns the transient in-app notifications reporting the download events.
|
2026-07-04 15:58:32 +02:00
|
|
|
@ObservationIgnored
|
2026-07-05 13:43:49 +02:00
|
|
|
let notifier: Notifier
|
|
|
|
|
|
|
|
|
|
/// The service that preinstalls the speech model assets of a picked locale.
|
|
|
|
|
@ObservationIgnored
|
2026-07-05 15:45:09 +02:00
|
|
|
private let preinstaller: any Preinstalling
|
2026-07-05 13:43:49 +02:00
|
|
|
|
2026-07-05 22:06:45 +02:00
|
|
|
/// The service that reports the lifecycle of the recording flow outside the app's UI.
|
|
|
|
|
@ObservationIgnored
|
|
|
|
|
let reporter: any Reporting
|
|
|
|
|
|
2026-07-05 13:43:49 +02:00
|
|
|
/// The task that listens to the events emitted by the preinstalling service, or `nil` until the first preinstallation starts.
|
|
|
|
|
@ObservationIgnored
|
|
|
|
|
private var taskEvents: Task<Void, Never>?
|
2026-07-04 15:58:32 +02:00
|
|
|
|
2026-07-05 11:51:39 +02:00
|
|
|
/// The task that preinstalls the speech model assets of the last picked locale, or `nil` until the first locale is picked.
|
|
|
|
|
@ObservationIgnored
|
|
|
|
|
private var taskPreinstall: Task<Void, Never>?
|
|
|
|
|
|
2026-07-05 15:45:09 +02:00
|
|
|
/// The service that transcribes the recorded audio into text.
|
2026-07-04 10:32:59 +02:00
|
|
|
@ObservationIgnored
|
2026-07-05 15:45:09 +02:00
|
|
|
let transcriber: any Transcribing
|
2026-07-05 13:43:49 +02:00
|
|
|
|
2026-07-04 12:59:18 +02:00
|
|
|
// MARK: Initializers
|
|
|
|
|
|
2026-07-05 15:45:09 +02:00
|
|
|
/// Creates a model set to the user's current locale, with no supported locales loaded yet, attached to the given services
|
|
|
|
|
/// and notifier.
|
|
|
|
|
///
|
|
|
|
|
/// - Parameters:
|
|
|
|
|
/// - capturer: The service that captures the audio from a microphone. Defaults to the on-device ``AudioCapturing``.
|
|
|
|
|
/// - preinstaller: The service that preinstalls the speech model assets of a picked locale. Defaults to ``AssetPreinstalling``.
|
2026-07-05 22:06:45 +02:00
|
|
|
/// - reporter: The service that reports the lifecycle of the recording flow outside the app's UI. Defaults to
|
|
|
|
|
/// ``ActivityReporting``, which surfaces the flow in a Live Activity on the platforms that support it.
|
2026-07-05 15:45:09 +02:00
|
|
|
/// - transcriber: The service that transcribes the recorded audio into text. Defaults to the on-device ``AudioTranscribing``.
|
|
|
|
|
/// - notifier: The notifier that owns the transient in-app notifications. Defaults to a notifier with its standard dismissal delay.
|
|
|
|
|
init(
|
|
|
|
|
capturer: any Capturing = AudioCapturing(),
|
|
|
|
|
preinstaller: any Preinstalling = AssetPreinstalling(),
|
2026-07-05 22:06:45 +02:00
|
|
|
reporter: any Reporting = ActivityReporting(),
|
2026-07-05 15:45:09 +02:00
|
|
|
transcriber: any Transcribing = AudioTranscribing(),
|
|
|
|
|
notifier: Notifier = .init(),
|
|
|
|
|
) {
|
2026-07-04 12:59:18 +02:00
|
|
|
self.locale = .current
|
|
|
|
|
self.locales = []
|
2026-07-05 15:45:09 +02:00
|
|
|
self.capturer = capturer
|
|
|
|
|
self.notifier = notifier
|
|
|
|
|
self.preinstaller = preinstaller
|
2026-07-05 22:06:45 +02:00
|
|
|
self.reporter = reporter
|
2026-07-05 15:45:09 +02:00
|
|
|
self.transcriber = transcriber
|
2026-07-04 12:59:18 +02:00
|
|
|
}
|
2026-07-04 10:32:59 +02:00
|
|
|
|
|
|
|
|
// MARK: Methods
|
|
|
|
|
|
2026-07-06 23:05:24 +02:00
|
|
|
/// Discards, once per launch, any reporting surface left over from an earlier run — a Live Activity the app was killed before it
|
|
|
|
|
/// could end, for example — since a fresh launch has no recording it could still belong to. It then loads the locales the
|
|
|
|
|
/// transcriber supports — resolved through the preinstalling service — into ``locales``, sorted by their
|
2026-07-05 21:04:29 +02:00
|
|
|
/// localized names, and aligns ``locale`` with the supported equivalent of its current value — restored first from the given
|
|
|
|
|
/// persisted identifier when one exists, and falling back to the supported equivalent of a default locale when no equivalent
|
|
|
|
|
/// exists — so the locale picker starts with a valid selection. It then kicks off the preinstallation of the aligned locale's
|
|
|
|
|
/// speech model assets.
|
|
|
|
|
///
|
|
|
|
|
/// - Parameter identifier: The identifier of the locale persisted across launches, or an empty string when none has been
|
|
|
|
|
/// persisted yet.
|
|
|
|
|
func load(
|
|
|
|
|
identifier: String
|
|
|
|
|
) async {
|
2026-07-06 23:05:24 +02:00
|
|
|
if !didReset {
|
|
|
|
|
didReset = true
|
|
|
|
|
|
|
|
|
|
await reporter.reset()
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-05 21:04:29 +02:00
|
|
|
if !identifier.isEmpty {
|
|
|
|
|
locale = .init(identifier: identifier)
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-05 15:45:09 +02:00
|
|
|
locales =
|
|
|
|
|
await preinstaller
|
2026-07-05 13:43:49 +02:00
|
|
|
.supportedLocales()
|
2026-07-05 11:51:39 +02:00
|
|
|
.sorted {
|
|
|
|
|
name(for: $0).localizedStandardCompare(name(for: $1)) == .orderedAscending
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-05 15:45:09 +02:00
|
|
|
locale =
|
|
|
|
|
if let equivalent = await preinstaller.supportedLocale(equivalentTo: locale) {
|
|
|
|
|
equivalent
|
|
|
|
|
}
|
|
|
|
|
else if let fallback = await preinstaller.supportedLocale(equivalentTo: .byDefault) {
|
|
|
|
|
fallback
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
.current
|
|
|
|
|
}
|
2026-07-04 14:58:14 +02:00
|
|
|
|
2026-07-05 11:51:39 +02:00
|
|
|
preinstall()
|
2026-07-04 12:59:18 +02:00
|
|
|
}
|
2026-07-05 11:51:39 +02:00
|
|
|
|
|
|
|
|
/// Preinstalls the speech model assets for the current ``locale`` in a cancellable task, cancelling the preinstallation of any
|
2026-07-05 13:43:49 +02:00
|
|
|
/// previously picked locale still in flight — which the ``notifier`` reports as a cancelled download. Does nothing when the
|
|
|
|
|
/// current locale is the one already preinstalled or being preinstalled.
|
2026-07-05 11:51:39 +02:00
|
|
|
func preinstall() {
|
|
|
|
|
guard localePreinstalling != locale else {
|
2026-07-04 15:41:45 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-05 11:51:39 +02:00
|
|
|
taskPreinstall?.cancel()
|
2026-07-04 15:58:32 +02:00
|
|
|
|
2026-07-05 11:51:39 +02:00
|
|
|
localePreinstalling = locale
|
2026-07-04 15:41:45 +02:00
|
|
|
|
2026-07-05 13:43:49 +02:00
|
|
|
listenToEvents()
|
|
|
|
|
|
2026-07-05 11:51:39 +02:00
|
|
|
taskPreinstall = Task { [locale] in
|
2026-07-05 13:43:49 +02:00
|
|
|
await preinstaller.preinstall(for: locale)
|
2026-07-04 15:58:32 +02:00
|
|
|
}
|
2026-07-04 15:41:45 +02:00
|
|
|
}
|
|
|
|
|
|
2026-07-04 12:59:18 +02:00
|
|
|
/// 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 {
|
2026-07-04 14:58:14 +02:00
|
|
|
Locale
|
|
|
|
|
.current
|
2026-07-04 12:59:18 +02:00
|
|
|
.localizedString(
|
|
|
|
|
forIdentifier: locale.identifier
|
|
|
|
|
)?.localizedCapitalized
|
2026-07-05 15:45:09 +02:00
|
|
|
?? locale.identifier
|
2026-07-04 12:59:18 +02:00
|
|
|
}
|
|
|
|
|
|
2026-07-04 12:11:24 +02:00
|
|
|
/// Handles the transcription of a processed recording, presenting it in the modal sheet.
|
2026-07-04 10:32:59 +02:00
|
|
|
///
|
2026-07-04 12:11:24 +02:00
|
|
|
/// - Parameter transcription: The transcription of the processed recording.
|
2026-07-04 10:32:59 +02:00
|
|
|
func received(
|
2026-07-04 12:11:24 +02:00
|
|
|
_ transcription: Transcription
|
2026-07-04 10:32:59 +02:00
|
|
|
) {
|
2026-07-04 12:11:24 +02:00
|
|
|
self.transcription = transcription
|
2026-07-04 10:32:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Handles the dismissal of the modal sheet.
|
|
|
|
|
func dismissed() {
|
|
|
|
|
transcription = nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2026-07-04 14:58:14 +02:00
|
|
|
|
2026-07-05 11:51:39 +02:00
|
|
|
// MARK: - Helpers
|
|
|
|
|
|
|
|
|
|
private extension ContentView.Model {
|
|
|
|
|
|
|
|
|
|
// MARK: Methods
|
|
|
|
|
|
2026-07-05 13:43:49 +02:00
|
|
|
/// Starts listening to the events emitted by the preinstalling service, unless already listening: every started, cancelled, and
|
|
|
|
|
/// failed download is posted to the ``ContentView/Model/notifier``, and a cancellation or failure clears the preinstalled locale
|
2026-07-05 15:45:09 +02:00
|
|
|
/// — only when it is still the affected one, so a newer preinstallation is never forgotten — letting a re-pick retry.
|
2026-07-05 13:43:49 +02:00
|
|
|
func listenToEvents() {
|
|
|
|
|
guard taskEvents == nil else {
|
2026-07-05 11:51:39 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-05 13:43:49 +02:00
|
|
|
taskEvents = Task { [weak self, events = preinstaller.events] in
|
|
|
|
|
for await event in events {
|
|
|
|
|
guard let self else {
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-07-05 11:51:39 +02:00
|
|
|
|
2026-07-05 13:43:49 +02:00
|
|
|
switch event {
|
2026-07-05 15:45:09 +02:00
|
|
|
case .cancelled(let locale):
|
|
|
|
|
if localePreinstalling == locale {
|
|
|
|
|
localePreinstalling = nil
|
|
|
|
|
}
|
2026-07-05 11:51:39 +02:00
|
|
|
|
2026-07-05 13:43:49 +02:00
|
|
|
notifier.post(
|
|
|
|
|
.warning,
|
|
|
|
|
message: String(localized: .viewRecordingNotificationDownloadCancelled(name(for: locale))),
|
|
|
|
|
symbol: Constant.Symbol.cancelled
|
|
|
|
|
)
|
2026-07-05 15:45:09 +02:00
|
|
|
case .failed(let locale):
|
|
|
|
|
if localePreinstalling == locale {
|
|
|
|
|
localePreinstalling = nil
|
|
|
|
|
}
|
2026-07-05 11:51:39 +02:00
|
|
|
|
2026-07-05 13:43:49 +02:00
|
|
|
notifier.post(
|
|
|
|
|
.error,
|
|
|
|
|
message: String(localized: .viewRecordingNotificationDownloadFailed(name(for: locale))),
|
|
|
|
|
symbol: Constant.Symbol.failed
|
|
|
|
|
)
|
2026-07-05 15:45:09 +02:00
|
|
|
case .started(let locale):
|
2026-07-05 13:43:49 +02:00
|
|
|
notifier.post(
|
|
|
|
|
.info,
|
|
|
|
|
message: String(localized: .viewRecordingNotificationDownloadStarted(name(for: locale))),
|
|
|
|
|
symbol: Constant.Symbol.started
|
|
|
|
|
)
|
|
|
|
|
}
|
2026-07-05 11:51:39 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-04 14:58:14 +02:00
|
|
|
// MARK: - Constants
|
|
|
|
|
|
2026-07-05 11:51:39 +02:00
|
|
|
/// The constant values used across the model.
|
|
|
|
|
private enum Constant {
|
2026-07-05 13:43:49 +02:00
|
|
|
/// The symbol constants.
|
|
|
|
|
enum Symbol {
|
|
|
|
|
/// The system symbol of a cancelled download.
|
|
|
|
|
static let cancelled = "xmark.circle"
|
|
|
|
|
/// The system symbol of a failed download.
|
|
|
|
|
static let failed = "exclamationmark.triangle"
|
|
|
|
|
/// The system symbol of a started download.
|
|
|
|
|
static let started = "arrow.down.circle"
|
2026-07-05 11:51:39 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-04 14:58:14 +02:00
|
|
|
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")
|
|
|
|
|
}
|