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

230 lines
8 KiB
Swift
Raw Normal View History

2023-11-07 21:20:11 -08:00
//
// MeshMap.swift
// Meshtastic
//
// Copyright(c) Garth Vander Houwen 11/7/23.
//
import SwiftUI
import CoreData
import CoreLocation
2023-11-14 18:11:25 -08:00
import Foundation
2024-06-03 02:17:55 -07:00
import OSLog
2023-11-07 21:20:11 -08:00
import MapKit
struct MeshMap: View {
2023-11-07 21:20:11 -08:00
@Environment(\.managedObjectContext) var context
@EnvironmentObject var bleManager: BLEManager
@ObservedObject
var router: Router
2023-11-07 21:20:11 -08:00
/// Parameters
@State var showUserLocation: Bool = true
/// Map State User Defaults
@AppStorage("enableMapTraffic") private var showTraffic: Bool = false
@AppStorage("enableMapPointsOfInterest") private var showPointsOfInterest: Bool = false
@AppStorage("mapLayer") private var selectedMapLayer: MapLayer = .standard
2023-11-07 21:20:11 -08:00
// Map Configuration
@Namespace var mapScope
@State var mapStyle: MapStyle = MapStyle.standard(elevation: .flat, emphasis: MapStyle.StandardEmphasis.muted, pointsOfInterest: .excludingAll, showsTraffic: false)
2024-03-24 22:23:55 -07:00
@State var position = MapCameraPosition.automatic
2024-09-08 04:47:14 +00:00
@State private var distance = 10000.0
2024-08-16 16:26:54 -07:00
@State private var editingSettings = false
@State private var editingFilters = false
2023-11-07 21:20:11 -08:00
@State var selectedPosition: PositionEntity?
2023-11-11 09:44:03 -08:00
@State var editingWaypoint: WaypointEntity?
2023-11-07 21:20:11 -08:00
@State var selectedWaypoint: WaypointEntity?
@State var selectedWaypointId: String?
2024-03-23 09:01:44 -07:00
@State var newWaypointCoord: CLLocationCoordinate2D?
@State var isMeshMap = true
2024-08-16 16:26:54 -07:00
/// Filter
@State private var searchText = ""
@State private var viaLora = true
@State private var viaMqtt = true
@State private var isOnline = false
@State private var isPkiEncrypted = false
@State private var isFavorite = false
@State private var isEnvironment = false
@State private var distanceFilter = false
@State private var maxDistance: Double = 800000
@State private var hopsAway: Double = -1.0
@State private var roleFilter = false
@State private var deviceRoles: Set<Int> = []
2024-03-25 15:21:38 -07:00
2023-11-07 21:20:11 -08:00
var body: some View {
2023-11-07 21:20:11 -08:00
NavigationStack {
ZStack {
MapReader { reader in
2024-09-08 04:47:14 +00:00
Map(
position: $position,
bounds: MapCameraBounds(minimumDistance: 1, maximumDistance: .infinity),
scope: mapScope
) {
MeshMapContent(
showUserLocation: $showUserLocation,
showTraffic: $showTraffic,
showPointsOfInterest: $showPointsOfInterest,
selectedMapLayer: $selectedMapLayer,
selectedPosition: $selectedPosition,
selectedWaypoint: $selectedWaypoint
)
2023-11-07 21:20:11 -08:00
}
2023-11-26 10:45:03 -08:00
.mapScope(mapScope)
.mapStyle(mapStyle)
.mapControls {
MapScaleView(scope: mapScope)
.mapControlVisibility(.automatic)
MapPitchToggle(scope: mapScope)
.mapControlVisibility(.automatic)
MapCompass(scope: mapScope)
.mapControlVisibility(.automatic)
}
.controlSize(.regular)
2024-09-08 04:47:14 +00:00
.onMapCameraChange(frequency: MapCameraUpdateFrequency.continuous, { context in
distance = context.camera.distance
})
.onTapGesture(count: 1, perform: { position in
2024-03-27 16:06:24 -07:00
newWaypointCoord = reader.convert(position, from: .local) ?? CLLocationCoordinate2D.init()
2024-03-23 18:01:20 -07:00
})
.gesture(
LongPressGesture(minimumDuration: 0.5)
.sequenced(before: SpatialTapGesture(coordinateSpace: .local))
.onEnded { value in
switch value {
2024-06-02 18:32:14 -07:00
case let .second(_, tapValue):
guard let point = tapValue?.location else {
2024-06-03 02:17:55 -07:00
Logger.services.error("Unable to retreive tap location from gesture data.")
2024-06-02 18:32:14 -07:00
return
}
2024-06-02 18:32:14 -07:00
guard let coordinate = reader.convert(point, from: .local) else {
2024-06-03 02:17:55 -07:00
Logger.services.error("Unable to convert local point to coordinate on map.")
2024-06-02 18:32:14 -07:00
return
}
2024-09-08 04:47:14 +00:00
centerMapAt(coordinate: coordinate)
2024-03-23 18:01:20 -07:00
2024-06-02 18:32:14 -07:00
newWaypointCoord = coordinate
editingWaypoint = WaypointEntity(context: context)
editingWaypoint!.name = "Waypoint Pin"
editingWaypoint!.expire = Date.now.addingTimeInterval(60 * 480)
editingWaypoint!.latitudeI = Int32((newWaypointCoord?.latitude ?? 0) * 1e7)
editingWaypoint!.longitudeI = Int32((newWaypointCoord?.longitude ?? 0) * 1e7)
editingWaypoint!.expire = Date.now.addingTimeInterval(60 * 480)
editingWaypoint!.id = 0
2024-06-03 02:17:55 -07:00
Logger.services.debug("Long press occured at Lat: \(coordinate.latitude) Long: \(coordinate.longitude)")
2024-06-02 18:32:14 -07:00
default: return
2024-03-23 18:01:20 -07:00
}
2023-11-14 18:11:25 -08:00
})
2023-11-07 21:20:11 -08:00
}
}
2023-11-08 18:41:47 -08:00
.sheet(item: $selectedPosition) { selection in
2023-11-09 17:43:38 -08:00
PositionPopover(position: selection, popover: false)
.padding()
}
.sheet(item: $selectedWaypoint) { selection in
2023-11-14 18:11:25 -08:00
WaypointForm(waypoint: selection)
2023-11-08 18:41:47 -08:00
.padding()
}
2023-11-11 09:44:03 -08:00
.sheet(item: $editingWaypoint) { selection in
2023-11-14 21:43:26 -08:00
WaypointForm(waypoint: selection, editMode: true)
2023-11-11 09:44:03 -08:00
.padding()
}
2024-08-16 16:26:54 -07:00
.sheet(isPresented: $editingSettings) {
MapSettingsForm(traffic: $showTraffic, pointsOfInterest: $showPointsOfInterest, mapLayer: $selectedMapLayer, meshMap: $isMeshMap)
2023-11-09 17:43:38 -08:00
}
.onChange(of: router.navigationState) {
guard case .map = router.navigationState.selectedTab else { return }
2024-08-04 21:42:02 -07:00
// TODO: handle deep link for waypoints
}
2024-10-05 16:35:42 -07:00
.onChange(of: selectedMapLayer) { _, newMapLayer in
2023-11-09 17:43:38 -08:00
switch selectedMapLayer {
case .standard:
UserDefaults.mapLayer = newMapLayer
mapStyle = MapStyle.standard(elevation: .realistic, pointsOfInterest: showPointsOfInterest ? .all : .excludingAll, showsTraffic: showTraffic)
case .hybrid:
UserDefaults.mapLayer = newMapLayer
mapStyle = MapStyle.hybrid(elevation: .realistic, pointsOfInterest: showPointsOfInterest ? .all : .excludingAll, showsTraffic: showTraffic)
case .satellite:
UserDefaults.mapLayer = newMapLayer
mapStyle = MapStyle.imagery(elevation: .realistic)
case .offline:
return
}
}
2024-08-16 16:26:54 -07:00
.sheet(isPresented: $editingFilters) {
NodeListFilter(
viaLora: $viaLora,
viaMqtt: $viaMqtt,
isOnline: $isOnline,
isPkiEncrypted: $isPkiEncrypted,
isFavorite: $isFavorite,
isEnvironment: $isEnvironment,
distanceFilter: $distanceFilter,
maximumDistance: $maxDistance,
hopsAway: $hopsAway,
roleFilter: $roleFilter,
deviceRoles: $deviceRoles
)
}
2024-04-02 11:16:32 -07:00
.safeAreaInset(edge: .bottom, alignment: .trailing) {
2023-11-09 17:43:38 -08:00
HStack {
Spacer()
2023-11-09 17:43:38 -08:00
Button(action: {
withAnimation {
2024-08-16 16:26:54 -07:00
editingSettings = !editingSettings
}
}) {
Image(systemName: editingSettings ? "info.circle.fill" : "info.circle")
.padding(.vertical, 5)
}
.tint(Color(UIColor.secondarySystemBackground))
.foregroundColor(.accentColor)
.buttonStyle(.borderedProminent)
2023-11-09 17:43:38 -08:00
}
.controlSize(.regular)
.padding(5)
}
2023-11-07 21:20:11 -08:00
}
2023-11-11 09:44:03 -08:00
.navigationBarItems(leading: MeshtasticLogo(), trailing: ZStack {
ConnectedDevice(bluetoothOn: bleManager.isSwitchedOn, deviceConnected: bleManager.connectedPeripheral != nil, name: (bleManager.connectedPeripheral != nil) ? bleManager.connectedPeripheral.shortName : "?")
2023-11-07 21:20:11 -08:00
})
2024-09-19 20:01:05 -07:00
.onFirstAppear {
2023-11-07 21:20:11 -08:00
UIApplication.shared.isIdleTimerDisabled = true
// let wayPointEntity = getWaypoint(id: Int64(deepLinkManager.waypointId) ?? -1, context: context)
// if wayPointEntity.id > 0 {
// position = .camera(MapCamera(centerCoordinate: wayPointEntity.coordinate, distance: 1000, heading: 0, pitch: 60))
2023-11-09 17:43:38 -08:00
switch selectedMapLayer {
case .standard:
mapStyle = MapStyle.standard(elevation: .realistic, pointsOfInterest: showPointsOfInterest ? .all : .excludingAll, showsTraffic: showTraffic)
case .hybrid:
mapStyle = MapStyle.hybrid(elevation: .realistic, pointsOfInterest: showPointsOfInterest ? .all : .excludingAll, showsTraffic: showTraffic)
case .satellite:
mapStyle = MapStyle.imagery(elevation: .realistic)
case .offline:
mapStyle = MapStyle.hybrid(elevation: .realistic, pointsOfInterest: showPointsOfInterest ? .all : .excludingAll, showsTraffic: showTraffic)
}
}
2023-11-07 21:20:11 -08:00
.onDisappear(perform: {
UIApplication.shared.isIdleTimerDisabled = false
})
}
2024-09-08 04:47:14 +00:00
// moves the map to a new coordinate
private func centerMapAt(coordinate: CLLocationCoordinate2D) {
withAnimation(.easeInOut(duration: 0.2), {
position = .camera(
MapCamera(
centerCoordinate: coordinate, // Set new center
distance: distance, // Preserve current zoom distance
heading: 0, // align north
pitch: 0 // set view to top down
)
)
})
}
2023-11-07 21:20:11 -08:00
}