Эх сурвалжийг харах

Доделал переключение каналов, небольшой рефакторинг

Denis V. Dedkov 7 жил өмнө
parent
commit
b6001e4e81
7 өөрчлөгдсөн 77 нэмэгдсэн , 37 устгасан
  1. 2 0
      GradientButton.qml
  2. 9 1
      HomeForm.qml
  3. 0 30
      HttpClient.qml
  4. 6 0
      LightGroup.qml
  5. 9 5
      LightsModel.qml
  6. 50 0
      NooLiteClient.qml
  7. 1 1
      qml.qrc

+ 2 - 0
GradientButton.qml

@@ -26,6 +26,8 @@ Rectangle {
         id: ma
 
         anchors.fill: parent
+
+        onClicked: root.clicked()
     }
 
     gradient: ma.pressed ? pressedGradient : normalGradient

+ 9 - 1
HomeForm.qml

@@ -4,12 +4,16 @@ import QtQuick.Controls 2.0
 Page {
     title: qsTr("nooLight")
 
+    function showError(text) {
+        ToolTip.show(text, 1000)
+    }
+
     LightsModel {
         id: lightsModel
 
         serviceUrl: settings.serviceUrl
 
-        onError: console.log(text)
+        onError: showError(text)
     }
 
     ListView {
@@ -25,6 +29,10 @@ Page {
 
             title: groupName || ""
             lights: channels
+
+            onChannelClicked: {
+                lightsModel.switchChannel(channelId)
+            }
         }
     }
 

+ 0 - 30
HttpClient.qml

@@ -1,30 +0,0 @@
-import QtQuick 2.0
-
-QtObject {
-    id: root
-
-    signal reply(var data)
-    signal error(string text)
-
-    function get(url) {
-        var request = new XMLHttpRequest()
-
-        request.open('GET', url)
-        request.onreadystatechange = function () {
-            if (request.readyState !== XMLHttpRequest.DONE) {
-                return
-            }
-
-            if (request.status === 200) {
-                root.reply(JSON.parse(request.responseText))
-                return
-            }
-
-            root.error(qsTr("[%1] Request error: %2").
-                       arg(request.status).
-                       arg(request.statusText))
-        }
-
-        request.send()
-    }
-}

+ 6 - 0
LightGroup.qml

@@ -7,6 +7,8 @@ Item {
     property string title: ""
     property QtObject lights: undefined
 
+    signal channelClicked(int channelId)
+
     Column {
         width: root.width
 
@@ -71,6 +73,10 @@ Item {
                             text: name
                         }
                     }
+
+                    onClicked: {
+                        root.channelClicked(id)
+                    }
                 }
             }
         }

+ 9 - 5
LightsModel.qml

@@ -3,21 +3,21 @@ import QtQml.Models 2.1
 ListModel {
     id: root
 
-    readonly property var httpClient: HttpClient {
-        id: httpClient
+    readonly property var client: NooLiteClient {
+        id: nooLiteClient
 
         onError: {
             root.error(text)
             root.isLoading = false
         }
 
-        onReply: {
+        onModelLoad: {
             root.populateModel(data)
             root.isLoading = false
         }
     }
 
-    property string serviceUrl: undefined
+    property alias serviceUrl: nooLiteClient.serviceUrl
     property bool isLoading: false
 
     signal error(string text)
@@ -25,7 +25,7 @@ ListModel {
     onServiceUrlChanged: reload()
 
     function reload() {
-        root.httpClient.get(root.serviceUrl + '/static/channels.js')
+        root.client.loadModel()
         root.isLoading = true
     }
 
@@ -34,4 +34,8 @@ ListModel {
             root.append(group)
         })
     }
+
+    function switchChannel(channelId) {
+        root.client.switchChannel(channelId)
+    }
 }

+ 50 - 0
NooLiteClient.qml

@@ -0,0 +1,50 @@
+import QtQuick 2.0
+
+QtObject {
+    id: root
+
+    property string serviceUrl: undefined
+
+    signal modelLoad(var data)
+    signal error(string text)
+
+    function _get(url, callback) {
+        var request = new XMLHttpRequest()
+
+        request.open('GET', url)
+        request.onreadystatechange = function () {
+            if (request.readyState !== XMLHttpRequest.DONE) {
+                return
+            }
+
+            if (request.status === 200) {
+                if (callback !== undefined) {
+                    callback(JSON.parse(request.responseText))
+                }
+
+                return
+            }
+
+            root.error(qsTr("[%1] Request error: %2").
+                       arg(request.status).
+                       arg(request.statusText))
+        }
+
+        request.send()
+    }
+
+    function loadModel() {
+        _get(root.serviceUrl + '/static/channels.js', root.modelLoad)
+    }
+
+    function switchChannel(channelId) {
+        _get(root.serviceUrl + '/noolite/switch/%1'.arg(channelId), function (data) {
+            if (data.error) {
+                root.error(qsTr("Server error: %1").arg(data.error))
+                return
+            }
+
+            console.log(data.command, data.channel)
+        })
+    }
+}

+ 1 - 1
qml.qrc

@@ -11,6 +11,6 @@
         <file>GradientButton.qml</file>
         <file>lamp.png</file>
         <file>Off.png</file>
-        <file>HttpClient.qml</file>
+        <file>NooLiteClient.qml</file>
     </qresource>
 </RCC>