Integrated the Notifier and the AssetPreinstalling to the ContentView view model in the Sample app target.

This commit is contained in:
2026-07-05 13:43:49 +02:00
parent d22a8dda13
commit ee212cb6e3
3 changed files with 90 additions and 123 deletions
+4 -2
View File
@@ -2,8 +2,10 @@ import SwiftUI
/// The entry point of the Attendi sample app.
///
/// The app showcases the Recording feature of the `Features` package, attached to its real audio services: it records audio from the
/// device's microphone, transcribes the recording into text on device, and presents the result in a modal sheet.
/// The app is a thin shell over the `Features` package. It showcases the Recording feature attached to its real audio services
/// recording audio from the device's microphone, transcribing it on device in a locale picked from a toolbar menu, and presenting
/// the result in a modal sheet while the speech model assets of every picked locale preinstall in the background, with their
/// download events reported through the Notifying feature's transient in-app notifications.
@main
struct AttendiApp: App {
+69 -107
View File
@@ -1,9 +1,7 @@
import Accessibility
import Foundation
import Notifying
import Observation
import OSLog
import Recording
import Speech
extension ContentView {
@@ -14,9 +12,9 @@ extension ContentView {
/// currently presented in the view's modal sheet: ``received(_:)`` presents the transcription of a processed recording, and
/// ``dismissed()`` clears it when the sheet closes.
///
/// Every picked locale kicks off ``preinstall()``, a cancellable task that preinstalls the locale's speech model assets cancelling
/// any download still in flight for a previously picked locale and reports the started, cancelled, and failed downloads through the
/// transient, self-dismissing ``notifications``.
/// 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``.
@MainActor
@Observable
final class Model {
@@ -25,16 +23,13 @@ extension ContentView {
/// 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 in-app notifications currently presented, each dismissing itself a few seconds after it was posted.
private(set) var notifications: [AppNotification] = []
/// The service that captures the audio from the device's microphone.
@ObservationIgnored
let capturer: AudioCapturing
@@ -43,12 +38,17 @@ extension ContentView {
@ObservationIgnored
private var localePreinstalling: Locale?
/// The logger that records the failures of the speech model asset management.
/// The notifier that owns the transient in-app notifications reporting the download events.
@ObservationIgnored
private let logger = Logger(
subsystem: Bundle.main.bundleIdentifier ?? "Attendi",
category: "ContentView.Model"
)
let notifier: Notifier
/// The service that preinstalls the speech model assets of a picked locale.
@ObservationIgnored
private let preinstaller: AssetPreinstalling
/// 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>?
/// The task that preinstalls the speech model assets of the last picked locale, or `nil` until the first locale is picked.
@ObservationIgnored
@@ -57,15 +57,17 @@ extension ContentView {
/// 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.
/// Creates a model set to the user's current locale, with no supported locales loaded yet, attached to the on-device recording,
/// transcribing, and preinstalling services, and to a notifier for the download events.
init() {
self.locale = .current
self.locales = []
self.capturer = .init()
self.notifier = .init()
self.preinstaller = .init()
self.transcriber = .init()
}
@@ -75,14 +77,15 @@ extension ContentView {
/// 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
locales = await preinstaller
.supportedLocales()
.sorted {
name(for: $0).localizedStandardCompare(name(for: $1)) == .orderedAscending
}
locale = if let equivalent = await localeInTranscriber(equivalentTo: locale) {
locale = if let equivalent = await preinstaller.supportedLocale(equivalentTo: locale) {
equivalent
} else if let fallback = await localeInTranscriber(equivalentTo: .byDefault) {
} else if let fallback = await preinstaller.supportedLocale(equivalentTo: .byDefault) {
fallback
} else {
.current
@@ -92,8 +95,8 @@ extension ContentView {
}
/// Preinstalls the speech model assets for the current ``locale`` in a cancellable task, cancelling the preinstallation of any
/// previously picked locale still in flight which posts its cancellation to ``notifications``. Does nothing when the current
/// locale is the one already preinstalled or being preinstalled.
/// 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.
func preinstall() {
guard localePreinstalling != locale else {
return
@@ -103,8 +106,10 @@ extension ContentView {
localePreinstalling = locale
listenToEvents()
taskPreinstall = Task { [locale] in
await preinstallAssets(for: locale)
await preinstaller.preinstall(for: locale)
}
}
@@ -147,91 +152,44 @@ private extension ContentView.Model {
// MARK: Methods
func localeInTranscriber(
equivalentTo locale: Locale
) async -> Locale? {
await SpeechTranscriber.supportedLocale(equivalentTo: locale)
}
/// Posts a notification for a download event of the given locale, announcing it to assistive technologies and scheduling its
/// automatic dismissal a few seconds later the only way a notification is dismissed.
///
/// - Parameters:
/// - event: The download event to notify.
/// - locale: The locale whose speech model the event is about.
func post(
_ event: AppNotification.Event,
for locale: Locale
) {
let notification = AppNotification(
event: event,
localeName: name(for: locale)
)
notifications.append(notification)
AccessibilityNotification.Announcement(
String(localized: notification.textMessage)
).post()
Task {
try? await Task.sleep(for: Constant.Delay.dismissal)
notifications.removeAll { $0.id == notification.id }
}
}
/// Preinstalls the speech model assets for the supported equivalent of the given locale, so the first transcription in that locale
/// does not have to download them mid-processing posting a notification when a download starts, is cancelled, or fails; a
/// download that finishes, or assets that are already installed, post none.
///
/// The locale reservations of any other locales are released beforehand: the app transcribes a single locale at a time, and
/// the system only permits a limited number of reservations exceeding it would make the installation request throw.
/// Failures are logged but not surfaced as errors: the transcribing service installs any missing assets itself as a fallback when
/// a transcription starts.
///
/// - Parameter target: The picked locale to preinstall the speech model assets for.
func preinstallAssets(
for target: Locale
) async {
guard let locale = await localeInTranscriber(equivalentTo: target) else {
/// 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
/// so picking it again retries.
func listenToEvents() {
guard taskEvents == nil else {
return
}
for reserved in await AssetInventory.reservedLocales where reserved != locale {
_ = await AssetInventory.release(reservedLocale: reserved)
}
taskEvents = Task { [weak self, events = preinstaller.events] in
for await event in events {
guard let self else {
return
}
let transcriber = SpeechTranscriber(
locale: locale,
preset: .transcription
)
switch event {
case let .cancelled(locale):
localePreinstalling = nil
do {
guard let request = try await AssetInventory.assetInstallationRequest(
supporting: [transcriber]
) else {
return
}
notifier.post(
.warning,
message: String(localized: .viewRecordingNotificationDownloadCancelled(name(for: locale))),
symbol: Constant.Symbol.cancelled
)
case let .failed(locale):
localePreinstalling = nil
post(.started, for: locale)
try await withTaskCancellationHandler {
try await request.downloadAndInstall()
} onCancel: { [progress = request.progress] in
progress.cancel()
}
} catch {
if localePreinstalling == target {
localePreinstalling = nil
}
if error is CancellationError || Task.isCancelled {
post(.cancelled, for: locale)
} else {
post(.failed, for: locale)
logger.error("The speech model assets for the \"\(locale.identifier, privacy: .public)\" locale failed to preinstall: \(String(describing: error), privacy: .public)")
notifier.post(
.error,
message: String(localized: .viewRecordingNotificationDownloadFailed(name(for: locale))),
symbol: Constant.Symbol.failed
)
case let .started(locale):
notifier.post(
.info,
message: String(localized: .viewRecordingNotificationDownloadStarted(name(for: locale))),
symbol: Constant.Symbol.started
)
}
}
}
}
@@ -242,10 +200,14 @@ private extension ContentView.Model {
/// The constant values used across the model.
private enum Constant {
/// The delay constants.
enum Delay {
/// The time a posted notification stays visible before its automatic dismissal.
static let dismissal: Duration = .seconds(4)
/// 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"
}
}
+17 -14
View File
@@ -1,3 +1,4 @@
import Notifying
import Recording
import SwiftUI
@@ -6,16 +7,18 @@ import SwiftUI
/// The view hosts the `RecordingView` feature from the `Recording` target of the `Features` package, which drives the whole recording flow,
/// inside a navigation stack, and presents the transcribed text of every processed recording in a modal sheet of its own navigation stack.
/// A toolbar menu picks the locale of the spoken language to transcribe, and every pick kicks off the preinstallation of the locale's speech
/// model assets, whose download events surface as transient in-app notifications overlaying the feature just below the navigation bar.
/// model assets, whose download events surface as transient in-app notifications banners wearing the `Notifying` target's label
/// style overlaying the feature just below the navigation bar.
///
/// The services attached to the feature, the transcription shown in the sheet, and the posted notifications live in the view's ``Model``;
/// the view itself only renders it and forwards the feature's output, the picker's changes, and the sheet's dismissal. All user-facing text
/// is localized through the app's string catalog.
/// The services attached to the feature, the transcription shown in the sheet, and the notifier owning the notifications live in the view's
/// ``Model``; the view itself only renders it and forwards the feature's output, the picker's changes, and the sheet's dismissal. All
/// user-facing text is localized through the app's string catalog.
struct ContentView: View {
// MARK: Properties
/// The model that owns the attached services, the transcription presented in the modal sheet, and the posted in-app notifications.
/// The model that owns the attached services, the transcription presented in the modal sheet, and the notifier of the in-app
/// notifications.
@State private var model = Model()
// MARK: Body
@@ -23,10 +26,10 @@ struct ContentView: View {
/// The content of the view: a navigation stack with the recording feature's view, attached to the model's services and expanded to fill
/// the available space, with a toolbar picker for the locale of the spoken language to transcribe, and a modal sheet presenting the
/// transcribed text of every processed recording or a content unavailable message when the transcription is empty closable
/// through its toolbar button or a swipe. The model's in-app notifications overlay the feature, stacking downward just below the
/// through its toolbar button or a swipe. The notifier's in-app notifications overlay the feature, stacking downward just below the
/// navigation bar; each one slides in from the leading edge when posted and out again when its scheduled dismissal arrives, without
/// displacing the recording controls underneath. Every posted notification is also announced to assistive technologies, since the
/// banners themselves are transient.
/// displacing the recording controls underneath. The notifier also announces every posted notification to assistive technologies,
/// since the banners themselves are transient.
var body: some View {
NavigationStack {
RecordingView(
@@ -104,7 +107,7 @@ private extension ContentView {
.accessibilityHint(Text("view.recording.picker.locale.hint"))
}
/// The stack of the model's in-app notifications, each one sliding in from the leading edge when posted and out again when
/// The stack of the notifier's in-app notifications, each one sliding in from the leading edge when posted and out again when
/// its scheduled dismissal arrives.
var stackNotifications: some View {
VStack(
@@ -112,16 +115,16 @@ private extension ContentView {
spacing: Constant.Spacing.stack
) {
ForEach(
model.notifications
model.notifier.notifications
) { notification in
Label {
Text(notification.textMessage)
Text(notification.message)
} icon: {
Image(systemName: notification.imageSymbol)
Image(systemName: notification.symbol)
.accessibilityHidden(true)
}
.labelStyle(.notification(
event: notification.event
kind: notification.kind
))
.accessibilityElement(
children: .combine
@@ -136,7 +139,7 @@ private extension ContentView {
.padding(.horizontal)
.animation(
.spring,
value: model.notifications
value: model.notifier.notifications
)
}