refactor: Improve code formatting and readability in database and note repository

This commit is contained in:
2026-05-19 09:11:52 +02:00
parent 6de318786b
commit bb8caeef93
2 changed files with 95 additions and 42 deletions
+52 -28
View File
@@ -6,14 +6,15 @@ import 'package:notas/models/note.dart';
import 'package:notas/models/category.dart';
import 'package:notas/data/note_encryption.dart';
class NoteRepository {
NoteRepository({
required AppDatabase database,
required AuthApi authApi,
required String masterKey,
}) : _database = database,
_authApi = authApi,
_masterKey = masterKey;
required String masterKey,
}) : _database = database,
_authApi = authApi,
_masterKey = masterKey;
final AppDatabase _database;
final AuthApi _authApi;
@@ -42,7 +43,9 @@ class NoteRepository {
}
Future<Note> updateNote(Note note) async {
final int noteId = note.id ?? (throw ArgumentError('Note id is required to update a note.'));
final int noteId =
note.id ??
(throw ArgumentError('Note id is required to update a note.'));
await _database.updateNoteRow(
DbNote(
@@ -63,16 +66,17 @@ class NoteRepository {
}
Future<void> deleteNote(Note note) async {
final int noteId = note.id ?? (throw ArgumentError('Note id is required to delete a note.'));
final int noteId =
note.id ??
(throw ArgumentError('Note id is required to delete a note.'));
await _database.deleteNoteAndShift(
id: noteId,
removedIndex: note.index,
);
await _database.deleteNoteAndShift(id: noteId, removedIndex: note.index);
}
Future<void> moveNote(Note note, int newIndex) async {
final int noteId = note.id ?? (throw ArgumentError('Note id is required to reorder a note.'));
final int noteId =
note.id ??
(throw ArgumentError('Note id is required to reorder a note.'));
await _database.moveNote(
id: noteId,
@@ -89,19 +93,39 @@ class NoteRepository {
try {
// Get last sync timestamp
final DateTime? lastSync = await _authApi.getLastSyncAt();
final DateTime? lastSyncForRequest = forceFull ? DateTime.utc(1970, 1, 1) : lastSync;
// Collect pending changes
final List<DbNote> unsyncedNotes = await _database.getUnsyncedNotes();
final List<DbCategory> unsyncedCategories = await _database.getUnsyncedCategories();
final DateTime? lastSyncForRequest = forceFull
? DateTime.utc(1970, 1, 1)
: lastSync;
// Collect pending local changes.
// If we already synced at least once, use updatedAt to avoid re-sending
// old notes that were already uploaded.
final List<DbNote> unsyncedNotes;
final List<DbCategory> unsyncedCategories;
if (forceFull || lastSync == null) {
unsyncedNotes = await _database.getUnsyncedNotes();
unsyncedCategories = await _database.getUnsyncedCategories();
} else {
unsyncedNotes = await _database.getNotesChangedSince(lastSync);
unsyncedCategories = await _database.getCategoriesChangedSince(
lastSync,
);
}
// Build sync request (note: we send encrypted data, but locally we have plaintext)
// Encrypt all notes before sending
final List<SyncNotePayload> encryptedNotesPayload = [];
for (final dbNote in unsyncedNotes) {
final note = _fromDbNote(dbNote);
final encryptedTitle = await NoteEncryption.encryptNote(note.title, _masterKey);
final encryptedBody = await NoteEncryption.encryptNote(note.body, _masterKey);
final encryptedTitle = await NoteEncryption.encryptNote(
note.title,
_masterKey,
);
final encryptedBody = await NoteEncryption.encryptNote(
note.body,
_masterKey,
);
encryptedNotesPayload.add(
SyncNotePayload.fromNote(
note,
@@ -112,9 +136,7 @@ class NoteRepository {
}
final List<SyncCategoryPayload> categoriesPayload = unsyncedCategories
.map((cat) => SyncCategoryPayload.fromCategory(
_fromDbCategory(cat),
))
.map((cat) => SyncCategoryPayload.fromCategory(_fromDbCategory(cat)))
.toList();
final SyncRequest syncRequest = SyncRequest(
@@ -126,8 +148,7 @@ class NoteRepository {
);
// Call sync API
final Map<String, dynamic> syncResult =
await _authApi.sync(syncRequest);
final Map<String, dynamic> syncResult = await _authApi.sync(syncRequest);
if (syncResult['error'] == true) {
return {'error': true, 'message': syncResult['body']};
@@ -154,7 +175,8 @@ class NoteRepository {
Future<void> _applySyncResponse(SyncResponse response) async {
// Apply categories from server
for (final SyncCategoryResponse catResponse in response.changes.categories) {
for (final SyncCategoryResponse catResponse
in response.changes.categories) {
await _database.upsertCategory(
CategoriesCompanion(
uuid: Value(catResponse.id),
@@ -168,9 +190,9 @@ class NoteRepository {
// Apply notes from server
for (final SyncNoteResponse noteResponse in response.changes.notes) {
final existingNote = await (_database.select(_database.notes)
..where((n) => n.uuid.equals(noteResponse.id)))
.getSingleOrNull();
final existingNote = await (_database.select(
_database.notes,
)..where((n) => n.uuid.equals(noteResponse.id))).getSingleOrNull();
// Decrypt note content
String decryptedTitle = 'Encrypted';
@@ -207,7 +229,9 @@ class NoteRepository {
);
} else {
// Insert new note
await _database.into(_database.notes).insert(
await _database
.into(_database.notes)
.insert(
NotesCompanion(
uuid: Value(noteResponse.id),
title: Value(decryptedTitle),