From eb18b61f032d34237204f7cce30ffbaeba6b5edb Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Wed, 21 Aug 2024 14:13:16 -0700 Subject: [PATCH] Add wind to environment metrics --- .../Weather/LocalWeatherConditions.swift | 8 +++-- .../Views/Nodes/EnvironmentMetricsLog.swift | 6 ++-- .../Views/Nodes/Helpers/NodeDetail.swift | 31 ++++++++++++++++++- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/Meshtastic/Views/Helpers/Weather/LocalWeatherConditions.swift b/Meshtastic/Views/Helpers/Weather/LocalWeatherConditions.swift index eb93897f..7dc8e92c 100644 --- a/Meshtastic/Views/Helpers/Weather/LocalWeatherConditions.swift +++ b/Meshtastic/Views/Helpers/Weather/LocalWeatherConditions.swift @@ -158,11 +158,13 @@ struct WindCompactWidget: View { VStack(alignment: .leading) { Label { Text("WIND") } icon: { Image(systemName: "wind").foregroundColor(.accentColor) } Text("\(direction)") - .font(.caption) + .font(gust.isEmpty ? .callout : .caption) .padding(.bottom, 10) Text(speed) - .font(.system(size: 35)) - Text("Gusts \(gust)") + .font(gust.isEmpty ? .system(size: 45) : .system(size: 35)) + if !gust.isEmpty { + Text("Gusts \(gust)") + } } .padding(10) .frame(maxWidth: .infinity) diff --git a/Meshtastic/Views/Nodes/EnvironmentMetricsLog.swift b/Meshtastic/Views/Nodes/EnvironmentMetricsLog.swift index 571511cf..0cd35248 100644 --- a/Meshtastic/Views/Nodes/EnvironmentMetricsLog.swift +++ b/Meshtastic/Views/Nodes/EnvironmentMetricsLog.swift @@ -94,10 +94,12 @@ struct EnvironmentMetricsLog: View { } } TableColumn("Wind Speed") { em in - Text("\(String(format: "%.1f", em.windSpeed)) hPa") + let windSpeed = Measurement(value: Double(em.windSpeed), unit: UnitSpeed.kilometersPerHour) + Text(windSpeed.formatted(.measurement(width: .abbreviated, numberFormatStyle: .number.precision(.fractionLength(0))))) } TableColumn("Wind Direction") { em in - Text("\(String(format: "%.1f", em.windDirection)) hPa") + let direction = cardinalValue(from: Double(em.windDirection)) + Text(direction) } TableColumn("timestamp") { em in Text(em.time?.formattedDate(format: dateFormatString) ?? "unknown.age".localized) diff --git a/Meshtastic/Views/Nodes/Helpers/NodeDetail.swift b/Meshtastic/Views/Nodes/Helpers/NodeDetail.swift index 8a78c08c..6724e805 100644 --- a/Meshtastic/Views/Nodes/Helpers/NodeDetail.swift +++ b/Meshtastic/Views/Nodes/Helpers/NodeDetail.swift @@ -194,7 +194,11 @@ struct NodeDetail: View { PressureCompactWidget(pressure: String(format: "%.2f", node.latestEnvironmentMetrics?.barometricPressure ?? 0.0), unit: "hPA", low: node.latestEnvironmentMetrics?.barometricPressure ?? 0.0 <= 1009.144) } if node.latestEnvironmentMetrics?.windSpeed ?? 0.0 > 0.0 { - WindCompactWidget(speed: String(node.latestEnvironmentMetrics?.windSpeed ?? 0.0), gust: String(node.latestEnvironmentMetrics?.windGust ?? 0.0), direction: "") + let windSpeed = Measurement(value: Double(node.latestEnvironmentMetrics?.windSpeed ?? 0.0), unit: UnitSpeed.metersPerSecond) + let windGust = Measurement(value: Double(node.latestEnvironmentMetrics?.windGust ?? 0.0), unit: UnitSpeed.metersPerSecond) + let direction = cardinalValue(from: Double(node.latestEnvironmentMetrics?.windDirection ?? 0)) + WindCompactWidget(speed: windSpeed.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) } } .padding(node.latestEnvironmentMetrics?.iaq ?? -1 > 0 ? .bottom : .vertical) @@ -411,3 +415,28 @@ struct NodeDetail: View { } } } + +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 "" + } +}