feat: added notifications for the Disk module

This commit is contained in:
Serhiy Mytrovtsiy
2023-12-14 19:53:35 +01:00
parent f7452dcb2d
commit 336a9f6804
2 changed files with 62 additions and 24 deletions

View File

@@ -202,24 +202,22 @@ public class Disk: Module {
private let popupView: Popup = Popup()
private let settingsView: Settings = Settings()
private let portalView: Portal = Portal()
private let notificationsView: Notifications
private var capacityReader: CapacityReader = CapacityReader(.disk)
private var activityReader: ActivityReader = ActivityReader()
private var processReader: ProcessReader = ProcessReader(.disk)
private var selectedDisk: String = ""
private var notificationLevelState: Bool = false
private var notificationID: String? = nil
private var notificationLevel: String {
Store.shared.string(key: "\(Disk.name)_notificationLevel", defaultValue: "Disabled")
}
public init() {
self.notificationsView = Notifications(.disk)
super.init(
popup: self.popupView,
settings: self.settingsView,
portal: self.portalView
portal: self.portalView,
notifications: self.notificationsView
)
guard self.available else { return }
@@ -294,7 +292,7 @@ public class Disk: Module {
let percentage = Double(usedSpace) / Double(total)
self.portalView.loadCallback(percentage)
self.checkNotificationLevel(percentage)
self.notificationsView.utilizationCallback(percentage)
self.menuBar.widgets.filter{ $0.isActive }.forEach { (w: Widget) in
switch w.item {
@@ -331,20 +329,4 @@ 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 {
removeNotification(id)
self.notificationID = nil
self.notificationLevelState = false
} else if value >= level && !self.notificationLevelState {
self.notificationID = showNotification(
title: localizedString("Disk utilization threshold"),
subtitle: localizedString("Disk utilization is", "\(Int((value)*100))%")
)
self.notificationLevelState = true
}
}
}

View File

@@ -0,0 +1,56 @@
//
// notifications.swift
// Disk
//
// Created by Serhiy Mytrovtsiy on 05/12/2023
// Using Swift 5.0
// Running on macOS 14.1
//
// Copyright © 2023 Serhiy Mytrovtsiy. All rights reserved.
//
import Cocoa
import Kit
class Notifications: NotificationsWrapper {
private let utilizationID: String = "usage"
private var utilizationLevel: String = ""
public init(_ module: ModuleType) {
super.init(module, [self.utilizationID])
if Store.shared.exist(key: "\(self.module)_notificationLevel") {
let value = Store.shared.string(key: "\(self.module)_notificationLevel", defaultValue: self.utilizationLevel)
Store.shared.set(key: "\(self.module)_notifications_utilization", value: value)
Store.shared.remove("\(self.module)_notificationLevel")
}
self.utilizationLevel = Store.shared.string(key: "\(self.module)_notifications_utilization", defaultValue: self.utilizationLevel)
self.addArrangedSubview(selectSettingsRow(
title: localizedString("Usage"),
action: #selector(self.changeUsage),
items: notificationLevels,
selected: self.utilizationLevel
))
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
internal func utilizationCallback(_ value: Double) {
let title = localizedString("Disk utilization threshold")
if let threshold = Double(self.utilizationLevel) {
let subtitle = localizedString("Disk utilization is", "\(Int((value)*100))%")
self.checkDouble(id: self.utilizationID, value: value, threshold: threshold, title: title, subtitle: subtitle)
}
}
@objc private func changeUsage(_ sender: NSMenuItem) {
guard let key = sender.representedObject as? String else { return }
self.utilizationLevel = key.isEmpty ? "" : "\(Double(key) ?? 0)"
Store.shared.set(key: "\(self.module)_notifications_utilization", value: self.utilizationLevel)
}
}