diff --git a/lib/core/checklist.dart b/lib/core/checklist.dart index ceff35f8..28084441 100644 --- a/lib/core/checklist.dart +++ b/lib/core/checklist.dart @@ -102,10 +102,11 @@ class Checklist { for (var item in items) { _lines[item.lineNo] = item.toString(); } - _note.body = _lines.join('\n'); + var body = _lines.join('\n'); if (endsWithNewLine) { - _note.body += '\n'; + body += '\n'; } + _note.apply(body: body); return _note; } diff --git a/lib/core/note.dart b/lib/core/note.dart index cebbdd02..563bd65f 100644 --- a/lib/core/note.dart +++ b/lib/core/note.dart @@ -96,7 +96,7 @@ class Note { Map extraProps = const {}, String fileName = "", }) : fileLastModified = DateTime.fromMillisecondsSinceEpoch(0) { - created = DateTime.now(); + _created = DateTime.now(); _loadState = NoteLoadState.Loaded; _fileFormat = NoteFileFormat.Markdown; var settings = NoteSerializationSettings.fromConfig(parent.config); @@ -158,9 +158,72 @@ class Note { return _filePath as String; } - set filePath(String newpath) { - _filePath = newpath; - _notifyModified(); + void apply({ + String? filePath, + DateTime? created, + DateTime? modified, + String? body, + String? title, + NoteType? type, + Map? extraProps, + Set? tags, + NoteFileFormat? fileFormat, + NoteLoadState? loadState, + }) { + var changed = false; + if (filePath != null) { + _filePath = filePath; + changed = true; + } + if (canHaveMetadata) { + if (created != null && created != _created) { + _created = created; + changed = true; + } + if (modified != null && modified != _modified) { + _modified = modified; + changed = true; + } + + if (type != null && type != _type) { + _type = type; + changed = true; + } + + if (extraProps != null) { + _extraProps = extraProps; + changed = true; + } + + if (tags != null) { + _tags = tags; + changed = true; + } + } + + if (body != null && body != _body) { + _body = body; + changed = true; + } + + if (title != null && title != _title) { + _title = title; + changed = true; + } + + if (fileFormat != null && _fileFormat != fileFormat) { + _fileFormat = fileFormat; + changed = true; + } + + if (loadState != null && _loadState != loadState) { + _loadState = loadState; + changed = true; + } + + if (changed) { + _notifyModified(); + } } String get fileName { @@ -171,88 +234,35 @@ class Note { return _created; } - set created(DateTime? dt) { - if (!canHaveMetadata) return; - - _created = dt; - _notifyModified(); - } - DateTime? get modified { return _modified; } - set modified(DateTime? dt) { - if (!canHaveMetadata) return; - - _modified = dt; - _notifyModified(); - } - void updateModified() { - modified = DateTime.now(); + _modified = DateTime.now(); + _notifyModified(); } String get body { return _body; } - set body(String newBody) { - if (newBody == _body) { - return; - } - - _body = newBody; - - _notifyModified(); - } - String get title { return _title; } - set title(String title) { - if (title != _title) { - _title = title; - _notifyModified(); - } - } - NoteType get type { return _type; } - set type(NoteType type) { - if (!canHaveMetadata) return; - - if (type != _type) { - _type = type; - _notifyModified(); - } - } - Set get tags { return _tags; } - set tags(Set tags) { - if (!canHaveMetadata) return; - - _tags = tags; - _notifyModified(); - } - Map get extraProps { return _extraProps; } - set extraProps(Map props) { - if (!canHaveMetadata) return; - - _extraProps = props; - _notifyModified(); - } - bool get canHaveMetadata { if (_fileFormat == NoteFileFormat.Txt || _fileFormat == NoteFileFormat.OrgMode) { @@ -277,11 +287,6 @@ class Note { return _loadState; } - set loadState(NoteLoadState state) { - _loadState = state; - _notifyModified(); - } - @override int get hashCode => _filePath.hashCode; @@ -341,11 +346,6 @@ class Note { NoteFileFormat? get fileFormat { return _fileFormat; } - - set fileFormat(NoteFileFormat? format) { - _fileFormat = format; - _notifyModified(); - } } String ensureFileNameUnique(String parentDir, String name, String ext) { diff --git a/lib/core/note_serializer.dart b/lib/core/note_serializer.dart index d2ee8a43..d0acacca 100644 --- a/lib/core/note_serializer.dart +++ b/lib/core/note_serializer.dart @@ -141,6 +141,7 @@ class NoteSerializer implements NoteSerializerInterface { void decode(MdYamlDoc data, Note note) { var propsUsed = {}; + DateTime? modified; var modifiedKeyOptions = [ "modified", "mod", @@ -154,10 +155,10 @@ class NoteSerializer implements NoteSerializerInterface { var val = data.props[possibleKey]; if (val != null) { if (val is int) { - note.modified = parseUnixTimeStamp(val); + modified = parseUnixTimeStamp(val); settings.modifiedFormat = DateFormat.UnixTimeStamp; } else { - note.modified = parseDateTime(val.toString()); + modified = parseDateTime(val.toString()); settings.modifiedFormat = DateFormat.Iso8601; } settings.modifiedKey = possibleKey; @@ -167,8 +168,9 @@ class NoteSerializer implements NoteSerializerInterface { } } - note.body = settings.emojify ? emojiParser.emojify(data.body) : data.body; + var body = settings.emojify ? emojiParser.emojify(data.body) : data.body; + DateTime? created; var createdKeyOptions = [ "created", "date", @@ -177,10 +179,10 @@ class NoteSerializer implements NoteSerializerInterface { var val = data.props[possibleKey]; if (val != null) { if (val is int) { - note.created = parseUnixTimeStamp(val); + created = parseUnixTimeStamp(val); settings.createdFormat = DateFormat.UnixTimeStamp; } else { - note.created = parseDateTime(val.toString()); + created = parseDateTime(val.toString()); settings.createdFormat = DateFormat.Iso8601; } settings.createdKey = possibleKey; @@ -193,15 +195,16 @@ class NoteSerializer implements NoteSerializerInterface { // // Title parsing // + String? title; if (data.props.containsKey(settings.titleKey)) { - var title = data.props[settings.titleKey]?.toString() ?? ""; - note.title = settings.emojify ? emojiParser.emojify(title) : title; + title = data.props[settings.titleKey]?.toString() ?? ""; + title = settings.emojify ? emojiParser.emojify(title) : title; propsUsed.add(settings.titleKey); settings.titleSettings = SettingsTitle.InYaml; } else { var startsWithH1 = false; - for (var line in LineSplitter.split(note.body)) { + for (var line in LineSplitter.split(body)) { if (line.trim().isEmpty) { continue; } @@ -210,35 +213,36 @@ class NoteSerializer implements NoteSerializerInterface { } if (startsWithH1) { - var titleStartIndex = note.body.indexOf('#'); - var titleEndIndex = note.body.indexOf('\n', titleStartIndex); - if (titleEndIndex == -1 || titleEndIndex == note.body.length) { - note.title = note.body.substring(titleStartIndex + 1).trim(); - note.body = ""; + var titleStartIndex = body.indexOf('#'); + var titleEndIndex = body.indexOf('\n', titleStartIndex); + if (titleEndIndex == -1 || titleEndIndex == body.length) { + title = body.substring(titleStartIndex + 1).trim(); + body = ""; } else { - note.title = - note.body.substring(titleStartIndex + 1, titleEndIndex).trim(); - note.body = note.body.substring(titleEndIndex + 1).trim(); + title = body.substring(titleStartIndex + 1, titleEndIndex).trim(); + body = body.substring(titleEndIndex + 1).trim(); } } } - var type = data.props[settings.typeKey]; - switch (type) { + NoteType? type; + var typeStr = data.props[settings.typeKey]; + switch (typeStr) { case "Checklist": - note.type = NoteType.Checklist; + type = NoteType.Checklist; break; case "Journal": - note.type = NoteType.Journal; + type = NoteType.Journal; break; default: - note.type = NoteType.Unknown; + type = NoteType.Unknown; break; } - if (type != null) { + if (typeStr != null) { propsUsed.add(settings.typeKey); } + Set? _tags; try { var tagKeyOptions = [ "tags", @@ -249,9 +253,9 @@ class NoteSerializer implements NoteSerializerInterface { var tags = data.props[possibleKey]; if (tags != null) { if (tags is YamlList) { - note.tags = tags.map((t) => t.toString()).toSet(); + _tags = tags.map((t) => t.toString()).toSet(); } else if (tags is List) { - note.tags = tags.map((t) => t.toString()).toSet(); + _tags = tags.map((t) => t.toString()).toSet(); } else if (tags is String) { settings.tagsInString = true; var allTags = tags.split(' '); @@ -261,7 +265,7 @@ class NoteSerializer implements NoteSerializerInterface { allTags = allTags.map((e) => e.substring(1)).toList(); } - note.tags = allTags.toSet(); + _tags = allTags.toSet(); } else { Log.e("Note Tags Decoding Failed: $tags"); } @@ -276,13 +280,23 @@ class NoteSerializer implements NoteSerializerInterface { } // Extra Props - note.extraProps = {}; + var extraProps = {}; data.props.forEach((key, val) { if (propsUsed.contains(key)) { return; } - note.extraProps[key] = val; + extraProps[key] = val; }); + + note.apply( + created: created, + modified: modified, + body: body, + title: title, + type: type, + extraProps: extraProps, + tags: _tags, + ); } } diff --git a/lib/core/note_storage.dart b/lib/core/note_storage.dart index ccbe1618..8cd81aee 100644 --- a/lib/core/note_storage.dart +++ b/lib/core/note_storage.dart @@ -56,14 +56,12 @@ class NoteStorage { } catch (e, stackTrace) { if (e is FileSystemException && e.osError!.errorCode == 2 /* File Not Found */) { - note.loadState = NoteLoadState.NotExists; - note.parent.noteModified(note); + note.apply(loadState: NoteLoadState.NotExists); return Result(note.loadState); } logExceptionWarning(e, stackTrace); - note.loadState = NoteLoadState.Error; - note.parent.noteModified(note); + note.apply(loadState: NoteLoadState.Error); return Result(note.loadState); } Log.d("Note modified: $note.filePath"); @@ -78,49 +76,48 @@ class NoteStorage { var dataResult = await mdYamlDocLoader.loadDoc(note.filePath); if (dataResult.isSuccess) { note.data = dataResult.getOrThrow(); - note.fileFormat = NoteFileFormat.Markdown; + note.apply(fileFormat: NoteFileFormat.Markdown); } else { if (dataResult.error is MdYamlDocNotFoundException) { - note.loadState = NoteLoadState.NotExists; - note.parent.noteModified(note); + note.apply(loadState: NoteLoadState.NotExists); return Result(note.loadState); } if (dataResult.error is MdYamlParsingException) { - note.loadState = NoteLoadState.Error; - note.parent.noteModified(note); + note.apply(loadState: NoteLoadState.Error); return Result(note.loadState); } } } else if (isTxt) { try { - note.body = await File(note.filePath).readAsString(); - note.fileFormat = NoteFileFormat.Txt; + note.apply( + body: await File(note.filePath).readAsString(), + fileFormat: NoteFileFormat.Txt, + ); } catch (e, stackTrace) { logExceptionWarning(e, stackTrace); - note.loadState = NoteLoadState.Error; - note.parent.noteModified(note); + note.apply(loadState: NoteLoadState.Error); return Result(note.loadState); } } else if (isOrg) { try { - note.body = await File(note.filePath).readAsString(); - note.fileFormat = NoteFileFormat.OrgMode; + note.apply( + body: await File(note.filePath).readAsString(), + fileFormat: NoteFileFormat.OrgMode, + ); } catch (e, stackTrace) { logExceptionWarning(e, stackTrace); - note.loadState = NoteLoadState.Error; - note.parent.noteModified(note); + note.apply(loadState: NoteLoadState.Error); return Result(note.loadState); } } else { - note.loadState = NoteLoadState.Error; - note.parent.noteModified(note); + note.apply(loadState: NoteLoadState.Error); return Result(note.loadState); } note.fileLastModified = file.lastModifiedSync(); - note.loadState = NoteLoadState.Loaded; + note.apply(loadState: NoteLoadState.Loaded); note.parent.noteModified(note); return Result(note.loadState); diff --git a/lib/core/notes_folder_fs.dart b/lib/core/notes_folder_fs.dart index 2ae0e538..629be388 100644 --- a/lib/core/notes_folder_fs.dart +++ b/lib/core/notes_folder_fs.dart @@ -557,7 +557,7 @@ class NotesFolderFS with NotesFolderNotifier implements NotesFolder { if (File(oldFilePath).existsSync()) { File(note.filePath).renameSync(newFilePath); } - note.filePath = newFilePath; + note.apply(filePath: newFilePath); _noteRenamed(note, oldFilePath); } diff --git a/lib/core/transformers/emoji.dart b/lib/core/transformers/emoji.dart index a3598d6b..81edccb7 100644 --- a/lib/core/transformers/emoji.dart +++ b/lib/core/transformers/emoji.dart @@ -13,15 +13,19 @@ class EmojiTransformer implements NoteReadTransformer, NoteWriteTransformer { @override Future onRead(Note note) async { - note.title = _emojiParser.emojify(note.title); - note.body = _emojiParser.emojify(note.body); + note.apply( + body: _emojiParser.emojify(note.body), + title: _emojiParser.emojify(note.title), + ); return note; } @override Future onWrite(Note note) async { - note.body = _emojiParser.unemojify(note.body); - note.title = _emojiParser.unemojify(note.title); + note.apply( + body: _emojiParser.unemojify(note.body), + title: _emojiParser.unemojify(note.title), + ); return note; } } diff --git a/lib/editors/checklist_editor.dart b/lib/editors/checklist_editor.dart index 80d5811a..5b4d02eb 100644 --- a/lib/editors/checklist_editor.dart +++ b/lib/editors/checklist_editor.dart @@ -226,8 +226,10 @@ class ChecklistEditorState extends State } var note = checklist.note; - note.title = _titleTextController.text.trim(); - note.type = NoteType.Checklist; + note.apply( + title: _titleTextController.text.trim(), + type: NoteType.Checklist, + ); return note; } @@ -313,7 +315,7 @@ class ChecklistEditorState extends State Future addImage(String filePath) async { var note = getNote(); var image = await core.Image.copyIntoFs(note.parent, filePath); - note.body += image.toMarkup(note.fileFormat); + note.apply(body: note.body + image.toMarkup(note.fileFormat)); setState(() { checklist = Checklist(note); diff --git a/lib/editors/journal_editor.dart b/lib/editors/journal_editor.dart index 111f35c5..dda40b28 100644 --- a/lib/editors/journal_editor.dart +++ b/lib/editors/journal_editor.dart @@ -134,8 +134,10 @@ class JournalEditorState extends State @override Note getNote() { - note.body = _textController.text.trim(); - note.type = NoteType.Journal; + note.apply( + body: _textController.text.trim(), + type: NoteType.Journal, + ); return note; } @@ -174,7 +176,7 @@ class JournalEditorState extends State Future addImage(String filePath) async { var note = getNote(); var image = await core.Image.copyIntoFs(note.parent, filePath); - note.body += image.toMarkup(note.fileFormat); + note.apply(body: note.body + image.toMarkup(note.fileFormat)); setState(() { _textController.text = note.body; diff --git a/lib/editors/markdown_editor.dart b/lib/editors/markdown_editor.dart index 16d4d842..a107e5b8 100644 --- a/lib/editors/markdown_editor.dart +++ b/lib/editors/markdown_editor.dart @@ -162,9 +162,11 @@ class MarkdownEditorState extends State } void _updateNote() { - note.title = _titleTextController.text.trim(); - note.body = _textController.text; - note.type = NoteType.Unknown; + note.apply( + body: _textController.text.trim(), + title: _titleTextController.text.trim(), + type: NoteType.Unknown, + ); } @override @@ -218,7 +220,7 @@ class MarkdownEditorState extends State Future addImage(String filePath) async { var note = getNote(); var image = await core.Image.copyIntoFs(note.parent, filePath); - note.body += image.toMarkup(note.fileFormat); + note.apply(body: note.body + image.toMarkup(note.fileFormat)); setState(() { _textController.text = note.body; diff --git a/lib/editors/org_editor.dart b/lib/editors/org_editor.dart index 21a3d122..21032173 100644 --- a/lib/editors/org_editor.dart +++ b/lib/editors/org_editor.dart @@ -154,7 +154,7 @@ class OrgEditorState extends State Future addImage(String filePath) async { var note = getNote(); var image = await core.Image.copyIntoFs(note.parent, filePath); - note.body += image.toMarkup(note.fileFormat); + note.apply(body: note.body + image.toMarkup(note.fileFormat)); setState(() { _textController.text = note.body; diff --git a/lib/editors/raw_editor.dart b/lib/editors/raw_editor.dart index cefbe1a8..03a323a1 100644 --- a/lib/editors/raw_editor.dart +++ b/lib/editors/raw_editor.dart @@ -162,7 +162,7 @@ class RawEditorState extends State Future addImage(String filePath) async { var note = getNote(); var image = await core.Image.copyIntoFs(note.parent, filePath); - note.body += image.toMarkup(note.fileFormat); + note.apply(body: note.body + image.toMarkup(note.fileFormat)); setState(() { _textController.text = note.body; diff --git a/lib/screens/note_editor.dart b/lib/screens/note_editor.dart index cafa063a..f8ef56eb 100644 --- a/lib/screens/note_editor.dart +++ b/lib/screens/note_editor.dart @@ -117,7 +117,7 @@ class NoteEditorState extends State with WidgetsBindingObserver { ) { note = Note.newNote(folder, extraProps: extraProps, fileName: fileName); if (existingText.isNotEmpty) { - note!.body = existingText; + note!.apply(body: existingText); } if (existingImages.isNotEmpty) { @@ -125,7 +125,7 @@ class NoteEditorState extends State with WidgetsBindingObserver { () async { try { var image = await core.Image.copyIntoFs(note!.parent, imagePath); - note!.body += image.toMarkup(note!.fileFormat); + note!.apply(body: note!.body + image.toMarkup(note!.fileFormat)); } catch (e, st) { Log.e("New Note Existing Image", ex: e, stacktrace: st); } @@ -489,7 +489,7 @@ class NoteEditorState extends State with WidgetsBindingObserver { if (!eq(note!.tags, newTags)) { setState(() { Log.i("Settings tags to: $newTags"); - note!.tags = newTags; + note!.apply(tags: newTags); }); } } diff --git a/lib/settings/settings_note_metadata.dart b/lib/settings/settings_note_metadata.dart index 45aa44a5..1850dd9a 100644 --- a/lib/settings/settings_note_metadata.dart +++ b/lib/settings/settings_note_metadata.dart @@ -51,23 +51,28 @@ class _NoteMetadataSettingsScreenState var parent = NotesFolderFS(null, '', folderConfig); var note = Note(parent, "fileName.md", DateTime.now()); - note.title = tr("settings.noteMetaData.exampleTitle"); - note.body = tr("settings.noteMetaData.exampleBody"); - note.created = created; - note.modified = modified; - note.tags = { - tr("settings.noteMetaData.exampleTag1"), - tr("settings.noteMetaData.exampleTag2"), - }; + Map? extraProps; if (settings.customMetaData != "") { var customMetaDataMap = MarkdownYAMLCodec.parseYamlText(settings.customMetaData); if (customMetaDataMap.isNotEmpty) { - note.extraProps = customMetaDataMap; + extraProps = customMetaDataMap; } } + note.apply( + title: tr("settings.noteMetaData.exampleTitle"), + body: tr("settings.noteMetaData.exampleBody"), + created: created, + modified: modified, + extraProps: extraProps, + tags: { + tr("settings.noteMetaData.exampleTag1"), + tr("settings.noteMetaData.exampleTag2"), + }, + ); + var body = Column( children: [ Padding( diff --git a/test/core/flattened_filtered_notes_folder_test.dart b/test/core/flattened_filtered_notes_folder_test.dart index 421ec0a4..6ee91f7b 100644 --- a/test/core/flattened_filtered_notes_folder_test.dart +++ b/test/core/flattened_filtered_notes_folder_test.dart @@ -47,8 +47,10 @@ void main() { for (var i = 0; i < 3; i++) { var note = Note(rootFolder, _getRandomFilePath(rootFolder.folderPath), DateTime.now()); - note.modified = DateTime(2020, 1, 10 + (i * 2)); - note.body = "$i\n"; + note.apply( + modified: DateTime(2020, 1, 10 + (i * 2)), + body: "$i\n", + ); await NoteStorage().save(note).throwOnError(); } @@ -61,8 +63,10 @@ void main() { for (var i = 0; i < 2; i++) { var note = Note(sub1Folder, _getRandomFilePath(sub1Folder.folderPath), DateTime.now()); - note.modified = DateTime(2020, 1, 10 + (i * 2)); - note.body = "sub1-$i\n"; + note.apply( + modified: DateTime(2020, 1, 10 + (i * 2)), + body: "sub1-$i\n", + ); await NoteStorage().save(note).throwOnError(); } @@ -71,8 +75,10 @@ void main() { for (var i = 0; i < 2; i++) { var note = Note(sub2Folder, _getRandomFilePath(sub2Folder.folderPath), DateTime.now()); - note.modified = DateTime(2020, 1, 10 + (i * 2)); - note.body = "sub2-$i\n"; + note.apply( + modified: DateTime(2020, 1, 10 + (i * 2)), + body: "sub2-$i\n", + ); await NoteStorage().save(note).throwOnError(); } @@ -81,8 +87,10 @@ void main() { for (var i = 0; i < 2; i++) { var note = Note( p1Folder, _getRandomFilePath(p1Folder.folderPath), DateTime.now()); - note.modified = DateTime(2020, 1, 10 + (i * 2)); - note.body = "p1-$i\n"; + note.apply( + modified: DateTime(2020, 1, 10 + (i * 2)), + body: "p1-$i\n", + ); await NoteStorage().save(note).throwOnError(); } diff --git a/test/flattened_notes_folder_large_test.dart b/test/flattened_notes_folder_large_test.dart index 97014e0b..d1e6fa22 100644 --- a/test/flattened_notes_folder_large_test.dart +++ b/test/flattened_notes_folder_large_test.dart @@ -69,8 +69,9 @@ Future _writeRandomNote( } var note = Note(NotesFolderFS(null, dirPath, config), path, DateTime.now()); - note.modified = DateTime(2014, 1, 1 + (random.nextInt(2000))); - note.body = "p1"; - + note.apply( + modified: DateTime(2014, 1, 1 + (random.nextInt(2000))), + body: "p1", + ); await NoteStorage().save(note).throwOnError(); } diff --git a/test/flattened_notes_folder_test.dart b/test/flattened_notes_folder_test.dart index 81a4db37..77164fb3 100644 --- a/test/flattened_notes_folder_test.dart +++ b/test/flattened_notes_folder_test.dart @@ -47,8 +47,10 @@ void main() { for (var i = 0; i < 3; i++) { var note = Note(rootFolder, _getRandomFilePath(rootFolder.folderPath), DateTime.now()); - note.modified = DateTime(2020, 1, 10 + (i * 2)); - note.body = "$i\n"; + note.apply( + modified: DateTime(2020, 1, 10 + (i * 2)), + body: "$i\n", + ); await NoteStorage().save(note).throwOnError(); } @@ -61,8 +63,10 @@ void main() { for (var i = 0; i < 2; i++) { var note = Note(sub1Folder, _getRandomFilePath(sub1Folder.folderPath), DateTime.now()); - note.modified = DateTime(2020, 1, 10 + (i * 2)); - note.body = "sub1-$i\n"; + note.apply( + modified: DateTime(2020, 1, 10 + (i * 2)), + body: "sub1-$i\n", + ); await NoteStorage().save(note).throwOnError(); } @@ -71,8 +75,10 @@ void main() { for (var i = 0; i < 2; i++) { var note = Note(sub2Folder, _getRandomFilePath(sub2Folder.folderPath), DateTime.now()); - note.modified = DateTime(2020, 1, 10 + (i * 2)); - note.body = "sub2-$i\n"; + note.apply( + modified: DateTime(2020, 1, 10 + (i * 2)), + body: "sub2-$i\n", + ); await NoteStorage().save(note).throwOnError(); } @@ -81,8 +87,10 @@ void main() { for (var i = 0; i < 2; i++) { var note = Note( p1Folder, _getRandomFilePath(p1Folder.folderPath), DateTime.now()); - note.modified = DateTime(2020, 1, 10 + (i * 2)); - note.body = "p1-$i\n"; + note.apply( + modified: DateTime(2020, 1, 10 + (i * 2)), + body: "p1-$i\n", + ); await NoteStorage().save(note).throwOnError(); } @@ -120,8 +128,10 @@ void main() { var p1 = (f.fsFolder as NotesFolderFS).getFolderWithSpec("sub1/p1")!; var note = Note(p1, p.join(p1.folderPath, "new.md"), DateTime.now()); - note.modified = DateTime(2020, 2, 1); - note.body = "new\n"; + note.apply( + modified: DateTime(2020, 2, 1), + body: "new\n", + ); await NoteStorage().save(note).throwOnError(); p1.add(note); diff --git a/test/note_serializer_test.dart b/test/note_serializer_test.dart index 82568fd4..f3e07e37 100644 --- a/test/note_serializer_test.dart +++ b/test/note_serializer_test.dart @@ -42,8 +42,7 @@ void main() { expect(note.body, "I ❤️ you"); expect(note.title, "Why not ☕?"); - note.body = "Why not ☕?"; - note.title = "I ❤️ you"; + note.apply(body: "Why not ☕?", title: "I ❤️ you"); serializer.encode(note, doc); expect(doc.body, "Why not :coffee:?"); @@ -64,8 +63,7 @@ void main() { expect(note.body, "I heart you"); expect(note.title, "Why not coffee?"); - note.body = "Why not coffee?"; - note.title = "I heart you"; + note.apply(body: "Why not coffee?", title: "I heart you"); serializer.encode(note, doc); expect(doc.body, "# I heart you\n\nWhy not coffee?"); diff --git a/test/note_storage_test.dart b/test/note_storage_test.dart index bfea5e40..ef2c21b2 100644 --- a/test/note_storage_test.dart +++ b/test/note_storage_test.dart @@ -42,8 +42,7 @@ void main() { var parent = NotesFolderFS(null, tempDir.path, config); var n1 = Note(parent, n1Path, DateTime.now()); - n1.body = "test\n"; - n1.created = dt; + n1.apply(created: dt, body: "test\n"); var n2 = Note(parent, n2Path, DateTime.now()); n2.data = MdYamlDoc(body: "test2\n", props: props); diff --git a/test/note_test.dart b/test/note_test.dart index e68884dc..4fb66131 100644 --- a/test/note_test.dart +++ b/test/note_test.dart @@ -48,7 +48,7 @@ Hello var note = Note(parentFolder, notePath, DateTime.now()); await storage.load(note); - note.modified = DateTime.utc(2019, 12, 02, 4, 0, 0); + note.apply(modified: DateTime.utc(2019, 12, 02, 4, 0, 0)); await NoteStorage().save(note).throwOnError(); @@ -80,7 +80,7 @@ Hello var note = Note(parentFolder, notePath, DateTime.now()); await storage.load(note); - note.modified = DateTime.utc(2019, 12, 02, 4, 0, 0); + note.apply(modified: DateTime.utc(2019, 12, 02, 4, 0, 0)); await NoteStorage().save(note).throwOnError(); @@ -116,10 +116,7 @@ Hello expect(note.tags.contains('B'), true); expect(note.tags.length, 2); - note.tags = {...note.tags}..add('C'); - note.tags.add('D'); - note.tags.remove('B'); - + note.apply(tags: {'A', 'C', 'D'}); await NoteStorage().save(note).throwOnError(); var expectedContent = """--- @@ -260,8 +257,10 @@ Hello expect(note.modified, DateTime.parse('2021-07-14T10:14:49Z')); expect(note.created, DateTime.parse('2021-07-14T10:14:49Z')); - note.modified = DateTime.parse('2020-07-14T10:14:49Z'); - note.created = DateTime.parse('2020-06-13T10:14:49Z'); + note.apply( + created: DateTime.parse('2020-06-13T10:14:49Z'), + modified: DateTime.parse('2020-07-14T10:14:49Z'), + ); var expectedContent = """--- bar: Foo diff --git a/test/sorted_notes_folder_test.dart b/test/sorted_notes_folder_test.dart index 148b6b7a..6078e1a1 100644 --- a/test/sorted_notes_folder_test.dart +++ b/test/sorted_notes_folder_test.dart @@ -38,8 +38,10 @@ void main() { folder, p.join(folder.folderPath, "${random.nextInt(1000)}.md"), DateTime.now()); - note.modified = DateTime(2020, 1, 10 + (i * 2)); - note.body = "$i\n"; + note.apply( + modified: DateTime(2020, 1, 10 + (i * 2)), + body: "$i\n", + ); await NoteStorage().save(note).throwOnError(); } await folder.loadRecursively(); @@ -76,7 +78,7 @@ void main() { ); var i = sf.notes.indexWhere((n) => n.body == "1\n"); - sf.notes[i].modified = DateTime(2020, 2, 1); + sf.notes[i].apply(modified: DateTime(2020, 2, 1)); expect(sf.notes[0].body, "1\n"); expect(sf.notes[1].body, "4\n"); @@ -96,8 +98,10 @@ void main() { Note(folder, p.join(folder.folderPath, "new.md"), DateTime.now()); folder.add(note); - note.modified = DateTime(2020, 2, 1); - note.body = "new\n"; + note.apply( + modified: DateTime(2020, 2, 1), + body: "new\n", + ); await NoteStorage().save(note).throwOnError(); expect(sf.notes.length, 6); @@ -121,8 +125,10 @@ void main() { Note(folder, p.join(folder.folderPath, "new.md"), DateTime.now()); folder.add(note); - note.modified = DateTime(2020, 1, 1); - note.body = "new\n"; + note.apply( + modified: DateTime(2020, 1, 1), + body: "new\n", + ); await NoteStorage().save(note).throwOnError(); expect(sf.notes.length, 6); diff --git a/test/sorting_mode_test.dart b/test/sorting_mode_test.dart index f11b2f92..e52ff4c3 100644 --- a/test/sorting_mode_test.dart +++ b/test/sorting_mode_test.dart @@ -24,16 +24,13 @@ void main() { test('Created', () async { var folder = NotesFolderFS(null, '/tmp/', config); var n1 = Note(folder, '/tmp/1.md', DateTime.now()); - n1.created = DateTime(2020, 10, 01); + n1.apply(created: DateTime(2020, 10, 01)); var n2 = Note(folder, '/tmp/2.md', DateTime.now()); - n2.created = DateTime(2020, 10, 02); + n2.apply(created: DateTime(2020, 10, 02)); var n3 = Note(folder, '/tmp/3.md', DateTime.now()); - n3.created = null; - var n4 = Note(folder, '/tmp/4.md', DateTime.now()); - n4.created = null; var notes = [n1, n2, n3, n4]; var sortFn = SortingMode(SortingField.Created, SortingOrder.Descending) @@ -49,16 +46,13 @@ void main() { test('Modified', () async { var folder = NotesFolderFS(null, '/tmp/', config); var n1 = Note(folder, '/tmp/1.md', DateTime.now()); - n1.modified = DateTime(2020, 10, 01); + n1.apply(modified: DateTime(2020, 10, 01)); var n2 = Note(folder, '/tmp/2.md', DateTime.now()); - n2.modified = DateTime(2020, 10, 02); + n2.apply(modified: DateTime(2020, 10, 02)); var n3 = Note(folder, '/tmp/3.md', DateTime.now()); - n3.modified = null; - var n4 = Note(folder, '/tmp/4.md', DateTime.now()); - n4.modified = null; var notes = [n1, n2, n3, n4]; var sortFn = SortingMode(SortingField.Modified, SortingOrder.Descending)