Meshtastic-Apple/Meshtastic/MeshtasticApp.swift

120 lines
3.9 KiB
Swift
Raw Normal View History

// Copyright (C) 2022 Garth Vander Houwen
2021-08-18 22:33:05 -07:00
import SwiftUI
import CoreData
2021-08-18 22:33:05 -07:00
@main
2022-06-09 21:58:22 -07:00
struct MeshtasticAppleApp: App {
2023-03-06 10:33:18 -08:00
2021-12-12 17:17:46 -08:00
let persistenceController = PersistenceController.shared
@ObservedObject private var bleManager: BLEManager = BLEManager()
2022-11-11 19:21:52 -08:00
@Environment(\.scenePhase) var scenePhase
@State var saveChannels = false
@State var incomingUrl: URL?
@State var channelSettings: String?
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)
2022-10-12 15:26:25 -07:00
.sheet(isPresented: $saveChannels) {
SaveChannelQRCode(channelSetLink: channelSettings ?? "Empty Channel URL", bleManager: bleManager)
2022-10-12 15:26:25 -07:00
.presentationDetents([.medium, .large])
.presentationDragIndicator(.visible)
}
2022-06-28 20:20:02 -07:00
.onContinueUserActivity(NSUserActivityTypeBrowsingWeb) { userActivity in
2022-06-29 20:04:20 -07:00
print("URL received \(userActivity)")
2022-11-17 21:44:07 -08:00
self.incomingUrl = userActivity.webpageURL
2023-03-06 10:33:18 -08:00
if (self.incomingUrl?.absoluteString.lowercased().contains("meshtastic.org/e/#")) != nil {
2022-11-17 21:44:07 -08:00
if let components = self.incomingUrl?.absoluteString.components(separatedBy: "#") {
self.channelSettings = components.last!
}
2022-11-17 21:44:07 -08:00
self.saveChannels = true
print("User wants to open a Channel Settings URL: \(self.incomingUrl?.absoluteString ?? "No QR Code Link")")
}
2022-11-17 21:44:07 -08:00
if self.saveChannels {
print("User wants to open Channel Settings URL: \(String(describing: self.incomingUrl!.relativeString))")
}
2022-06-28 20:20:02 -07:00
}
2022-06-28 06:56:50 -07:00
.onOpenURL(perform: { (url) in
2023-03-06 10:33:18 -08:00
print("Some sort of URL was received \(url)")
2022-11-17 21:44:07 -08:00
self.incomingUrl = url
2023-03-06 10:33:18 -08:00
if url.absoluteString.lowercased().contains("meshtastic.org/e/#") {
2022-11-17 21:44:07 -08:00
if let components = self.incomingUrl?.absoluteString.components(separatedBy: "#") {
self.channelSettings = components.last!
}
2022-11-17 21:44:07 -08:00
self.saveChannels = true
print("User wants to open a Channel Settings URL: \(self.incomingUrl?.absoluteString ?? "No QR Code Link")")
} else {
2022-10-10 21:21:58 -07:00
saveChannels = false
2022-11-17 21:44:07 -08:00
print("User wants to import a MBTILES offline map file: \(self.incomingUrl?.absoluteString ?? "No Tiles link")")
}
2023-03-06 10:33:18 -08:00
// 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)
2023-03-06 10:33:18 -08:00
2022-11-17 21:44:07 -08:00
if !self.saveChannels {
2023-03-06 10:33:18 -08:00
// tell the system we want the file please
guard url.startAccessingSecurityScopedResource() else {
return
}
2023-03-06 10:33:18 -08:00
// do we need to delete an old one?
if fileManager.fileExists(atPath: destination.path) {
2022-10-10 21:21:58 -07:00
print(" Found an old map file. Deleting it")
try? fileManager.removeItem(atPath: destination.path)
}
2023-03-06 10:33:18 -08:00
2022-10-10 21:21:58 -07:00
do {
try fileManager.copyItem(at: url, to: destination)
} catch {
print("Copy MB Tile file failed. Error: \(error)")
}
2023-03-06 10:33:18 -08:00
if fileManager.fileExists(atPath: destination.path) {
2022-10-10 21:21:58 -07:00
print(" Saved the map file")
2023-03-06 10:33:18 -08:00
// need to tell the map view that it needs to update and try loading the new overlay
2022-10-10 21:21:58 -07:00
UserDefaults.standard.set(Date().timeIntervalSince1970, forKey: "lastUpdatedLocalMapFile")
2023-03-06 10:33:18 -08:00
2022-10-10 21:21:58 -07:00
} else {
print("💥 Didn't save the map file")
}
}
2022-06-28 06:56:50 -07:00
})
}
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
}
}