feat: hiding the low/high battery level notification when the battery is charged or discharged (#893)

This commit is contained in:
Serhiy Mytrovtsiy
2022-05-03 19:56:30 +02:00
parent b0ceb5964b
commit e82ace6595
3 changed files with 56 additions and 10 deletions

View File

@@ -50,6 +50,7 @@ public class Battery: Module {
private var lowLevelNotificationState: Bool = false
private var highLevelNotificationState: Bool = false
private var notificationID: String? = nil
public init() {
self.settingsView = Settings("Battery")
@@ -97,6 +98,18 @@ public class Battery: Module {
}
}
public override func willTerminate() {
guard self.isAvailable() else { return }
if let id = self.notificationID {
if #available(macOS 10.14, *) {
removeNotification(id)
} else {
removeNSNotification(id)
}
}
}
public override func isAvailable() -> Bool {
let snapshot = IOPSCopyPowerSourcesInfo().takeRetainedValue()
let sources = IOPSCopyPowerSourcesList(snapshot).takeRetainedValue() as Array
@@ -144,6 +157,14 @@ public class Battery: Module {
if (value.level > notificationLevel || value.powerSource != "Battery Power") && self.lowLevelNotificationState {
if value.level > notificationLevel {
if let id = self.notificationID {
if #available(macOS 10.14, *) {
removeNotification(id)
} else {
removeNSNotification(id)
}
self.notificationID = nil
}
self.lowLevelNotificationState = false
}
return
@@ -161,12 +182,12 @@ public class Battery: Module {
}
if #available(macOS 10.14, *) {
showNotification(
self.notificationID = showNotification(
title: title,
subtitle: subtitle
)
} else {
showNSNotification(
self.notificationID = showNSNotification(
title: title,
subtitle: subtitle
)
@@ -188,6 +209,14 @@ public class Battery: Module {
if (value.level < notificationLevel || value.powerSource == "Battery Power") && self.highLevelNotificationState {
if value.level < notificationLevel {
if let id = self.notificationID {
if #available(macOS 10.14, *) {
removeNotification(id)
} else {
removeNSNotification(id)
}
self.notificationID = nil
}
self.highLevelNotificationState = false
}
return
@@ -205,12 +234,12 @@ public class Battery: Module {
}
if #available(macOS 10.14, *) {
showNotification(
self.notificationID = showNotification(
title: title,
subtitle: subtitle
)
} else {
showNSNotification(
self.notificationID = showNSNotification(
title: title,
subtitle: subtitle
)