From 51ea8c3267d384b784410c898c43e39f518383eb Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Fri, 10 Feb 2023 09:35:47 +0100 Subject: [PATCH] feat: changed temperature value from int to decimals in the sensors popup view (#1292) --- Kit/helpers.swift | 7 +++++-- Modules/Sensors/popup.swift | 4 ++-- Modules/Sensors/values.swift | 35 ++++++++++++++++++++++++++--------- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/Kit/helpers.swift b/Kit/helpers.swift index 6ae7ebfc..3ee67242 100644 --- a/Kit/helpers.swift +++ b/Kit/helpers.swift @@ -731,10 +731,13 @@ public extension UnitTemperature { } // swiftlint:disable identifier_name -public func Temperature(_ value: Double, defaultUnit: UnitTemperature = UnitTemperature.celsius) -> String { +public func Temperature(_ value: Double, defaultUnit: UnitTemperature = UnitTemperature.celsius, fractionDigits: Int = 0) -> String { let formatter = MeasurementFormatter() formatter.locale = Locale.init(identifier: "en_US") - formatter.numberFormatter.maximumFractionDigits = 0 + formatter.numberFormatter.maximumFractionDigits = fractionDigits + if fractionDigits != 0 { + formatter.numberFormatter.minimumFractionDigits = fractionDigits + } formatter.unitOptions = .providedUnit var measurement = Measurement(value: value, unit: defaultUnit) diff --git a/Modules/Sensors/popup.swift b/Modules/Sensors/popup.swift index 2456f3ed..fa582fb4 100644 --- a/Modules/Sensors/popup.swift +++ b/Modules/Sensors/popup.swift @@ -182,7 +182,7 @@ internal class SensorView: NSStackView { } public func update(_ sensor: Sensor_p) { - self.valueView.update(sensor.formattedValue) + self.valueView.update(sensor.formattedPopupValue) } public func addHistoryPoint(_ sensor: Sensor_p) { @@ -278,7 +278,7 @@ internal class ChartSensorView: NSStackView { self.spacing = 0 self.layer?.cornerRadius = 3 - self.chart = LineChartView(frame: NSRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height), num: 120) + self.chart = LineChartView(frame: NSRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height), num: 120, scale: .linear) self.chart?.suffix = suffix if let view = self.chart { diff --git a/Modules/Sensors/values.swift b/Modules/Sensors/values.swift index f9dda9b6..3c649f4b 100644 --- a/Modules/Sensors/values.swift +++ b/Modules/Sensors/values.swift @@ -45,6 +45,7 @@ internal protocol Sensor_p { var unit: String { get } var formattedValue: String { get } var formattedMiniValue: String { get } + var formattedPopupValue: String { get } } internal struct Sensor: Sensor_p { @@ -97,6 +98,25 @@ internal struct Sensor: Sensor_p { } } } + var formattedPopupValue: String { + get { + switch self.type { + case .temperature: + return Temperature(value, fractionDigits: 1) + case .voltage: + let val = value >= 100 ? "\(Int(value))" : String(format: "%.3f", value) + return "\(val)\(unit)" + case .power, .energy: + let val = value >= 100 ? "\(Int(value))" : String(format: "%.2f", value) + return "\(val)\(unit)" + case .current: + let val = value >= 100 ? "\(Int(value))" : String(format: "%.2f", value) + return "\(val)\(unit)" + case .fan: + return "\(Int(value)) \(unit)" + } + } + } var formattedMiniValue: String { get { switch self.type { @@ -155,20 +175,17 @@ internal struct Fan: Sensor_p { var unit: String = "RPM" var formattedValue: String { - get { - return "\(Int(value)) RPM" - } + "\(Int(value)) RPM" } var formattedMiniValue: String { - get { - return "\(Int(value))" - } + return "\(Int(value))" + } + var formattedPopupValue: String { + "\(Int(value)) RPM" } var state: Bool { - get { - return Store.shared.bool(key: "sensor_\(self.key)", defaultValue: false) - } + Store.shared.bool(key: "sensor_\(self.key)", defaultValue: false) } var customSpeed: Int? {