2021-09-14 21:38:12 -07:00
|
|
|
//
|
2021-11-29 21:35:23 -08:00
|
|
|
// NodeList.swift
|
2022-11-11 08:06:45 -08:00
|
|
|
// Meshtastic
|
2021-09-14 21:38:12 -07:00
|
|
|
//
|
2022-11-11 08:06:45 -08:00
|
|
|
// Copyright(c) Garth Vander Houwen 8/7/21.
|
2021-09-14 21:38:12 -07:00
|
|
|
//
|
|
|
|
|
|
|
|
|
|
// Abstract:
|
2021-09-23 20:10:53 -07:00
|
|
|
// A view showing a list of devices that have been seen on the mesh network from the perspective of the connected device.
|
2021-09-14 21:38:12 -07:00
|
|
|
|
|
|
|
|
import SwiftUI
|
2022-08-19 23:26:02 -07:00
|
|
|
import CoreLocation
|
2021-09-14 21:38:12 -07:00
|
|
|
|
|
|
|
|
struct NodeList: View {
|
2021-12-25 23:48:12 -08:00
|
|
|
|
2021-12-15 23:53:45 -08:00
|
|
|
@Environment(\.managedObjectContext) var context
|
|
|
|
|
@EnvironmentObject var bleManager: BLEManager
|
2021-12-25 23:48:12 -08:00
|
|
|
|
2021-12-15 23:53:45 -08:00
|
|
|
@FetchRequest(
|
2022-09-13 05:44:04 -07:00
|
|
|
sortDescriptors: [NSSortDescriptor(key: "lastHeard", ascending: false)],
|
2021-12-15 23:53:45 -08:00
|
|
|
animation: .default)
|
2021-12-25 23:48:12 -08:00
|
|
|
|
2022-07-07 00:29:52 -07:00
|
|
|
private var nodes: FetchedResults<NodeInfoEntity>
|
2021-12-25 23:48:12 -08:00
|
|
|
|
2023-03-06 10:33:18 -08:00
|
|
|
@State private var selection: NodeInfoEntity? // Nothing selected by default.
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2023-04-18 00:09:13 -07:00
|
|
|
var body: some View {
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2022-09-13 05:44:04 -07:00
|
|
|
NavigationSplitView {
|
2023-05-16 05:54:12 -07:00
|
|
|
let connectedNodeNum = Int(bleManager.connectedPeripheral != nil ? bleManager.connectedPeripheral?.num ?? 0 : 0)
|
|
|
|
|
let connectedNode = nodes.first(where: { $0.num == connectedNodeNum })
|
2023-04-18 00:09:13 -07:00
|
|
|
List(nodes, id: \.self, selection: $selection) { node in
|
2021-12-15 23:53:45 -08:00
|
|
|
if nodes.count == 0 {
|
2022-12-12 22:33:06 -08:00
|
|
|
Text("no.nodes").font(.title)
|
2022-09-13 05:44:04 -07:00
|
|
|
} else {
|
2022-10-17 20:54:41 -07:00
|
|
|
NavigationLink(value: node) {
|
2023-03-04 08:52:40 -08:00
|
|
|
let connected: Bool = (bleManager.connectedPeripheral != nil && bleManager.connectedPeripheral?.num ?? -1 == node.num)
|
2023-05-21 08:03:23 -07:00
|
|
|
LazyVStack(alignment: .leading) {
|
2022-10-17 20:54:41 -07:00
|
|
|
HStack {
|
2023-05-16 15:15:58 -07:00
|
|
|
VStack(alignment: .leading) {
|
|
|
|
|
CircleText(text: node.user?.shortName ?? "???", color: Color(UIColor(hex: UInt32(node.num))), circleSize: 65, fontSize: (node.user?.shortName ?? "???").isEmoji() ? 44 : 22, brightness: 0.0, textColor: UIColor(hex: UInt32(node.num)).isLight() ? .black : .white)
|
|
|
|
|
.padding(.trailing, 5)
|
|
|
|
|
let deviceMetrics = node.telemetries?.filtered(using: NSPredicate(format: "metricsType == 0"))
|
|
|
|
|
if deviceMetrics?.count ?? 0 >= 1 {
|
|
|
|
|
let mostRecent = deviceMetrics?.lastObject as? TelemetryEntity
|
|
|
|
|
BatteryLevelCompact(batteryLevel: mostRecent?.batteryLevel, font: .caption2, iconFont: .callout, color: .accentColor)
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-19 13:21:24 -08:00
|
|
|
VStack(alignment: .leading) {
|
2023-05-10 13:44:27 -07:00
|
|
|
Text(node.user?.longName ?? "unknown".localized)
|
|
|
|
|
.fontWeight(.medium)
|
|
|
|
|
.font(.callout)
|
2022-11-19 13:21:24 -08:00
|
|
|
if connected {
|
2023-05-16 05:54:12 -07:00
|
|
|
HStack(alignment: .bottom) {
|
2022-11-19 13:21:24 -08:00
|
|
|
Image(systemName: "repeat.circle.fill")
|
2023-05-10 13:44:27 -07:00
|
|
|
.font(.callout)
|
2022-11-19 13:21:24 -08:00
|
|
|
.symbolRenderingMode(.hierarchical)
|
2023-05-10 13:44:27 -07:00
|
|
|
Text("connected").font(.callout)
|
2022-11-20 16:37:37 -08:00
|
|
|
.foregroundColor(.green)
|
2022-11-19 13:21:24 -08:00
|
|
|
}
|
|
|
|
|
}
|
2023-03-04 08:52:40 -08:00
|
|
|
if node.positions?.count ?? 0 > 0 && (bleManager.connectedPeripheral != nil && bleManager.connectedPeripheral?.num ?? -1 != node.num) {
|
2022-11-19 13:21:24 -08:00
|
|
|
HStack(alignment: .bottom) {
|
|
|
|
|
let lastPostion = node.positions!.reversed()[0] as! PositionEntity
|
2023-04-18 00:09:13 -07:00
|
|
|
let myCoord = CLLocation(latitude: LocationHelper.currentLocation.latitude, longitude: LocationHelper.currentLocation.longitude)
|
|
|
|
|
if lastPostion.nodeCoordinate != nil && myCoord.coordinate.longitude != LocationHelper.DefaultLocation.longitude && myCoord.coordinate.latitude != LocationHelper.DefaultLocation.latitude {
|
2023-01-10 06:49:19 -08:00
|
|
|
let nodeCoord = CLLocation(latitude: lastPostion.nodeCoordinate!.latitude, longitude: lastPostion.nodeCoordinate!.longitude)
|
2022-11-19 13:21:24 -08:00
|
|
|
let metersAway = nodeCoord.distance(from: myCoord)
|
|
|
|
|
Image(systemName: "lines.measurement.horizontal")
|
2023-05-10 13:44:27 -07:00
|
|
|
.font(.footnote)
|
2022-11-19 13:21:24 -08:00
|
|
|
.symbolRenderingMode(.hierarchical)
|
2023-05-10 13:44:27 -07:00
|
|
|
DistanceText(meters: metersAway).font(.footnote)
|
2022-11-19 13:21:24 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-02 15:00:15 -07:00
|
|
|
if node.channel > 0 {
|
|
|
|
|
HStack(alignment: .bottom) {
|
|
|
|
|
Image(systemName: "fibrechannel")
|
2023-05-10 13:44:27 -07:00
|
|
|
.font(.footnote)
|
2023-04-02 15:00:15 -07:00
|
|
|
.symbolRenderingMode(.hierarchical)
|
|
|
|
|
Text("Channel: \(node.channel)")
|
2023-05-10 13:44:27 -07:00
|
|
|
.font(.footnote)
|
2023-04-02 15:00:15 -07:00
|
|
|
}
|
|
|
|
|
}
|
2023-04-03 17:48:58 -07:00
|
|
|
HStack(alignment: .bottom) {
|
|
|
|
|
Image(systemName: "clock.badge.checkmark.fill")
|
2023-05-10 13:44:27 -07:00
|
|
|
.font(.caption)
|
2023-04-03 17:48:58 -07:00
|
|
|
.symbolRenderingMode(.hierarchical)
|
|
|
|
|
LastHeardText(lastHeard: node.lastHeard)
|
2023-05-10 13:44:27 -07:00
|
|
|
.font(.caption)
|
2023-04-03 17:48:58 -07:00
|
|
|
}
|
2023-05-16 05:54:12 -07:00
|
|
|
if !connected {
|
|
|
|
|
HStack(alignment: .bottom) { let preset = ModemPresets(rawValue: Int(connectedNode?.loRaConfig?.modemPreset ?? 0))
|
|
|
|
|
LoRaSignalStrengthMeter(snr: node.snr, rssi: node.rssi, preset: preset ?? ModemPresets.longFast, compact: true)
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-03 08:50:22 -07:00
|
|
|
}
|
2022-11-19 13:21:24 -08:00
|
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
2021-12-16 22:45:18 -08:00
|
|
|
}
|
2021-12-15 23:53:45 -08:00
|
|
|
}
|
2022-10-17 20:54:41 -07:00
|
|
|
}
|
2022-11-20 15:55:46 -08:00
|
|
|
.padding([.top, .bottom])
|
2022-09-13 05:44:04 -07:00
|
|
|
}
|
|
|
|
|
}
|
2023-08-10 10:27:54 -07:00
|
|
|
.navigationTitle(String.localizedStringWithFormat("nodes %@".localized, String(nodes.count)))
|
2022-10-17 21:25:15 -07:00
|
|
|
.navigationBarItems(leading:
|
|
|
|
|
MeshtasticLogo()
|
|
|
|
|
)
|
|
|
|
|
.onAppear {
|
2022-10-17 21:32:14 -07:00
|
|
|
self.bleManager.context = context
|
2022-10-17 21:25:15 -07:00
|
|
|
}
|
2022-10-14 22:18:28 -07:00
|
|
|
} detail: {
|
|
|
|
|
if let node = selection {
|
2023-03-06 10:33:18 -08:00
|
|
|
NodeDetail(node: node)
|
2022-10-14 22:18:28 -07:00
|
|
|
} else {
|
2022-12-12 22:33:06 -08:00
|
|
|
Text("select.node")
|
2022-09-13 05:44:04 -07:00
|
|
|
}
|
2022-10-14 22:18:28 -07:00
|
|
|
}
|
2022-10-17 21:32:14 -07:00
|
|
|
}
|
2021-09-14 21:38:12 -07:00
|
|
|
}
|