- add low battery notification with option to select battery level (disabled/3%/5%/10%/15%/20%/30%/40%/50%)

This commit is contained in:
Serhiy Mytrovtsiy
2020-07-15 22:18:24 +02:00
parent eaeeb476ac
commit 706e8219ce
7 changed files with 152 additions and 10 deletions

View File

@@ -27,7 +27,7 @@ struct Battery_Usage: value_t {
var temperature: Double = 0
var ACwatts: Int = 0
var ACstatus: Bool = true
var ACstatus: Bool = false
var timeToEmpty: Int = 0
var timeToCharge: Int = 0
@@ -42,12 +42,18 @@ struct Battery_Usage: value_t {
public class Battery: Module {
private var usageReader: UsageReader? = nil
private let popupView: Popup = Popup()
private var settingsView: Settings
private let store: UnsafePointer<Store>
public init(_ store: UnsafePointer<Store>) {
self.store = store
self.settingsView = Settings("Battery", store: store)
super.init(
store: store,
popup: self.popupView,
settings: nil
settings: self.settingsView
)
guard self.available else { return }
@@ -76,6 +82,7 @@ public class Battery: Module {
return
}
self.checkNotification(value: value!)
self.popupView.usageCallback(value!)
if let widget = self.widget as? Mini {
widget.setValue(abs(value!.level), sufix: "%")
@@ -88,4 +95,24 @@ public class Battery: Module {
)
}
}
private func checkNotification(value: Battery_Usage) {
let level = self.store.pointee.string(key: "\(self.config.name)_lowLevelNotification", defaultValue: "0.15")
if level == "Disabled" {
return
}
var subtitle = "\((Int(value.level*100)))% remaining"
if value.timeToEmpty != 0 {
subtitle += " (\(Double(value.timeToEmpty*60).printSecondsToHoursMinutesSeconds()))"
}
if let notificationLevel = Double(level), value.level <= notificationLevel {
showNotification(
title: "Low battery",
subtitle: subtitle,
id: "battery-level",
icon: NSImage(named: NSImage.Name("low-battery"))!
)
}
}
}

View File

@@ -0,0 +1,80 @@
//
// settings.swift
// Battery
//
// Created by Serhiy Mytrovtsiy on 15/07/2020.
// Using Swift 5.0.
// Running on macOS 10.15.
//
// Copyright © 2020 Serhiy Mytrovtsiy. All rights reserved.
//
import Cocoa
import StatsKit
import ModuleKit
import SystemConfiguration
internal class Settings: NSView, Settings_v {
public var callback: (() -> Void) = {}
private let title: String
private let store: UnsafePointer<Store>
private var button: NSPopUpButton?
private let levelsList: [String] = ["Disabled", "0.03", "0.05", "0.1", "0.15", "0.2", "0.25", "0.3", "0.4", "0.5"]
private var lowLevelNotification: String {
get {
return self.store.pointee.string(key: "\(self.title)_lowLevelNotification", defaultValue: "0.15")
}
}
public init(_ title: String, store: UnsafePointer<Store>) {
self.title = title
self.store = store
super.init(frame: CGRect(x: Constants.Settings.margin, y: Constants.Settings.margin, width: Constants.Settings.width - (Constants.Settings.margin*2), height: 0))
self.wantsLayer = true
self.canDrawConcurrently = true
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public func load(widget: widget_t) {
self.subviews.forEach{ $0.removeFromSuperview() }
let rowHeight: CGFloat = 30
let levels: [String] = self.levelsList.map { (v: String) -> String in
if let level = Double(v) {
return "\(Int(level*100))%"
}
return v
}
self.addSubview(SelectTitleRow(
frame: NSRect(
x:Constants.Settings.margin,
y: Constants.Settings.margin + (rowHeight + Constants.Settings.margin) * 0,
width: self.frame.width - (Constants.Settings.margin*2),
height: rowHeight
),
title: "Low level notification",
action: #selector(changeUpdateInterval),
items: levels,
selected: self.lowLevelNotification == "Disabled" ? self.lowLevelNotification : "\(Int((Double(self.lowLevelNotification) ?? 0)*100))%"
))
self.setFrameSize(NSSize(width: self.frame.width, height: 30 + (Constants.Settings.margin*2)))
}
@objc private func changeUpdateInterval(_ sender: NSMenuItem) {
if sender.title == "Disabled" {
store.pointee.set(key: "\(self.title)_lowLevelNotification", value: sender.title)
} else if let value = Double(sender.title.replacingOccurrences(of: "%", with: "")) {
store.pointee.set(key: "\(self.title)_lowLevelNotification", value: "\(value/100)")
}
}
}