Integrated the Notifier and the AssetPreinstalling to the ContentView view model in the Sample app target.
This commit is contained in:
@@ -2,8 +2,10 @@ import SwiftUI
|
|||||||
|
|
||||||
/// The entry point of the Attendi sample app.
|
/// 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
|
/// The app is a thin shell over the `Features` package. It showcases the Recording feature attached to its real audio services —
|
||||||
/// device's microphone, transcribes the recording into text on device, and presents the result in a modal sheet.
|
/// 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
|
@main
|
||||||
struct AttendiApp: App {
|
struct AttendiApp: App {
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
import Accessibility
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
import Notifying
|
||||||
import Observation
|
import Observation
|
||||||
import OSLog
|
|
||||||
import Recording
|
import Recording
|
||||||
import Speech
|
|
||||||
|
|
||||||
extension ContentView {
|
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
|
/// currently presented in the view's modal sheet: ``received(_:)`` presents the transcription of a processed recording, and
|
||||||
/// ``dismissed()`` clears it when the sheet closes.
|
/// ``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
|
/// Every picked locale kicks off ``preinstall()``, a cancellable task that preinstalls the locale's speech model assets through the
|
||||||
/// any download still in flight for a previously picked locale — and reports the started, cancelled, and failed downloads through the
|
/// preinstalling service — cancelling any download still in flight for a previously picked locale. The service's started, cancelled, and
|
||||||
/// transient, self-dismissing ``notifications``.
|
/// failed download events are mapped to the transient, self-dismissing notifications of the ``notifier``.
|
||||||
@MainActor
|
@MainActor
|
||||||
@Observable
|
@Observable
|
||||||
final class Model {
|
final class Model {
|
||||||
@@ -32,9 +30,6 @@ extension ContentView {
|
|||||||
/// The locales the transcriber supports, sorted by their localized names, or empty while ``load()`` has not finished yet.
|
/// The locales the transcriber supports, sorted by their localized names, or empty while ``load()`` has not finished yet.
|
||||||
private(set) var locales: [Locale]
|
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.
|
/// The service that captures the audio from the device's microphone.
|
||||||
@ObservationIgnored
|
@ObservationIgnored
|
||||||
let capturer: AudioCapturing
|
let capturer: AudioCapturing
|
||||||
@@ -43,12 +38,17 @@ extension ContentView {
|
|||||||
@ObservationIgnored
|
@ObservationIgnored
|
||||||
private var localePreinstalling: Locale?
|
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
|
@ObservationIgnored
|
||||||
private let logger = Logger(
|
let notifier: Notifier
|
||||||
subsystem: Bundle.main.bundleIdentifier ?? "Attendi",
|
|
||||||
category: "ContentView.Model"
|
/// 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.
|
/// The task that preinstalls the speech model assets of the last picked locale, or `nil` until the first locale is picked.
|
||||||
@ObservationIgnored
|
@ObservationIgnored
|
||||||
@@ -60,12 +60,14 @@ extension ContentView {
|
|||||||
|
|
||||||
// MARK: Initializers
|
// MARK: Initializers
|
||||||
|
|
||||||
/// Creates a model set to the user's current locale, with no supported locales loaded yet, attached to the on-device recording
|
/// 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.
|
/// transcribing, and preinstalling services, and to a notifier for the download events.
|
||||||
init() {
|
init() {
|
||||||
self.locale = .current
|
self.locale = .current
|
||||||
self.locales = []
|
self.locales = []
|
||||||
self.capturer = .init()
|
self.capturer = .init()
|
||||||
|
self.notifier = .init()
|
||||||
|
self.preinstaller = .init()
|
||||||
self.transcriber = .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 —
|
/// 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.
|
/// so the locale picker starts with a valid selection.
|
||||||
func load() async {
|
func load() async {
|
||||||
locales = await SpeechTranscriber.supportedLocales
|
locales = await preinstaller
|
||||||
|
.supportedLocales()
|
||||||
.sorted {
|
.sorted {
|
||||||
name(for: $0).localizedStandardCompare(name(for: $1)) == .orderedAscending
|
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
|
equivalent
|
||||||
} else if let fallback = await localeInTranscriber(equivalentTo: .byDefault) {
|
} else if let fallback = await preinstaller.supportedLocale(equivalentTo: .byDefault) {
|
||||||
fallback
|
fallback
|
||||||
} else {
|
} else {
|
||||||
.current
|
.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
|
/// 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
|
/// previously picked locale still in flight — which the ``notifier`` reports as a cancelled download. Does nothing when the
|
||||||
/// locale is the one already preinstalled or being preinstalled.
|
/// current locale is the one already preinstalled or being preinstalled.
|
||||||
func preinstall() {
|
func preinstall() {
|
||||||
guard localePreinstalling != locale else {
|
guard localePreinstalling != locale else {
|
||||||
return
|
return
|
||||||
@@ -103,8 +106,10 @@ extension ContentView {
|
|||||||
|
|
||||||
localePreinstalling = locale
|
localePreinstalling = locale
|
||||||
|
|
||||||
|
listenToEvents()
|
||||||
|
|
||||||
taskPreinstall = Task { [locale] in
|
taskPreinstall = Task { [locale] in
|
||||||
await preinstallAssets(for: locale)
|
await preinstaller.preinstall(for: locale)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -147,91 +152,44 @@ private extension ContentView.Model {
|
|||||||
|
|
||||||
// MARK: Methods
|
// MARK: Methods
|
||||||
|
|
||||||
func localeInTranscriber(
|
/// Starts listening to the events emitted by the preinstalling service, unless already listening: every started, cancelled, and
|
||||||
equivalentTo locale: Locale
|
/// failed download is posted to the ``ContentView/Model/notifier``, and a cancellation or failure clears the preinstalled locale
|
||||||
) async -> Locale? {
|
/// so picking it again retries.
|
||||||
await SpeechTranscriber.supportedLocale(equivalentTo: locale)
|
func listenToEvents() {
|
||||||
}
|
guard taskEvents == nil else {
|
||||||
|
|
||||||
/// 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 {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for reserved in await AssetInventory.reservedLocales where reserved != locale {
|
taskEvents = Task { [weak self, events = preinstaller.events] in
|
||||||
_ = await AssetInventory.release(reservedLocale: reserved)
|
for await event in events {
|
||||||
}
|
guard let self else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
let transcriber = SpeechTranscriber(
|
switch event {
|
||||||
locale: locale,
|
case let .cancelled(locale):
|
||||||
preset: .transcription
|
localePreinstalling = nil
|
||||||
)
|
|
||||||
|
|
||||||
do {
|
notifier.post(
|
||||||
guard let request = try await AssetInventory.assetInstallationRequest(
|
.warning,
|
||||||
supporting: [transcriber]
|
message: String(localized: .viewRecordingNotificationDownloadCancelled(name(for: locale))),
|
||||||
) else {
|
symbol: Constant.Symbol.cancelled
|
||||||
return
|
)
|
||||||
}
|
case let .failed(locale):
|
||||||
|
localePreinstalling = nil
|
||||||
|
|
||||||
post(.started, for: locale)
|
notifier.post(
|
||||||
|
.error,
|
||||||
try await withTaskCancellationHandler {
|
message: String(localized: .viewRecordingNotificationDownloadFailed(name(for: locale))),
|
||||||
try await request.downloadAndInstall()
|
symbol: Constant.Symbol.failed
|
||||||
} onCancel: { [progress = request.progress] in
|
)
|
||||||
progress.cancel()
|
case let .started(locale):
|
||||||
}
|
notifier.post(
|
||||||
} catch {
|
.info,
|
||||||
if localePreinstalling == target {
|
message: String(localized: .viewRecordingNotificationDownloadStarted(name(for: locale))),
|
||||||
localePreinstalling = nil
|
symbol: Constant.Symbol.started
|
||||||
}
|
)
|
||||||
|
}
|
||||||
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)")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -242,10 +200,14 @@ private extension ContentView.Model {
|
|||||||
|
|
||||||
/// The constant values used across the model.
|
/// The constant values used across the model.
|
||||||
private enum Constant {
|
private enum Constant {
|
||||||
/// The delay constants.
|
/// The symbol constants.
|
||||||
enum Delay {
|
enum Symbol {
|
||||||
/// The time a posted notification stays visible before its automatic dismissal.
|
/// The system symbol of a cancelled download.
|
||||||
static let dismissal: Duration = .seconds(4)
|
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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import Notifying
|
||||||
import Recording
|
import Recording
|
||||||
import SwiftUI
|
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,
|
/// 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.
|
/// 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
|
/// 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 services attached to the feature, the transcription shown in the sheet, and the notifier owning the notifications live in the view's
|
||||||
/// 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
|
/// ``Model``; the view itself only renders it and forwards the feature's output, the picker's changes, and the sheet's dismissal. All
|
||||||
/// is localized through the app's string catalog.
|
/// user-facing text is localized through the app's string catalog.
|
||||||
struct ContentView: View {
|
struct ContentView: View {
|
||||||
|
|
||||||
// MARK: Properties
|
// 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()
|
@State private var model = Model()
|
||||||
|
|
||||||
// MARK: Body
|
// 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 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
|
/// 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
|
/// 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
|
/// 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
|
/// displacing the recording controls underneath. The notifier also announces every posted notification to assistive technologies,
|
||||||
/// banners themselves are transient.
|
/// since the banners themselves are transient.
|
||||||
var body: some View {
|
var body: some View {
|
||||||
NavigationStack {
|
NavigationStack {
|
||||||
RecordingView(
|
RecordingView(
|
||||||
@@ -104,7 +107,7 @@ private extension ContentView {
|
|||||||
.accessibilityHint(Text("view.recording.picker.locale.hint"))
|
.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.
|
/// its scheduled dismissal arrives.
|
||||||
var stackNotifications: some View {
|
var stackNotifications: some View {
|
||||||
VStack(
|
VStack(
|
||||||
@@ -112,16 +115,16 @@ private extension ContentView {
|
|||||||
spacing: Constant.Spacing.stack
|
spacing: Constant.Spacing.stack
|
||||||
) {
|
) {
|
||||||
ForEach(
|
ForEach(
|
||||||
model.notifications
|
model.notifier.notifications
|
||||||
) { notification in
|
) { notification in
|
||||||
Label {
|
Label {
|
||||||
Text(notification.textMessage)
|
Text(notification.message)
|
||||||
} icon: {
|
} icon: {
|
||||||
Image(systemName: notification.imageSymbol)
|
Image(systemName: notification.symbol)
|
||||||
.accessibilityHidden(true)
|
.accessibilityHidden(true)
|
||||||
}
|
}
|
||||||
.labelStyle(.notification(
|
.labelStyle(.notification(
|
||||||
event: notification.event
|
kind: notification.kind
|
||||||
))
|
))
|
||||||
.accessibilityElement(
|
.accessibilityElement(
|
||||||
children: .combine
|
children: .combine
|
||||||
@@ -136,7 +139,7 @@ private extension ContentView {
|
|||||||
.padding(.horizontal)
|
.padding(.horizontal)
|
||||||
.animation(
|
.animation(
|
||||||
.spring,
|
.spring,
|
||||||
value: model.notifications
|
value: model.notifier.notifications
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user