TeraProvider.swift (1538B)
1 import Combine 2 import SwiftUI 3 import UIKit 4 5 struct TeraProvider<Content: View>: View { 6 @Environment(\.scenePhase) private var scenePhase 7 @StateObject private var appModel: TeraAppModel 8 private let content: () -> Content 9 10 init( 11 appModel: @autoclosure @escaping () -> TeraAppModel = TeraAppModel(), 12 @ViewBuilder content: @escaping () -> Content 13 ) { 14 _appModel = StateObject(wrappedValue: appModel()) 15 self.content = content 16 } 17 18 var body: some View { 19 content() 20 .environmentObject(appModel) 21 .environmentObject(appModel.diagnosticsStore) 22 .task { await appModel.resume() } 23 .onChange(of: scenePhase) { _, phase in 24 if phase == .active { 25 Task { await appModel.resume() } 26 } else if phase == .background { 27 Task { await appModel.suspend() } 28 } 29 } 30 .onReceive( 31 NotificationCenter.default.publisher( 32 for: UIApplication.protectedDataDidBecomeAvailableNotification 33 ) 34 ) { _ in 35 Task { await appModel.updateProtectedDataAvailability(true) } 36 } 37 .onReceive( 38 NotificationCenter.default.publisher( 39 for: UIApplication.protectedDataWillBecomeUnavailableNotification 40 ) 41 ) { _ in 42 Task { await appModel.updateProtectedDataAvailability(false) } 43 } 44 } 45 }