diff --git a/Meshtastic/Export/WriteCsvFile.swift b/Meshtastic/Export/WriteCsvFile.swift index f71206cd..9edc9929 100644 --- a/Meshtastic/Export/WriteCsvFile.swift +++ b/Meshtastic/Export/WriteCsvFile.swift @@ -32,7 +32,7 @@ func TelemetryToCsvFile(telemetry: [TelemetryEntity], metricsType: Int) -> Strin for dm in telemetry{ if dm.metricsType == 1 { csvString += "\n" - csvString += String(dm.temperature) + csvString += String(dm.temperature.localeTemperature()) csvString += ", " csvString += String(dm.relativeHumidity) csvString += ", " diff --git a/Meshtastic/Helpers/BLEManager.swift b/Meshtastic/Helpers/BLEManager.swift index c3c1da54..1c672bf1 100644 --- a/Meshtastic/Helpers/BLEManager.swift +++ b/Meshtastic/Helpers/BLEManager.swift @@ -531,11 +531,12 @@ class BLEManager: NSObject, CBPeripheralDelegate, ObservableObject { // Use a RunLoop to prevent the timer from running on the main UI thread if userSettings?.provideLocation ?? false { if positionTimer != nil { - positionTimer!.invalidate() } positionTimer = Timer.scheduledTimer(timeInterval: TimeInterval((userSettings?.provideLocationInterval ?? 900)), target: self, selector: #selector(positionTimerFired), userInfo: context, repeats: true) - RunLoop.current.add(positionTimer!, forMode: .common) + if positionTimer != nil { + RunLoop.current.add(positionTimer!, forMode: .common) + } } if decodedInfo.configCompleteID != 0 && decodedInfo.configCompleteID == configNonce { diff --git a/Meshtastic/Helpers/Extensions.swift b/Meshtastic/Helpers/Extensions.swift index 0c36df3c..c1b41f99 100644 --- a/Meshtastic/Helpers/Extensions.swift +++ b/Meshtastic/Helpers/Extensions.swift @@ -23,6 +23,25 @@ extension Date { } } +extension Float { + + func formattedTemperature() -> String { + let temperature = Measurement(value: Double(self), unit: .celsius) + return temperature.formatted(.measurement(width: .abbreviated, usage: .weather)) + } + func localeTemperature() -> Double { + let temperature = Measurement(value: Double(self), unit: .celsius) + let locale = NSLocale.current as NSLocale + let localeUnit = locale.object(forKey: NSLocale.Key(rawValue: "kCFLocaleTemperatureUnitKey")) + var format: UnitTemperature = .celsius + + if localeUnit! as! String == "Fahrenheit" { + format = .fahrenheit + } + return temperature.converted(to: format).value + } +} + extension Int { func numberOfDigits() -> Int { diff --git a/Meshtastic/Views/Nodes/EnvironmentMetricsLog.swift b/Meshtastic/Views/Nodes/EnvironmentMetricsLog.swift index a7efebf0..a8d2c2b3 100644 --- a/Meshtastic/Views/Nodes/EnvironmentMetricsLog.swift +++ b/Meshtastic/Views/Nodes/EnvironmentMetricsLog.swift @@ -17,18 +17,17 @@ struct EnvironmentMetricsLog: View { @State var exportString = "" var node: NodeInfoEntity - + var body: some View { NavigationStack { - let tempReadingType = (!(node.telemetryConfig?.environmentDisplayFahrenheit ?? false)) ? "°C" : "°F" if UIDevice.current.userInterfaceIdiom == .pad || UIDevice.current.userInterfaceIdiom == .mac { //Add a table for mac and ipad Table(node.telemetries!.reversed() as! [TelemetryEntity]) { TableColumn("Temperature") { em in if em.metricsType == 1 { - Text("\(String(format: "%.2f", em.temperature))\(tempReadingType)") + Text(em.temperature.formattedTemperature()) } } TableColumn("Humidity") { em in @@ -98,7 +97,7 @@ struct EnvironmentMetricsLog: View { GridRow { - Text("\(String(format: "%.2f", em.temperature))\(tempReadingType)") + Text(em.temperature.formattedTemperature()) .font(.caption) Text("\(String(format: "%.2f", em.relativeHumidity))") .font(.caption)