-
-
Notifications
You must be signed in to change notification settings - Fork 929
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added notifications view to the Battery module
- Loading branch information
Showing
10 changed files
with
109 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// | ||
// notifications.swift | ||
// Battery | ||
// | ||
// Created by Serhiy Mytrovtsiy on 17/12/2023 | ||
// Using Swift 5.0 | ||
// Running on macOS 14.2 | ||
// | ||
// Copyright © 2023 Serhiy Mytrovtsiy. All rights reserved. | ||
// | ||
|
||
import Cocoa | ||
import Kit | ||
|
||
class Notifications: NotificationsWrapper { | ||
private let lowID: String = "low" | ||
private let highID: String = "high" | ||
private var lowLevel: String = "" | ||
private var highLevel: String = "" | ||
|
||
public init(_ module: ModuleType) { | ||
super.init(module, [self.lowID, self.highID]) | ||
|
||
if Store.shared.exist(key: "\(self.module)_lowLevelNotification") { | ||
let value = Store.shared.string(key: "\(self.module)_lowLevelNotification", defaultValue: self.lowID) | ||
Store.shared.set(key: "\(self.module)_notifications_low", value: value) | ||
Store.shared.remove("\(self.module)_lowLevelNotification") | ||
} | ||
if Store.shared.exist(key: "\(self.module)_highLevelNotification") { | ||
let value = Store.shared.string(key: "\(self.module)_highLevelNotification", defaultValue: self.highLevel) | ||
Store.shared.set(key: "\(self.module)_notifications_high", value: value) | ||
Store.shared.remove("\(self.module)_highLevelNotification") | ||
} | ||
|
||
self.lowLevel = Store.shared.string(key: "\(self.module)_notifications_low", defaultValue: self.lowLevel) | ||
self.highLevel = Store.shared.string(key: "\(self.module)_notifications_high", defaultValue: self.highLevel) | ||
|
||
self.addArrangedSubview(selectSettingsRow( | ||
title: localizedString("Low level notification"), | ||
action: #selector(self.changeLowLevel), | ||
items: notificationLevels, | ||
selected: self.lowLevel | ||
)) | ||
self.addArrangedSubview(selectSettingsRow( | ||
title: localizedString("High level notification"), | ||
action: #selector(self.changeHighLevel), | ||
items: notificationLevels, | ||
selected: self.highLevel | ||
)) | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
internal func usageCallback(_ value: Battery_Usage) { | ||
if let threshold = Double(self.lowLevel) { | ||
let title = localizedString("Low battery") | ||
var subtitle = localizedString("Battery remaining", "\(Int(value.level*100))") | ||
if value.timeToEmpty > 0 { | ||
subtitle += " (\(Double(value.timeToEmpty*60).printSecondsToHoursMinutesSeconds()))" | ||
} | ||
self.checkDouble(id: self.lowID, value: value.level, threshold: threshold, title: title, subtitle: subtitle, less: true) | ||
} | ||
|
||
if let threshold = Double(self.highLevel) { | ||
let title = localizedString("High battery") | ||
var subtitle = localizedString("Battery remaining to full charge", "\(Int((1-value.level)*100))") | ||
if value.timeToCharge > 0 { | ||
subtitle += " (\(Double(value.timeToCharge*60).printSecondsToHoursMinutesSeconds()))" | ||
} | ||
self.checkDouble(id: self.lowID, value: value.level, threshold: threshold, title: title, subtitle: subtitle) | ||
} | ||
} | ||
|
||
@objc private func changeLowLevel(_ sender: NSMenuItem) { | ||
guard let key = sender.representedObject as? String else { return } | ||
self.lowLevel = key.isEmpty ? "" : "\(Double(key) ?? 0)" | ||
Store.shared.set(key: "\(self.module)_notifications_low", value: self.lowLevel) | ||
} | ||
|
||
@objc private func changeHighLevel(_ sender: NSMenuItem) { | ||
guard let key = sender.representedObject as? String else { return } | ||
self.highLevel = key.isEmpty ? "" : "\(Double(key) ?? 0)" | ||
Store.shared.set(key: "\(self.module)_notifications_high", value: self.highLevel) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.