Improved API handling and sorting.

This commit is contained in:
Scharel Clemens 2018-11-26 17:21:02 +01:00
parent 6275eaa869
commit 29335f9c51
2 changed files with 30 additions and 62 deletions

View file

@ -1,4 +1,4 @@
import QtQuick 2.0 import QtQuick 2.5
import Sailfish.Silica 1.0 import Sailfish.Silica 1.0
import Nemo.Configuration 1.0 import Nemo.Configuration 1.0
@ -22,17 +22,13 @@ Item {
property int status: 204 property int status: 204
property string statusText: "No Content" property string statusText: "No Content"
onModelDataChanged: {
console.log("modelData changed")
// TODO
}
onStatusChanged: { onStatusChanged: {
console.log("Network status: " + statusText + " (" + status + ")") console.log("Network status: " + statusText + " (" + status + ")")
} }
Connections { Connections {
target: appSettings target: appSettings
onSortByChanged: sortModelData() onSortByChanged: mapDataToModel()
} }
ConfigurationGroup { ConfigurationGroup {
@ -84,76 +80,32 @@ Item {
apiReq.onreadystatechange = function() { apiReq.onreadystatechange = function() {
if (apiReq.readyState === XMLHttpRequest.DONE) { if (apiReq.readyState === XMLHttpRequest.DONE) {
if (apiReq.status === 200) { if (apiReq.status === 200) {
//console.log("Network status: " + apiReq.statusText + " (" + apiReq.status + ")")
var json = JSON.parse(apiReq.responseText) var json = JSON.parse(apiReq.responseText)
switch(method) { switch(method) {
case "GET": case "GET":
if (Array.isArray(json)) { if (Array.isArray(json)) {
console.log("Received all notes via API: " + endpoint) console.log("Received all notes via API: " + endpoint)
//model.clear()
modelData = json modelData = json
sortModelData() mapDataToModel()
for (var element in json) {
model.set(element, json[element])
model.setProperty(element, "date", getPrettyDate(json[element].modified))
}
element++
while (model.count > element) {
model.remove(element)
}
update = new Date() update = new Date()
} }
else { else {
console.log("Received a single note via API: " + endpoint) console.log("Received a single note via API: " + endpoint)
addToModelData(json) addToModelData(json)
var noteModified = false
//json.date = getPrettyDate(json.modified)
for (var i = 0; i < model.count; i++) {
var listItem = model.get(i)
if (listItem.id === json.id){
model.set(i, json)
model.setProperty(i, "date", getPrettyDate(json.modified))
noteModified = true
}
}
if (!noteModified) {
//json.date = getPrettyDate(json.modified)
model.set(i, json)
model.setProperty(i, "date", getPrettyDate(json.modified))
}
} }
break; break;
case "POST": case "POST":
console.log("Created a note via API: " + endpoint) console.log("Created a note via API: " + endpoint)
addToModelData(json) addToModelData(json)
model.set(model.count, json)
model.setProperty(model.count-1, "date", getPrettyDate(json.modified))
model.move(model.count-1, 0, 1)
break; break;
case "PUT": case "PUT":
console.log("Updated a note via API: " + endpoint) console.log("Updated a note via API: " + endpoint)
addToModelData(json) addToModelData(json)
for (i = 0; i < model.count; i++) {
listItem = model.get(i)
if (listItem.id === json.id){
model.set(i, json)
model.setProperty(i, "date", getPrettyDate(json.modified))
}
}
break; break;
case "DELETE": case "DELETE":
console.log("Deleted a note via API: " + endpoint) console.log("Deleted a note via API: " + endpoint)
for (i = 0; i < model.count; i++) { removeFromModelData(data.id)
listItem = model.get(i)
if (listItem.id === data.id){
model.remove(i)
}
}
break; break;
default: default:
console.log("Unsupported method: " + method) console.log("Unsupported method: " + method)
@ -174,7 +126,6 @@ Item {
} }
statusText = apiReq.statusText statusText = apiReq.statusText
status = apiReq.status status = apiReq.status
//model.sync()
busy = false busy = false
} }
} }
@ -216,7 +167,6 @@ Item {
} }
function addToModelData(data) { function addToModelData(data) {
data.date = getPrettyDate(data.modified)
for (var i = 0; i < modelData.length; i++) { for (var i = 0; i < modelData.length; i++) {
if (modelData[i].id === data.id) { if (modelData[i].id === data.id) {
modelData[i] = data modelData[i] = data
@ -225,11 +175,22 @@ Item {
} }
if (i === modelData.length) { if (i === modelData.length) {
modelData.push(data) modelData.push(data)
sortModelData()
} }
mapDataToModel()
} }
function sortModelData() { function removeFromModelData(id) {
for (var i = 0; i < modelData.length; i++) {
if (modelData[i].id === id) {
modelData.splice(i, 1)
break
}
}
mapDataToModel()
}
function mapDataToModel() {
modelData.forEach(function(value) { value.date = getPrettyDate(value.modified) } )
switch(appSettings.sortBy) { switch(appSettings.sortBy) {
case "date": case "date":
modelData.sort(function(a, b) { return b.modified-a.modified } ) modelData.sort(function(a, b) { return b.modified-a.modified } )
@ -241,7 +202,14 @@ Item {
modelData.sort(function(a, b) { return ((a.title > b.title) ? 1 : ((b.title > a.title) ? -1 : 0)) } ) modelData.sort(function(a, b) { return ((a.title > b.title) ? 1 : ((b.title > a.title) ? -1 : 0)) } )
break break
} }
//console.log(JSON.stringify(modelData, null, 4)) for (var element in modelData) {
model.set(element, modelData[element])
//model.setProperty(element, "date", getPrettyDate(modelData[element].modified))
}
element++
while (model.count > element) {
model.remove(element)
}
} }
function refresh() { function refresh() {

View file

@ -159,7 +159,7 @@ Page {
anchors.right: parent.right anchors.right: parent.right
anchors.rightMargin: Theme.horizontalPageMargin anchors.rightMargin: Theme.horizontalPageMargin
anchors.top: titleLabel.bottom anchors.top: titleLabel.bottom
text: content text: parseText(content)
font.pixelSize: Theme.fontSizeExtraSmall font.pixelSize: Theme.fontSizeExtraSmall
textFormat: Text.PlainText textFormat: Text.PlainText
wrapMode: Text.Wrap wrapMode: Text.Wrap
@ -167,11 +167,11 @@ Page {
maximumLineCount: appSettings.previewLineCount > 0 ? appSettings.previewLineCount : 1 maximumLineCount: appSettings.previewLineCount > 0 ? appSettings.previewLineCount : 1
visible: appSettings.previewLineCount > 0 visible: appSettings.previewLineCount > 0
color: note.highlighted ? Theme.secondaryHighlightColor : Theme.secondaryColor color: note.highlighted ? Theme.secondaryHighlightColor : Theme.secondaryColor
Component.onCompleted: { function parseText (preText) {
var lines = text.split('\n') var lines = preText.split('\n')
lines.splice(0,1); lines.splice(0,1);
text = lines.join('\n'); var newText = lines.join('\n');
text = text.replace(/^\s*$(?:\r\n?|\n)/gm, "") return newText.replace(/^\s*$(?:\r\n?|\n)/gm, "")
} }
} }