mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-15 00:34:08 +09:00
feat: added notifications to the Network module: connectivity, interface, local and public IP, WiFi network (#2261)
This commit is contained in:
@@ -81,7 +81,7 @@
|
||||
<key>popup</key>
|
||||
<true/>
|
||||
<key>notifications</key>
|
||||
<false/>
|
||||
<true/>
|
||||
</dict>
|
||||
</dict>
|
||||
</plist>
|
||||
|
||||
@@ -126,6 +126,7 @@ public class Network: Module {
|
||||
private let popupView: Popup
|
||||
private let settingsView: Settings
|
||||
private let portalView: Portal
|
||||
private let notificationsView: Notifications
|
||||
|
||||
private var usageReader: UsageReader? = nil
|
||||
private var processReader: ProcessReader? = nil
|
||||
@@ -154,12 +155,14 @@ public class Network: Module {
|
||||
self.settingsView = Settings(.network)
|
||||
self.popupView = Popup(.network)
|
||||
self.portalView = Portal(.network)
|
||||
self.notificationsView = Notifications(.network)
|
||||
|
||||
super.init(
|
||||
moduleType: .network,
|
||||
popup: self.popupView,
|
||||
settings: self.settingsView,
|
||||
portal: self.portalView
|
||||
portal: self.portalView,
|
||||
notifications: self.notificationsView
|
||||
)
|
||||
guard self.available else { return }
|
||||
|
||||
@@ -220,6 +223,7 @@ public class Network: Module {
|
||||
|
||||
self.popupView.usageCallback(value)
|
||||
self.portalView.usageCallback(value)
|
||||
self.notificationsView.usageCallback(value)
|
||||
|
||||
var upload: Int64 = value.bandwidth.upload
|
||||
var download: Int64 = value.bandwidth.download
|
||||
@@ -313,6 +317,7 @@ public class Network: Module {
|
||||
guard let value = raw, self.enabled else { return }
|
||||
|
||||
self.popupView.connectivityCallback(value)
|
||||
self.notificationsView.connectivityCallback(value)
|
||||
|
||||
self.menuBar.widgets.filter{ $0.isActive }.forEach { (w: SWidget) in
|
||||
switch w.item {
|
||||
|
||||
141
Modules/Net/notifications.swift
Normal file
141
Modules/Net/notifications.swift
Normal file
@@ -0,0 +1,141 @@
|
||||
//
|
||||
// notifications.swift
|
||||
// Net
|
||||
//
|
||||
// Created by Serhiy Mytrovtsiy on 25/01/2025
|
||||
// Using Swift 6.0
|
||||
// Running on macOS 15.1
|
||||
//
|
||||
// Copyright © 2025 Serhiy Mytrovtsiy. All rights reserved.
|
||||
//
|
||||
|
||||
import Cocoa
|
||||
import Kit
|
||||
|
||||
class Notifications: NotificationsWrapper {
|
||||
private let connectionID: String = "connection"
|
||||
private let interfaceID: String = "interface"
|
||||
private let localID: String = "localIP"
|
||||
private let publicID: String = "publicIP"
|
||||
private let wifiID: String = "wifi"
|
||||
|
||||
private var connectionState: Bool = false
|
||||
private var interfaceState: Bool = false
|
||||
private var localIPState: Bool = false
|
||||
private var publicIPState: Bool = false
|
||||
private var wifiState: Bool = false
|
||||
|
||||
private var connection: Bool?
|
||||
private var interface: String?
|
||||
private var localIP: String?
|
||||
private var publicIP: String?
|
||||
private var wifi: String?
|
||||
|
||||
public init(_ module: ModuleType) {
|
||||
super.init(module, [self.connectionID, self.interfaceID, self.localID, self.publicID, self.wifiID])
|
||||
|
||||
self.connectionState = Store.shared.bool(key: "\(self.module)_notifications_connection_state", defaultValue: self.connectionState)
|
||||
self.interfaceState = Store.shared.bool(key: "\(self.module)_notifications_interface_state", defaultValue: self.interfaceState)
|
||||
self.localIPState = Store.shared.bool(key: "\(self.module)_notifications_localIP_state", defaultValue: self.localIPState)
|
||||
self.publicIPState = Store.shared.bool(key: "\(self.module)_notifications_publicIP_state", defaultValue: self.publicIPState)
|
||||
self.wifiState = Store.shared.bool(key: "\(self.module)_notifications_wifi_state", defaultValue: self.wifiState)
|
||||
|
||||
self.addArrangedSubview(PreferencesSection([
|
||||
PreferencesRow(localizedString("Status"), component: switchView(
|
||||
action: #selector(self.toggleConnectionState),
|
||||
state: self.connectionState
|
||||
)),
|
||||
PreferencesRow(localizedString("Network interface"), component: switchView(
|
||||
action: #selector(self.toggleInterfaceState),
|
||||
state: self.interfaceState
|
||||
)),
|
||||
PreferencesRow(localizedString("Local IP"), component: switchView(
|
||||
action: #selector(self.toggleLocalIPState),
|
||||
state: self.localIPState
|
||||
)),
|
||||
PreferencesRow(localizedString("Public IP"), component: switchView(
|
||||
action: #selector(self.toggleNPublicIPState),
|
||||
state: self.publicIPState
|
||||
)),
|
||||
PreferencesRow(localizedString("WiFi network"), component: switchView(
|
||||
action: #selector(self.toggleWiFiState),
|
||||
state: self.wifiState
|
||||
))
|
||||
]))
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
internal func usageCallback(_ value: Network_Usage) {
|
||||
if self.interfaceState {
|
||||
if value.interface?.BSDName != self.interface {
|
||||
self.newNotification(id: self.interfaceID, title: localizedString("Network interface changed"), subtitle: nil)
|
||||
}
|
||||
self.interface = value.interface?.BSDName
|
||||
}
|
||||
|
||||
if self.localIPState {
|
||||
if value.laddr != self.localIP {
|
||||
self.newNotification(id: self.localID, title: localizedString("Local IP changed"), subtitle: nil)
|
||||
}
|
||||
self.localIP = value.laddr
|
||||
}
|
||||
|
||||
if self.publicIPState {
|
||||
if value.raddr.v4 ?? value.raddr.v6 != self.publicIP {
|
||||
self.newNotification(id: self.publicID, title: localizedString("Public IP changed"), subtitle: nil)
|
||||
}
|
||||
self.publicIP = value.raddr.v4 ?? value.raddr.v6
|
||||
}
|
||||
|
||||
if self.wifiState {
|
||||
if value.wifiDetails.ssid != self.wifi {
|
||||
self.newNotification(id: self.wifiID, title: localizedString("WiFi network changed"), subtitle: nil)
|
||||
}
|
||||
self.wifi = value.wifiDetails.ssid
|
||||
}
|
||||
}
|
||||
|
||||
internal func connectivityCallback(_ value: Network_Connectivity) {
|
||||
guard self.connectionState else { return }
|
||||
|
||||
if self.connection == nil {
|
||||
self.connection = value.status
|
||||
return
|
||||
}
|
||||
|
||||
if self.connection != value.status {
|
||||
var title: String
|
||||
if value.status {
|
||||
title = localizedString("Internet connection established")
|
||||
} else {
|
||||
title = localizedString("Internet connection lost")
|
||||
}
|
||||
self.newNotification(id: self.connectionID, title: title, subtitle: nil)
|
||||
}
|
||||
self.connection = value.status
|
||||
}
|
||||
|
||||
@objc private func toggleConnectionState(_ sender: NSControl) {
|
||||
self.interfaceState = controlState(sender)
|
||||
Store.shared.set(key: "\(self.module)_notifications_connection_state", value: self.interfaceState)
|
||||
}
|
||||
@objc private func toggleInterfaceState(_ sender: NSControl) {
|
||||
self.interfaceState = controlState(sender)
|
||||
Store.shared.set(key: "\(self.module)_notifications_interface_state", value: self.interfaceState)
|
||||
}
|
||||
@objc private func toggleLocalIPState(_ sender: NSControl) {
|
||||
self.interfaceState = controlState(sender)
|
||||
Store.shared.set(key: "\(self.module)_notifications_localIP_state", value: self.interfaceState)
|
||||
}
|
||||
@objc private func toggleNPublicIPState(_ sender: NSControl) {
|
||||
self.interfaceState = controlState(sender)
|
||||
Store.shared.set(key: "\(self.module)_notifications_publicIP_state", value: self.interfaceState)
|
||||
}
|
||||
@objc private func toggleWiFiState(_ sender: NSControl) {
|
||||
self.interfaceState = controlState(sender)
|
||||
Store.shared.set(key: "\(self.module)_notifications_wifi_state", value: self.interfaceState)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user