Meshtastic-Apple/Meshtastic/Views/Nodes/NodeMap.swift

141 lines
4.4 KiB
Swift
Raw Normal View History

2021-08-20 07:56:05 -07:00
//
2021-11-29 21:35:23 -08:00
// NodeMap.swift
// 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
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 {
self.customMapOverlay = MapViewSwiftUI.CustomMapOverlay(
2022-01-10 06:09:31 +13:00
mapName: customTileServer,
tileType: "png",
canReplaceMapContent: true
)
}
}
}
@AppStorage("meshMapType") private var meshMapType = "hybridFlyover"
@AppStorage("meshMapUserTrackingMode") private var meshMapUserTrackingMode = 0
@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
@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)
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
), animation: .none)
2023-01-13 22:30:10 -08:00
private var waypoints: FetchedResults<WaypointEntity>
2023-03-06 10:33:18 -08:00
@State private var mapType: MKMapType = .standard
@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
@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
var body: some View {
2023-03-06 10:33:18 -08:00
NavigationStack {
ZStack {
2023-03-06 10:33:18 -08:00
2023-01-20 21:49:54 -08:00
MapViewSwiftUI(onLongPress: { coord in
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),
waypoints: Array(waypoints),
mapViewType: mapType,
userTrackingMode: userTrackingMode,
2023-03-27 17:04:24 -07:00
showNodeHistory: meshMapShowNodeHistory,
2023-03-20 17:49:29 -07:00
showRouteLines: meshMapShowRouteLines,
customMapOverlay: self.customMapOverlay
2022-01-10 06:09:31 +13: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-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)
}
2022-12-30 13:18:02 -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
}
}
2022-10-17 21:18:57 -07:00
.navigationBarItems(leading:
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
userTrackingMode = UserTrackingModes(rawValue: meshMapUserTrackingMode)?.MKUserTrackingModeValue() ?? MKUserTrackingMode.none
switch meshMapType {
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
}
2022-10-17 21:18:57 -07:00
})
2023-03-06 10:33:18 -08:00
.onDisappear(perform: {
UIApplication.shared.isIdleTimerDisabled = false
})
2021-08-20 07:56:05 -07:00
}
}