1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-08-13 22:12:50 +02:00

Refactoring to make cache read-only

This commit is contained in:
Laurent Cozic
2017-01-21 11:36:50 +00:00
parent 25b769bbf1
commit b3e5b4f091
9 changed files with 40 additions and 11 deletions

View File

@@ -40,6 +40,7 @@ Item {
target: model
onDataChanged: {
if (currentItemId !== "") {
print("Connection.onDataChanged");
selectItemById(currentItemId);
}
}

View File

@@ -19,6 +19,7 @@ Item {
if (list.model.virtualItemShown()) {
list.model.hideVirtualItem();
list.model.addData(text)
print("handleItemListEditingAccepted");
list.selectItemById(list.model.lastInsertId());
} else {
list.model.setData(index, text, "title")

View File

@@ -45,10 +45,15 @@ bool AbstractListModel::setData(const QModelIndex &index, const QVariant &value,
if (!model) return false;
if (role == TitleRole) {
model->setValue("title", value.toString());
if (!model->save()) return false;
cacheClear();
BaseModel temp;
temp.clone(*model);
qDebug() << model->value("title").toString();
return true;
// model->setValue("title", value.toString());
// if (!model->save()) return false;
// cacheClear();
// return true;
}
qWarning() << "Unsupported role" << role;

View File

@@ -393,6 +393,10 @@ void BaseModel::setValue(const QString &name, const QJsonValue &value, QMetaType
}
}
//void BaseModel::setValues(const QHash<QString, BaseModel::Value> values) {
// values_ = values;
//}
BaseModel::Value BaseModel::id() const {
if (!valueIsSet(primaryKey())) return QVariant();
return value(primaryKey());
@@ -411,6 +415,12 @@ QString BaseModel::valuesToString() const {
return s;
}
void BaseModel::clone(const BaseModel &baseModel) {
values_ = baseModel.values_;
changedFields_.clear();
isNew_ = false;
}
QString BaseModel::tableName(Table t) {
if (t == jop::FoldersTable) return "folders";
if (t == jop::NotesTable) return "notes";

View File

@@ -71,27 +71,26 @@ public:
void setValue(const QString& name, const QString& value);
void setValue(const QString& name, int value);
void setValue(const QString& name, const QJsonValue& value, QMetaType::Type type);
//void setValues(const QHash<QString, Value> values);
Value id() const;
QString idString() const;
QString valuesToString() const;
void clone(const BaseModel& baseModel);
static QString tableName(Table t);
protected:
QHash<QString, bool> changedFields_;
Table table_;
QHash<QString, Value> values_;
int isNew_;
static QVariant cacheGet(const QString& key);
static void cacheSet(const QString& key, const QVariant& value);
static void cacheDelete(const QString& key);
static QMap<int, QVector<BaseModel::Field>> tableFields_;
static QHash<QString, QVariant> cache_;
int isNew_;
};
}

View File

@@ -46,6 +46,7 @@ int FolderModel::idToIndex(const QString &id) const {
int count = this->rowCount();
for (int i = 0; i < count; i++) {
Folder* folder = (Folder*)atIndex(i);
if (!folder) return -1;
if (folder->idString() == id) return i;
}
return -1;

View File

@@ -38,8 +38,6 @@ public slots:
void addData(const QString& title);
void deleteData(const int index);
//bool setTitle(int index, const QVariant &value, int role = Qt::EditRole);
//bool setData(int index, const QVariant &value, int role = Qt::EditRole);
int idToIndex(const QString& id) const;
void dispatcher_folderCreated(const QString& folderId);

View File

@@ -54,6 +54,12 @@ Folder NoteModel::folder() const {
}
int NoteModel::idToIndex(const QString &id) const {
std::vector<int> indexes = cache_.indexes();
for (int i = 0; i < indexes.size(); i++) {
Note* note = cache_.get(indexes[i]);
if (note->idString() == id) return indexes[i];
}
Folder f = this->folder();
return f.noteIndexById(orderBy_, id);
}
@@ -67,6 +73,12 @@ void NoteModel::addData(const QString &title) {
lastInsertId_ = note.idString();
}
void NoteModel::deleteData(int index) {
Note* note = (Note*)atIndex(index);
if (!note) return;
note->dispose();
}
int NoteModel::baseModelCount() const {
return folder().noteCount();
}
@@ -107,7 +119,7 @@ void NoteModel::dispatcher_noteCreated(const QString &noteId) {
}
void NoteModel::dispatcher_noteUpdated(const QString &noteId) {
qDebug() << "NoteModel Folder udpated" << noteId;
qDebug() << "NoteModel note udpated" << noteId;
cacheClear();
@@ -117,9 +129,10 @@ void NoteModel::dispatcher_noteUpdated(const QString &noteId) {
}
void NoteModel::dispatcher_noteDeleted(const QString &noteId) {
qDebug() << "NoteModel Folder deleted" << noteId;
qDebug() << "NoteModel note deleted" << noteId;
int index = idToIndex(noteId);
qDebug() << "index" << index;
if (index < 0) return;
cacheClear();

View File

@@ -24,6 +24,7 @@ public slots:
int idToIndex(const QString& id) const;
void addData(const QString& title);
void deleteData(int index);
void dispatcher_noteCreated(const QString& noteId);
void dispatcher_noteUpdated(const QString& noteId);
void dispatcher_noteDeleted(const QString& noteId);