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

121 lines
4.6 KiB
Swift
Raw Normal View History

//
2021-11-29 21:35:23 -08:00
// NodeList.swift
// Meshtastic
//
// Copyright(c) Garth Vander Houwen 8/7/21.
//
// Abstract:
// A view showing a list of devices that have been seen on the mesh network from the perspective of the connected device.
import SwiftUI
2022-08-19 23:26:02 -07:00
import CoreLocation
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(
sortDescriptors: [NSSortDescriptor(key: "lastHeard", ascending: false)],
2021-12-15 23:53:45 -08:00
animation: .default)
2021-12-25 23:48:12 -08: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
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)
} else {
NavigationLink(value: node) {
2023-03-04 08:52:40 -08:00
let connected: Bool = (bleManager.connectedPeripheral != nil && bleManager.connectedPeripheral?.num ?? -1 == node.num)
LazyVStack(alignment: .leading) {
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)
}
}
VStack(alignment: .leading) {
Text(node.user?.longName ?? "unknown".localized)
.fontWeight(.medium)
.font(.callout)
if connected {
2023-05-16 05:54:12 -07:00
HStack(alignment: .bottom) {
Image(systemName: "repeat.circle.fill")
.font(.callout)
.symbolRenderingMode(.hierarchical)
Text("connected").font(.callout)
.foregroundColor(.green)
}
}
2023-03-04 08:52:40 -08:00
if node.positions?.count ?? 0 > 0 && (bleManager.connectedPeripheral != nil && bleManager.connectedPeripheral?.num ?? -1 != node.num) {
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 {
let nodeCoord = CLLocation(latitude: lastPostion.nodeCoordinate!.latitude, longitude: lastPostion.nodeCoordinate!.longitude)
let metersAway = nodeCoord.distance(from: myCoord)
Image(systemName: "lines.measurement.horizontal")
.font(.footnote)
.symbolRenderingMode(.hierarchical)
DistanceText(meters: metersAway).font(.footnote)
}
}
}
2023-04-02 15:00:15 -07:00
if node.channel > 0 {
HStack(alignment: .bottom) {
Image(systemName: "fibrechannel")
.font(.footnote)
2023-04-02 15:00:15 -07:00
.symbolRenderingMode(.hierarchical)
Text("Channel: \(node.channel)")
.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")
.font(.caption)
2023-04-03 17:48:58 -07:00
.symbolRenderingMode(.hierarchical)
LastHeardText(lastHeard: node.lastHeard)
.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)
}
}
}
.frame(maxWidth: .infinity, alignment: .leading)
}
2021-12-15 23:53:45 -08:00
}
}
2022-11-20 15:55:46 -08:00
.padding([.top, .bottom])
}
}
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
}
} detail: {
if let node = selection {
2023-03-06 10:33:18 -08:00
NodeDetail(node: node)
} else {
2022-12-12 22:33:06 -08:00
Text("select.node")
}
}
2022-10-17 21:32:14 -07:00
}
}