feat: Enhance error handling in note loading and moving operations with detailed logging

This commit is contained in:
2026-05-20 10:05:47 +02:00
parent d0a985b4ab
commit 34f45a912f
2 changed files with 40 additions and 9 deletions
+26 -6
View File
@@ -133,21 +133,41 @@ class AppDatabase extends _$AppDatabase {
}
return transaction(() async {
if (oldIndex < newIndex) {
final List<DbNote> all = await (select(notes)
..where((n) => n.isDeleted.equals(false)))
.get();
final int count = all.length;
if (count == 0) {
return;
}
final int maxIndex = count - 1;
final int safeOld = oldIndex.clamp(0, maxIndex);
final int safeNew = newIndex.clamp(0, maxIndex);
if (safeOld == safeNew) {
return;
}
if (safeOld < safeNew) {
await customStatement(
'UPDATE notes SET sort_index = sort_index - 1 WHERE sort_index > ? AND sort_index <= ? AND is_deleted = 0',
[oldIndex, newIndex],
[safeOld, safeNew],
);
} else {
await customStatement(
'UPDATE notes SET sort_index = sort_index + 1 WHERE sort_index >= ? AND sort_index < ? AND is_deleted = 0',
[newIndex, oldIndex],
[safeNew, safeOld],
);
}
await customStatement(
'UPDATE notes SET sort_index = ?, updated_at = ? WHERE id = ?',
[newIndex, DateTime.now().toIso8601String(), id],
await (update(notes)..where((n) => n.id.equals(id))).write(
NotesCompanion(
sortIndex: Value<int>(safeNew),
updatedAt: Value<DateTime>(DateTime.now()),
),
);
});
}