mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-15 00:34:08 +09:00
feat: added notifications for the Disk module
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
56
Modules/Disk/notifications.swift
Normal file
56
Modules/Disk/notifications.swift
Normal 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user