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

269 lines
9.7 KiB
Swift
Raw Normal View History

2022-01-01 08:18:36 -08:00
/*
Abstract:
A view showing the details for a node.
*/
2022-01-01 08:18:36 -08:00
import SwiftUI
2023-01-27 20:22:17 -08:00
import WeatherKit
2022-01-01 08:18:36 -08:00
import MapKit
import CoreLocation
struct NodeDetail: View {
2023-03-06 10:33:18 -08:00
2022-01-01 08:18:36 -08:00
@Environment(\.managedObjectContext) var context
@EnvironmentObject var bleManager: BLEManager
2023-01-27 20:22:17 -08:00
@Environment(\.colorScheme) var colorScheme: ColorScheme
2023-04-25 17:56:57 -07:00
@AppStorage("meshMapType") private var meshMapType = 0
@AppStorage("meshMapShowNodeHistory") private var meshMapShowNodeHistory = false
@AppStorage("meshMapShowRouteLines") private var meshMapShowRouteLines = false
2023-05-06 16:15:12 -07:00
//@State private var mapType: MKMapType = .standard
@State private var selectedMapLayer: MapLayer = .standard
2023-05-05 17:13:35 -07:00
@State var mapRect: MKMapRect = MKMapRect()
@State var waypointCoordinate: WaypointCoordinate?
2023-01-16 17:40:28 -08:00
@State var editingWaypoint: Int = 0
2023-03-19 21:09:02 -07:00
@State private var loadedWeather: Bool = false
2022-09-17 09:00:53 -07:00
@State private var showingDetailsPopover = false
2023-02-25 20:34:25 -08:00
@State private var showingForecast = false
2022-09-17 09:00:53 -07:00
@State private var showingShutdownConfirm: Bool = false
@State private var showingRebootConfirm: Bool = false
2023-01-13 09:40:52 -08:00
@State private var showOverlays: Bool = true
@State private var customMapOverlay: MapViewSwiftUI.CustomMapOverlay? = MapViewSwiftUI.CustomMapOverlay(
mapName: "offlinemap",
tileType: "png",
canReplaceMapContent: true
)
2023-03-06 10:33:18 -08:00
2022-01-01 08:18:36 -08:00
var node: NodeInfoEntity
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
2023-01-27 20:22:17 -08:00
/// The current weather condition for the city.
@State private var condition: WeatherCondition?
@State private var temperature: Measurement<UnitTemperature>?
2023-02-20 22:15:35 -08:00
@State private var humidity: Int?
2023-01-27 20:22:17 -08:00
@State private var symbolName: String = "cloud.fill"
2023-03-06 10:33:18 -08:00
2023-01-27 20:22:17 -08:00
@State private var attributionLink: URL?
@State private var attributionLogo: URL?
2022-01-01 08:18:36 -08:00
var body: some View {
2023-03-06 10:33:18 -08:00
let hwModelString = node.user?.hwModel ?? "UNSET"
2023-05-16 05:54:12 -07:00
let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral?.num ?? -1, context: context)
2022-09-13 07:10:02 -07:00
NavigationStack {
2022-01-01 08:18:36 -08:00
GeometryReader { bounds in
VStack {
if node.positions?.count ?? 0 > 0 {
ZStack {
let positionArray = node.positions?.array as? [PositionEntity] ?? []
2023-03-28 06:50:23 -07:00
let lastTenThousand = Array(positionArray.prefix(10000))
// let todaysPositions = positionArray.filter { $0.time! >= Calendar.current.startOfDay(for: Date()) }
2022-05-27 23:56:34 -07:00
ZStack {
2023-01-20 21:49:54 -08:00
MapViewSwiftUI(onLongPress: { coord in
waypointCoordinate = WaypointCoordinate(id: .init(), coordinate: coord, waypointId: 0)
}, onWaypointEdit: { wpId in
if wpId > 0 {
waypointCoordinate = WaypointCoordinate(id: .init(), coordinate: nil, waypointId: Int64(wpId))
}
2023-05-05 17:13:35 -07:00
},
2023-05-06 16:15:12 -07:00
//visibleMapRect: $mapRect,
selectedMapLayer: selectedMapLayer,
2023-05-05 17:13:35 -07:00
positions: lastTenThousand,
waypoints: Array(waypoints),
2023-05-06 16:15:12 -07:00
//mapViewType: mapType,
userTrackingMode: MKUserTrackingMode.none,
showNodeHistory: meshMapShowNodeHistory,
showRouteLines: meshMapShowRouteLines,
customMapOverlay: self.customMapOverlay
2023-01-13 09:40:52 -08:00
)
2023-03-06 10:33:18 -08:00
VStack(alignment: .leading) {
Spacer()
2023-03-06 10:33:18 -08:00
HStack(alignment: .bottom, spacing: 1) {
2023-05-16 05:54:12 -07:00
Picker("Map Type", selection: $selectedMapLayer) {
ForEach(MapLayer.allCases, id: \.self) { layer in
if layer == MapLayer.offline && UserDefaults.enableOfflineMaps {
Text(layer.localized)
} else if layer != MapLayer.offline {
Text(layer.localized)
}
}
}
.onChange(of: (selectedMapLayer)) { newMapLayer in
UserDefaults.mapLayer = newMapLayer
}
.background(.thinMaterial, in: RoundedRectangle(cornerRadius: 12, style: .continuous))
.pickerStyle(.menu)
.padding(5)
2023-01-27 20:22:17 -08:00
VStack {
Label(temperature?.formatted(.measurement(width: .narrow)) ?? "??", systemImage: symbolName)
2023-01-27 20:22:17 -08:00
.font(.caption)
2023-03-06 10:33:18 -08:00
2023-02-20 22:15:35 -08:00
Label("\(humidity ?? 0)%", systemImage: "humidity")
.font(.caption2)
2023-01-27 20:22:17 -08:00
}
.padding(10)
.background(.thinMaterial, in: RoundedRectangle(cornerRadius: 12, style: .continuous))
.padding(5)
2023-02-25 20:34:25 -08:00
#if targetEnvironment(macCatalyst)
.popover(isPresented: $showingForecast,
arrowEdge: .top) {
Text("Today's Weather Forecast")
.font(.title)
.padding()
2023-03-14 14:33:27 -07:00
let nodeLocation = node.positions?.lastObject as? PositionEntity
NodeWeatherForecastView(location: CLLocation(latitude: nodeLocation?.nodeCoordinate!.latitude ?? LocationHelper.currentLocation.latitude, longitude: nodeLocation?.nodeCoordinate!.longitude ?? LocationHelper.currentLocation.longitude) )
2023-02-25 20:34:25 -08:00
.frame(height: 250)
}
#else
.sheet(isPresented: $showingForecast) {
Text("Today's Weather Forecast")
.font(.title)
.padding()
2023-03-14 14:33:27 -07:00
let nodeLocation = node.positions?.lastObject as? PositionEntity
NodeWeatherForecastView(location: CLLocation(latitude: nodeLocation?.nodeCoordinate!.latitude ?? LocationHelper.currentLocation.latitude, longitude: nodeLocation?.nodeCoordinate!.longitude ?? LocationHelper.currentLocation.longitude) ).frame(height: 250)
2023-02-25 20:34:25 -08:00
.presentationDetents([.medium])
.presentationDragIndicator(.automatic)
}
#endif
.gesture(
LongPressGesture(minimumDuration: 0.5)
2023-03-06 10:33:18 -08:00
.onEnded { _ in
2023-02-25 20:34:25 -08:00
showingForecast = true
}
)
}
2022-10-05 05:06:45 -07:00
}
2022-01-01 08:18:36 -08:00
}
.ignoresSafeArea(.all, edges: [.top, .leading, .trailing])
.frame(idealWidth: bounds.size.width, minHeight: bounds.size.height / 1.65)
2022-07-08 06:31:47 -07:00
}
} else {
HStack {
}
.padding([.top], 20)
2022-07-08 06:31:47 -07:00
}
2023-03-06 10:33:18 -08:00
2023-04-03 17:48:58 -07:00
ScrollView() {
NodeInfoView(node: node)
2023-03-06 10:33:18 -08:00
if self.bleManager.connectedPeripheral != nil && node.metadata != nil {
2022-10-02 09:19:03 -07:00
HStack {
2023-05-16 05:54:12 -07:00
if node.metadata?.canShutdown ?? false {
2023-03-06 10:33:18 -08:00
2022-10-02 09:19:03 -07:00
Button(action: {
showingShutdownConfirm = true
}) {
Label("Power Off", systemImage: "power")
}
.buttonStyle(.bordered)
.buttonBorderShape(.capsule)
.controlSize(.large)
.padding()
.confirmationDialog(
2022-12-13 08:47:14 -08:00
"are.you.sure",
2022-10-02 09:19:03 -07:00
isPresented: $showingShutdownConfirm
) {
Button("Shutdown Node?", role: .destructive) {
if !bleManager.sendShutdown(fromUser: connectedNode!.user!, toUser: node.user!, adminIndex: connectedNode!.myInfo!.adminIndex) {
2022-10-02 09:19:03 -07:00
print("Shutdown Failed")
}
}
}
}
2023-03-06 10:33:18 -08:00
2022-10-02 09:19:03 -07:00
Button(action: {
showingRebootConfirm = true
}) {
Label("reboot", systemImage: "arrow.triangle.2.circlepath")
2022-10-02 09:19:03 -07:00
}
.buttonStyle(.bordered)
.buttonBorderShape(.capsule)
.controlSize(.large)
.padding()
.confirmationDialog("are.you.sure",
2023-01-29 00:16:17 -08:00
isPresented: $showingRebootConfirm
) {
Button("reboot.node", role: .destructive) {
if !bleManager.sendReboot(fromUser: connectedNode!.user!, toUser: node.user!, adminIndex: connectedNode!.myInfo!.adminIndex) {
print("Reboot Failed")
2022-10-02 09:19:03 -07:00
}
}
}
}
.padding(5)
2023-01-29 00:16:17 -08:00
Divider()
}
if node.positions?.count ?? 0 > 0 {
VStack {
AsyncImage(url: attributionLogo) { image in
image
.resizable()
.scaledToFit()
} placeholder: {
ProgressView()
.controlSize(.mini)
}
.frame(height: 15)
2023-03-06 10:33:18 -08:00
Link("Other data sources", destination: attributionLink ?? URL(string: "https://weather-data.apple.com/legal-attribution.html")!)
2023-01-29 00:16:17 -08:00
}
.font(.footnote)
}
2022-01-01 08:18:36 -08:00
}
}
.edgesIgnoringSafeArea([.leading, .trailing])
.sheet(item: $waypointCoordinate, content: { wpc in
WaypointFormView(coordinate: wpc)
.presentationDetents([.medium, .large])
.presentationDragIndicator(.automatic)
})
.navigationBarTitle(String(node.user?.longName ?? "unknown".localized), displayMode: .inline)
2022-09-13 09:56:03 -07:00
.navigationBarItems(trailing:
ZStack {
ConnectedDevice(
bluetoothOn: bleManager.isSwitchedOn,
deviceConnected: bleManager.connectedPeripheral != nil,
name: (bleManager.connectedPeripheral != nil) ? bleManager.connectedPeripheral.shortName : "????")
})
2022-09-13 09:56:03 -07:00
.onAppear {
2022-11-12 08:48:01 -08:00
self.bleManager.context = context
2023-05-06 16:15:12 -07:00
//mapType = .standard// MeshMapTypes(rawValue: meshMapType)?.MKMapTypeValue() ?? .standard
2022-09-13 09:56:03 -07:00
}
2023-03-19 21:09:02 -07:00
.task(id: node.num) {
if !loadedWeather {
do {
if node.positions?.count ?? 0 > 0 {
let mostRecent = node.positions?.lastObject as? PositionEntity
let weather = try await WeatherService.shared.weather(for: mostRecent?.nodeLocation ?? CLLocation(latitude: LocationHelper.currentLocation.latitude, longitude: LocationHelper.currentLocation.longitude))
condition = weather.currentWeather.condition
temperature = weather.currentWeather.temperature
humidity = Int(weather.currentWeather.humidity * 100)
symbolName = weather.currentWeather.symbolName
let attribution = try await WeatherService.shared.attribution
attributionLink = attribution.legalPageURL
attributionLogo = colorScheme == .light ? attribution.combinedMarkLightURL : attribution.combinedMarkDarkURL
loadedWeather = true
}
} catch {
print("Could not gather weather information...", error.localizedDescription)
condition = .clear
symbolName = "cloud.fill"
}
}
}
}
.padding(.bottom, 2)
}
2022-01-01 08:18:36 -08:00
}
}