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) } }