diff --git a/lib/data/note_repository.dart b/lib/data/note_repository.dart index 4c56411..c16d818 100644 --- a/lib/data/note_repository.dart +++ b/lib/data/note_repository.dart @@ -58,12 +58,20 @@ class NoteRepository { Future createCategory(Category category) async { debugPrint('createCategory called with: ${category.name}'); + final DbCategory? existingCategory = await ( + _database.select(_database.categories)..where((c) => c.id.equals(category.id)) + ).getSingleOrNull(); + final int effectiveServerVersion = math.max( + category.serverVersion, + existingCategory?.serverVersion ?? category.serverVersion, + ); + await _database.upsertCategory( CategoriesCompanion.insert( id: category.id, name: category.name, updatedAt: category.updatedAt, - serverVersion: const Value(0), + serverVersion: Value(effectiveServerVersion), isDeleted: const Value(false), isDirty: const Value(true), colorValue: Value(category.colorValue), diff --git a/lib/screens/home_screen.dart b/lib/screens/home_screen.dart index 029390c..9a0eada 100644 --- a/lib/screens/home_screen.dart +++ b/lib/screens/home_screen.dart @@ -884,6 +884,7 @@ class _CategoryDialogState extends State<_CategoryDialog> { final Category newCategory = Category( id: widget.category?.id, name: name, + serverVersion: widget.category?.serverVersion ?? 0, updatedAt: DateTime.now(), colorValue: _selectedColor?.toARGB32(), iconCodePoint: _selectedIcon?.codePoint,