Skip to content

Instantly share code, notes, and snippets.

@stephenquan
Last active March 23, 2018 05:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephenquan/27e0638d5f291dce987bb99eb8d339c8 to your computer and use it in GitHub Desktop.
Save stephenquan/27e0638d5f291dce987bb99eb8d339c8 to your computer and use it in GitHub Desktop.
appstudio-networkrequest-sample.qml
import QtQuick 2.7
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.1
import ArcGIS.AppFramework 1.0
import ArcGIS.AppFramework.Networking 1.0
App {
id: app
width: 640
height: 480
property var config: toArray(Networking.configurations)
property bool isOnline: isLAN || isWIFI || isMobileData
property bool isLAN: config.some(isConfigActiveLAN)
property bool isWIFI: config.some(isConfigActiveWIFI)
property bool isMobileData: config.some(isConfigActiveMobileData)
property bool isMobileDataOnly: isMobileData && !isWIFI && !isLAN
ColumnLayout {
anchors.fill: parent
anchors.margins: 10
RowLayout {
Layout.fillWidth: true
TextField {
id: urlTextField
Layout.fillWidth: true
text: "https://appstudio.arcgis.com/api?f=pjson"
enabled: !networkRequest.busy
onAccepted: run()
}
Button {
Layout.preferredWidth: height / 2
text: qsTr(">")
enabled: !networkRequest.busy
onClicked: run()
}
}
TextArea {
id: textArea
selectByMouse: true
Layout.fillWidth: true
Layout.fillHeight: true
}
Flickable {
Layout.fillWidth: true
Layout.preferredHeight: row.height
contentWidth: row.width
contentHeight: row.height
Row {
id: row
spacing: 5
TextField {
text: " LAN "
color: "white"
background: Rectangle { color: isLAN ? "green" : "red" }
}
TextField {
text: " WIFI "
color: "white"
background: Rectangle { color: isWIFI ? "green" : "red" }
}
TextField {
text: " MobileData "
color: "white"
background: Rectangle { color: isMobileData ? "green" : "red" }
}
CheckBox {
id: checkBox
text: qsTr("Simulate Offline")
}
}
}
}
NetworkRequest {
id: networkRequest
property bool busy: readyState >= 2 && readyState <= 3
url: urlTextField.text
onReadyStateChanged: {
log("readyState: %1".arg(readyState));
if (readyState !== NetworkRequest.ReadyStateComplete) return;
log("status: %1".arg(status));
log("errorCode: %1".arg(errorCode));
log("errorText: %1".arg(errorText));
log(responseText);
}
}
Connections {
target: AppFramework.logging
onEnabledChanged: {
log("enabled: %1".arg(AppFramework.logging.enabled))
if (!enabled) return;
}
}
function log(str) {
console.log(str);
textArea.text = textArea.text + str + "\n";
}
function run() {
textArea.text = "";
Networking.networkAccessible = (checkBox.checked || !isOnline) ? Networking.NetworkAccessibilityNotAccessible : Networking.NetworkAccessibilityAccessible;
networkRequest.send()
}
function toArray(o) {
return Object.keys(o).map(function (e) { return o[e]; } );
}
function isConfigActive(c) {
return (c.state & NetworkConfiguration.StateActive) === NetworkConfiguration.StateActive;
}
function isConfigLAN(c) {
return c.bearerType === NetworkConfiguration.BearerEthernet;
}
function isConfigWIFI(c) {
if (c.bearerType === NetworkConfiguration.BearerWLAN) {
return true;
}
if (Qt.platform.os === "ios") {
return c.name === "en0";
}
return false;
}
function isConfigMobileData(c) {
switch (c.bearerType) {
case NetworkConfiguration.Bearer2G:
case NetworkConfiguration.BearerCDMA2000:
case NetworkConfiguration.BearerWCDMA:
case NetworkConfiguration.BearerHSPA:
case NetworkConfiguration.BearerWiMAX:
case NetworkConfiguration.BearerEVDO:
case NetworkConfiguration.BearerLTE:
case NetworkConfiguration.Bearer3G:
case NetworkConfiguration.Bearer4G:
return true;
}
if (Qt.platform.os === "ios") {
return c.name === "pdp_ip0";
}
return false;
}
function isConfigActiveLAN(c) {
return isConfigActive(c) && isConfigLAN(c);
}
function isConfigActiveWIFI(c) {
return isConfigActive(c) && isConfigWIFI(c);
}
function isConfigActiveMobileData(c) {
return isConfigActive(c) && isConfigMobileData(c);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment