mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-14 00:04:15 +09:00
- update battery low level notification algorithm
- fix showing a low level notification on each measurement lower than set
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -46,6 +46,8 @@ public class Battery: Module {
|
||||
|
||||
private let store: UnsafePointer<Store>
|
||||
|
||||
private var notification: NSUserNotification? = nil
|
||||
|
||||
public init(_ store: UnsafePointer<Store>) {
|
||||
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",
|
||||
|
||||
@@ -59,7 +59,8 @@ internal class UsageReader: Reader<Battery_Usage> {
|
||||
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<Battery_Usage> {
|
||||
}
|
||||
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)
|
||||
})
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user