feat: changed temperature value from int to decimals in the sensors popup view (#1292)

This commit is contained in:
Serhiy Mytrovtsiy
2023-02-10 09:35:47 +01:00
parent f647abca42
commit 51ea8c3267
3 changed files with 33 additions and 13 deletions

View File

@@ -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)

View File

@@ -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 {

View File

@@ -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? {