From 36f2055f1019b336bdbbcd4d90aa93e8e09b61a9 Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Thu, 15 Sep 2022 16:59:15 +0200 Subject: [PATCH] fix: remove battery level from the popup view if not longer available (#1083) --- Modules/Bluetooth/popup.swift | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Modules/Bluetooth/popup.swift b/Modules/Bluetooth/popup.swift index 93e5fe2d..a9e3ad8e 100644 --- a/Modules/Bluetooth/popup.swift +++ b/Modules/Bluetooth/popup.swift @@ -70,6 +70,8 @@ internal class BLEView: NSStackView { return CGSize(width: self.bounds.width, height: self.bounds.height) } + private var levels: [NSTextField] = [] + public init(width: CGFloat, address: String, name: String, batteryLevel: [KeyValue_t]) { self.address = address @@ -103,8 +105,13 @@ internal class BLEView: NSStackView { } public func update(_ batteryLevel: [KeyValue_t]) { + self.levels.filter{ v in !batteryLevel.contains(where: { $0.key == v.identifier?.rawValue }) }.forEach { (v: NSView) in + v.removeFromSuperview() + } + self.levels = self.levels.filter{ v in batteryLevel.contains(where: { $0.key == v.identifier?.rawValue }) } + batteryLevel.forEach { (pair: KeyValue_t) in - if let view = self.subviews.first(where: { $0.identifier?.rawValue == pair.key }) as? NSTextField { + if let view = self.levels.first(where: { $0.identifier?.rawValue == pair.key }) { view.stringValue = "\(pair.value)%" } else { self.addLevel(pair) @@ -119,5 +126,6 @@ internal class BLEView: NSStackView { valueView.stringValue = "\(pair.value)%" valueView.toolTip = pair.key self.addArrangedSubview(valueView) + self.levels.append(valueView) } }