krz/daybyday

iOS learning games for young children: days, months, seasons, numbers, letters, animals and more. education ios kids swift https://krz.sh/apps/daybyday/

DayByDay/DayByDayApp.swift

062549ad26607ce9e973ed2a9a7ef6f853885f83
daybyday/DayByDay/DayByDayApp.swift history · blame · raw

69 lines · 1936 bytes

1 symbol in this file
 1//
 2//  DayByDayApp.swift
 3//  DayByDay
 4//
 5//  Created by cmc on 2026-03-09.
 6//
 7
 8import AVFoundation
 9import SwiftData
10import SwiftUI
11
12@main
13struct DayByDayApp: App {
14    #if DEBUG
15    private let screenshot = ScreenshotScene.requested
16    #endif
17    private let container: ModelContainer
18    @State private var session: ProfileSession
19
20    init() {
21        HeadingFont.register()
22        #if DEBUG
23        container = ProfileStore.makeContainer(inMemory: screenshot != nil)
24        #else
25        container = ProfileStore.makeContainer(inMemory: false)
26        #endif
27        let session = ProfileSession(store: ProfileStore(context: container.mainContext))
28        #if DEBUG
29        if screenshot != nil { ScreenshotScene.seed(session) }
30        #endif
31        _session = State(initialValue: session)
32
33        Task.detached(priority: .background) {
34            // Activate the audio session early so the first real tap doesn't block.
35            try? AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
36            try? AVAudioSession.sharedInstance().setActive(true)
37
38            // Force singleton init (voice selection) and prime the TTS engine
39            // with a silent utterance so subsequent speaks are instant.
40            await MainActor.run {
41                let warmup = AVSpeechUtterance(string: "")
42                warmup.volume = 0
43                SpeechSynthesizer.shared.speakUtterance(warmup)
44                SpeechSynthesizer.shared.warmUpClips()
45            }
46        }
47    }
48
49    var body: some Scene {
50        WindowGroup {
51            root
52                .environment(session)
53                .modelContainer(container)
54        }
55    }
56
57    @ViewBuilder @MainActor
58    private var root: some View {
59        #if DEBUG
60        if let screenshot {
61            screenshot.view(session)
62        } else {
63            ContentView()
64        }
65        #else
66        ContentView()
67        #endif
68    }
69}