diff --git a/lib/src/kdbx_file.dart b/lib/src/kdbx_file.dart index 8c930d1..f987d1b 100644 --- a/lib/src/kdbx_file.dart +++ b/lib/src/kdbx_file.dart @@ -18,7 +18,7 @@ import 'package:xml/xml.dart' as xml; final _logger = Logger('kdbx_file'); -typedef FileSaveCallback = Future Function(Uint8List bytes); +typedef FileSaveCallback = Future Function(Uint8List bytes); class KdbxFile { KdbxFile( @@ -57,7 +57,15 @@ class KdbxFile { Stream> get dirtyObjectsChanged => _dirtyObjectsChanged.stream; - Future save([FileSaveCallback? saveBytes]) async { + static final FileSaveCallback _saveToBytes = + (bytes) async => bytes; + + // @Deprecated('Use [saveTo] instead.') + Future save() async { + return kdbxFormat.save(this, _saveToBytes); + } + + Future saveTo(FileSaveCallback saveBytes) { return kdbxFormat.save(this, saveBytes); } diff --git a/lib/src/kdbx_format.dart b/lib/src/kdbx_format.dart index 69ef538..6af8c75 100644 --- a/lib/src/kdbx_format.dart +++ b/lib/src/kdbx_format.dart @@ -577,19 +577,19 @@ class KdbxFormat { } /// Saves the given file. - Future save(KdbxFile file, FileSaveCallback? saveBytes) async { + Future save(KdbxFile file, FileSaveCallback saveBytes) async { _logger.finer('Saving ${file.body.rootGroup.uuid} ' '(locked: ${file.saveLock.locked})'); return file.saveLock.synchronized(() async { final savedAt = TimeSequence.now(); final bytes = await _saveSynchronized(file); - if (saveBytes != null) { - _logger.fine('Saving bytes.'); - final byteCount = await saveBytes(bytes); - _logger.fine('Saved bytes. $byteCount'); - } + + _logger.fine('Saving bytes.'); + final ret = await saveBytes(bytes); + _logger.fine('Saved bytes.'); + file.onSaved(savedAt); - return bytes; + return ret; }); } diff --git a/test/kdbx_dirty_save_test.dart b/test/kdbx_dirty_save_test.dart index ed665a3..2f916e7 100644 --- a/test/kdbx_dirty_save_test.dart +++ b/test/kdbx_dirty_save_test.dart @@ -14,12 +14,11 @@ void main() { await file.save(); const value1 = 'new'; - const value2 = 'new2'; entry.setString(TestUtil.keyTitle, PlainValue(value1)); entry2.setString(TestUtil.keyTitle, PlainValue(value1)); expect(file.isDirty, isTrue); - await file.save((bytes) async { + await file.saveTo((bytes) async { // must still be dirty as long as we are not finished saving. expect(file.isDirty, isTrue); expect(entry.isDirty, isTrue); @@ -42,7 +41,7 @@ void main() { entry.setString(TestUtil.keyTitle, PlainValue(value2)); entry2.setString(TestUtil.keyTitle, PlainValue(value2)); - await file.save((bytes) async { + await file.saveTo((bytes) async { // must still be dirty as long as we are not finished saving. expect(file.isDirty, isTrue); expect(entry.isDirty, isTrue);