style: Format code for consistency and readability across database and note positioning files

This commit is contained in:
2026-05-22 17:31:49 +02:00
parent 729e575a60
commit 814f8f7c04
4 changed files with 62 additions and 58 deletions
+56 -49
View File
@@ -18,7 +18,8 @@ class Categories extends Table {
BoolColumn get isDeleted => BoolColumn get isDeleted =>
boolean().named('is_deleted').withDefault(const Constant(false))(); boolean().named('is_deleted').withDefault(const Constant(false))();
IntColumn get colorValue => integer().nullable().named('color_value')(); IntColumn get colorValue => integer().nullable().named('color_value')();
IntColumn get iconCodePoint => integer().nullable().named('icon_code_point')(); IntColumn get iconCodePoint =>
integer().nullable().named('icon_code_point')();
BoolColumn get isDirty => BoolColumn get isDirty =>
boolean().named('is_dirty').withDefault(const Constant(true))(); boolean().named('is_dirty').withDefault(const Constant(true))();
DateTimeColumn get updatedAt => dateTime().named('updated_at')(); DateTimeColumn get updatedAt => dateTime().named('updated_at')();
@@ -40,11 +41,11 @@ class Notes extends Table {
BoolColumn get isDeleted => BoolColumn get isDeleted =>
boolean().named('is_deleted').withDefault(const Constant(false))(); boolean().named('is_deleted').withDefault(const Constant(false))();
TextColumn get categoryId => text().nullable().named('category_id')(); TextColumn get categoryId => text().nullable().named('category_id')();
BoolColumn get isDirty => BoolColumn get isDirty =>
boolean().named('is_dirty').withDefault(const Constant(true))(); boolean().named('is_dirty').withDefault(const Constant(true))();
@override @override
Set<Column> get primaryKey => {id}; Set<Column> get primaryKey => {id};
} }
@DriftDatabase(tables: [Notes, Categories]) @DriftDatabase(tables: [Notes, Categories])
@@ -72,12 +73,13 @@ class AppDatabase extends _$AppDatabase {
await customStatement('UPDATE categories SET icon_code_point = NULL'); await customStatement('UPDATE categories SET icon_code_point = NULL');
} }
if (from < 4) { if (from < 4) {
final List<DbNote> activeNotes = await (select(notes) final List<DbNote> activeNotes =
..where((n) => n.isDeleted.equals(false)) await (select(notes)
..orderBy([ ..where((n) => n.isDeleted.equals(false))
(note) => OrderingTerm(expression: note.sortIndex), ..orderBy([
])) (note) => OrderingTerm(expression: note.sortIndex),
.get(); ]))
.get();
final List<int> rebalancedPositions = rebalanceNotePositions( final List<int> rebalancedPositions = rebalanceNotePositions(
activeNotes.length, activeNotes.length,
@@ -134,24 +136,25 @@ class AppDatabase extends _$AppDatabase {
Future<int> insertNoteAtTop(NotesCompanion note) { Future<int> insertNoteAtTop(NotesCompanion note) {
return transaction(() async { return transaction(() async {
final DbNote? topNote = await (select(notes) final DbNote? topNote =
..where((n) => n.isDeleted.equals(false)) await (select(notes)
..orderBy([ ..where((n) => n.isDeleted.equals(false))
(note) => OrderingTerm( ..orderBy([
expression: note.sortIndex, (note) => OrderingTerm(
mode: OrderingMode.desc, expression: note.sortIndex,
), mode: OrderingMode.desc,
]) ),
..limit(1)) ])
.getSingleOrNull(); ..limit(1))
.getSingleOrNull();
final int nextSortIndex = topNote == null final int nextSortIndex = topNote == null
? 0 ? 0
: topNote.sortIndex + notePositionStep; : topNote.sortIndex + notePositionStep;
await into(notes).insert( await into(
note.copyWith(sortIndex: Value<int>(nextSortIndex)), notes,
); ).insert(note.copyWith(sortIndex: Value<int>(nextSortIndex)));
return nextSortIndex; return nextSortIndex;
}); });
} }
@@ -206,17 +209,20 @@ class AppDatabase extends _$AppDatabase {
required int newIndex, required int newIndex,
}) { }) {
return transaction(() async { return transaction(() async {
final List<DbNote> orderedNotes = await (select(notes) final List<DbNote> orderedNotes =
..where((n) => n.isDeleted.equals(false)) await (select(notes)
..orderBy([ ..where((n) => n.isDeleted.equals(false))
(note) => OrderingTerm( ..orderBy([
expression: note.sortIndex, (note) => OrderingTerm(
mode: OrderingMode.desc, expression: note.sortIndex,
), mode: OrderingMode.desc,
])) ),
.get(); ]))
.get();
final int currentIndex = orderedNotes.indexWhere((DbNote row) => row.id == id); final int currentIndex = orderedNotes.indexWhere(
(DbNote row) => row.id == id,
);
if (currentIndex == -1) { if (currentIndex == -1) {
return; return;
} }
@@ -277,22 +283,23 @@ class AppDatabase extends _$AppDatabase {
} }
Future<List<DbNote>> getDeletedNotes() { Future<List<DbNote>> getDeletedNotes() {
// A note is considered deleted (in the trash) when `is_deleted` is true // A note is considered deleted (in the trash) when `is_deleted` is true
// and at least one of `title` or `body` is not empty. Previously the // and at least one of `title` or `body` is not empty. Previously the
// query required both title AND body to be non-empty which excluded // query required both title AND body to be non-empty which excluded
// notes that had an empty body (common) from appearing in the trash. // notes that had an empty body (common) from appearing in the trash.
return (select(notes) return (select(notes)
..where( ..where(
(n) => n.isDeleted.equals(true) & (n) =>
(n.title.isNotValue('') | n.body.isNotValue('')), n.isDeleted.equals(true) &
) (n.title.isNotValue('') | n.body.isNotValue('')),
..orderBy([ )
(note) => OrderingTerm( ..orderBy([
expression: note.sortIndex, (note) => OrderingTerm(
mode: OrderingMode.desc, expression: note.sortIndex,
), mode: OrderingMode.desc,
])) ),
.get(); ]))
.get();
} }
Future<List<DbCategory>> getCategoriesChangedSince(DateTime since) { Future<List<DbCategory>> getCategoriesChangedSince(DateTime since) {
+3 -3
View File
@@ -59,9 +59,9 @@ class NoteRepository {
Future<void> createCategory(Category category) async { Future<void> createCategory(Category category) async {
debugPrint('createCategory called with: ${category.name}'); debugPrint('createCategory called with: ${category.name}');
final DbCategory? existingCategory = await ( final DbCategory? existingCategory = await (_database.select(
_database.select(_database.categories)..where((c) => c.id.equals(category.id)) _database.categories,
).getSingleOrNull(); )..where((c) => c.id.equals(category.id))).getSingleOrNull();
final int effectiveServerVersion = math.max( final int effectiveServerVersion = math.max(
category.serverVersion, category.serverVersion,
existingCategory?.serverVersion ?? category.serverVersion, existingCategory?.serverVersion ?? category.serverVersion,
+1 -4
View File
@@ -14,10 +14,7 @@ void main() {
midpointNotePosition(higherPosition: 20000, lowerPosition: 10000), midpointNotePosition(higherPosition: 20000, lowerPosition: 10000),
15000, 15000,
); );
expect( expect(midpointNotePosition(higherPosition: 2, lowerPosition: 1), isNull);
midpointNotePosition(higherPosition: 2, lowerPosition: 1),
isNull,
);
expect(rebalanceNotePositions(3), <int>[20000, 10000, 0]); expect(rebalanceNotePositions(3), <int>[20000, 10000, 0]);
}); });
} }