feat: moved Disk notification settings to the new selectors

This commit is contained in:
Serhiy Mytrovtsiy
2024-05-10 20:28:32 +02:00
parent 2b70064eb5
commit d055ded9c7
4 changed files with 43 additions and 63 deletions

View File

@@ -14,24 +14,28 @@ import Kit
class Notifications: NotificationsWrapper {
private let utilizationID: String = "usage"
private var utilizationLevel: String = ""
private var utilizationState: Bool = false
private var utilization: Int = 80
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")
let value = Store.shared.string(key: "\(self.module)_notifications_free", defaultValue: "")
if let v = Double(value) {
Store.shared.set(key: "\(self.module)_notifications_utilization_state", value: true)
Store.shared.set(key: "\(self.module)_notifications_utilization_value", value: Int(v*100))
Store.shared.remove("\(self.module)_notificationLevel")
}
}
self.utilizationLevel = Store.shared.string(key: "\(self.module)_notifications_utilization", defaultValue: self.utilizationLevel)
self.utilizationState = Store.shared.bool(key: "\(self.module)_notifications_utilization_state", defaultValue: self.utilizationState)
self.utilization = Store.shared.int(key: "\(self.module)_notifications_utilization_value", defaultValue: self.utilization)
self.addArrangedSubview(PreferencesSection([
PreferencesRow(localizedString("Usage"), component: selectView(
action: #selector(self.changeUsage),
items: notificationLevels,
selected: self.utilizationLevel
PreferencesRow(localizedString("Usage"), component: PreferencesSwitch(
action: self.toggleUtilization, state: self.utilizationState, with: StepperInput(self.utilization, callback: self.changeUtilization)
))
]))
}
@@ -43,15 +47,18 @@ class Notifications: NotificationsWrapper {
internal func utilizationCallback(_ value: Double) {
let title = localizedString("Disk utilization threshold")
if let threshold = Double(self.utilizationLevel) {
if self.utilizationState {
let subtitle = localizedString("Disk utilization is", "\(Int((value)*100))%")
self.checkDouble(id: self.utilizationID, value: value, threshold: threshold, title: title, subtitle: subtitle)
self.checkDouble(id: self.utilizationID, value: value, threshold: Double(self.utilization)/100, 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)
@objc private func toggleUtilization(_ sender: NSControl) {
self.utilizationState = controlState(sender)
Store.shared.set(key: "\(self.module)_notifications_utilization_state", value: self.utilizationState)
}
@objc private func changeUtilization(_ newValue: Int) {
self.utilization = newValue
Store.shared.set(key: "\(self.module)_notifications_utilization_value", value: self.utilization)
}
}

View File

@@ -543,22 +543,11 @@ internal class Popup: PopupWrapper {
items: Scale.allCases,
selected: self.chartScale.key
)),
PreferencesRow(localizedString("Scale value"), component: {
let view: NSStackView = NSStackView()
view.orientation = .horizontal
view.spacing = 2
let valueField = StepperInput(self.chartFixedScale, range: NSRange(location: 1, length: 1023))
valueField.callback = self.toggleFixedScale
valueField.widthAnchor.constraint(equalToConstant: 80).isActive = true
view.addArrangedSubview(NSView())
view.addArrangedSubview(valueField)
view.addArrangedSubview(selectView(
action: #selector(self.toggleUploadScaleSize),
items: SizeUnit.allCases,
selected: self.chartFixedScaleSize.key
))
return view
}())
PreferencesRow(localizedString("Scale value"), component: StepperInput(
self.chartFixedScale, range: NSRange(location: 1, length: 1023),
unit: self.chartFixedScaleSize.key, units: SizeUnit.allCases,
callback: self.toggleFixedScale, unitCallback: self.toggleFixedScaleSize
))
])
view.addArrangedSubview(self.chartPrefSection!)
self.chartPrefSection?.toggleVisibility(2, newState: self.chartScale == .fixed)
@@ -639,12 +628,10 @@ internal class Popup: PopupWrapper {
self.chart?.setScale(self.chartScale, Double(self.chartFixedScaleSize.toBytes(newValue)))
Store.shared.set(key: "\(self.title)_chartFixedScale", value: newValue)
}
@objc private func toggleUploadScaleSize(_ sender: NSMenuItem) {
guard let key = sender.representedObject as? String,
let value = SizeUnit.allCases.first(where: { $0.key == key }) else { return }
self.chartFixedScaleSize = value
self.chart?.setScale(self.chartScale, Double(self.chartFixedScaleSize.toBytes(self.chartFixedScale)))
Store.shared.set(key: "\(self.title)_chartFixedScaleSize", value: key)
private func toggleFixedScaleSize(_ newValue: KeyValue_p) {
guard let newUnit = newValue as? SizeUnit else { return }
self.chartFixedScaleSize = newUnit
Store.shared.set(key: "\(self.title)_chartFixedScaleSize", value: self.chartFixedScaleSize.key)
self.display()
}

View File

@@ -146,26 +146,13 @@ internal class Settings: NSStackView, Settings_v, NSTextFieldDelegate {
self.section = section
self.widgetThresholdSection = PreferencesSection([
PreferencesRow(localizedString("Widget activation threshold"), component: switchView(
action: #selector(self.toggleWidgetActivationThreshold),
state: self.widgetActivationThresholdState
)),
PreferencesRow(localizedString("Value"), component: {
let view: NSStackView = NSStackView()
view.orientation = .horizontal
view.spacing = 2
let valueField = StepperInput(self.widgetActivationThreshold, range: NSRange(location: 1, length: 1023))
valueField.callback = self.changeWidgetActivationThreshold
valueField.widthAnchor.constraint(equalToConstant: 80).isActive = true
view.addArrangedSubview(NSView())
view.addArrangedSubview(valueField)
view.addArrangedSubview(selectView(
action: #selector(self.toggleWidgetActivationThresholdSize),
items: SizeUnit.allCases,
selected: self.widgetActivationThresholdSize.key
))
return view
}())
PreferencesRow(localizedString("Widget activation threshold"), component: PreferencesSwitch(
action: self.toggleWidgetActivationThreshold, state: self.widgetActivationThresholdState, with: StepperInput(
self.widgetActivationThreshold, range: NSRange(location: 1, length: 1023),
unit: self.widgetActivationThresholdSize.key, units: SizeUnit.allCases,
callback: self.changeWidgetActivationThreshold, unitCallback: self.toggleWidgetActivationThresholdSize
)
))
])
self.addArrangedSubview(self.widgetThresholdSection!)
self.widgetThresholdSection?.toggleVisibility(1, newState: self.widgetActivationThresholdState)
@@ -234,11 +221,10 @@ internal class Settings: NSStackView, Settings_v, NSTextFieldDelegate {
self.widgetActivationThreshold = newValue
Store.shared.set(key: "\(self.title)_widgetActivationThreshold", value: newValue)
}
@objc private func toggleWidgetActivationThresholdSize(_ sender: NSMenuItem) {
guard let key = sender.representedObject as? String,
let value = SizeUnit.allCases.first(where: { $0.key == key }) else { return }
self.widgetActivationThresholdSize = value
Store.shared.set(key: "\(self.title)_widgetActivationThresholdSize", value: key)
private func toggleWidgetActivationThresholdSize(_ newValue: KeyValue_p) {
guard let newUnit = newValue as? SizeUnit else { return }
self.widgetActivationThresholdSize = newUnit
Store.shared.set(key: "\(self.title)_widgetActivationThresholdSize", value: self.widgetActivationThresholdSize.key)
self.display()
}