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

113 lines
3.1 KiB
Swift
Raw Normal View History

2021-08-20 07:56:05 -07:00
//
2021-11-29 21:35:23 -08:00
// NodeMap.swift
// MeshtasticClient
2021-08-20 07:56:05 -07:00
//
// Created by Garth Vander Houwen on 8/7/21.
// Copyright © 2021 Apple. All rights reserved.
//
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
2021-12-15 23:53:45 -08:00
//@AppStorage("meshMapType") var meshMapType: String = "hybrid"
2021-12-20 22:29:28 -08:00
@State private var showLabels: Bool = false
@State private var annotationItems: [MapLocation] = []
@FetchRequest( sortDescriptors: [NSSortDescriptor(keyPath: \NodeInfoEntity.lastHeard, ascending: false)], animation: .default)
2021-12-15 23:53:45 -08:00
2021-12-15 23:53:45 -08:00
private var locationNodes: FetchedResults<NodeInfoEntity>
2021-12-20 22:29:28 -08:00
var annotations: [MapLocation] = [MapLocation]()
2021-11-29 15:59:06 -08:00
2021-08-20 07:56:05 -07:00
var body: some View {
let location = LocationHelper.currentLocation
let currentCoordinatePosition = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
2021-08-20 07:56:05 -07:00
let regionBinding = Binding<MKCoordinateRegion>(
get: {
MKCoordinateRegion(center: currentCoordinatePosition, span: MKCoordinateSpan(latitudeDelta: 0.0359, longitudeDelta: 0.0359))
2021-08-20 07:56:05 -07:00
},
set: { _ in }
)
2021-12-20 22:29:28 -08:00
2021-12-21 20:15:07 +13:00
/*ForEach ( locationNodes ) { node in
let mostRecent = node.positions?.lastObject as! PositionEntity
if mostRecent.coordinate != nil {
annotations.append(MapLocation(name: node.user?.shortName! ?? "???", coordinate: mostRecent.coordinate!))
}
}*/
NavigationView {
2021-11-29 15:59:06 -08:00
2021-12-24 20:18:42 +13:00
ZStack {
2021-12-16 14:13:54 -08:00
2021-12-21 20:15:07 +13:00
/*Map(coordinateRegion: regionBinding,
interactionModes: [.all],
showsUserLocation: true,
2021-12-21 20:15:07 +13:00
userTrackingMode: .constant(.follow),
annotationItems: self.locationNodes.filter({ nodeinfo in
return nodeinfo.positions != nil && nodeinfo.positions!.count > 0// && (nodeinfo.positions?.lastObject as? AnyObject)?.coordinate != nil
2021-12-24 20:18:42 +13:00
2021-12-21 20:15:07 +13:00
})
) { locationNode in
2021-12-20 22:29:28 -08:00
2021-12-21 20:15:07 +13:00
return MapAnnotation(
coordinate: (locationNode.positions!.lastObject as! PositionEntity).coordinate ?? CLLocationCoordinate2D(latitude: 0, longitude: 0),
content: {
CircleText(text: locationNode.user!.shortName ?? "???", color: .accentColor)
}
)
2021-12-24 20:18:42 +13:00
}*/
MapView(nodes: self.locationNodes)//.environmentObject(userSettings)
2021-12-20 22:29:28 -08:00
//}
.frame(maxHeight: .infinity)
2021-12-20 22:29:28 -08:00
.ignoresSafeArea(.all, edges: [.leading, .trailing])
}
.navigationTitle("Mesh Map")
.navigationBarTitleDisplayMode(.inline)
.navigationBarItems(trailing:
2021-11-29 15:59:06 -08:00
ZStack {
ConnectedDevice(
bluetoothOn: bleManager.isSwitchedOn,
deviceConnected: bleManager.connectedPeripheral != nil,
name: (bleManager.connectedPeripheral != nil) ? bleManager.connectedPeripheral.shortName :
"???")
})
.onAppear(perform: {
self.bleManager.context = context
2021-11-29 15:59:06 -08:00
})
}
.navigationViewStyle(StackNavigationViewStyle())
2021-08-20 07:56:05 -07:00
}
}
2021-09-18 21:11:54 -07:00
struct NodeMap_Previews: PreviewProvider {
static let bleManager = BLEManager()
2021-09-18 21:11:54 -07:00
static var previews: some View {
NodeMap()
2021-09-18 21:11:54 -07:00
}
}