From eceb304a8d798776cdcd9037bd45ee823e458a2e Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Thu, 7 Jul 2022 19:12:07 +0200 Subject: [PATCH] feat: added a notification to the Disk usage (#913) --- Modules/Disk/main.swift | 36 ++++++++++++++++++++++++++++++++++++ Modules/Disk/settings.swift | 19 +++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/Modules/Disk/main.swift b/Modules/Disk/main.swift index 837d4484..1fa8b619 100644 --- a/Modules/Disk/main.swift +++ b/Modules/Disk/main.swift @@ -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 + } + } } diff --git a/Modules/Disk/settings.swift b/Modules/Disk/settings.swift index 62033afa..f472df06 100644 --- a/Modules/Disk/settings.swift +++ b/Modules/Disk/settings.swift @@ -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)