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 unencryptedConnection
property var modelData: [ ]
property var model: ListModel { }
//property string file: StandardPaths.data + "/" + uuid + ".json"
//property bool saveFile: false
property bool busy: false
property int status: 204
property string statusText: "No Content"
onModelDataChanged: {
console.log("modelData changed")
// TODO
}
onStatusChanged: {
console.log("Network status: " + statusText + " (" + status + ")")
}
Connections {
target: appSettings
onSortByChanged: sortModelData()
}
ConfigurationGroup {
id: account
path: "/apps/harbour-nextcloudnotes/accounts/" + uuid
@ -81,9 +92,12 @@ Item {
if (Array.isArray(json)) {
console.log("Received all notes via API: " + endpoint)
//model.clear()
modelData = json
sortModelData()
for (var element in json) {
model.set(element, json[element])
model.setProperty(element, "date", getDisplayDate(json[element].modified))
model.setProperty(element, "date", getPrettyDate(json[element].modified))
}
element++
while (model.count > element) {
@ -93,36 +107,42 @@ Item {
}
else {
console.log("Received a single note via API: " + endpoint)
addToModelData(json)
var noteModified = false
//json.date = getDisplayDate(json.modified)
//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", getDisplayDate(json.modified))
model.setProperty(i, "date", getPrettyDate(json.modified))
noteModified = true
}
}
if (!noteModified) {
//json.date = getDisplayDate(json.modified)
//json.date = getPrettyDate(json.modified)
model.set(i, json)
model.setProperty(i, "date", getDisplayDate(json.modified))
model.setProperty(i, "date", getPrettyDate(json.modified))
}
}
break;
case "POST":
console.log("Created a note via API: " + endpoint)
addToModelData(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)
break;
case "PUT":
console.log("Updated a note via API: " + endpoint)
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", getDisplayDate(json.modified))
model.setProperty(i, "date", getPrettyDate(json.modified))
}
}
break;
@ -195,6 +215,35 @@ Item {
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() {
search("")
}
@ -216,7 +265,7 @@ Item {
}
// source: https://stackoverflow.com/a/14339782
function getDisplayDate(date) {
function getPrettyDate(date) {
var today = new Date()
today.setHours(0)
today.setMinutes(0)

View file

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

View file

@ -276,19 +276,23 @@
<translation type="unfinished"></translation>
</message>
<message>
<source>by Date</source>
<source>Date</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>by Category</source>
<source>Category</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Sort notes</source>
<source>Title alphabetically</source>
<translation type="unfinished"></translation>
</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>
</message>
</context>