2022-01-01 08:18:36 -08:00
|
|
|
/*
|
2023-01-10 06:49:19 -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
|
2023-03-19 22:11:46 -07:00
|
|
|
@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()
|
2023-04-22 12:31:06 -07:00
|
|
|
@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
|
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-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?
|
2023-03-20 13:10:59 -07:00
|
|
|
|
|
|
|
|
|
2022-01-01 08:18:36 -08:00
|
|
|
var body: some View {
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2022-06-12 22:07:58 -07: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 {
|
2022-07-07 00:29:52 -07:00
|
|
|
if node.positions?.count ?? 0 > 0 {
|
2023-01-10 06:49:19 -08:00
|
|
|
ZStack {
|
2023-03-22 09:58:46 -07:00
|
|
|
let positionArray = node.positions?.array as? [PositionEntity] ?? []
|
2023-03-28 06:50:23 -07:00
|
|
|
let lastTenThousand = Array(positionArray.prefix(10000))
|
2023-03-24 22:57:36 -07:00
|
|
|
// 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
|
2023-04-22 12:31:06 -07:00
|
|
|
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,
|
2023-03-03 19:41:02 -08:00
|
|
|
userTrackingMode: MKUserTrackingMode.none,
|
2023-03-19 22:11:46 -07:00
|
|
|
showNodeHistory: meshMapShowNodeHistory,
|
2023-03-21 17:53:53 -07:00
|
|
|
showRouteLines: meshMapShowRouteLines,
|
2023-03-20 13:10:59 -07:00
|
|
|
customMapOverlay: self.customMapOverlay
|
2023-01-13 09:40:52 -08:00
|
|
|
)
|
2023-03-06 10:33:18 -08:00
|
|
|
VStack(alignment: .leading) {
|
2023-01-10 06:49:19 -08:00
|
|
|
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 {
|
2023-02-15 14:52:49 -08:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
)
|
2023-01-10 06:49:19 -08:00
|
|
|
}
|
2022-10-05 05:06:45 -07:00
|
|
|
}
|
2022-01-01 08:18:36 -08:00
|
|
|
}
|
2023-01-10 06:49:19 -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 {
|
2022-09-04 21:45:03 -07:00
|
|
|
HStack {
|
|
|
|
|
}
|
2023-01-10 06:49:19 -08:00
|
|
|
.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
|
|
|
|
2023-03-13 19:27:36 -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) {
|
2023-02-06 18:45:03 -08:00
|
|
|
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
|
|
|
|
|
}) {
|
2022-12-30 22:30:15 -08:00
|
|
|
Label("reboot", systemImage: "arrow.triangle.2.circlepath")
|
2022-10-02 09:19:03 -07:00
|
|
|
}
|
|
|
|
|
.buttonStyle(.bordered)
|
|
|
|
|
.buttonBorderShape(.capsule)
|
|
|
|
|
.controlSize(.large)
|
|
|
|
|
.padding()
|
2022-12-30 22:30:15 -08:00
|
|
|
.confirmationDialog("are.you.sure",
|
2023-01-29 00:16:17 -08:00
|
|
|
isPresented: $showingRebootConfirm
|
2023-01-10 06:49:19 -08:00
|
|
|
) {
|
|
|
|
|
Button("reboot.node", role: .destructive) {
|
2023-02-06 18:45:03 -08:00
|
|
|
if !bleManager.sendReboot(fromUser: connectedNode!.user!, toUser: node.user!, adminIndex: connectedNode!.myInfo!.adminIndex) {
|
2023-01-10 06:49:19 -08:00
|
|
|
print("Reboot Failed")
|
2022-10-02 09:19:03 -07:00
|
|
|
}
|
2023-01-10 06:49:19 -08:00
|
|
|
}
|
|
|
|
|
}
|
2023-01-12 10:51:51 -08:00
|
|
|
}
|
|
|
|
|
.padding(5)
|
2023-01-29 00:16:17 -08:00
|
|
|
Divider()
|
|
|
|
|
}
|
2023-02-02 08:54:56 -08:00
|
|
|
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
|
|
|
|
2023-02-02 08:54:56 -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
|
|
|
}
|
2023-02-02 08:54:56 -08:00
|
|
|
.font(.footnote)
|
2023-01-12 10:51:51 -08:00
|
|
|
}
|
2022-01-01 08:18:36 -08:00
|
|
|
}
|
2022-01-06 08:41:44 -08:00
|
|
|
}
|
|
|
|
|
.edgesIgnoringSafeArea([.leading, .trailing])
|
2023-04-22 12:31:06 -07:00
|
|
|
.sheet(item: $waypointCoordinate, content: { wpc in
|
|
|
|
|
WaypointFormView(coordinate: wpc)
|
|
|
|
|
.presentationDetents([.medium, .large])
|
|
|
|
|
.presentationDragIndicator(.automatic)
|
|
|
|
|
})
|
2023-05-05 09:27:24 -07:00
|
|
|
.navigationBarTitle(String(node.user?.longName ?? "unknown".localized), displayMode: .inline)
|
2022-09-13 09:56:03 -07:00
|
|
|
.navigationBarItems(trailing:
|
2023-01-19 22:04:18 -08:00
|
|
|
ZStack {
|
2023-01-10 06:49:19 -08:00
|
|
|
ConnectedDevice(
|
|
|
|
|
bluetoothOn: bleManager.isSwitchedOn,
|
|
|
|
|
deviceConnected: bleManager.connectedPeripheral != nil,
|
|
|
|
|
name: (bleManager.connectedPeripheral != nil) ? bleManager.connectedPeripheral.shortName : "????")
|
2023-01-19 22:04:18 -08:00
|
|
|
})
|
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"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-07 00:29:52 -07:00
|
|
|
}
|
2023-04-22 12:31:06 -07:00
|
|
|
.padding(.bottom, 2)
|
2022-07-07 00:29:52 -07:00
|
|
|
}
|
2022-01-01 08:18:36 -08:00
|
|
|
}
|
|
|
|
|
}
|