Worked on sorting. For some reason it works, but I don't know why.

This commit is contained in:
Scharel Clemens 2018-11-25 20:39:03 +01:00
parent e600a72036
commit 6275eaa869
3 changed files with 68 additions and 14 deletions

View file

@ -14,16 +14,27 @@ Item {
property bool unsecureConnection property bool unsecureConnection
property bool unencryptedConnection property bool unencryptedConnection
property var modelData: [ ]
property var model: ListModel { } property var model: ListModel { }
//property string file: StandardPaths.data + "/" + uuid + ".json" //property string file: StandardPaths.data + "/" + uuid + ".json"
//property bool saveFile: false //property bool saveFile: false
property bool busy: false property bool busy: false
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 {
target: appSettings
onSortByChanged: sortModelData()
}
ConfigurationGroup { ConfigurationGroup {
id: account id: account
path: "/apps/harbour-nextcloudnotes/accounts/" + uuid path: "/apps/harbour-nextcloudnotes/accounts/" + uuid
@ -81,9 +92,12 @@ Item {
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() //model.clear()
modelData = json
sortModelData()
for (var element in json) { for (var element in json) {
model.set(element, json[element]) model.set(element, json[element])
model.setProperty(element, "date", getDisplayDate(json[element].modified)) model.setProperty(element, "date", getPrettyDate(json[element].modified))
} }
element++ element++
while (model.count > element) { while (model.count > element) {
@ -93,36 +107,42 @@ Item {
} }
else { else {
console.log("Received a single note via API: " + endpoint) console.log("Received a single note via API: " + endpoint)
addToModelData(json)
var noteModified = false var noteModified = false
//json.date = getDisplayDate(json.modified) //json.date = getPrettyDate(json.modified)
for (var i = 0; i < model.count; i++) { for (var i = 0; i < model.count; i++) {
var listItem = model.get(i) var listItem = model.get(i)
if (listItem.id === json.id){ if (listItem.id === json.id){
model.set(i, json) model.set(i, json)
model.setProperty(i, "date", getDisplayDate(json.modified)) model.setProperty(i, "date", getPrettyDate(json.modified))
noteModified = true noteModified = true
} }
} }
if (!noteModified) { if (!noteModified) {
//json.date = getDisplayDate(json.modified) //json.date = getPrettyDate(json.modified)
model.set(i, json) model.set(i, json)
model.setProperty(i, "date", getDisplayDate(json.modified)) 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)
model.set(model.count, json) model.set(model.count, json)
model.setProperty(model.count-1, "date", getDisplayDate(json.modified)) model.setProperty(model.count-1, "date", getPrettyDate(json.modified))
model.move(model.count-1, 0, 1) 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)
for (i = 0; i < model.count; i++) { for (i = 0; i < model.count; i++) {
listItem = model.get(i) listItem = model.get(i)
if (listItem.id === json.id){ if (listItem.id === json.id){
model.set(i, json) model.set(i, json)
model.setProperty(i, "date", getDisplayDate(json.modified)) model.setProperty(i, "date", getPrettyDate(json.modified))
} }
} }
break; break;
@ -195,6 +215,35 @@ Item {
callApi("DELETE", { 'id': id } ) callApi("DELETE", { 'id': id } )
} }
function addToModelData(data) {
data.date = getPrettyDate(data.modified)
for (var i = 0; i < modelData.length; i++) {
if (modelData[i].id === data.id) {
modelData[i] = data
break
}
}
if (i === modelData.length) {
modelData.push(data)
sortModelData()
}
}
function sortModelData() {
switch(appSettings.sortBy) {
case "date":
modelData.sort(function(a, b) { return b.modified-a.modified } )
break
case "category":
modelData.sort(function(a, b) { return ((a.category > b.category) ? 1 : ((b.category > a.category) ? -1 : 0)) } )
break
case "title":
modelData.sort(function(a, b) { return ((a.title > b.title) ? 1 : ((b.title > a.title) ? -1 : 0)) } )
break
}
//console.log(JSON.stringify(modelData, null, 4))
}
function refresh() { function refresh() {
search("") search("")
} }
@ -216,7 +265,7 @@ Item {
} }
// source: https://stackoverflow.com/a/14339782 // source: https://stackoverflow.com/a/14339782
function getDisplayDate(date) { function getPrettyDate(date) {
var today = new Date() var today = new Date()
today.setHours(0) today.setHours(0)
today.setMinutes(0) today.setMinutes(0)

View file

@ -100,8 +100,9 @@ Page {
} }
ComboBox { ComboBox {
id: sortByComboBox id: sortByComboBox
property var names: [qsTr("by Date"), qsTr("by Category"), qsTr("Alphabetically")] property var names: [qsTr("Date"), qsTr("Category"), qsTr("Title alphabetically")]
label: qsTr("Sort notes") label: qsTr("Sort notes by")
description: qsTr("This will also change how the notes are segmented")
menu: ContextMenu { menu: ContextMenu {
Repeater { Repeater {
id: sortByRepeater id: sortByRepeater

View file

@ -276,19 +276,23 @@
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<source>by Date</source> <source>Date</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<source>by Category</source> <source>Category</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<source>Sort notes</source> <source>Title alphabetically</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<source>Alphabetically</source> <source>Sort notes by</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>This will also change how the notes are segmented</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
</context> </context>