Implemented the Persisting the selected locale across launches on the ContentView view in the Sample app target.
This commit is contained in:
@@ -8,7 +8,8 @@ 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
|
||||
/// 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
|
||||
/// currently presented in the view's modal sheet: ``received(_:)`` presents the transcription of a processed recording, and
|
||||
/// ``dismissed()`` clears it when the sheet closes.
|
||||
///
|
||||
@@ -30,7 +31,7 @@ extension ContentView {
|
||||
/// 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.
|
||||
/// The locales the transcriber supports, sorted by their localized names, or empty while ``load(identifier:)`` has not finished yet.
|
||||
private(set) var locales: [Locale]
|
||||
|
||||
/// The service that captures the audio from a microphone.
|
||||
@@ -88,10 +89,20 @@ extension ContentView {
|
||||
// MARK: Methods
|
||||
|
||||
/// Loads the locales the transcriber supports — resolved through the preinstalling service — 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. It then kicks off the
|
||||
/// preinstallation of the aligned locale's speech model assets.
|
||||
func load() async {
|
||||
/// 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 {
|
||||
if !identifier.isEmpty {
|
||||
locale = .init(identifier: identifier)
|
||||
}
|
||||
|
||||
locales =
|
||||
await preinstaller
|
||||
.supportedLocales()
|
||||
|
||||
@@ -8,7 +8,8 @@ import SwiftUI
|
||||
/// 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 — banners wearing the `Notifying` target's label
|
||||
/// style — overlaying the feature just below the navigation bar.
|
||||
/// style — overlaying the feature just below the navigation bar. The picked locale persists across launches in the user defaults, and is
|
||||
/// restored — realigned to the supported locales — before the feature loads.
|
||||
///
|
||||
/// 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
|
||||
@@ -17,6 +18,10 @@ struct ContentView: View {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
/// The identifier of the picked locale, persisted across launches.
|
||||
@AppStorage(Constant.Key.locale)
|
||||
private var localeIdentifier: String = ""
|
||||
|
||||
/// The model that owns the attached services, the transcription presented in the modal sheet, and the notifier of the in-app
|
||||
/// notifications.
|
||||
@State
|
||||
@@ -36,7 +41,8 @@ struct ContentView: View {
|
||||
// MARK: Body
|
||||
|
||||
/// 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 — restored from the user defaults
|
||||
/// before the model loads, and persisted back on every change — 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 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
|
||||
@@ -70,12 +76,16 @@ struct ContentView: View {
|
||||
stackNotifications
|
||||
}
|
||||
.task {
|
||||
await model.load()
|
||||
await model.load(
|
||||
identifier: localeIdentifier
|
||||
)
|
||||
}
|
||||
.onChange(
|
||||
of: model.locale,
|
||||
initial: false
|
||||
) {
|
||||
localeIdentifier = model.locale.identifier
|
||||
|
||||
model.preinstall()
|
||||
}
|
||||
}
|
||||
@@ -211,6 +221,12 @@ private extension ContentView {
|
||||
|
||||
/// The constant values used across the view.
|
||||
private enum Constant {
|
||||
/// The key constants.
|
||||
enum Key {
|
||||
/// The user defaults key of the persisted locale identifier.
|
||||
static let locale = "com.rock-n-code.app.attendi.sample.user-defaults.ket.selected-locale"
|
||||
}
|
||||
|
||||
/// The spacing constants.
|
||||
enum Spacing {
|
||||
/// The spacing between the elements of a stack.
|
||||
|
||||
Reference in New Issue
Block a user