Remove Notes array in NotesApp class as the NotesModel hadles it.
This commit is contained in:
parent
5cb5ec8637
commit
829660271a
3 changed files with 10 additions and 44 deletions
|
@ -80,8 +80,8 @@ ApplicationWindow
|
||||||
Connections {
|
Connections {
|
||||||
target: appSettings
|
target: appSettings
|
||||||
onCurrentAccountChanged: {
|
onCurrentAccountChanged: {
|
||||||
console.log("Current account: " + account.username + "@" + account.url)
|
|
||||||
account.path = appSettings.path + "/accounts/" + appSettings.currentAccount
|
account.path = appSettings.path + "/accounts/" + appSettings.currentAccount
|
||||||
|
console.log("Current account: " + account.username + "@" + account.url)
|
||||||
notesApp.model().account = appSettings.currentAccount
|
notesApp.model().account = appSettings.currentAccount
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,12 +46,12 @@ bool NotesApp::getAllNotes(const QStringList& exclude) {
|
||||||
QNetworkReply* reply = m_api->get(QString(NOTES_APP_ENDPOINT).append("/notes"), query);
|
QNetworkReply* reply = m_api->get(QString(NOTES_APP_ENDPOINT).append("/notes"), query);
|
||||||
if (reply->error() == QNetworkReply::NoError) {
|
if (reply->error() == QNetworkReply::NoError) {
|
||||||
m_replies << reply;
|
m_replies << reply;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NotesApp::getNote(const int id) {
|
bool NotesApp::getNote(const int id) {
|
||||||
|
@ -59,94 +59,63 @@ bool NotesApp::getNote(const int id) {
|
||||||
QNetworkReply* reply = m_api->get(QString(NOTES_APP_ENDPOINT).append("/notes/%1").arg(id));
|
QNetworkReply* reply = m_api->get(QString(NOTES_APP_ENDPOINT).append("/notes/%1").arg(id));
|
||||||
if (reply->error() == QNetworkReply::NoError) {
|
if (reply->error() == QNetworkReply::NoError) {
|
||||||
m_replies << reply;
|
m_replies << reply;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NotesApp::createNote(const QJsonObject& note, bool local) {
|
bool NotesApp::createNote(const QJsonObject& note, bool local) {
|
||||||
qDebug() << "Creating note";
|
qDebug() << "Creating note";
|
||||||
QJsonValue value = QJsonValue(note);
|
QJsonValue value = QJsonValue(note);
|
||||||
if (!m_notes.contains(value)) {
|
|
||||||
m_notes.append(value);
|
|
||||||
}
|
|
||||||
if (!local) {
|
if (!local) {
|
||||||
QNetworkReply* reply = m_api->post(QString(NOTES_APP_ENDPOINT).append("/notes"), QJsonDocument(note).toJson());
|
QNetworkReply* reply = m_api->post(QString(NOTES_APP_ENDPOINT).append("/notes"), QJsonDocument(note).toJson());
|
||||||
if (reply->error() == QNetworkReply::NoError) {
|
if (reply->error() == QNetworkReply::NoError) {
|
||||||
m_replies << reply;
|
m_replies << reply;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// TODO update model
|
||||||
|
m_notesModel.insert(-1, note);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NotesApp::updateNote(const int id, const QJsonObject& note, bool local) {
|
bool NotesApp::updateNote(const int id, const QJsonObject& note, bool local) {
|
||||||
qDebug() << "Updating note:" << id;
|
qDebug() << "Updating note:" << id;
|
||||||
bool done = true;
|
|
||||||
if (!m_notes.contains(QJsonValue(note))) {
|
|
||||||
done = false;
|
|
||||||
QJsonArray::iterator i;
|
|
||||||
for (i = m_notes.begin(); i != m_notes.end() && !done; ++i) {
|
|
||||||
QJsonObject localNote = i->toObject();
|
|
||||||
int localId = localNote.value("id").toInt(-1);
|
|
||||||
if (localId > 0) {
|
|
||||||
if (localId == id) {
|
|
||||||
*i = QJsonValue(note);
|
|
||||||
done = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (localNote.value("content") == note.value("content")) {
|
|
||||||
*i = QJsonValue(note);
|
|
||||||
done = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!local) {
|
if (!local) {
|
||||||
QNetworkReply* reply = m_api->put(QString(NOTES_APP_ENDPOINT).append("/notes/%1").arg(id), QJsonDocument(note).toJson());
|
QNetworkReply* reply = m_api->put(QString(NOTES_APP_ENDPOINT).append("/notes/%1").arg(id), QJsonDocument(note).toJson());
|
||||||
if (reply->error() == QNetworkReply::NoError) {
|
if (reply->error() == QNetworkReply::NoError) {
|
||||||
m_replies << reply;
|
m_replies << reply;
|
||||||
return true;
|
// TODO update model
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return done;
|
m_notesModel.update(-1, note);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NotesApp::deleteNote(const int id, bool local) {
|
bool NotesApp::deleteNote(const int id, bool local) {
|
||||||
qDebug() << "Deleting note: " << id;
|
qDebug() << "Deleting note: " << id;
|
||||||
bool done = false;
|
|
||||||
QJsonArray::iterator i;
|
|
||||||
for (i = m_notes.begin(); i != m_notes.end() && !done; ++i) {
|
|
||||||
QJsonObject localNote = i->toObject();
|
|
||||||
if (localNote.value("id").toInt() == id) {
|
|
||||||
m_notes.erase(i);
|
|
||||||
done = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!local) {
|
if (!local) {
|
||||||
QNetworkReply* reply = m_api->del(QString(NOTES_APP_ENDPOINT).append("/notes/%1").arg(id));
|
QNetworkReply* reply = m_api->del(QString(NOTES_APP_ENDPOINT).append("/notes/%1").arg(id));
|
||||||
if (reply->error() == QNetworkReply::NoError) {
|
if (reply->error() == QNetworkReply::NoError) {
|
||||||
m_replies << reply;
|
m_replies << reply;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return done;
|
m_notesModel.remove(id);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NotesApp::updateReply(QNetworkReply* reply) {
|
void NotesApp::updateReply(QNetworkReply* reply) {
|
||||||
|
|
|
@ -59,9 +59,6 @@ private:
|
||||||
|
|
||||||
NotesModel m_notesModel;
|
NotesModel m_notesModel;
|
||||||
NotesProxyModel m_notesProxy;
|
NotesProxyModel m_notesProxy;
|
||||||
|
|
||||||
QJsonArray m_notes;
|
|
||||||
QJsonObject m_settings;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // NOTESAPP_H
|
#endif // NOTESAPP_H
|
||||||
|
|
Loading…
Reference in a new issue