Meshtastic-Apple/MeshtasticClient/MeshtasticClientApp.swift

71 lines
2.2 KiB
Swift
Raw Normal View History

2021-08-18 22:33:05 -07:00
import SwiftUI
import CoreData
2021-08-18 22:33:05 -07:00
@main
struct MeshtasticClientApp: App {
2022-01-17 14:03:48 +13:00
2021-12-12 17:17:46 -08:00
let persistenceController = PersistenceController.shared
2021-12-25 23:48:12 -08:00
2021-12-12 17:17:46 -08:00
@ObservedObject private var bleManager: BLEManager = BLEManager.shared
@ObservedObject private var userSettings: UserSettings = UserSettings()
2021-11-04 08:36:55 -07:00
@Environment(\.scenePhase) var scenePhase
2021-08-18 22:33:05 -07:00
var body: some Scene {
WindowGroup {
ContentView()
2021-12-12 17:17:46 -08:00
.environment(\.managedObjectContext, persistenceController.container.viewContext)
.environmentObject(bleManager)
.environmentObject(userSettings)
.onOpenURL(perform: { (url) in
//we are expecting a .mbtiles map file that contains raster data
//save it to the documents directory, and name it offline_map.mbtiles
let fileManager = FileManager.default
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!
let destination = documentsDirectory.appendingPathComponent("offline_map.mbtiles", isDirectory: false)
//do we need to delete an old one?
if (fileManager.fileExists(atPath: destination.path)) {
print(" Found an old map file. Deleting it")
try? fileManager.removeItem(atPath: destination.path)
}
try? fileManager.copyItem(at: url, to: destination)
if (fileManager.fileExists(atPath: destination.path)) {
print(" Saved the map file")
//need to tell the map view that it needs to update and try loading the new overlay
UserDefaults.standard.set(Date().timeIntervalSince1970, forKey: "lastUpdatedLocalMapFile")
} else {
print("💥 Didn't save the map file")
}
}
)
}
2021-11-04 08:36:55 -07:00
.onChange(of: scenePhase) { (newScenePhase) in
switch newScenePhase {
case .background:
print(" Scene is in the background")
2021-12-15 23:53:45 -08:00
do {
2021-12-25 23:48:12 -08:00
2021-12-15 23:53:45 -08:00
try persistenceController.container.viewContext.save()
print("💾 Saved CoreData ViewContext when the app went to the background.")
2021-12-25 23:48:12 -08:00
2021-12-15 23:53:45 -08:00
} catch {
2021-12-25 23:48:12 -08:00
print("💥 Failed to save viewContext when the app goes to the background.")
2021-12-15 23:53:45 -08:00
}
2021-11-04 08:36:55 -07:00
case .inactive:
print(" Scene is inactive")
2021-11-04 08:36:55 -07:00
case .active:
print(" Scene is active")
2021-11-04 08:36:55 -07:00
@unknown default:
print("💥 Apple must have changed something")
2021-11-04 08:36:55 -07:00
}
}
2021-08-18 22:33:05 -07:00
}
}