mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-14 00:04:15 +09:00
feat: added notifications to the Bluetooth module (#1891)
This commit is contained in:
@@ -39,7 +39,7 @@
|
||||
<key>popup</key>
|
||||
<false/>
|
||||
<key>notifications</key>
|
||||
<false/>
|
||||
<true/>
|
||||
</dict>
|
||||
</dict>
|
||||
</plist>
|
||||
|
||||
@@ -28,15 +28,14 @@ public struct BLEDevice: Codable {
|
||||
var isPeripheralInitialized: Bool = false
|
||||
|
||||
var id: String {
|
||||
get {
|
||||
return self.uuid?.uuidString ?? self.address
|
||||
}
|
||||
get { self.uuid?.uuidString ?? self.address }
|
||||
}
|
||||
|
||||
var state: Bool {
|
||||
get {
|
||||
return Store.shared.bool(key: "ble_\(self.id)", defaultValue: false)
|
||||
}
|
||||
get { Store.shared.bool(key: "ble_\(self.id)", defaultValue: false) }
|
||||
}
|
||||
var notificationThreshold: String {
|
||||
Store.shared.string(key: "ble_\(self.id)_notification", defaultValue: "")
|
||||
}
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
@@ -80,11 +79,15 @@ public class Bluetooth: Module {
|
||||
private var devicesReader: DevicesReader?
|
||||
private let popupView: Popup = Popup()
|
||||
private let settingsView: Settings = Settings()
|
||||
private let notificationsView: Notifications
|
||||
|
||||
public init() {
|
||||
self.notificationsView = Notifications(.bluetooth)
|
||||
|
||||
super.init(
|
||||
popup: self.popupView,
|
||||
settings: self.settingsView
|
||||
settings: self.settingsView,
|
||||
notifications: self.notificationsView
|
||||
)
|
||||
guard self.available else { return }
|
||||
|
||||
@@ -106,6 +109,7 @@ public class Bluetooth: Module {
|
||||
DispatchQueue.main.async(execute: {
|
||||
self.popupView.batteryCallback(active)
|
||||
self.settingsView.setList(active)
|
||||
self.notificationsView.callback(active)
|
||||
})
|
||||
|
||||
var list: [Stack_t] = []
|
||||
|
||||
84
Modules/Bluetooth/notifications.swift
Normal file
84
Modules/Bluetooth/notifications.swift
Normal file
@@ -0,0 +1,84 @@
|
||||
//
|
||||
// notifications.swift
|
||||
// Bluetooth
|
||||
//
|
||||
// Created by Serhiy Mytrovtsiy on 24/06/2024
|
||||
// Using Swift 5.0
|
||||
// Running on macOS 14.5
|
||||
//
|
||||
// Copyright © 2024 Serhiy Mytrovtsiy. All rights reserved.
|
||||
//
|
||||
|
||||
import Cocoa
|
||||
import Kit
|
||||
|
||||
class Notifications: NotificationsWrapper {
|
||||
private var list: [String: Bool] = [:]
|
||||
|
||||
private let emptyView: EmptyView = EmptyView(msg: localizedString("No Bluetooth devices are available"))
|
||||
private var section: PreferencesSection = PreferencesSection()
|
||||
|
||||
public init(_ module: ModuleType) {
|
||||
super.init(module)
|
||||
|
||||
self.addArrangedSubview(self.emptyView)
|
||||
self.addArrangedSubview(self.section)
|
||||
self.section.isHidden = true
|
||||
|
||||
self.addArrangedSubview(NSView())
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
internal func callback(_ list: [BLEDevice]) {
|
||||
if self.list.count != list.count && !self.list.isEmpty {
|
||||
self.section.removeFromSuperview()
|
||||
self.section = PreferencesSection()
|
||||
self.addArrangedSubview(self.section)
|
||||
self.list = [:]
|
||||
}
|
||||
|
||||
if list.isEmpty && self.emptyView.isHidden {
|
||||
self.emptyView.isHidden = false
|
||||
self.section.isHidden = true
|
||||
return
|
||||
} else if !list.isEmpty && !self.emptyView.isHidden {
|
||||
self.emptyView.isHidden = true
|
||||
self.section.isHidden = false
|
||||
}
|
||||
|
||||
list.forEach { (d: BLEDevice) in
|
||||
if self.list[d.id] == nil {
|
||||
let btn = selectView(
|
||||
action: #selector(self.changeSensorNotificaion),
|
||||
items: notificationLevels,
|
||||
selected: d.notificationThreshold
|
||||
)
|
||||
btn.identifier = NSUserInterfaceItemIdentifier(rawValue: "\(d.uuid?.uuidString ?? d.address)")
|
||||
section.add(PreferencesRow(d.name, component: btn))
|
||||
self.list[d.id] = true
|
||||
}
|
||||
}
|
||||
|
||||
let devices = list.filter({ !$0.notificationThreshold.isEmpty })
|
||||
let title = localizedString("Bluetooth threshold")
|
||||
|
||||
for d in devices {
|
||||
if let threshold = Double(d.notificationThreshold) {
|
||||
for l in d.batteryLevel {
|
||||
let subtitle = localizedString("\(localizedString(d.name)): \(l.value)%")
|
||||
if let value = Double(l.value) {
|
||||
self.checkDouble(id: d.id, value: value/100, threshold: threshold, title: title, subtitle: subtitle, less: true)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@objc private func changeSensorNotificaion(_ sender: NSMenuItem) {
|
||||
guard let id = sender.identifier, let key = sender.representedObject as? String else { return }
|
||||
Store.shared.set(key: "ble_\(id.rawValue)_notification", value: key)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user