refactor: Update Note and Category models to use 'id' instead of 'uuid', and adjust related database operations

- Changed 'uuid' to 'id' in Note and Category models for consistency.
- Updated database operations in NoteRepository to reflect the new 'id' field.
- Modified sync models to accommodate changes in Note and Category structures.
- Adjusted the handling of notes and categories during synchronization.
- Refactored the note editor and home screen to use the new 'id' field.
- Ensured that the 'isDirty' flag is properly set and utilized across models.
This commit is contained in:
2026-05-20 11:05:30 +02:00
parent 34f45a912f
commit def755e1c5
10 changed files with 520 additions and 323 deletions
+52 -26
View File
@@ -9,22 +9,23 @@ part 'app_database.g.dart';
@DataClassName('DbCategory')
class Categories extends Table {
TextColumn get uuid => text().unique()();
TextColumn get id => text()();
TextColumn get encryptedName => text().named('encrypted_name')();
IntColumn get serverVersion =>
integer().named('server_version').withDefault(const Constant(0))();
BoolColumn get isDeleted =>
boolean().named('is_deleted').withDefault(const Constant(false))();
BoolColumn get isDirty =>
boolean().named('is_dirty').withDefault(const Constant(true))();
DateTimeColumn get updatedAt => dateTime().named('updated_at')();
@override
Set<Column> get primaryKey => {uuid};
Set<Column> get primaryKey => {id};
}
@DataClassName('DbNote')
class Notes extends Table {
IntColumn get id => integer().autoIncrement()();
TextColumn get uuid => text().unique()();
TextColumn get id => text().named('id')();
TextColumn get title => text()();
TextColumn get body => text()();
DateTimeColumn get createdAt => dateTime().named('created_at')();
@@ -35,12 +36,34 @@ class Notes extends Table {
BoolColumn get isDeleted =>
boolean().named('is_deleted').withDefault(const Constant(false))();
TextColumn get categoryId => text().nullable().named('category_id')();
BoolColumn get isDirty =>
boolean().named('is_dirty').withDefault(const Constant(true))();
@override
Set<Column> get primaryKey => {id};
}
@DriftDatabase(tables: [Notes, Categories])
class AppDatabase extends _$AppDatabase {
@override
int get schemaVersion => 1;
int get schemaVersion => 2;
@override
MigrationStrategy get migration => MigrationStrategy(
onCreate: (Migrator migrator) async {
await migrator.createAll();
},
onUpgrade: (Migrator migrator, int from, int to) async {
if (from < 2) {
await migrator.addColumn(notes, notes.isDirty);
await migrator.addColumn(categories, categories.isDirty);
await customStatement('UPDATE notes SET is_dirty = 0');
await customStatement('UPDATE categories SET is_dirty = 0');
}
},
);
AppDatabase({required String encryptionKey})
: super(_openConnection(encryptionKey));
@@ -53,8 +76,8 @@ class AppDatabase extends _$AppDatabase {
return into(categories).insertOnConflictUpdate(category);
}
Future<void> deleteCategory(String uuid) {
return (update(categories)..where((c) => c.uuid.equals(uuid))).write(
Future<void> deleteCategory(String id) {
return (update(categories)..where((c) => c.id.equals(id))).write(
CategoriesCompanion(isDeleted: Value(true)),
);
}
@@ -70,7 +93,7 @@ class AppDatabase extends _$AppDatabase {
Future<int> insertNoteAtTop(NotesCompanion note) {
return transaction(() async {
await customStatement(
'UPDATE notes SET sort_index = sort_index + 1 WHERE is_deleted = 0',
'UPDATE notes SET sort_index = sort_index + 1, is_dirty = 1 WHERE is_deleted = 0',
);
return into(notes).insert(note.copyWith(sortIndex: const Value<int>(0)));
});
@@ -90,28 +113,29 @@ class AppDatabase extends _$AppDatabase {
return update(notes).replace(note);
}
Future<void> deleteNote(int id, int removedIndex) async {
Future<void> deleteNote(String id, int removedIndex) async {
await (update(notes)..where((n) => n.id.equals(id))).write(
NotesCompanion(
isDeleted: const Value(true),
updatedAt: Value(DateTime.now()),
isDirty: const Value(true),
),
);
await customStatement(
'UPDATE notes SET sort_index = sort_index - 1 WHERE sort_index > ? AND is_deleted = 0',
'UPDATE notes SET sort_index = sort_index - 1, is_dirty = 1 WHERE sort_index > ? AND is_deleted = 0',
[removedIndex],
);
}
Future<void> deleteNoteAndShift({
required int id,
required String id,
required int removedIndex,
}) {
return deleteNote(id, removedIndex);
}
Future<void> permanentlyDeleteNote(int id) async {
Future<void> permanentlyDeleteNote(String id) async {
await (update(notes)..where((n) => n.id.equals(id))).write(
NotesCompanion(
title: const Value(''),
@@ -119,12 +143,13 @@ class AppDatabase extends _$AppDatabase {
categoryId: const Value(null),
isDeleted: const Value(true),
updatedAt: Value(DateTime.now()),
isDirty: const Value(true),
),
);
}
Future<void> moveNote({
required int id,
required String id,
required int oldIndex,
required int newIndex,
}) {
@@ -133,9 +158,9 @@ class AppDatabase extends _$AppDatabase {
}
return transaction(() async {
final List<DbNote> all = await (select(notes)
..where((n) => n.isDeleted.equals(false)))
.get();
final List<DbNote> all = await (select(
notes,
)..where((n) => n.isDeleted.equals(false))).get();
final int count = all.length;
if (count == 0) {
@@ -153,12 +178,12 @@ class AppDatabase extends _$AppDatabase {
if (safeOld < safeNew) {
await customStatement(
'UPDATE notes SET sort_index = sort_index - 1 WHERE sort_index > ? AND sort_index <= ? AND is_deleted = 0',
'UPDATE notes SET sort_index = sort_index - 1, is_dirty = 1 WHERE sort_index > ? AND sort_index <= ? AND is_deleted = 0',
[safeOld, safeNew],
);
} else {
await customStatement(
'UPDATE notes SET sort_index = sort_index + 1 WHERE sort_index >= ? AND sort_index < ? AND is_deleted = 0',
'UPDATE notes SET sort_index = sort_index + 1, is_dirty = 1 WHERE sort_index >= ? AND sort_index < ? AND is_deleted = 0',
[safeNew, safeOld],
);
}
@@ -167,6 +192,7 @@ class AppDatabase extends _$AppDatabase {
NotesCompanion(
sortIndex: Value<int>(safeNew),
updatedAt: Value<DateTime>(DateTime.now()),
isDirty: const Value(true),
),
);
});
@@ -179,8 +205,12 @@ class AppDatabase extends _$AppDatabase {
}
Future<List<DbNote>> getDeletedNotes() {
return (select(notes)
..where((n) => n.isDeleted.equals(true) & n.title.isNotValue('') & n.body.isNotValue('')))
return (select(notes)..where(
(n) =>
n.isDeleted.equals(true) &
n.title.isNotValue('') &
n.body.isNotValue(''),
))
.get();
}
@@ -192,15 +222,11 @@ class AppDatabase extends _$AppDatabase {
// ========== Sync helpers ==========
Future<List<DbNote>> getUnsyncedNotes() {
return (select(notes)
..where((n) => n.isDeleted.equals(true) | n.serverVersion.equals(0)))
.get();
return (select(notes)..where((n) => n.isDirty.equals(true))).get();
}
Future<List<DbCategory>> getUnsyncedCategories() {
return (select(categories)
..where((c) => c.isDeleted.equals(true) | c.serverVersion.equals(0)))
.get();
return (select(categories)..where((c) => c.isDirty.equals(true))).get();
}
}