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

This commit is contained in:
Serhiy Mytrovtsiy
2022-07-07 19:12:07 +02:00
parent 31b68e439e
commit eceb304a8d
2 changed files with 55 additions and 0 deletions

View File

@@ -133,6 +133,14 @@ public class Disk: Module {
private var activityReader: ActivityReader? = nil
private var settingsView: Settings
private var selectedDisk: String = ""
private var notificationLevelState: Bool = false
private var notificationID: String? = nil
private var notificationLevel: String {
get {
return Store.shared.string(key: "\(self.config.name)_notificationLevel", defaultValue: "Disabled")
}
}
public init() {
self.settingsView = Settings("Disk")
@@ -209,6 +217,8 @@ public class Disk: Module {
}
let percentage = Double(usedSpace) / Double(total)
self.checkNotificationLevel(percentage)
self.widgets.filter{ $0.isActive }.forEach { (w: Widget) in
switch w.item {
case let widget as Mini: widget.setValue(percentage)
@@ -244,4 +254,30 @@ public class Disk: 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("Disk utilization threshold")
let subtitle = localizedString("Disk 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
}
}
}

View File

@@ -15,6 +15,7 @@ import Kit
internal class Settings: NSStackView, Settings_v {
private var removableState: Bool = false
private var updateIntervalValue: Int = 10
private var notificationLevel: String = "Disabled"
public var selectedDiskHandler: (String) -> Void = {_ in }
public var callback: (() -> Void) = {}
@@ -32,6 +33,7 @@ internal class Settings: NSStackView, Settings_v {
self.selectedDisk = Store.shared.string(key: "\(self.title)_disk", defaultValue: "")
self.removableState = Store.shared.bool(key: "\(self.title)_removable", defaultValue: self.removableState)
self.updateIntervalValue = Store.shared.int(key: "\(self.title)_updateInterval", defaultValue: self.updateIntervalValue)
self.notificationLevel = Store.shared.string(key: "\(self.title)_notificationLevel", defaultValue: self.notificationLevel)
super.init(frame: NSRect(x: 0, y: 0, width: 0, height: 0))
@@ -69,6 +71,13 @@ internal class Settings: NSStackView, Settings_v {
action: #selector(toggleRemovable),
state: self.removableState
))
self.addArrangedSubview(selectSettingsRow(
title: localizedString("Notification level"),
action: #selector(changeNotificationLevel),
items: notificationLevels,
selected: self.notificationLevel == "Disabled" ? self.notificationLevel : "\(Int((Double(self.notificationLevel) ?? 0)*100))%"
))
}
private func addDiskSelector() {
@@ -139,6 +148,16 @@ internal class Settings: NSStackView, Settings_v {
}
}
@objc func changeNotificationLevel(_ sender: NSMenuItem) {
guard let key = sender.representedObject as? String else { return }
if key == "Disabled" {
Store.shared.set(key: "\(self.title)_notificationLevel", value: key)
} else if let value = Double(key.replacingOccurrences(of: "%", with: "")) {
Store.shared.set(key: "\(self.title)_notificationLevel", value: "\(value/100)")
}
}
public func setUpdateInterval(value: Int) {
self.updateIntervalValue = value
Store.shared.set(key: "\(self.title)_updateInterval", value: value)