From 095e60499a18d9f711927022560b7cf1053cce41 Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Mon, 20 Jul 2020 17:51:49 +0200 Subject: [PATCH] - update battery low level notification algorithm - fix showing a low level notification on each measurement lower than set --- ModuleKit/Widgets/Battery.swift | 2 +- Modules/Battery/main.swift | 26 ++++++++++++++++++++++---- Modules/Battery/readers.swift | 9 ++------- StatsKit/extensions.swift | 4 +++- 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/ModuleKit/Widgets/Battery.swift b/ModuleKit/Widgets/Battery.swift index 5b29500b..9824f5a6 100644 --- a/ModuleKit/Widgets/Battery.swift +++ b/ModuleKit/Widgets/Battery.swift @@ -171,7 +171,7 @@ public class BatterykWidget: Widget { var updated: Bool = false if self.percentage != percentage { - self.percentage = abs(percentage) + self.percentage = percentage updated = true } if self.ACStatus != ACStatus { diff --git a/Modules/Battery/main.swift b/Modules/Battery/main.swift index e6ed2eff..a21b1c1d 100644 --- a/Modules/Battery/main.swift +++ b/Modules/Battery/main.swift @@ -46,6 +46,8 @@ public class Battery: Module { private let store: UnsafePointer + private var notification: NSUserNotification? = nil + public init(_ store: UnsafePointer) { self.store = store self.settingsView = Settings("Battery", store: store) @@ -103,13 +105,29 @@ public class Battery: Module { return } - if let notificationLevel = Double(level), abs(value.level) <= notificationLevel { - var subtitle = "\((Int(abs(value.level)*100)))% remaining" - if value.timeToEmpty != 0 { + guard let notificationLevel = Double(level) else { + return + } + + if (value.level > notificationLevel || value.powerSource != "Battery Power") && self.notification != nil { + NSUserNotificationCenter.default.removeDeliveredNotification(self.notification!) + if value.level > notificationLevel { + self.notification = nil + } + return + } + + if value.isCharging { + return + } + + if value.level <= notificationLevel && self.notification == nil { + var subtitle = "\((Int(value.level*100)))% remaining" + if value.timeToEmpty > 0 { subtitle += " (\(Double(value.timeToEmpty*60).printSecondsToHoursMinutesSeconds()))" } - showNotification( + self.notification = showNotification( title: "Low battery", subtitle: subtitle, id: "battery-level", diff --git a/Modules/Battery/readers.swift b/Modules/Battery/readers.swift index fd2495ef..72d6b410 100644 --- a/Modules/Battery/readers.swift +++ b/Modules/Battery/readers.swift @@ -59,7 +59,8 @@ internal class UsageReader: Reader { self.usage.powerSource = list[kIOPSPowerSourceStateKey] as? String ?? "AC Power" self.usage.state = list[kIOPSBatteryHealthKey] as! String self.usage.isCharged = list[kIOPSIsChargedKey] as? Bool ?? false - var cap = Double(list[kIOPSCurrentCapacityKey] as! Int) / 100 + self.usage.isCharging = self.getBoolValue("IsCharging" as CFString) ?? false + self.usage.level = Double(list[kIOPSCurrentCapacityKey] as! Int) / 100 self.usage.timeToEmpty = Int(list[kIOPSTimeToEmptyKey] as! Int) self.usage.timeToCharge = Int(list[kIOPSTimeToFullChargeKey] as! Int) @@ -85,12 +86,6 @@ internal class UsageReader: Reader { } self.usage.ACwatts = ACwatts - if self.usage.powerSource == "Battery Power" { - cap = 0 - cap - } - self.usage.level = cap - self.usage.isCharging = self.getBoolValue("IsCharging" as CFString) ?? false - DispatchQueue.main.async(execute: { self.callback(self.usage) }) diff --git a/StatsKit/extensions.swift b/StatsKit/extensions.swift index 514a828a..0445405a 100644 --- a/StatsKit/extensions.swift +++ b/StatsKit/extensions.swift @@ -797,7 +797,7 @@ public enum updateIntervals: updateInterval { } extension updateIntervals: CaseIterable {} -public func showNotification(title: String, subtitle: String, id: String = UUID().uuidString, icon: NSImage? = nil) { +public func showNotification(title: String, subtitle: String, id: String = UUID().uuidString, icon: NSImage? = nil) -> NSUserNotification { let notification = NSUserNotification() notification.identifier = id @@ -811,4 +811,6 @@ public func showNotification(title: String, subtitle: String, id: String = UUID( } NSUserNotificationCenter.default.deliver(notification) + + return notification }