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 { Item {
property string uuid property string uuid
property string response
property var model: ListModel { } property var model: ListModel { }
property var categories: [ ] property var categories: [ ]
property string file: StandardPaths.data + "/" + uuid + ".json" property string file: StandardPaths.data + "/" + uuid + ".json"
@ -67,7 +68,8 @@ Item {
status = apiReq.status status = apiReq.status
if (apiReq.status === 200) { if (apiReq.status === 200) {
//console.log(apiReq.responseText) //console.log(apiReq.responseText)
var json = JSON.parse(apiReq.responseText) response = apiReq.responseText
var json = JSON.parse(response)
switch(method) { switch(method) {
case "GET": case "GET":
if (Array.isArray(json)) { if (Array.isArray(json)) {

View file

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

View file

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

View file

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

View file

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