From e0f226d3bc480b335e6045d34e5095ca6194b86f Mon Sep 17 00:00:00 2001 From: Marcos Date: Fri, 22 May 2026 10:11:41 +0200 Subject: [PATCH] feat: Refactor note decryption method and update category handling for improved clarity --- lib/data/note_encryption.dart | 2 +- lib/data/note_repository.dart | 42 +++++++++++---------------------- lib/data/sync_models.dart | 13 +++++----- lib/widgets/category_style.dart | 2 +- 4 files changed, 23 insertions(+), 36 deletions(-) diff --git a/lib/data/note_encryption.dart b/lib/data/note_encryption.dart index 86c1eac..ad0503a 100644 --- a/lib/data/note_encryption.dart +++ b/lib/data/note_encryption.dart @@ -44,7 +44,7 @@ class NoteEncryption { } /// Desencripta el contenido de una nota usando el master key - static Future decryptNote( + static Future decrypt( String encodedBox, String masterKey, ) async { diff --git a/lib/data/note_repository.dart b/lib/data/note_repository.dart index 0e017c4..4c56411 100644 --- a/lib/data/note_repository.dart +++ b/lib/data/note_repository.dart @@ -305,20 +305,19 @@ class NoteRepository { // Apply categories from server for (final SyncCategoryResponse catResponse in response.changes.categories) { - final String plainName = await _plainCategoryName( - catResponse.encryptedName, - _masterKey, + final Category category = await catResponse.toCategory( + masterKey: _masterKey, ); await _database.upsertCategory( CategoriesCompanion( - id: Value(catResponse.id), - name: Value(plainName), - serverVersion: Value(catResponse.serverVersion), - isDeleted: Value(catResponse.isDeleted), - colorValue: Value(catResponse.colorValue), - iconCodePoint: Value(catResponse.iconCodePoint), - updatedAt: Value(catResponse.updatedAt), + id: Value(category.id), + name: Value(category.name), + serverVersion: Value(category.serverVersion), + isDeleted: Value(category.isDeleted), + colorValue: Value(category.colorValue), + iconCodePoint: Value(category.iconCodePoint), + updatedAt: Value(category.updatedAt), isDirty: const Value(false), ), ); @@ -479,16 +478,11 @@ Future> _encryptCategories( final List payloads = []; for (final DbCategory row in categories) { - // The DB stores the plain category name locally. - // Use it directly when building the sync payload and preserve - // color/icon values from the DB row so they are sent to the server. - final String plainName = row.name; - payloads.add( - SyncCategoryPayload.fromCategory( + await SyncCategoryPayload.fromCategory( Category( id: row.id, - name: plainName, + name: row.name, serverVersion: row.serverVersion, isDeleted: row.isDeleted, updatedAt: row.updatedAt, @@ -496,7 +490,7 @@ Future> _encryptCategories( colorValue: row.colorValue, iconCodePoint: row.iconCodePoint, ), - encryptedName: await NoteEncryption.encryptNote(plainName, masterKey), + masterKey: masterKey, ), ); } @@ -657,11 +651,11 @@ Future>> _decryptNoteBatch( String decryptedBody = 'Encrypted'; if (!isPermanentlyDeleted) { try { - decryptedTitle = await NoteEncryption.decryptNote( + decryptedTitle = await NoteEncryption.decrypt( note['encryptedTitle']! as String, masterKey, ); - decryptedBody = await NoteEncryption.decryptNote( + decryptedBody = await NoteEncryption.decrypt( note['encryptedBody']! as String, masterKey, ); @@ -698,11 +692,3 @@ int _parallelWorkerCount(int itemCount) { ); return math.max(1, math.min(itemCount, cappedByCpu)); } - -Future _plainCategoryName(String storedName, String masterKey) async { - try { - return await NoteEncryption.decryptNote(storedName, masterKey); - } catch (_) { - return storedName; - } -} diff --git a/lib/data/sync_models.dart b/lib/data/sync_models.dart index a4b9196..8897dfe 100644 --- a/lib/data/sync_models.dart +++ b/lib/data/sync_models.dart @@ -1,3 +1,4 @@ +import 'package:notas/data/note_encryption.dart'; import 'package:notas/models/note.dart'; import 'package:notas/models/category.dart'; import 'dart:convert'; @@ -108,13 +109,13 @@ class SyncCategoryPayload { final int? iconCodePoint; final DateTime updatedAt; - factory SyncCategoryPayload.fromCategory( + static Future fromCategory( Category category, { - required String encryptedName, - }) { + required String masterKey, + }) async { return SyncCategoryPayload( id: category.id, - encryptedName: encryptedName, + encryptedName: await NoteEncryption.encryptNote(category.name, masterKey), serverVersion: category.serverVersion, isDeleted: category.isDeleted, colorValue: category.colorValue, @@ -249,10 +250,10 @@ class SyncCategoryResponse { ); } - Category toCategory({required String name}) { + Future toCategory({required String masterKey}) async { return Category( id: id, - name: name, + name: await NoteEncryption.decrypt(encryptedName, masterKey), serverVersion: serverVersion, isDeleted: isDeleted, colorValue: colorValue, diff --git a/lib/widgets/category_style.dart b/lib/widgets/category_style.dart index 6574381..1e3da9a 100644 --- a/lib/widgets/category_style.dart +++ b/lib/widgets/category_style.dart @@ -15,7 +15,7 @@ class CategoryStyle { ]; static const List icons = [ - Icons.folder, + Icons.label_outline_rounded, Icons.work, Icons.star, Icons.home,