From e73188fa92ffc831a3d79686b9717bc20c04f0f7 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Mon, 13 Jul 2020 00:12:15 +0200 Subject: [PATCH] Improve color picker throttling --- nymea-app/ui/devicepages/LightDevicePage.qml | 50 +++++++++++++++----- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/nymea-app/ui/devicepages/LightDevicePage.qml b/nymea-app/ui/devicepages/LightDevicePage.qml index 95662866..3cb014bb 100644 --- a/nymea-app/ui/devicepages/LightDevicePage.qml +++ b/nymea-app/ui/devicepages/LightDevicePage.qml @@ -262,6 +262,20 @@ DevicePageBase { id: colorPicker anchors.fill: parent + property int pendingCommand: -1 + + property var queuedColor: null + + function sendColor(color) { + var params = []; + var param1 = {}; + param1["paramTypeId"] = root.colorActionType.paramTypes.get(0).id; + param1["value"] = color; + params.push(param1) + colorPicker.pendingCommand = engine.deviceManager.executeAction(root.device.id, root.colorActionType.id, params) + print("sent command", colorPicker.pendingCommand, color) + } + color: root.colorState ? root.colorState.value : "white" touchDelegate: Rectangle { height: 15 @@ -283,19 +297,33 @@ DevicePageBase { } } - property var lastSentTime: new Date() - onColorChanged: { - var currentTime = new Date(); - if (pressed && currentTime - lastSentTime > 200) { - var params = []; - var param1 = {}; - param1["paramTypeId"] = root.colorActionType.paramTypes.get(0).id; - param1["value"] = color; - params.push(param1) - engine.deviceManager.executeAction(root.device.id, root.colorActionType.id, params) - lastSentTime = currentTime + + Connections { + target: engine.deviceManager + onExecuteActionReply: { + print("action finished", JSON.stringify(params)) + if (params.id === colorPicker.pendingCommand) { + colorPicker.pendingCommand = -1; + if (colorPicker.queuedColor) { + colorPicker.sendColor(colorPicker.queuedColor); + colorPicker.queuedColor = null + } + } } } + + onColorChanged: { + if (!pressed) { + return; + } + + if (pendingCommand != -1) { + queuedColor = color; + return; + } + + sendColor(color); + } } } }