From 8d26def6d46f2836d27c976537425b7978dc45bb Mon Sep 17 00:00:00 2001 From: Egor Nemtsev Date: Wed, 14 Aug 2019 16:01:26 +0300 Subject: Make AlexaInterface object a singleton for qml Task-number: AUTOSUITE-1163 Change-Id: Ia6d863c9fa4e3d1d61dee7221676239dd448658e Reviewed-by: Bramastyo Harimukti Santoso --- app/AlexaView.qml | 23 +++++++++++------------ app/AuthView.qml | 7 +++---- app/Header.qml | 12 +++++------- app/main.qml | 17 +++++++---------- plugins/alexainterface/AlexaInterface.h | 10 ++++++++++ plugins/alexainterface/alexainterface_plugin.cpp | 3 ++- 6 files changed, 38 insertions(+), 34 deletions(-) diff --git a/app/AlexaView.qml b/app/AlexaView.qml index d9e9542..cae6809 100644 --- a/app/AlexaView.qml +++ b/app/AlexaView.qml @@ -44,7 +44,6 @@ import alexainterface 1.0 Control { id: root - property var alexaInterface property string neptuneState: "Maximized" state: { @@ -187,7 +186,7 @@ Control { } Connections { - target: alexaInterface + target: AlexaInterface onCardReady: { if (card.type === BaseCard.Weather) { priv.cardData = card @@ -246,7 +245,7 @@ Control { anchors.right: parent.right height: parent.height Rectangle { - width: interactionButton.width * ( 1.0 + 0.3 * alexaInterface.audioLevel ) + width: interactionButton.width * ( 1.0 + 0.3 * AlexaInterface.audioLevel ) height: width anchors.centerIn: interactionButton radius: width / 2 @@ -272,24 +271,24 @@ Control { background: Rectangle { anchors.fill: parent radius: width / 2 - color: alexaInterface.connectionStatus === AlexaInterface.Connected ? "#00caff" : "lightgrey" + color: AlexaInterface.connectionStatus === Alexa.Connected ? "#00caff" : "lightgrey" } icon.height: interactionButton.height/2 icon.width: interactionButton.width/2 icon.source: { - if (alexaInterface.dialogState === AlexaInterface.Speaking) { + if (AlexaInterface.dialogState === Alexa.Speaking) { return Qt.resolvedUrl("assets/ic_speaking.png") - } else if (alexaInterface.dialogState === AlexaInterface.Thinking) { + } else if (AlexaInterface.dialogState === Alexa.Thinking) { return Qt.resolvedUrl("assets/ic_thinking.png") } else { return Qt.resolvedUrl("assets/ic_microphone.png") } } - enabled: alexaInterface.connectionStatus === AlexaInterface.Connected + enabled: AlexaInterface.connectionStatus === Alexa.Connected onClicked: { - if (alexaInterface.dialogState === AlexaInterface.Idle) { - alexaInterface.tapToTalk() + if (AlexaInterface.dialogState === Alexa.Idle) { + AlexaInterface.tapToTalk() } } @@ -306,7 +305,7 @@ Control { anchors.fill: busyIndicator source: busyIndicator color: "#0071ff" - visible: (alexaInterface.dialogState === AlexaInterface.Listening) || (alexaInterface.dialogState === AlexaInterface.Thinking) + visible: (AlexaInterface.dialogState === Alexa.Listening) || (AlexaInterface.dialogState === Alexa.Thinking) RotationAnimation on rotation { loops: Animation.Infinite from: 0 @@ -323,12 +322,12 @@ Control { anchors.topMargin: Sizes.dp(600) + interactionButton.height + Sizes.dp(100) width: parent.width > Sizes.dp(270) ? Sizes.dp(270) : parent.width height: Sizes.dp(70) - opacity: (alexaInterface.dialogState === AlexaInterface.Listening) || (alexaInterface.dialogState === AlexaInterface.Speaking) ? 1 : 0 + opacity: (AlexaInterface.dialogState === Alexa.Listening) || (AlexaInterface.dialogState === Alexa.Speaking) ? 1 : 0 Behavior on opacity { NumberAnimation { duration: 100 } } visible: opacity > 0 text: qsTr("STOP") font.pixelSize: Sizes.fontSizeM - onClicked: alexaInterface.stopTalking() + onClicked: AlexaInterface.stopTalking() background: ButtonBackground { anchors.fill: parent opacity: 1 diff --git a/app/AuthView.qml b/app/AuthView.qml index 2aeff77..e34f35f 100644 --- a/app/AuthView.qml +++ b/app/AuthView.qml @@ -46,7 +46,6 @@ import shared.Sizes 1.0 Control { id: root - property var alexaInterface property var alexaAuth property bool authorizationRequested: false @@ -122,8 +121,8 @@ Control { height: Sizes.dp(60) verticalAlignment: Text.AlignVCenter font.pixelSize: Sizes.fontSizeM - text: "Your code: " + alexaInterface.authCode - visible: alexaInterface.authCode !== "" + text: "Your code: " + AlexaInterface.authCode + visible: AlexaInterface.authCode !== "" } WebView { @@ -132,7 +131,7 @@ Control { width: parent.width height: Sizes.dp(700) anchors.horizontalCenter: parent.horizontalCenter - url: alexaInterface.authUrl + url: AlexaInterface.authUrl visible: url !== "" } diff --git a/app/Header.qml b/app/Header.qml index ef649ad..18c91cb 100644 --- a/app/Header.qml +++ b/app/Header.qml @@ -37,9 +37,7 @@ import alexainterface 1.0 Control { id: root - property bool unfoldHeader: false - property var alexaInterface QtObject { id: privateData @@ -82,17 +80,17 @@ Control { width: Sizes.dp(26) height: width radius: width/2 - color: alexaInterface.connectionStatus === AlexaInterface.Connected ? "#32CD32" : - (alexaInterface.connectionStatus === AlexaInterface.Disconnected) ? - "red" : "grey" + color: AlexaInterface.connectionStatus === Alexa.Connected ? "#32CD32" : + (AlexaInterface.connectionStatus === Alexa.Disconnected) ? + "red" : "grey" } Label { anchors.verticalCenter: parent.verticalCenter font.pixelSize: Sizes.fontSizeS text: { - if (alexaInterface.connectionStatus === AlexaInterface.Pending) + if (AlexaInterface.connectionStatus === Alexa.Pending) return qsTr("Pending") - else if (alexaInterface.connectionStatus === AlexaInterface.Connected) + else if (AlexaInterface.connectionStatus === Alexa.Connected) return qsTr("Connected") else return qsTr("Disconnected") } diff --git a/app/main.qml b/app/main.qml index 2b84fee..2c99142 100644 --- a/app/main.qml +++ b/app/main.qml @@ -73,9 +73,8 @@ ApplicationCCWindow { httpUserAgent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36" } - AlexaInterface { - id: alexa - logLevel: AlexaInterface.Info + Connections { + target: AlexaInterface onAuthCodeChanged: { if (authCode !== "") { alexaAuth.authCode = authCode @@ -84,6 +83,9 @@ ApplicationCCWindow { onAuthUrlChanged: { alexaAuth.authUrl = authUrl } + Component.onCompleted: { + AlexaInterface.logLevel = Alexa.Debug9 + } } Header { @@ -91,7 +93,6 @@ ApplicationCCWindow { anchors.topMargin: Sizes.dp(80) anchors.horizontalCenter: parent.horizontalCenter unfoldHeader: alexaView.visible || authView.visible - alexaInterface: alexa visible: root.neptuneState === "Maximized" } @@ -105,20 +106,16 @@ ApplicationCCWindow { AlexaView { id: alexaView anchors.fill: parent - alexaInterface: alexa - visible: alexaInterface.authState === AlexaInterface.Refreshed + visible: AlexaInterface.authState === Alexa.Refreshed neptuneState: root.neptuneState } AuthView { id: authView anchors.fill: parent - alexaInterface: alexa alexaAuth: alexaAuth - visible: alexaInterface.authState !== AlexaInterface.Refreshed + visible: AlexaInterface.authState !== Alexa.Refreshed } } - - Component.onCompleted: alexa.initAlexaQMLClient() } } diff --git a/plugins/alexainterface/AlexaInterface.h b/plugins/alexainterface/AlexaInterface.h index 46cb138..bcfa56e 100644 --- a/plugins/alexainterface/AlexaInterface.h +++ b/plugins/alexainterface/AlexaInterface.h @@ -61,6 +61,7 @@ #include #include +#include #include "WeatherCard.h" #include "InfoCard.h" @@ -333,5 +334,14 @@ private: #endif }; +static QObject *alexaInterfaceSingletonProvider(QQmlEngine *engine, QJSEngine *scriptEngine) +{ + Q_UNUSED(engine) + Q_UNUSED(scriptEngine) + + AlexaInterface *singletonObject = new AlexaInterface(); + singletonObject->initAlexaQMLClient(); + return singletonObject; +} #endif // ALEXA_INTERFACE_H_ diff --git a/plugins/alexainterface/alexainterface_plugin.cpp b/plugins/alexainterface/alexainterface_plugin.cpp index cf81ad8..8f67078 100644 --- a/plugins/alexainterface/alexainterface_plugin.cpp +++ b/plugins/alexainterface/alexainterface_plugin.cpp @@ -41,7 +41,8 @@ void AlexaPlugin::registerTypes(const char *uri) { // @uri alexa - qmlRegisterType(uri, 1, 0, "AlexaInterface"); + qmlRegisterSingletonType(uri, 1, 0, "AlexaInterface", alexaInterfaceSingletonProvider); + qmlRegisterUncreatableType(uri, 1, 0, "Alexa", "Non instantiable type"); qmlRegisterUncreatableType(uri, 1, 0, "InfoCard", "Non instantiable type"); qmlRegisterUncreatableType(uri, 1, 0, "WeatherCard", "Non instantiable type"); qmlRegisterUncreatableType(uri, 1, 0, "BaseCard", "Non instantiable type"); -- cgit v1.2.3