native: simple data loading

This commit is contained in:
Johannes Jöns 2024-07-11 16:01:37 +02:00
parent 58c9a9f530
commit 8d90d84733

View file

@ -62,36 +62,84 @@ Rectangle {
} }
color: getColors().window color: getColors().window
TableView { ListModel {
id: apiDataModel
}
ListView {
anchors { anchors {
topMargin: 5 topMargin: 5
fill: s2 fill: s2
} }
columnSpacing: 0
rowSpacing: 1
clip: true clip: true
boundsBehavior: Flickable.StopAtBounds boundsBehavior: Flickable.StopAtBounds
model: Data {} model: apiDataModel
delegate: Rectangle {
id: r delegate: Item {
anchors{ width: parent.width
} height: 50
color: getColors().base
implicitWidth: 100 Row {
implicitHeight: 50 spacing: 10
border.width: 0
Text { Text {
anchors { text: model.name
horizontalCenter: r.horizontalCenter color: getColors().text
verticalCenter: r.verticalCenter }
}
text: display Text {
text: model.key
color: getColors().text
}
Text {
text: model.amount
color: getColors().text
}
Text {
text: model.start
color: getColors().text
}
Text {
text: model.end
color: getColors().text
}
Text {
text: model.group
color: getColors().text color: getColors().text
anchors.centerIn: parent
} }
} }
} }
} }
} }
Component.onCompleted: {
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://api.clan-war.net/api/v1/licenses");
xhr.setRequestHeader("Authorization", "Bearer " + authToken);
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
var jsonResponse = JSON.parse(xhr.responseText);
apiDataModel.clear();
for (var i = 0; i < jsonResponse.length; i++) {
for (var ii = 0; ii < jsonResponse[i].licenses.length; ii++) {
apiDataModel.append({
group: jsonResponse[i].name,
name: jsonResponse[i].licenses[ii].name,
start: jsonResponse[i].licenses[ii].start ?? "Indefinitely",
end: jsonResponse[i].licenses[ii].end ?? "Indefinitely",
amount: jsonResponse[i].licenses[ii].amount ?? "∞",
key: jsonResponse[i].licenses[ii].key
});
}
}
} else {
console.log("Error: " + xhr.status);
}
}
}
xhr.send();
}
}