feat: added a notification to the RAM usage (#913)

This commit is contained in:
Serhiy Mytrovtsiy
2022-07-06 20:26:12 +02:00
parent 1cb90efbb7
commit 31b68e439e
2 changed files with 54 additions and 0 deletions

View File

@@ -54,11 +54,19 @@ public class RAM: Module {
private var usageReader: UsageReader? = nil
private var processReader: ProcessReader? = nil
private var notificationLevelState: Bool = false
private var notificationID: String? = nil
private var splitValueState: Bool {
get {
return Store.shared.bool(key: "\(self.config.name)_splitValue", defaultValue: false)
}
}
private var notificationLevel: String {
get {
return Store.shared.string(key: "\(self.config.name)_notificationLevel", defaultValue: "Disabled")
}
}
public init() {
self.settingsView = Settings("RAM")
@@ -118,6 +126,7 @@ public class RAM: Module {
}
self.popupView.loadCallback(value)
self.checkNotificationLevel(value.usage)
let total: Double = value.total == 0 ? 1 : value.total
self.widgets.filter{ $0.isActive }.forEach { (w: Widget) in
@@ -160,4 +169,30 @@ public class RAM: Module {
}
}
}
private func checkNotificationLevel(_ value: Double) {
guard self.notificationLevel != "Disabled", let level = Double(self.notificationLevel) else { return }
if let id = self.notificationID, value < level && self.notificationLevelState {
if #available(macOS 10.14, *) {
removeNotification(id)
} else {
removeNSNotification(id)
}
self.notificationID = nil
self.notificationLevelState = false
} else if value >= level && !self.notificationLevelState {
let title = localizedString("RAM utilization threshold")
let subtitle = localizedString("RAM utilization is", "\(Int((value)*100))%")
if #available(macOS 10.14, *) {
self.notificationID = showNotification(title: title, subtitle: subtitle)
} else {
self.notificationID = showNSNotification(title: title, subtitle: subtitle)
}
self.notificationLevelState = true
}
}
}