2021-08-20 07:56:05 -07:00
|
|
|
//
|
2021-11-29 21:35:23 -08:00
|
|
|
// NodeMap.swift
|
2022-06-09 22:11:54 -07:00
|
|
|
// MeshtasticApple
|
2021-08-20 07:56:05 -07:00
|
|
|
//
|
|
|
|
|
// Created by Garth Vander Houwen on 8/7/21.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
|
import MapKit
|
|
|
|
|
import CoreLocation
|
2021-12-20 22:29:28 -08:00
|
|
|
import CoreData
|
2021-08-20 07:56:05 -07:00
|
|
|
|
2021-09-18 15:33:35 -07:00
|
|
|
struct NodeMap: View {
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-12-15 23:53:45 -08:00
|
|
|
@Environment(\.managedObjectContext) var context
|
2021-11-29 15:59:06 -08:00
|
|
|
@EnvironmentObject var bleManager: BLEManager
|
2022-02-22 09:08:06 -10:00
|
|
|
@EnvironmentObject var userSettings: UserSettings
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2022-01-10 06:09:31 +13:00
|
|
|
@AppStorage("meshMapCustomTileServer") var customTileServer: String = "" {
|
|
|
|
|
didSet {
|
|
|
|
|
if customTileServer == "" {
|
|
|
|
|
self.customMapOverlay = nil
|
|
|
|
|
} else {
|
2023-01-13 15:49:25 -08:00
|
|
|
self.customMapOverlay = MapViewSwiftUI.CustomMapOverlay(
|
2022-01-10 06:09:31 +13:00
|
|
|
mapName: customTileServer,
|
|
|
|
|
tileType: "png",
|
|
|
|
|
canReplaceMapContent: true
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-20 20:00:00 -08:00
|
|
|
@AppStorage("meshMapType") private var meshMapType = "hybridFlyover"
|
2023-03-03 19:41:02 -08:00
|
|
|
@AppStorage("meshMapUserTrackingMode") private var meshMapUserTrackingMode = 0
|
2023-03-19 22:11:46 -07:00
|
|
|
@AppStorage("meshMapShowNodeHistory") private var meshMapShowNodeHistory = false
|
2023-03-20 17:49:29 -07:00
|
|
|
@AppStorage("meshMapShowRouteLines") private var meshMapShowRouteLines = false
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2023-02-10 20:50:25 -08:00
|
|
|
@FetchRequest(sortDescriptors: [NSSortDescriptor(key: "time", ascending: true)],
|
2023-02-07 07:35:07 -08:00
|
|
|
predicate: NSPredicate(format: "time >= %@ && nodePosition != nil", Calendar.current.startOfDay(for: Date()) as NSDate), animation: .none)
|
2023-01-13 15:49:25 -08:00
|
|
|
private var positions: FetchedResults<PositionEntity>
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2023-01-15 19:15:00 -08:00
|
|
|
@FetchRequest(sortDescriptors: [NSSortDescriptor(key: "name", ascending: false)],
|
|
|
|
|
predicate: NSPredicate(
|
|
|
|
|
format: "expire == nil || expire >= %@", Date() as NSDate
|
2023-02-06 18:45:03 -08:00
|
|
|
), animation: .none)
|
2023-01-13 22:30:10 -08:00
|
|
|
private var waypoints: FetchedResults<WaypointEntity>
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2023-02-20 20:00:00 -08:00
|
|
|
@State private var mapType: MKMapType = .standard
|
2023-03-03 19:41:02 -08:00
|
|
|
@State private var userTrackingMode: MKUserTrackingMode = .none
|
2023-04-08 00:34:39 -07:00
|
|
|
@State var waypointCoordinate: CLLocationCoordinate2D = LocationHelper.DefaultLocation.coordinate
|
2023-01-16 17:40:28 -08:00
|
|
|
@State var editingWaypoint: Int = 0
|
2023-01-13 15:49:25 -08:00
|
|
|
@State private var presentingWaypointForm = false
|
|
|
|
|
@State private var customMapOverlay: MapViewSwiftUI.CustomMapOverlay? = MapViewSwiftUI.CustomMapOverlay(
|
2022-01-17 14:03:48 +13:00
|
|
|
mapName: "offlinemap",
|
2022-01-10 06:09:31 +13:00
|
|
|
tileType: "png",
|
|
|
|
|
canReplaceMapContent: true
|
|
|
|
|
)
|
2023-03-14 12:44:10 -07:00
|
|
|
|
2023-02-27 11:16:46 -08:00
|
|
|
var body: some View {
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2023-02-27 11:16:46 -08:00
|
|
|
NavigationStack {
|
2023-01-13 15:49:25 -08:00
|
|
|
ZStack {
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2023-01-20 21:49:54 -08:00
|
|
|
MapViewSwiftUI(onLongPress: { coord in
|
2023-01-13 15:49:25 -08:00
|
|
|
waypointCoordinate = coord
|
2023-01-20 21:49:54 -08:00
|
|
|
editingWaypoint = 0
|
2023-04-08 00:34:39 -07:00
|
|
|
if waypointCoordinate.distance(from: LocationHelper.DefaultLocation.coordinate) == 0.0 {
|
2023-01-21 07:28:50 -08:00
|
|
|
print("Apple Park")
|
|
|
|
|
} else {
|
|
|
|
|
presentingWaypointForm = true
|
|
|
|
|
}
|
2023-01-16 23:16:57 -08:00
|
|
|
}, onWaypointEdit: { wpId in
|
2023-01-18 11:38:43 -08:00
|
|
|
if wpId > 0 {
|
|
|
|
|
editingWaypoint = wpId
|
|
|
|
|
presentingWaypointForm = true
|
|
|
|
|
}
|
2023-01-31 10:50:17 -08:00
|
|
|
}, positions: Array(positions),
|
2023-02-27 11:16:46 -08:00
|
|
|
waypoints: Array(waypoints),
|
|
|
|
|
mapViewType: mapType,
|
2023-03-03 19:41:02 -08:00
|
|
|
userTrackingMode: userTrackingMode,
|
2023-03-27 17:04:24 -07:00
|
|
|
showNodeHistory: meshMapShowNodeHistory,
|
2023-03-20 17:49:29 -07:00
|
|
|
showRouteLines: meshMapShowRouteLines,
|
2023-03-20 13:10:59 -07:00
|
|
|
customMapOverlay: self.customMapOverlay
|
2022-01-10 06:09:31 +13:00
|
|
|
)
|
2023-01-13 15:49:25 -08:00
|
|
|
VStack {
|
|
|
|
|
Spacer()
|
2023-01-13 22:30:10 -08:00
|
|
|
Picker("Map Type", selection: $mapType) {
|
2023-01-16 17:40:28 -08:00
|
|
|
ForEach(MeshMapType.allCases) { map in
|
|
|
|
|
Text(map.description).tag(map.MKMapTypeValue())
|
|
|
|
|
}
|
2023-01-13 15:49:25 -08:00
|
|
|
}
|
2023-01-27 20:22:17 -08:00
|
|
|
.background(.thinMaterial, in: RoundedRectangle(cornerRadius: 12, style: .continuous))
|
2023-01-13 22:30:10 -08:00
|
|
|
.pickerStyle(.menu)
|
2023-01-27 20:22:17 -08:00
|
|
|
.padding(.bottom, 5)
|
2023-01-13 15:49:25 -08:00
|
|
|
}
|
2022-12-30 13:18:02 -08:00
|
|
|
}
|
2023-01-13 15:49:25 -08:00
|
|
|
.ignoresSafeArea(.all, edges: [.top, .leading, .trailing])
|
|
|
|
|
.frame(maxHeight: .infinity)
|
2023-03-06 10:33:18 -08:00
|
|
|
.sheet(isPresented: $presentingWaypointForm ) {// , onDismiss: didDismissSheet) {
|
2023-01-21 07:28:50 -08:00
|
|
|
WaypointFormView(coordinate: waypointCoordinate, waypointId: editingWaypoint)
|
2023-01-20 22:20:35 -08:00
|
|
|
.presentationDetents([.medium, .large])
|
|
|
|
|
.presentationDragIndicator(.automatic)
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2023-01-13 16:07:38 -08:00
|
|
|
}
|
2023-02-27 11:16:46 -08:00
|
|
|
}
|
2022-10-17 21:18:57 -07:00
|
|
|
.navigationBarItems(leading:
|
2023-02-27 11:16:46 -08:00
|
|
|
MeshtasticLogo(), trailing:
|
|
|
|
|
ZStack {
|
2022-10-17 21:18:57 -07:00
|
|
|
ConnectedDevice(
|
|
|
|
|
bluetoothOn: bleManager.isSwitchedOn,
|
|
|
|
|
deviceConnected: bleManager.connectedPeripheral != nil,
|
|
|
|
|
name: (bleManager.connectedPeripheral != nil) ? bleManager.connectedPeripheral.shortName :
|
|
|
|
|
"????")
|
|
|
|
|
})
|
|
|
|
|
.onAppear(perform: {
|
2023-02-26 08:57:13 -08:00
|
|
|
UIApplication.shared.isIdleTimerDisabled = true
|
2022-10-17 21:18:57 -07:00
|
|
|
self.bleManager.context = context
|
|
|
|
|
self.bleManager.userSettings = userSettings
|
2023-03-03 19:41:02 -08:00
|
|
|
userTrackingMode = UserTrackingModes(rawValue: meshMapUserTrackingMode)?.MKUserTrackingModeValue() ?? MKUserTrackingMode.none
|
2023-02-20 19:52:46 -08:00
|
|
|
switch meshMapType {
|
2023-02-27 11:16:46 -08:00
|
|
|
case "standard":
|
|
|
|
|
mapType = .standard
|
|
|
|
|
case "mutedStandard":
|
|
|
|
|
mapType = .mutedStandard
|
|
|
|
|
case "hybrid":
|
|
|
|
|
mapType = .hybrid
|
|
|
|
|
case "hybridFlyover":
|
|
|
|
|
mapType = .hybridFlyover
|
|
|
|
|
case "satellite":
|
|
|
|
|
mapType = .satellite
|
|
|
|
|
case "satelliteFlyover":
|
|
|
|
|
mapType = .satelliteFlyover
|
|
|
|
|
default:
|
|
|
|
|
mapType = .hybridFlyover
|
2023-02-20 19:52:46 -08:00
|
|
|
}
|
2022-10-17 21:18:57 -07:00
|
|
|
})
|
2023-03-06 10:33:18 -08:00
|
|
|
.onDisappear(perform: {
|
2023-02-27 11:16:46 -08:00
|
|
|
UIApplication.shared.isIdleTimerDisabled = false
|
|
|
|
|
})
|
2021-08-20 07:56:05 -07:00
|
|
|
}
|
|
|
|
|
}
|