|
| 1 | +// |
| 2 | +// AppEnvironment.swift |
| 3 | +// DecoderSec/App |
| 4 | +// |
| 5 | +// Single assembly point: builds every singleton once, exposes them |
| 6 | +// via .environmentObject / @EnvironmentObject so no View ever reaches |
| 7 | +// for .shared directly. Phase 2 rewrite. |
| 8 | +// |
| 9 | + |
| 10 | +import SwiftUI |
| 11 | + |
| 12 | +/// Owns the app's singletons and injects them into the SwiftUI tree. |
| 13 | +/// Add it to the root scene modifier chain once: |
| 14 | +/// |
| 15 | +/// WindowGroup { ContentView() } |
| 16 | +/// .modifier(AppEnvironment.modifier) |
| 17 | +/// |
| 18 | +@MainActor |
| 19 | +final class AppEnvironment: ObservableObject { |
| 20 | + |
| 21 | + // MARK: - Singletons (assembled once here, injected everywhere else) |
| 22 | + |
| 23 | + let tunnel = TunnelManager.shared |
| 24 | + let store = ConfigurationStore.shared |
| 25 | + let appState = AppState.shared |
| 26 | + let deepLinks = DeepLinkCenter.shared |
| 27 | + let routing = RoutingProfileStore.shared |
| 28 | + |
| 29 | + // MARK: - Singleton (environment modifier) |
| 30 | + |
| 31 | + static let shared = AppEnvironment() |
| 32 | + |
| 33 | + private init() {} |
| 34 | + |
| 35 | + /// Apply to the root `WindowGroup` scene. |
| 36 | + struct Modifier: ViewModifier { |
| 37 | + @ObservedObject private var env = AppEnvironment.shared |
| 38 | + |
| 39 | + func body(content: Content) -> some View { |
| 40 | + content |
| 41 | + .environmentObject(env.tunnel) |
| 42 | + .environmentObject(env.store) |
| 43 | + .environmentObject(env.appState) |
| 44 | + .environmentObject(env.deepLinks) |
| 45 | + .environmentObject(env.routing) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + static var modifier: Modifier { Modifier() } |
| 50 | +} |
0 commit comments