2021-08-18 22:33:05 -07:00
|
|
|
|
import SwiftUI
|
2021-11-28 17:03:03 -08:00
|
|
|
|
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
|
2021-10-22 10:03:50 -07:00
|
|
|
|
@ObservedObject private var userSettings: UserSettings = UserSettings()
|
2021-11-29 21:11:27 -08:00
|
|
|
|
|
2021-11-04 08:36:55 -07:00
|
|
|
|
@Environment(\.scenePhase) var scenePhase
|
2021-11-29 21:11:27 -08:00
|
|
|
|
|
2021-08-18 22:33:05 -07:00
|
|
|
|
var body: some Scene {
|
|
|
|
|
|
WindowGroup {
|
2021-10-24 01:26:04 -07:00
|
|
|
|
ContentView()
|
2021-12-12 17:17:46 -08:00
|
|
|
|
.environment(\.managedObjectContext, persistenceController.container.viewContext)
|
2021-11-28 17:03:03 -08:00
|
|
|
|
.environmentObject(bleManager)
|
2021-12-16 22:45:18 -08:00
|
|
|
|
.environmentObject(userSettings)
|
2022-01-22 15:23:09 +13:00
|
|
|
|
.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)
|
2022-01-29 14:25:08 +13:00
|
|
|
|
|
|
|
|
|
|
//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)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-22 15:23:09 +13:00
|
|
|
|
try? fileManager.copyItem(at: url, to: destination)
|
|
|
|
|
|
|
|
|
|
|
|
if (fileManager.fileExists(atPath: destination.path)) {
|
|
|
|
|
|
print("ℹ️ Saved the map file")
|
2022-01-29 14:25:08 +13:00
|
|
|
|
|
|
|
|
|
|
//need to tell the map view that it needs to update and try loading the new overlay
|
|
|
|
|
|
UserDefaults.standard.set(Date().timeIntervalSince1970, forKey: "lastUpdatedLocalMapFile")
|
|
|
|
|
|
|
2022-01-22 15:23:09 +13:00
|
|
|
|
} else {
|
|
|
|
|
|
print("💥 Didn't save the map file")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
)
|
2021-10-24 01:26:04 -07:00
|
|
|
|
}
|
2021-11-04 08:36:55 -07:00
|
|
|
|
.onChange(of: scenePhase) { (newScenePhase) in
|
|
|
|
|
|
switch newScenePhase {
|
|
|
|
|
|
case .background:
|
2021-12-24 21:50:10 -08:00
|
|
|
|
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()
|
2021-12-24 21:50:10 -08:00
|
|
|
|
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
|
|
|
|
|
2021-12-24 21:50:10 -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:
|
2021-12-24 21:50:10 -08:00
|
|
|
|
print("ℹ️ Scene is inactive")
|
2021-11-04 08:36:55 -07:00
|
|
|
|
case .active:
|
2021-12-24 21:50:10 -08:00
|
|
|
|
print("ℹ️ Scene is active")
|
2021-11-04 08:36:55 -07:00
|
|
|
|
@unknown default:
|
2021-12-24 21:50:10 -08:00
|
|
|
|
print("💥 Apple must have changed something")
|
2021-11-04 08:36:55 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-08-18 22:33:05 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|