Meshtastic-Apple/Meshtastic/Views/Nodes/Helpers/NodeDetail.swift
Garth Vander Houwen 6bb880d503
Two Column Node List (#1425)
* Restore BLE State

* Log privacy

* AccessoryManager to handle restored connection

* Comment task out

* Switch the node list to a two column layout

* Keep asian translations of channel details string

* Update restore state function based on conversation with jake

* Update Meshtastic/Accessory/Transports/Bluetooth Low Energy/BLETransport.swift

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update Meshtastic/Accessory/Transports/Bluetooth Low Energy/BLETransport.swift

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* always show node list search bar

* Update auto correct modifier

* Dont show online animations for ios 17, remove online animation from node map, remove online circle from position popover

* Work in progress.

* Update detents

* Gate the discovery process while restoring

* Use geometry reader to size weather tiles on node details

* Update BLE Transport

* Update location weather condistion styles

* Log privacy in didReceive

* Remove extra dividers from admin key config, fix onboarding typo

* Bump minimum catalyst target

* Bump mac target version

* Use @FetchRequest for UserList to try and use less memory on ios 17

* Revert change to @fetchrequest

* Stab in the dark for Devices crash

* Updated UserList (back?) to @FetchRequest

* Set mac minimum to 15

* Nil out continuation after use

* Use @FetchRequest for the node list to stop crashes on iOS 17

* Handle failed connections during restoration

---------

Co-authored-by: Jake-B <jake-b@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-09-24 20:55:42 -07:00

609 lines
20 KiB
Swift

/*
Abstract:
A view showing the details for a node.
*/
import SwiftUI
import WeatherKit
import MapKit
import CoreLocation
import OSLog
struct NodeDetail: View {
private let gridItemLayout = Array(repeating: GridItem(.flexible(), spacing: 10), count: 2)
private static let relativeFormatter: RelativeDateTimeFormatter = {
let formatter = RelativeDateTimeFormatter()
formatter.unitsStyle = .full
return formatter
}()
var modemPreset: ModemPresets = ModemPresets(
rawValue: UserDefaults.modemPreset
) ?? ModemPresets.longFast
@Environment(\.managedObjectContext) var context
@EnvironmentObject var accessoryManager: AccessoryManager
@State private var showingShutdownConfirm: Bool = false
@State private var showingRebootConfirm: Bool = false
@State private var dateFormatRelative: Bool = true
var connectedNode: NodeInfoEntity?
@ObservedObject var node: NodeInfoEntity
@State private var environmentSectionHeight: CGFloat = 0
var body: some View {
NavigationStack {
List {
let connectedNode = getNodeInfo(
id: accessoryManager.activeDeviceNum ?? -1,
context: context
)
Section("Hardware") {
NodeInfoItem(node: node)
}
.accessibilityElement(children: .combine)
Section("Node") { // Node
HStack(alignment: .center) {
Spacer()
CircleText(
text: node.user?.shortName ?? "?",
color: Color(UIColor(hex: UInt32(node.num))),
circleSize: 75
)
if node.snr != 0 && !node.viaMqtt && node.hopsAway == 0 {
Spacer()
VStack {
let signalStrength = getLoRaSignalStrength(snr: node.snr, rssi: node.rssi, preset: modemPreset)
LoRaSignalStrengthIndicator(signalStrength: signalStrength)
Text("Signal \(signalStrength.description)").font(.footnote)
Text("SNR \(String(format: "%.2f", node.snr))dB")
.foregroundColor(getSnrColor(snr: node.snr, preset: modemPreset))
.font(.caption)
Text("RSSI \(node.rssi)dB")
.foregroundColor(getRssiColor(rssi: node.rssi))
.font(.caption)
}
.accessibilityElement(children: .combine)
}
if node.telemetries?.count ?? 0 > 0 {
Spacer()
BatteryGauge(node: node)
}
Spacer()
}
.accessibilityElement(children: .combine)
.listRowSeparator(.hidden)
if let user = node.user {
if !user.keyMatch {
Label {
VStack(alignment: .leading) {
Text("Public Key Mismatch")
.font(.title3)
.foregroundStyle(.red)
Text("Verify who you are messaging with by comparing public keys in person or over the phone. The most recent public key for this node does not match the previously recorded key. You can delete the node and let it exchange keys again if the key change was due to a factory reset or other intentional action but this also may indicate a more serious security problem.")
.foregroundStyle(.secondary)
.font(.callout)
}
.accessibilityElement(children: .combine)
} icon: {
Image(systemName: "key.slash.fill")
.symbolRenderingMode(.multicolor)
.foregroundStyle(.red)
}
}
}
HStack {
Label {
Text("Node Number")
} icon: {
Image(systemName: "number")
.symbolRenderingMode(.hierarchical)
}
Spacer()
Text(String(node.num))
.textSelection(.enabled)
}
.accessibilityElement(children: .combine)
HStack {
Label {
Text("User Id")
} icon: {
Image(systemName: "person")
.symbolRenderingMode(.multicolor)
}
Spacer()
Text(node.num.toHex())
.textSelection(.enabled)
}
.accessibilityElement(children: .combine)
let connectedNode = getNodeInfo(id: accessoryManager.activeDeviceNum ?? 0, context: context)
if let user = node.user, user.keyMatch {
let publicKey = node.num == connectedNode?.num
? node.securityConfig?.publicKey?.base64EncodedString() ?? ""
: user.publicKey?.base64EncodedString() ?? ""
HStack {
Label {
Text("Public Key")
} icon: {
Image(systemName: "lock.fill")
.foregroundColor(.green)
}
Spacer()
Button(action: {
context.perform {
UIPasteboard.general.string = publicKey
}
}) {
HStack {
Image(systemName: "key.horizontal.fill")
Text("Copy")
}
}
}
.accessibilityElement(children: .combine)
}
if let metadata = node.metadata {
HStack {
Label {
Text("Firmware Version")
} icon: {
Image(systemName: "memorychip")
.symbolRenderingMode(.multicolor)
}
Spacer()
Text(metadata.firmwareVersion ?? "Unknown".localized)
}
.accessibilityElement(children: .combine)
}
if let role = node.user?.role, let deviceRole = DeviceRoles(rawValue: Int(role)) {
HStack {
Label {
Text("Role")
} icon: {
Image(systemName: deviceRole.systemName)
.symbolRenderingMode(.multicolor)
}
Spacer()
Text(deviceRole.name)
}
.accessibilityElement(children: .combine)
}
if node.user?.unmessagable ?? false {
HStack {
Label {
Text("Messaging")
} icon: {
Image(systemName: "iphone.slash")
.symbolRenderingMode(.multicolor)
}
Spacer()
Text("Unmonitored")
}
.accessibilityElement(children: .combine)
}
if let dm = node.telemetries?.filtered(using: NSPredicate(format: "metricsType == 0")).lastObject as? TelemetryEntity, let uptimeSeconds = dm.uptimeSeconds {
HStack {
Label {
Text("\("Uptime".localized)")
} icon: {
Image(systemName: "checkmark.circle.fill")
.foregroundColor(.green)
.symbolRenderingMode(.hierarchical)
}
Spacer()
let now = Date.now
let later = now + TimeInterval(uptimeSeconds)
let uptime = (now..<later).formatted(.components(style: .narrow))
Text(uptime)
.textSelection(.enabled)
}
.accessibilityElement(children: .combine)
}
if let firstHeard = node.firstHeard, firstHeard.timeIntervalSince1970 > 0 && firstHeard < Calendar.current.date(byAdding: .year, value: 1, to: Date())! {
HStack {
Label {
Text("First heard")
} icon: {
Image(systemName: "clock")
.symbolRenderingMode(.multicolor)
}
Spacer()
if dateFormatRelative, let text = Self.relativeFormatter.string(for: firstHeard) {
Text(text)
.textSelection(.enabled)
} else {
Text(firstHeard.formatted())
.textSelection(.enabled)
}
}
.accessibilityElement(children: .combine)
.onTapGesture {
dateFormatRelative.toggle()
}
}
if let lastHeard = node.lastHeard, lastHeard.timeIntervalSince1970 > 0 && lastHeard < Calendar.current.date(byAdding: .year, value: 1, to: Date())! {
HStack {
Label {
Text("Last heard")
} icon: {
Image(systemName: "clock.arrow.circlepath")
.symbolRenderingMode(.multicolor)
}
Spacer()
if dateFormatRelative, let text = Self.relativeFormatter.string(for: lastHeard) {
if lastHeard.formatted() != "Unknown Age".localized {
Text(text)
.textSelection(.enabled)
}
} else {
Text(lastHeard.formatted())
.textSelection(.enabled)
}
}
.accessibilityElement(children: .combine)
.onTapGesture {
dateFormatRelative.toggle()
}
}
}
if node.hasPositions && UserDefaults.environmentEnableWeatherKit
|| node.hasDataForLatestEnvironmentMetrics(attributes: ["iaq", "temperature", "relativeHumidity", "barometricPressure", "windSpeed", "radiation", "weight", "Distance", "soilTemperature", "soilMoisture"]) {
Section("Environment") {
VStack(spacing: 0) {
if !node.hasEnvironmentMetrics {
LocalWeatherConditions(location: node.latestPosition?.nodeLocation)
.frame(height: environmentSectionHeight) // Use the state to set the frame
.onPreferenceChange(WeatherKitTilesHeightKey.self) { newHeight in
// Update the state with the new height
self.environmentSectionHeight = newHeight
}
} else {
VStack {
if node.latestEnvironmentMetrics?.iaq ?? -1 > 0 {
IndoorAirQuality(iaq: Int(node.latestEnvironmentMetrics?.iaq ?? 0), displayMode: .gradient)
.padding(.vertical)
}
LazyVGrid(columns: gridItemLayout) {
if let temperature = node.latestEnvironmentMetrics?.temperature?.shortFormattedTemperature() {
WeatherConditionsCompactWidget(temperature: String(temperature), symbolName: "cloud.sun", description: "TEMP")
}
if let humidity = node.latestEnvironmentMetrics?.relativeHumidity {
if let temperature = node.latestEnvironmentMetrics?.temperature {
let dewPoint = calculateDewPoint(temp: temperature, relativeHumidity: humidity)
.formatted(.number.precision(.fractionLength(0))) + "°"
HumidityCompactWidget(humidity: Int(humidity), dewPoint: dewPoint)
} else {
HumidityCompactWidget(humidity: Int(humidity), dewPoint: nil)
}
}
if let pressure = node.latestEnvironmentMetrics?.barometricPressure {
PressureCompactWidget(pressure: pressure.formatted(.number.precision(.fractionLength(2))), unit: "hPA", low: pressure <= 1009.144)
}
if let windSpeed = node.latestEnvironmentMetrics?.windSpeed {
let windSpeedMeasurement = Measurement(value: Double(windSpeed), unit: UnitSpeed.metersPerSecond)
let windGust = node.latestEnvironmentMetrics?.windGust.map { Measurement(value: Double($0), unit: UnitSpeed.metersPerSecond) }
let direction = cardinalValue(from: Double(node.latestEnvironmentMetrics?.windDirection ?? 0))
WindCompactWidget(speed: windSpeedMeasurement.formatted(.measurement(width: .abbreviated, numberFormatStyle: .number.precision(.fractionLength(0)))),
gust: node.latestEnvironmentMetrics?.windGust ?? 0.0 > 0.0 ? windGust?.formatted(.measurement(width: .abbreviated, numberFormatStyle: .number.precision(.fractionLength(0)))) : "", direction: direction)
}
if let rainfall1h = node.latestEnvironmentMetrics?.rainfall1H {
let locale = NSLocale.current as NSLocale
let usesMetricSystem = locale.usesMetricSystem // Returns true for metric (mm), false for imperial (inches)
let unit = usesMetricSystem ? UnitLength.millimeters : UnitLength.inches
let unitLabel = usesMetricSystem ? "mm" : "in"
let measurement = Measurement(value: Double(rainfall1h), unit: UnitLength.millimeters)
let decimals = usesMetricSystem ? 0 : 1
let formattedRain = measurement.converted(to: unit).value.formatted(.number.precision(.fractionLength(decimals)))
RainfallCompactWidget(timespan: .rainfall1H, rainfall: formattedRain, unit: unitLabel)
}
if let rainfall24h = node.latestEnvironmentMetrics?.rainfall24H {
let locale = NSLocale.current as NSLocale
let usesMetricSystem = locale.usesMetricSystem // Returns true for metric (mm), false for imperial (inches)
let unit = usesMetricSystem ? UnitLength.millimeters : UnitLength.inches
let unitLabel = usesMetricSystem ? "mm" : "in"
let measurement = Measurement(value: Double(rainfall24h), unit: UnitLength.millimeters)
let decimals = usesMetricSystem ? 0 : 1
let formattedRain = measurement.converted(to: unit).value.formatted(.number.precision(.fractionLength(decimals)))
RainfallCompactWidget(timespan: .rainfall24H, rainfall: formattedRain, unit: unitLabel)
}
if let radiation = node.latestEnvironmentMetrics?.radiation {
RadiationCompactWidget(radiation: radiation.formatted(.number.precision(.fractionLength(1))), unit: "µR/hr")
}
if let weight = node.latestEnvironmentMetrics?.weight {
WeightCompactWidget(weight: weight.formatted(.number.precision(.fractionLength(1))), unit: "kg")
}
if let distance = node.latestEnvironmentMetrics?.distance {
DistanceCompactWidget(distance: distance.formatted(.number.precision(.fractionLength(0))), unit: "mm")
}
if let soilTemperature = node.latestEnvironmentMetrics?.soilTemperature {
let locale = NSLocale.current as NSLocale
let localeUnit = locale.object(forKey: NSLocale.Key(rawValue: "kCFLocaleTemperatureUnitKey"))
let unit = localeUnit as? String ?? "Celsius" == "Fahrenheit" ? "°F" : "°C"
SoilTemperatureCompactWidget(temperature: soilTemperature.localeTemperature().formatted(.number.precision(.fractionLength(0))), unit: unit)
}
if let soilMoisture = node.latestEnvironmentMetrics?.soilMoisture {
SoilMoistureCompactWidget(moisture: soilMoisture.formatted(.number.precision(.fractionLength(0))), unit: "%")
}
}
.padding(node.latestEnvironmentMetrics?.iaq ?? -1 > 0 ? .bottom : .vertical)
}
}
}
.accessibilityElement(children: .combine)
}
}
if node.hasPowerMetrics && node.latestPowerMetrics != nil {
Section("Power") {
VStack {
if let metric = node.latestPowerMetrics {
PowerMetrics(metric: metric)
}
}
.accessibilityElement(children: .combine)
}
}
Section("Logs") {
NavigationLink {
DeviceMetricsLog(node: node)
} label: {
Label {
Text("Device Metrics Log")
} icon: {
Image(systemName: "flipphone")
.symbolRenderingMode(.multicolor)
}
}
.disabled(!node.hasDeviceMetrics)
NavigationLink {
NodeMapSwiftUI(node: node, showUserLocation: connectedNode?.num ?? 0 == node.num)
} label: {
Label {
Text("Node Map")
} icon: {
Image(systemName: "map")
.symbolRenderingMode(.multicolor)
}
}
.disabled(!node.hasPositions)
NavigationLink {
PositionLog(node: node)
} label: {
Label {
Text("Position Log")
} icon: {
Image(systemName: "mappin.and.ellipse")
.symbolRenderingMode(.multicolor)
}
}
.disabled(!node.hasPositions)
NavigationLink {
EnvironmentMetricsLog(node: node)
} label: {
Label {
Text("Environment Metrics Log")
} icon: {
Image(systemName: "cloud.sun.rain")
.symbolRenderingMode(.multicolor)
}
}
.disabled(!node.hasEnvironmentMetrics)
NavigationLink {
TraceRouteLog(node: node)
} label: {
Label {
Text("Trace Route Log")
} icon: {
Image(systemName: "signpost.right.and.left")
.symbolRenderingMode(.multicolor)
}
}
.disabled(node.traceRoutes?.count ?? 0 == 0)
NavigationLink {
PowerMetricsLog(node: node)
} label: {
Label {
Text("Power Metrics Log")
} icon: {
Image(systemName: "bolt")
.symbolRenderingMode(.multicolor)
}
}
.disabled(!node.hasPowerMetrics)
NavigationLink {
DetectionSensorLog(node: node)
} label: {
Label {
Text("Detection Sensor Log")
} icon: {
Image(systemName: "sensor")
.symbolRenderingMode(.multicolor)
}
}
.disabled(!node.hasDetectionSensorMetrics)
if node.hasPax {
NavigationLink {
PaxCounterLog(node: node)
} label: {
Label {
Text("paxcounter.log")
} icon: {
Image(systemName: "figure.walk.motion")
.symbolRenderingMode(.multicolor)
}
}
.disabled(!node.hasPax)
}
}
Section("Actions") {
if let user = node.user {
NodeAlertsButton(
context: context,
node: node,
user: user
)
}
if let connectedNode {
FavoriteNodeButton(
node: node
)
if connectedNode.num != node.num {
if !(node.user?.unmessagable ?? true) {
Button(action: {
if let url = URL(string: "meshtastic:///messages?userNum=\(node.num)") {
UIApplication.shared.open(url)
}
}) {
Label("Message", systemImage: "message")
}
}
ExchangePositionsButton(
node: node
)
TraceRouteButton(
node: node
)
if node.isStoreForwardRouter {
ClientHistoryButton(
connectedNode: connectedNode,
node: node
)
}
if node.hasPositions {
NavigateToButton(node: node)
}
IgnoreNodeButton(
node: node
)
DeleteNodeButton(
connectedNode: connectedNode,
node: node
)
}
}
}
if let metadata = node.metadata,
let connectedNode,
accessoryManager.isConnected {
Section("Administration") {
if UserDefaults.enableAdministration {
Button {
Task {
do {
_ = try await accessoryManager.requestDeviceMetadata(
fromUser: connectedNode.user!,
toUser: node.user!
)
Logger.mesh.info("Sent node metadata request from node details")
} catch {
Logger.mesh.error("Faild to send node metadata request from node details")
}
}
} label: {
Label {
Text("Refresh device metadata")
} icon: {
Image(systemName: "arrow.clockwise")
}
}
}
if metadata.canShutdown {
Button {
showingShutdownConfirm = true
} label: {
Label("Power Off", systemImage: "power")
}.confirmationDialog(
"Are you sure?",
isPresented: $showingShutdownConfirm
) {
Button("Shutdown Node?", role: .destructive) {
Task {
do {
try await accessoryManager.sendShutdown(
fromUser: connectedNode.user!,
toUser: node.user!
)
} catch {
Logger.mesh.warning("Shutdown Failed")
}
}
}
}
}
Button {
showingRebootConfirm = true
} label: {
Label(
"Reboot",
systemImage: "arrow.triangle.2.circlepath"
)
}.confirmationDialog(
"Are you sure?",
isPresented: $showingRebootConfirm
) {
Button("Reboot node?", role: .destructive) {
Task {
do {
try await accessoryManager.sendReboot(
fromUser: connectedNode.user!,
toUser: node.user! )
} catch {
Logger.mesh.warning("Reboot Failed")
}
}
}
}
}
}
}
.listStyle(.insetGrouped)
.navigationTitle(String(node.user?.longName?.addingVariationSelectors ?? "Unknown".localized))
}
}
}
func cardinalValue(from heading: Double) -> String {
switch heading {
case 0 ..< 22.5:
return "North"
case 22.5 ..< 67.5:
return "North East"
case 67.5 ..< 112.5:
return "East"
case 112.5 ..< 157.5:
return "South East"
case 157.5 ..< 202.5:
return "South"
case 202.5 ..< 247.5:
return "South West"
case 247.5 ..< 292.5:
return "West"
case 292.5 ..< 337.5:
return "North West"
case 337.5 ... 360.0:
return "North"
default:
return ""
}
}
func abbreviatedCardinalValue(from heading: Double) -> String {
switch heading {
case 0 ..< 22.5:
return "N"
case 22.5 ..< 67.5:
return "NE"
case 67.5 ..< 112.5:
return "E"
case 112.5 ..< 157.5:
return "E"
case 157.5 ..< 202.5:
return "S"
case 202.5 ..< 247.5:
return "SW"
case 247.5 ..< 292.5:
return "W"
case 292.5 ..< 337.5:
return "NW"
case 337.5 ... 360.0:
return "N"
default:
return ""
}
}