Began testing the new C++ Model.

This commit is contained in:
Scharel Clemens 2019-01-26 18:09:28 +01:00
parent 6419b45f52
commit ddb778795b
5 changed files with 32 additions and 8 deletions

View file

@ -5,6 +5,7 @@ import Nemo.Configuration 1.0
Item {
property string uuid
property string response
property var model: ListModel { }
property var categories: [ ]
property string file: StandardPaths.data + "/" + uuid + ".json"
@ -67,7 +68,8 @@ Item {
status = apiReq.status
if (apiReq.status === 200) {
//console.log(apiReq.responseText)
var json = JSON.parse(apiReq.responseText)
response = apiReq.responseText
var json = JSON.parse(response)
switch(method) {
case "GET":
if (Array.isArray(json)) {

View file

@ -39,7 +39,7 @@ ApplicationWindow
property int autoSyncInterval: value("autoSyncInterval", 0)
property int previewLineCount: value("previewLineCount", 4)
property bool favoritesOnTop: value("favoritesOnTop", true, Boolean)
property string sortBy: value("sortBy", "date", Date)
property string sortBy: value("sortBy", "date", String)
property bool showSeparator: value("showSeparator", false, Boolean)
property bool useMonoFont: value("useMonoFont", false, Boolean)
property bool useCapitalX: value("useCapitalX", false, Boolean)
@ -109,8 +109,15 @@ ApplicationWindow
NotesApi {
id: api
uuid: appSettings.currentAccount
//onResponseChanged: noteListModel.applyJSON(response)
}
/*NotesModel {
id: noteListModel
sortBy: 0
favoritesOnTop: appSettings.favoritesOnTop
}*/
NoteDelegateModel {
id: noteListModel
model: api.model

View file

@ -63,7 +63,7 @@ Page {
checked: modelData === api.uuid
onClicked: {
api.uuid = modelData
api.getNotes()
api.getNotesFromApi()
}
onPressAndHold: openMenu()
}

View file

@ -2,6 +2,7 @@
#include <QJsonDocument>
#include <QJsonArray>
#include <QtMath>
#include <QDebug>
const QHash<int, QByteArray> noteRoles = QHash<int, QByteArray>{
{NotesModel::visible, "visible"},
@ -104,28 +105,35 @@ void NotesModel::setFavoritesOnTop(bool favoritesOnTop) {
}
bool NotesModel::applyJSON(QString json, bool replaceIfArray) {
qDebug() << "Applying JSON...";// << json;
QJsonDocument jdoc = QJsonDocument::fromJson(json.toUtf8());
int notesModified = 0;
if (!jdoc.isNull()) {
if (jdoc.isArray()) {
qDebug() << "It's an array...";
QJsonArray jarr = jdoc.array();
QList<int> notesToRemove;
QList<ModelNote<Note, int> > notesToAdd;
for (int i = 0; i < m_notes.size(); i++)
notesToRemove << i;
while (!jarr.empty()) {
qDebug() << jarr.count() << "JSON Objects to handle...";
QJsonValue jval = jarr.first();
if (jval.isObject()) {
qDebug() << "It's an object, all fine...";
QJsonObject jobj = jval.toObject();
if (!jobj.isEmpty() && !jobj.value(noteRoles[errorRole]).toBool(true)) {
qDebug() << "Adding it to the model...";
Note note = Note::fromjson(jobj);
int position = indexOf(note.id);
if (position >= 0 && replaceIfArray) {
qDebug() << "Replacing note" << note.title << "on position" << position;
m_notes[position].note = note;
emit dataChanged(index(position), index(position));
notesToRemove.removeAt(position);
}
else {
qDebug() << "New note" << note.title << "adding it to the notes to add...";
position = insertPosition(note);
//beginInsertRows(QModelIndex(), position, position);
ModelNote<Note, int> noteToAdd;
@ -136,23 +144,30 @@ bool NotesModel::applyJSON(QString json, bool replaceIfArray) {
}
notesModified++;
}
else {
qDebug() << "Something is wrong, skipping it...";
}
}
jarr.pop_front();
}
for (int i = 0; i < notesToRemove.size(); i++) {
// TODO the current implementation does not respect the changement of the index
/*for (int i = 0; i < notesToRemove.size(); i++) {
qDebug() << "Removing note" << m_notes[notesToRemove[i]].note.title;
beginRemoveRows(QModelIndex(), notesToRemove[i], notesToRemove[i]);
m_notes.removeAt(notesToRemove[i]);
endRemoveRows();
}
}*/
for (int i = 0; i < notesToAdd.size(); i++) {
beginInsertRows(QModelIndex(), notesToAdd[i].param, notesToAdd[i].param);
ModelNote<Note, bool> note;
note.note = notesToAdd[i].note;
qDebug() << "Adding note"<< note.note.title;
m_notes.insert(notesToAdd[i].param, note);
endInsertRows();
}
}
else if (jdoc.isObject()) {
qDebug() << "It's a single object...";
QJsonObject jobj = jdoc.object();
if (!jobj.isEmpty() && !jobj.value(noteRoles[errorRole]).toBool(true)) {
Note note;

View file

@ -272,17 +272,17 @@
<context>
<name>NotesApi</name>
<message>
<location filename="../qml/components/NotesApi.qml" line="107"/>
<location filename="../qml/components/NotesApi.qml" line="109"/>
<source>Unable to connect</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/components/NotesApi.qml" line="245"/>
<location filename="../qml/components/NotesApi.qml" line="247"/>
<source>Today</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/components/NotesApi.qml" line="247"/>
<location filename="../qml/components/NotesApi.qml" line="249"/>
<source>Yesterday</source>
<translation type="unfinished"></translation>
</message>