From a5ac0d25dce984f0380601851c3e28057b21574f Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Fri, 7 May 2021 18:55:45 +0200 Subject: [PATCH] feat: add information when last time charger was connected in the battery popup (#450) --- Modules/Battery/main.swift | 1 + Modules/Battery/popup.swift | 35 +++++++++++++++---- Modules/Battery/readers.swift | 4 +++ .../bg.lproj/Localizable.strings | 1 + .../cs.lproj/Localizable.strings | 1 + .../de.lproj/Localizable.strings | 1 + .../en.lproj/Localizable.strings | 1 + .../es.lproj/Localizable.strings | 1 + .../fr.lproj/Localizable.strings | 1 + .../hu.lproj/Localizable.strings | 1 + .../it.lproj/Localizable.strings | 1 + .../ja.lproj/Localizable.strings | 1 + .../ko.lproj/Localizable.strings | 1 + .../nb.lproj/Localizable.strings | 1 + .../nl.lproj/Localizable.strings | 1 + .../pl.lproj/Localizable.strings | 1 + .../pt-BR.lproj/Localizable.strings | 1 + .../pt-PT.lproj/Localizable.strings | 1 + .../ro.lproj/Localizable.strings | 1 + .../ru.lproj/Localizable.strings | 1 + .../tr.lproj/Localizable.strings | 1 + .../uk.lproj/Localizable.strings | 1 + .../vi.lproj/Localizable.strings | 1 + .../zh-Hans.lproj/Localizable.strings | 1 + .../zh-Hant.lproj/Localizable.strings | 1 + 25 files changed, 56 insertions(+), 6 deletions(-) diff --git a/Modules/Battery/main.swift b/Modules/Battery/main.swift index f911b7f6..8d3b3389 100644 --- a/Modules/Battery/main.swift +++ b/Modules/Battery/main.swift @@ -31,6 +31,7 @@ struct Battery_Usage: value_t { var timeToEmpty: Int = 0 var timeToCharge: Int = 0 + var timeOnACPower: Date? = nil public var widget_value: Double { get { diff --git a/Modules/Battery/popup.swift b/Modules/Battery/popup.swift index 2df56e7a..ea9481ed 100644 --- a/Modules/Battery/popup.swift +++ b/Modules/Battery/popup.swift @@ -21,7 +21,7 @@ internal class Popup: NSView, Popup_p { private let dashboardHeight: CGFloat = 90 private let detailsHeight: CGFloat = (22 * 5) + Constants.Popup.separatorHeight - private let batteryHeight: CGFloat = (22 * 4) + Constants.Popup.separatorHeight + private let batteryHeight: CGFloat = (22 * 5) + Constants.Popup.separatorHeight private let adapterHeight: CGFloat = (22 * 2) + Constants.Popup.separatorHeight private let processHeight: CGFloat = (22 * 1) @@ -37,6 +37,7 @@ internal class Popup: NSView, Popup_p { private var timeField: NSTextField? = nil private var healthField: NSTextField? = nil private var cyclesField: NSTextField? = nil + private var lastChargeField: NSTextField? = nil private var amperageField: NSTextField? = nil private var voltageField: NSTextField? = nil @@ -141,13 +142,14 @@ internal class Popup: NSView, Popup_p { let separator = SeparatorView(LocalizedString("Details"), origin: NSPoint(x: 0, y: self.detailsHeight-Constants.Popup.separatorHeight), width: self.frame.width) let container: NSView = NSView(frame: NSRect(x: 0, y: 0, width: self.frame.width, height: separator.frame.origin.y)) - self.levelField = PopupRow(container, n: 4, title: "\(LocalizedString("Level")):", value: "").1 - self.sourceField = PopupRow(container, n: 3, title: "\(LocalizedString("Source")):", value: "").1 - let t = self.labelValue(container, n: 2, title: "\(LocalizedString("Time")):", value: "") + self.levelField = PopupRow(container, n: 5, title: "\(LocalizedString("Level")):", value: "").1 + self.sourceField = PopupRow(container, n: 4, title: "\(LocalizedString("Source")):", value: "").1 + let t = self.labelValue(container, n: 3, title: "\(LocalizedString("Time")):", value: "") self.timeLabelField = t.0 self.timeField = t.1 - self.healthField = PopupRow(container, n: 1, title: "\(LocalizedString("Health")):", value: "").1 - self.cyclesField = PopupRow(container, n: 0, title: "\(LocalizedString("Cycles")):", value: "").1 + self.healthField = PopupRow(container, n: 2, title: "\(LocalizedString("Health")):", value: "").1 + self.cyclesField = PopupRow(container, n: 1, title: "\(LocalizedString("Cycles")):", value: "").1 + self.lastChargeField = PopupRow(container, n: 0, title: "\(LocalizedString("Last charge")):", value: "").1 view.addSubview(separator) view.addSubview(container) @@ -251,6 +253,27 @@ internal class Popup: NSView, Popup_p { } self.cyclesField?.stringValue = "\(value.cycles)" + let form = DateComponentsFormatter() + form.maximumUnitCount = 2 + form.unitsStyle = .full + form.allowedUnits = [.day, .hour, .minute] + if let timestamp = value.timeOnACPower { + if let duration = form.string(from: timestamp, to: Date()) { + let formatter = DateFormatter() + formatter.timeStyle = .short + formatter.dateStyle = .medium + + self.lastChargeField?.stringValue = duration + self.lastChargeField?.toolTip = formatter.string(from: timestamp) + } else { + self.lastChargeField?.stringValue = LocalizedString("Unknown") + self.lastChargeField?.toolTip = LocalizedString("Unknown") + } + } else { + self.lastChargeField?.stringValue = LocalizedString("Unknown") + self.lastChargeField?.toolTip = LocalizedString("Unknown") + } + self.amperageField?.stringValue = "\(abs(value.amperage)) mA" self.voltageField?.stringValue = "\(value.voltage.roundTo(decimalPlaces: 2)) V" let batteryPower = value.voltage * (Double(abs(value.amperage))/1000) diff --git a/Modules/Battery/readers.swift b/Modules/Battery/readers.swift index d3083e14..12ceed8b 100644 --- a/Modules/Battery/readers.swift +++ b/Modules/Battery/readers.swift @@ -74,6 +74,10 @@ internal class UsageReader: Reader { self.usage.timeToCharge = Int(time) } + if self.usage.powerSource == "AC Power" { + self.usage.timeOnACPower = Date() + } + self.usage.cycles = self.getIntValue("CycleCount" as CFString) ?? 0 let maxCapacity = self.getIntValue("MaxCapacity" as CFString) ?? 1 diff --git a/Stats/Supporting Files/bg.lproj/Localizable.strings b/Stats/Supporting Files/bg.lproj/Localizable.strings index a1024fd7..5a58c3d9 100644 --- a/Stats/Supporting Files/bg.lproj/Localizable.strings +++ b/Stats/Supporting Files/bg.lproj/Localizable.strings @@ -211,6 +211,7 @@ "Time and percentage" = "Време и процент"; "Time format" = "Формат на времето"; "Hide additional information when full" = "Скриване на допълнителна информация при пълен заряд"; +"Last charge" = "Последно зареждане"; // Fans "Fans" = "Вентилатори"; diff --git a/Stats/Supporting Files/cs.lproj/Localizable.strings b/Stats/Supporting Files/cs.lproj/Localizable.strings index 9a4d4fbb..6fea1c88 100644 --- a/Stats/Supporting Files/cs.lproj/Localizable.strings +++ b/Stats/Supporting Files/cs.lproj/Localizable.strings @@ -211,6 +211,7 @@ "Time and percentage" = "Čas a procenta"; "Time format" = "Formát času"; "Hide additional information when full" = "Skrýt další informace při plném nabití"; +"Last charge" = "Poslední poplatek"; // Fans "Fans" = "Ventilátory"; diff --git a/Stats/Supporting Files/de.lproj/Localizable.strings b/Stats/Supporting Files/de.lproj/Localizable.strings index 2d0d3f05..2de7d2d3 100644 --- a/Stats/Supporting Files/de.lproj/Localizable.strings +++ b/Stats/Supporting Files/de.lproj/Localizable.strings @@ -211,6 +211,7 @@ "Time and percentage" = "Zeit und Prozentsatz"; "Time format" = "Zeitformat"; "Hide additional information when full" = "Zusätzliche Informationen bei vollständiger Ladung verbergen"; +"Last charge" = "Letzte Ladung"; // Fans "Fans" = "Lüfter"; diff --git a/Stats/Supporting Files/en.lproj/Localizable.strings b/Stats/Supporting Files/en.lproj/Localizable.strings index a6636bf8..df443d24 100644 --- a/Stats/Supporting Files/en.lproj/Localizable.strings +++ b/Stats/Supporting Files/en.lproj/Localizable.strings @@ -211,6 +211,7 @@ "Time and percentage" = "Time and percentage"; "Time format" = "Time format"; "Hide additional information when full" = "Hide additional information when full"; +"Last charge" = "Last charge"; // Fans "Fans" = "Fans"; diff --git a/Stats/Supporting Files/es.lproj/Localizable.strings b/Stats/Supporting Files/es.lproj/Localizable.strings index 2de8e848..f8189c82 100644 --- a/Stats/Supporting Files/es.lproj/Localizable.strings +++ b/Stats/Supporting Files/es.lproj/Localizable.strings @@ -211,6 +211,7 @@ "Time and percentage" = "Tiempo y porcentaje"; "Time format" = "Formato de tiempo"; "Hide additional information when full" = "Ocultar información adicional cuando la batería está cargada"; +"Last charge" = "Última carga"; // Fans "Fans" = "Ventiladores"; diff --git a/Stats/Supporting Files/fr.lproj/Localizable.strings b/Stats/Supporting Files/fr.lproj/Localizable.strings index 53e1edc0..e31b300a 100644 --- a/Stats/Supporting Files/fr.lproj/Localizable.strings +++ b/Stats/Supporting Files/fr.lproj/Localizable.strings @@ -211,6 +211,7 @@ "Time and percentage" = "Temps et pourcentage"; "Time format" = "Format de l'heure"; "Hide additional information when full" = "Masquer les informations supplémentaires lorsque la batterie est pleine"; +"Last charge" = "Dernière charge"; // Fans "Fans" = "Fans"; diff --git a/Stats/Supporting Files/hu.lproj/Localizable.strings b/Stats/Supporting Files/hu.lproj/Localizable.strings index 814954ca..3213270c 100644 --- a/Stats/Supporting Files/hu.lproj/Localizable.strings +++ b/Stats/Supporting Files/hu.lproj/Localizable.strings @@ -211,6 +211,7 @@ "Time and percentage" = "Időtartam és százalék"; "Time format" = "Időformátum"; "Hide additional information when full" = "További információk elrejtése, ha teljesen fel van töltve"; +"Last charge" = "Utolsó töltés"; // Fans "Fans" = "Ventillátorok"; diff --git a/Stats/Supporting Files/it.lproj/Localizable.strings b/Stats/Supporting Files/it.lproj/Localizable.strings index 467db3b5..328a1e14 100644 --- a/Stats/Supporting Files/it.lproj/Localizable.strings +++ b/Stats/Supporting Files/it.lproj/Localizable.strings @@ -211,6 +211,7 @@ "Time and percentage" = "Tempo e percentuale"; "Time format" = "Formato del tempo"; "Hide additional information when full" = "Nascondi informazioni addizionali quando piena"; +"Last charge" = "Ultima carica"; // Fans "Fans" = "Fans"; diff --git a/Stats/Supporting Files/ja.lproj/Localizable.strings b/Stats/Supporting Files/ja.lproj/Localizable.strings index 57e9c96b..fcc4f33c 100644 --- a/Stats/Supporting Files/ja.lproj/Localizable.strings +++ b/Stats/Supporting Files/ja.lproj/Localizable.strings @@ -211,6 +211,7 @@ "Time and percentage" = "残り時間と残量(%)"; "Time format" = "時間の表示形式"; "Hide additional information when full" = "フル充電済みのときはアイコンのみ表示"; +"Last charge" = "ラストチャージ"; // Fans "Fans" = "ファン"; diff --git a/Stats/Supporting Files/ko.lproj/Localizable.strings b/Stats/Supporting Files/ko.lproj/Localizable.strings index faa73402..4526539c 100644 --- a/Stats/Supporting Files/ko.lproj/Localizable.strings +++ b/Stats/Supporting Files/ko.lproj/Localizable.strings @@ -211,6 +211,7 @@ "Time and percentage" = "시간과 비율"; "Time format" = "시간 형식"; "Hide additional information when full" = "배터리가 가득 차면 추가 정보 숨기기"; +"Last charge" = "마지막 청구"; // Fans "Fans" = "팬"; diff --git a/Stats/Supporting Files/nb.lproj/Localizable.strings b/Stats/Supporting Files/nb.lproj/Localizable.strings index 623d7c7c..9f6aa475 100644 --- a/Stats/Supporting Files/nb.lproj/Localizable.strings +++ b/Stats/Supporting Files/nb.lproj/Localizable.strings @@ -211,6 +211,7 @@ "Time and percentage" = "Tid og prosent"; "Time format" = "Tidsformat"; "Hide additional information when full" = "Skjul ekstra information når fulladet"; +"Last charge" = "Siste lading"; // Fans "Fans" = "Vifter"; diff --git a/Stats/Supporting Files/nl.lproj/Localizable.strings b/Stats/Supporting Files/nl.lproj/Localizable.strings index d683305e..34182bb3 100644 --- a/Stats/Supporting Files/nl.lproj/Localizable.strings +++ b/Stats/Supporting Files/nl.lproj/Localizable.strings @@ -211,6 +211,7 @@ "Time and percentage" = "Tijd and Percentage"; "Time format" = "Tijdnotatie"; "Hide additional information when full" = "Verberg aanvullende informatie wanneer deze vol is"; +"Last charge" = "Sidste opladning"; // Fans "Fans" = "Fans"; diff --git a/Stats/Supporting Files/pl.lproj/Localizable.strings b/Stats/Supporting Files/pl.lproj/Localizable.strings index 369008fd..a464d93a 100644 --- a/Stats/Supporting Files/pl.lproj/Localizable.strings +++ b/Stats/Supporting Files/pl.lproj/Localizable.strings @@ -211,6 +211,7 @@ "Time and percentage" = "Czas i procenty"; "Time format" = "Format czasu"; "Hide additional information when full" = "Ukryj dodatkowe informacje, gdy bateria jest naładowana"; +"Last charge" = "Ostatnie ładowanie"; // Fans "Fans" = "Wentylatory"; diff --git a/Stats/Supporting Files/pt-BR.lproj/Localizable.strings b/Stats/Supporting Files/pt-BR.lproj/Localizable.strings index 111398c6..983bc724 100644 --- a/Stats/Supporting Files/pt-BR.lproj/Localizable.strings +++ b/Stats/Supporting Files/pt-BR.lproj/Localizable.strings @@ -211,6 +211,7 @@ "Time and percentage" = "Tempo e porcentagem"; "Time format" = "Formato de hora"; "Hide additional information when full" = "Ocultar informações adicionais quando estiver cheia"; +"Last charge" = "Última carga"; // Fans "Fans" = "Ventoinhas"; diff --git a/Stats/Supporting Files/pt-PT.lproj/Localizable.strings b/Stats/Supporting Files/pt-PT.lproj/Localizable.strings index 7bc9f960..9778d6ba 100644 --- a/Stats/Supporting Files/pt-PT.lproj/Localizable.strings +++ b/Stats/Supporting Files/pt-PT.lproj/Localizable.strings @@ -211,6 +211,7 @@ "Time and percentage" = "Tempo e porcentagem"; "Time format" = "Formato de hora"; "Hide additional information when full" = "Ocultar informações adicionais quando estiver cheia"; +"Last charge" = "Última carga"; // Fans "Fans" = "Ventoinhas"; diff --git a/Stats/Supporting Files/ro.lproj/Localizable.strings b/Stats/Supporting Files/ro.lproj/Localizable.strings index 1cbf6fb1..6b645093 100644 --- a/Stats/Supporting Files/ro.lproj/Localizable.strings +++ b/Stats/Supporting Files/ro.lproj/Localizable.strings @@ -211,6 +211,7 @@ "Time and percentage" = "Timp si procentaj"; "Time format" = "Formatul timpului"; "Hide additional information when full" = "Ascunde restul informației cand e plină"; +"Last charge" = "Ultima încărcare"; // Fans "Fans" = "Ventilatoare"; diff --git a/Stats/Supporting Files/ru.lproj/Localizable.strings b/Stats/Supporting Files/ru.lproj/Localizable.strings index cca825b4..78f07ba8 100644 --- a/Stats/Supporting Files/ru.lproj/Localizable.strings +++ b/Stats/Supporting Files/ru.lproj/Localizable.strings @@ -211,6 +211,7 @@ "Time and percentage" = "Время и Проценты"; "Time format" = "Формат времени"; "Hide additional information when full" = "Скрыть дополнительную информацию при полном заряде"; +"Last charge" = "Последняя зарядка"; // Fans "Fans" = "Fans"; diff --git a/Stats/Supporting Files/tr.lproj/Localizable.strings b/Stats/Supporting Files/tr.lproj/Localizable.strings index 7f800636..1aa4e9cc 100644 --- a/Stats/Supporting Files/tr.lproj/Localizable.strings +++ b/Stats/Supporting Files/tr.lproj/Localizable.strings @@ -211,6 +211,7 @@ "Time and percentage" = "Zaman ve yüzde"; "Time format" = "Zaman formatı"; "Hide additional information when full" = "Pil dolduğunda ek bilgileri gizle"; +"Last charge" = "Son ödeme"; // Fans "Fans" = "Fanlar"; diff --git a/Stats/Supporting Files/uk.lproj/Localizable.strings b/Stats/Supporting Files/uk.lproj/Localizable.strings index 8d6d8379..70884dce 100644 --- a/Stats/Supporting Files/uk.lproj/Localizable.strings +++ b/Stats/Supporting Files/uk.lproj/Localizable.strings @@ -211,6 +211,7 @@ "Time and percentage" = "Час і проценти"; "Time format" = "Формат часу"; "Hide additional information when full" = "Приховати додаткову інформацію, якщо акумулятор заряджений"; +"Last charge" = "Остання зарядка"; // Fans "Fans" = "Fans"; diff --git a/Stats/Supporting Files/vi.lproj/Localizable.strings b/Stats/Supporting Files/vi.lproj/Localizable.strings index 8014f81e..f9446e4d 100644 --- a/Stats/Supporting Files/vi.lproj/Localizable.strings +++ b/Stats/Supporting Files/vi.lproj/Localizable.strings @@ -211,6 +211,7 @@ "Time and percentage" = "Thời gian và tỷ lệ phần trăm"; "Time format" = "Định dạng thời gian"; "Hide additional information when full" = "Ẩn thông tin bổ sung khi pin đầy"; +"Last charge" = "Lần sạc cuối cùng"; // Fans "Fans" = "Quạt"; diff --git a/Stats/Supporting Files/zh-Hans.lproj/Localizable.strings b/Stats/Supporting Files/zh-Hans.lproj/Localizable.strings index 9c1b247d..8b5c8a2b 100644 --- a/Stats/Supporting Files/zh-Hans.lproj/Localizable.strings +++ b/Stats/Supporting Files/zh-Hans.lproj/Localizable.strings @@ -211,6 +211,7 @@ "Time and percentage" = "时间和百分比"; "Time format" = "时间格式"; "Hide additional information when full" = "电池充满后隐藏其他信息"; +"Last charge" = "最后一次收费"; // Fans "Fans" = "风扇"; diff --git a/Stats/Supporting Files/zh-Hant.lproj/Localizable.strings b/Stats/Supporting Files/zh-Hant.lproj/Localizable.strings index 47478c3d..7c29cdb2 100644 --- a/Stats/Supporting Files/zh-Hant.lproj/Localizable.strings +++ b/Stats/Supporting Files/zh-Hant.lproj/Localizable.strings @@ -211,6 +211,7 @@ "Time and percentage" = "時間和百分比"; "Time format" = "時間格式"; "Hide additional information when full" = "電池充飽後隱藏其他資訊"; +"Last charge" = "最後一次收費"; // Fans "Fans" = "風扇";