Meshtastic-Apple/MeshtasticClient/MeshtasticClientApp.swift
2021-12-25 23:48:12 -08:00

43 lines
1.1 KiB
Swift
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import SwiftUI
import CoreData
@main
struct MeshtasticClientApp: App {
let persistenceController = PersistenceController.shared
@ObservedObject private var bleManager: BLEManager = BLEManager.shared
@ObservedObject private var userSettings: UserSettings = UserSettings()
@Environment(\.scenePhase) var scenePhase
var body: some Scene {
WindowGroup {
ContentView()
.environment(\.managedObjectContext, persistenceController.container.viewContext)
.environmentObject(bleManager)
.environmentObject(userSettings)
}
.onChange(of: scenePhase) { (newScenePhase) in
switch newScenePhase {
case .background:
print(" Scene is in the background")
do {
try persistenceController.container.viewContext.save()
print("💾 Saved CoreData ViewContext when the app went to the background.")
} catch {
print("💥 Failed to save viewContext when the app goes to the background.")
}
case .inactive:
print(" Scene is inactive")
case .active:
print(" Scene is active")
@unknown default:
print("💥 Apple must have changed something")
}
}
}
}