From 81055262d2cc6b762fa48772b7462d3e79e0f084 Mon Sep 17 00:00:00 2001 From: 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> Date: Wed, 30 Apr 2025 17:28:30 +1000 Subject: bluetooth: lazy update devices --- services/Bluetooth.qml | 45 ++++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 17 deletions(-) (limited to 'services/Bluetooth.qml') diff --git a/services/Bluetooth.qml b/services/Bluetooth.qml index 7cdd457..4092212 100644 --- a/services/Bluetooth.qml +++ b/services/Bluetooth.qml @@ -9,7 +9,7 @@ Singleton { property bool powered property bool discovering - property list devices: [] + readonly property list devices: [] Process { running: true @@ -41,27 +41,38 @@ Singleton { stdout: SplitParser { onRead: data => { const devices = JSON.parse(data); - root.devices = devices.map(d => deviceComp.createObject(root, { - name: d.Name, - alias: d.Alias, - address: d.Address, - icon: d.Icon, - connected: d.Connected === "yes", - paired: d.Paired === "yes", - trusted: d.Trusted === "yes" - })).filter(d => d); + const rDevices = root.devices; + + const len = rDevices.length; + for (let i = 0; i < len; i++) { + const device = rDevices[i]; + if (!devices.find(d => d.address === device?.Address)) + rDevices.splice(i, 1); + } + + for (const device of devices) { + const match = rDevices.find(d => d.address === device.Address); + if (match) { + match.lastIpcObject = device; + } else { + rDevices.push(deviceComp.createObject(root, { + lastIpcObject: device + })); + } + } } } } component Device: QtObject { - property string name - property string alias - property string address - property string icon - property bool connected - property bool paired - property bool trusted + required property var lastIpcObject + readonly property string name: lastIpcObject.Name + readonly property string alias: lastIpcObject.Alias + readonly property string address: lastIpcObject.Address + readonly property string icon: lastIpcObject.Icon + readonly property bool connected: lastIpcObject.Connected === "yes" + readonly property bool paired: lastIpcObject.Paired === "yes" + readonly property bool trusted: lastIpcObject.Trusted === "yes" } Component { -- cgit v1.2.3-freya