Skip to content

Commit

Permalink
Add max energy
Browse files Browse the repository at this point in the history
  • Loading branch information
decodism committed Sep 23, 2023
1 parent 8e9e0d5 commit d651b29
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 3 deletions.
32 changes: 32 additions & 0 deletions BatFiKit/Sources/AppShared/Formatters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import Foundation
import L10n

public let timeFormatter: DateComponentsFormatter = {
let formatter = DateComponentsFormatter()
Expand All @@ -31,3 +32,34 @@ public let percentageFormatter: NumberFormatter = {
formatter.maximumFractionDigits = 0
return formatter
}()

public let energyFormatter: MeasurementFormatter = {
let formatter = CustomMeasurementFormatter()
formatter.unitOptions = .providedUnit
formatter.unitStyle = .short
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
numberFormatter.minimumFractionDigits = 3
numberFormatter.maximumFractionDigits = 3
formatter.numberFormatter = numberFormatter
return formatter
}()

private class CustomMeasurementFormatter: MeasurementFormatter {
override func string(from measurement: Measurement<Unit>) -> String {
var string = super.string(from: measurement)
if unitStyle == .short {
if string.count >= measurement.unit.symbol.count + 2 {
let index = string.index(string.endIndex, offsetBy: -(measurement.unit.symbol.count + 1))
if string[index].isWhitespace {
string.remove(at: index)
}
}
}
return string
}
}

public extension UnitEnergy {
static let wattHours = UnitEnergy(symbol: L10n.UnitEnergy.Symbol.wattHours, converter: UnitConverterLinear(coefficient: 3600))
}
7 changes: 5 additions & 2 deletions BatFiKit/Sources/AppShared/PowerState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public struct PowerState: CustomStringConvertible, Equatable {
public let batteryTemperature: Double
public let chargerConnected: Bool
public let optimizedBatteryChargingEngaged: Bool
public let batteryMaxEnergy: Double

public init(
batteryLevel: Int,
Expand All @@ -29,7 +30,8 @@ public struct PowerState: CustomStringConvertible, Equatable {
batteryCapacity: Double,
batteryTemperature: Double,
chargerConnected: Bool,
optimizedBatteryChargingEngaged: Bool
optimizedBatteryChargingEngaged: Bool,
batteryMaxEnergy: Double
) {
self.batteryLevel = batteryLevel
self.isCharging = isCharging
Expand All @@ -41,11 +43,12 @@ public struct PowerState: CustomStringConvertible, Equatable {
self.batteryTemperature = batteryTemperature
self.chargerConnected = chargerConnected
self.optimizedBatteryChargingEngaged = optimizedBatteryChargingEngaged
self.batteryMaxEnergy = batteryMaxEnergy
}

public var description: String {
"""
PowerState |==> is charging: \(isCharging), battery level: \(batteryLevel), power source: \(powerSource), time left: \(timeLeft), time to charge: \(timeToCharge), cycle count: \(batteryCycleCount), battery capacity: \(batteryCapacity), battery temperature: \(batteryTemperature)°C, charger connected: \(chargerConnected), optimized battery charging engaged: \(optimizedBatteryChargingEngaged)
PowerState |==> is charging: \(isCharging), battery level: \(batteryLevel), power source: \(powerSource), time left: \(timeLeft), time to charge: \(timeToCharge), cycle count: \(batteryCycleCount), battery capacity: \(batteryCapacity), battery temperature: \(batteryTemperature)°C, charger connected: \(chargerConnected), optimized battery charging engaged: \(optimizedBatteryChargingEngaged), battery max energy: \(batteryMaxEnergy)Wh
"""
}
}
4 changes: 4 additions & 0 deletions BatFiKit/Sources/BatteryInfo/BatteryInfoView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public struct BatteryInfoView: View {
label: l10n.Additional.batteryCapacity,
info: percentageFormatter.string(from: NSNumber(floatLiteral: powerState.batteryCapacity))!
)
BatteryAdditionalInfo(
label: l10n.Additional.fullChargeEnergy,
info: energyFormatter.string(from: Measurement(value: powerState.batteryMaxEnergy, unit: UnitEnergy.wattHours))
)
}
.frame(maxWidth: .infinity)
}
Expand Down
14 changes: 13 additions & 1 deletion BatFiKit/Sources/ClientsLive/PowerSourceClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,17 @@ private func getPowerSourceInfo() throws -> PowerState {
throw PowerSourceError.infoMissing
}
let batteryHealth = Double(maxCapacity) / Double(designCapacity)

guard
let batteryData: [String: Any] = getValue("BatteryData", from: service),
let lifetimeData = batteryData["LifetimeData"] as? [String: Any],
let minimumPackVoltage = lifetimeData["MinimumPackVoltage"] as? Int,
let maximumPackVoltage = lifetimeData["MaximumPackVoltage"] as? Int
else {
throw PowerSourceError.infoMissing
}
let midVoltage = ((Double(minimumPackVoltage) / 1000) + (Double(maximumPackVoltage) / 1000)) / 2
let maxEnergy = (Double(maxCapacity) / 1000) * midVoltage

let powerState = PowerState(
batteryLevel: batteryLevel,
Expand All @@ -176,7 +187,8 @@ private func getPowerSourceInfo() throws -> PowerState {
batteryCapacity: batteryHealth,
batteryTemperature: batteryTemperature,
chargerConnected: chargerConnected,
optimizedBatteryChargingEngaged: optimizedBatteryCharging
optimizedBatteryChargingEngaged: optimizedBatteryCharging,
batteryMaxEnergy: maxEnergy
)
return powerState
}
22 changes: 22 additions & 0 deletions BatFiKit/Sources/L10n/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,17 @@
}
}
},
"battery_info.label.additional.full_charge_energy" : {
"extractionState" : "extracted_with_value",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "new",
"value" : "Full Charge Energy"
}
}
}
},
"battery_info.label.additional.power_source" : {
"extractionState" : "extracted_with_value",
"localizations" : {
Expand Down Expand Up @@ -3026,6 +3037,17 @@
}
}
}
},
"unit_energy.symbol.watt_hours" : {
"extractionState" : "extracted_with_value",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "new",
"value" : "Wh"
}
}
}
}
},
"version" : "1.0"
Expand Down
8 changes: 8 additions & 0 deletions BatFiKit/Sources/L10n/Strings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public enum L10n {
public static let powerSource = String(localized: "battery_info.label.additional.power_source", defaultValue: "Power Source", bundle: Bundle.module)
/// Temperature
public static let temperature = String(localized: "battery_info.label.additional.temperature", defaultValue: "Temperature", bundle: Bundle.module)
/// Full Charge Energy
public static let fullChargeEnergy = String(localized: "battery_info.label.additional.full_charge_energy", defaultValue: "Full Charge Energy", bundle: Bundle.module)
}
public enum Main {
/// Battery
Expand Down Expand Up @@ -375,4 +377,10 @@ public enum L10n {
}
}
}
public enum UnitEnergy {
public enum Symbol {
/// Wh
public static let wattHours = String(localized: "unit_energy.symbol.watt_hours", defaultValue: "Wh", bundle: Bundle.module)
}
}
}

0 comments on commit d651b29

Please sign in to comment.