Restructured the Sample app target in the Xcode project and added its unit tests target as well.

This commit is contained in:
2026-07-05 15:45:09 +02:00
parent 21d86cdc33
commit 8576d6bac6
7 changed files with 665 additions and 51 deletions
@@ -17,5 +17,5 @@ struct AttendiApp: App {
ContentView()
}
}
}
@@ -30,9 +30,9 @@ extension ContentView {
/// 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.
/// The service that captures the audio from a microphone.
@ObservationIgnored
let capturer: AudioCapturing
let capturer: any Capturing
/// The locale whose speech model assets are preinstalled or being preinstalled, or `nil` when none are.
@ObservationIgnored
@@ -44,7 +44,7 @@ extension ContentView {
/// The service that preinstalls the speech model assets of a picked locale.
@ObservationIgnored
private let preinstaller: AssetPreinstalling
private let preinstaller: any Preinstalling
/// The task that listens to the events emitted by the preinstalling service, or `nil` until the first preinstallation starts.
@ObservationIgnored
@@ -54,21 +54,32 @@ extension ContentView {
@ObservationIgnored
private var taskPreinstall: Task<Void, Never>?
/// The service that transcribes the recorded audio into text on device.
/// The service that transcribes the recorded audio into text.
@ObservationIgnored
let transcriber: AudioTranscribing
let transcriber: any Transcribing
// MARK: Initializers
/// 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() {
/// 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``.
/// - 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(),
transcriber: any Transcribing = AudioTranscribing(),
notifier: Notifier = .init(),
) {
self.locale = .current
self.locales = []
self.capturer = .init()
self.notifier = .init()
self.preinstaller = .init()
self.transcriber = .init()
self.capturer = capturer
self.notifier = notifier
self.preinstaller = preinstaller
self.transcriber = transcriber
}
// MARK: Methods
@@ -77,19 +88,23 @@ 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 preinstaller
locales =
await preinstaller
.supportedLocales()
.sorted {
name(for: $0).localizedStandardCompare(name(for: $1)) == .orderedAscending
}
locale = if let equivalent = await preinstaller.supportedLocale(equivalentTo: locale) {
equivalent
} else if let fallback = await preinstaller.supportedLocale(equivalentTo: .byDefault) {
fallback
} else {
.current
}
locale =
if let equivalent = await preinstaller.supportedLocale(equivalentTo: locale) {
equivalent
}
else if let fallback = await preinstaller.supportedLocale(equivalentTo: .byDefault) {
fallback
}
else {
.current
}
preinstall()
}
@@ -125,7 +140,7 @@ extension ContentView {
.localizedString(
forIdentifier: locale.identifier
)?.localizedCapitalized
?? locale.identifier
?? locale.identifier
}
/// Handles the transcription of a processed recording, presenting it in the modal sheet.
@@ -154,7 +169,7 @@ private extension ContentView.Model {
/// 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.
/// only when it is still the affected one, so a newer preinstallation is never forgotten letting a re-pick retry.
func listenToEvents() {
guard taskEvents == nil else {
return
@@ -167,23 +182,27 @@ private extension ContentView.Model {
}
switch event {
case let .cancelled(locale):
localePreinstalling = nil
case .cancelled(let locale):
if localePreinstalling == locale {
localePreinstalling = nil
}
notifier.post(
.warning,
message: String(localized: .viewRecordingNotificationDownloadCancelled(name(for: locale))),
symbol: Constant.Symbol.cancelled
)
case let .failed(locale):
localePreinstalling = nil
case .failed(let locale):
if localePreinstalling == locale {
localePreinstalling = nil
}
notifier.post(
.error,
message: String(localized: .viewRecordingNotificationDownloadFailed(name(for: locale))),
symbol: Constant.Symbol.failed
)
case let .started(locale):
case .started(let locale):
notifier.post(
.info,
message: String(localized: .viewRecordingNotificationDownloadStarted(name(for: locale))),
@@ -19,7 +19,19 @@ struct ContentView: View {
/// 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: Initializers
/// Creates a content view driven by the given model.
///
/// - Parameter model: The model that drives the view. Defaults to a model attached to the on-device services.
init(
model: Model = .init()
) {
self._model = State(initialValue: model)
}
// MARK: Body
@@ -43,9 +55,9 @@ struct ContentView: View {
maxWidth: .infinity,
maxHeight: .infinity
)
.navigationTitle("view.recording.navigation.title")
.navigationTitle(.viewRecordingNavigationTitle)
#if !os(macOS)
.navigationBarTitleDisplayMode(.inline)
.navigationBarTitleDisplayMode(.inline)
#endif
.toolbar {
ToolbarItem(placement: .primaryAction) {
@@ -85,7 +97,7 @@ private extension ContentView {
var menuLocale: some View {
Menu {
Picker(
"view.recording.picker.locale.title",
.viewRecordingPickerLocaleTitle,
selection: $model.locale
) {
ForEach(
@@ -99,12 +111,12 @@ private extension ContentView {
.pickerStyle(.inline)
} label: {
Label(
"view.recording.picker.locale.title",
.viewRecordingPickerLocaleTitle,
systemImage: "globe"
)
}
.disabled(model.locales.isEmpty)
.accessibilityHint(Text("view.recording.picker.locale.hint"))
.accessibilityHint(Text(.viewRecordingPickerLocaleHint))
}
/// The stack of the notifier's in-app notifications, each one sliding in from the leading edge when posted and out again when
@@ -123,17 +135,21 @@ private extension ContentView {
Image(systemName: notification.symbol)
.accessibilityHidden(true)
}
.labelStyle(.notification(
kind: notification.kind
))
.labelStyle(
.notification(
kind: notification.kind
)
)
.accessibilityElement(
children: .combine
)
.transition(.move(
edge: .leading
).combined(
with: .opacity
))
.transition(
.move(
edge: .leading
).combined(
with: .opacity
)
)
}
}
.padding(.horizontal)
@@ -157,11 +173,12 @@ private extension ContentView {
Group {
if transcription.isEmpty {
ContentUnavailableView(
"view.transcription.unavailable.title",
.viewTranscriptionUnavailableTitle,
systemImage: "text.page.slash",
description: Text("view.transcription.unavailable.description")
description: Text(.viewTranscriptionUnavailableDescription)
)
} else {
}
else {
ScrollView {
Text(transcription.text)
.font(.body)
@@ -175,9 +192,9 @@ private extension ContentView {
}
}
}
.navigationTitle("view.transcription.navigation.title")
.navigationTitle(.viewTranscriptionNavigationTitle)
#if !os(macOS)
.navigationBarTitleDisplayMode(.inline)
.navigationBarTitleDisplayMode(.inline)
#endif
.toolbar {
Button(role: .close) {
@@ -203,8 +220,41 @@ private enum Constant {
// MARK: - Previews
/// The preinstalling service used by the previews, which resolves every locale as supported and downloads nothing.
private struct PreviewPreinstalling: Preinstalling {
/// The stream of events the service emits, which finishes immediately.
let events: AsyncStream<PreinstallingEvent> = .init { continuation in
continuation.finish()
}
func preinstall(
for locale: Locale
) async {
// The previews preinstall no speech model assets.
}
func supportedLocale(
equivalentTo locale: Locale
) async -> Locale? {
locale
}
func supportedLocales() async -> [Locale] {
[
Locale(identifier: "en-US"),
Locale(identifier: "nl-NL"),
]
}
}
#Preview(
"Content view"
) {
ContentView()
ContentView(
model: .init(
preinstaller: PreviewPreinstalling()
)
)
}
@@ -0,0 +1,53 @@
import Foundation
import Recording
/// A preinstalling service that records its requests, resolves the supported locales from configurable tables, and emits events
/// on demand.
@MainActor
final class PreinstallingMock: Preinstalling {
/// The stream of events the service emits while preinstalling.
let events: AsyncStream<PreinstallingEvent>
/// The supported equivalents the service resolves, keyed by the requested locale.
var equivalents: [Locale: Locale] = [:]
/// The locales the service reports as supported.
var localesSupported: [Locale] = []
/// The locales a preinstallation was requested for, in request order.
private(set) var localesPreinstalled: [Locale] = []
/// The continuation that feeds ``events``.
private let continuation: AsyncStream<PreinstallingEvent>.Continuation
init() {
(events, continuation) = AsyncStream.makeStream(of: PreinstallingEvent.self)
}
/// Emits the given event through ``events``.
///
/// - Parameter event: The event to emit.
func emit(
_ event: PreinstallingEvent
) {
continuation.yield(event)
}
func preinstall(
for locale: Locale
) async {
localesPreinstalled.append(locale)
}
func supportedLocale(
equivalentTo locale: Locale
) async -> Locale? {
equivalents[locale]
}
func supportedLocales() async -> [Locale] {
localesSupported
}
}
@@ -0,0 +1,210 @@
import Foundation
import Notifying
import Recording
import Testing
@testable import Attendi
@Suite("Content view model")
struct ContentViewModelTests {
typealias Model = ContentView.Model
// MARK: Load
@MainActor
@Suite("Load")
struct Load {
@Test
func `load fills the supported locales and aligns the picked locale`() async {
let preinstaller = PreinstallingMock()
preinstaller.localesSupported = [.dutch, .english]
preinstaller.equivalents = [.current: .english]
let model = Model(preinstaller: preinstaller)
await model.load()
#expect(model.locale == .english)
#expect(model.locales.count == 2)
#expect(model.locales.contains(.dutch))
#expect(model.locales.contains(.english))
}
@Test
func `load preinstalls the aligned locale`() async throws {
let preinstaller = PreinstallingMock()
preinstaller.localesSupported = [.dutch, .english]
preinstaller.equivalents = [.current: .english]
let model = Model(preinstaller: preinstaller)
await model.load()
try await Task.sleep(for: .seconds(0.1))
#expect(preinstaller.localesPreinstalled == [.english])
}
}
// MARK: Preinstall
@MainActor
@Suite("Preinstall")
struct Preinstall {
@Test
func `preinstalling the same locale twice runs once`() async throws {
let preinstaller = PreinstallingMock()
let model = Model(preinstaller: preinstaller)
model.locale = .dutch
model.preinstall()
model.preinstall()
try await Task.sleep(for: .seconds(0.1))
#expect(preinstaller.localesPreinstalled == [.dutch])
}
@Test
func `preinstalling a newly picked locale runs again`() async throws {
let preinstaller = PreinstallingMock()
let model = Model(preinstaller: preinstaller)
model.locale = .dutch
model.preinstall()
model.locale = .english
model.preinstall()
try await Task.sleep(for: .seconds(0.1))
#expect(preinstaller.localesPreinstalled == [.dutch, .english])
}
}
// MARK: Events
@MainActor
@Suite("Events")
struct Events {
@Test
func `a started event posts an info notification`() async throws {
let preinstaller = PreinstallingMock()
let model = Model(preinstaller: preinstaller)
model.locale = .dutch
model.preinstall()
try await Task.sleep(for: .seconds(0.1))
preinstaller.emit(.started(.dutch))
try await Task.sleep(for: .seconds(0.1))
#expect(model.notifier.notifications.count == 1)
#expect(model.notifier.notifications.first?.kind == .info)
#expect(model.notifier.notifications.first?.symbol == "arrow.down.circle")
#expect(model.notifier.notifications.first?.message == String(localized: .viewRecordingNotificationDownloadStarted(model.name(for: .dutch))))
}
@Test
func `a cancelled event posts a warning notification`() async throws {
let preinstaller = PreinstallingMock()
let model = Model(preinstaller: preinstaller)
model.locale = .dutch
model.preinstall()
try await Task.sleep(for: .seconds(0.1))
preinstaller.emit(.cancelled(.dutch))
try await Task.sleep(for: .seconds(0.1))
#expect(model.notifier.notifications.first?.kind == .warning)
#expect(model.notifier.notifications.first?.symbol == "xmark.circle")
#expect(model.notifier.notifications.first?.message == String(localized: .viewRecordingNotificationDownloadCancelled(model.name(for: .dutch))))
}
@Test
func `a failed event posts an error notification`() async throws {
let preinstaller = PreinstallingMock()
let model = Model(preinstaller: preinstaller)
model.locale = .dutch
model.preinstall()
try await Task.sleep(for: .seconds(0.1))
preinstaller.emit(.failed(.dutch))
try await Task.sleep(for: .seconds(0.1))
#expect(model.notifier.notifications.first?.kind == .error)
#expect(model.notifier.notifications.first?.symbol == "exclamationmark.triangle")
#expect(model.notifier.notifications.first?.message == String(localized: .viewRecordingNotificationDownloadFailed(model.name(for: .dutch))))
}
@Test
func `a cancellation of the current locale lets a re-pick retry`() async throws {
let preinstaller = PreinstallingMock()
let model = Model(preinstaller: preinstaller)
model.locale = .dutch
model.preinstall()
try await Task.sleep(for: .seconds(0.1))
preinstaller.emit(.cancelled(.dutch))
try await Task.sleep(for: .seconds(0.1))
model.preinstall()
try await Task.sleep(for: .seconds(0.1))
#expect(preinstaller.localesPreinstalled == [.dutch, .dutch])
}
@Test
func `a cancellation of another locale keeps the current preinstallation`() async throws {
let preinstaller = PreinstallingMock()
let model = Model(preinstaller: preinstaller)
model.locale = .english
model.preinstall()
try await Task.sleep(for: .seconds(0.1))
preinstaller.emit(.cancelled(.dutch))
try await Task.sleep(for: .seconds(0.1))
model.preinstall()
try await Task.sleep(for: .seconds(0.1))
#expect(preinstaller.localesPreinstalled == [.english])
}
}
}
// MARK: - Constants
private extension Locale {
/// A supported locale to preinstall in the tests.
static let dutch = Locale(identifier: "nl-NL")
/// Another supported locale to preinstall in the tests.
static let english = Locale(identifier: "en-US")
}