From 59ab66844f7dae0faa448e0f7e2193ecf0fc32cf Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Tue, 12 May 2020 22:53:33 +0200 Subject: [PATCH] Note: Allow tags to be read and serialized This is just exposing the tags on the Note class. Not making it available through any interface. --- lib/core/note.dart | 12 +++++++++++ lib/core/note_serializer.dart | 18 +++++++++++++++++ test/note_test.dart | 38 +++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) diff --git a/lib/core/note.dart b/lib/core/note.dart index ddbab945..65b079f8 100644 --- a/lib/core/note.dart +++ b/lib/core/note.dart @@ -36,6 +36,7 @@ class Note with NotesNotifier { DateTime _modified; String _body = ""; NoteType _type = NoteType.Unknown; + List _tags = []; MdYamlDoc _data = MdYamlDoc(); NoteSerializer noteSerializer = NoteSerializer(); @@ -124,6 +125,17 @@ class Note with NotesNotifier { _notifyModified(); } + List get tags { + return _tags; + } + + set tags(List tags) { + if (!canHaveMetadata) return; + + _tags = tags; + _notifyModified(); + } + bool get canHaveMetadata { return Settings.instance.yamlHeaderEnabled; } diff --git a/lib/core/note_serializer.dart b/lib/core/note_serializer.dart index d0026a83..11237150 100644 --- a/lib/core/note_serializer.dart +++ b/lib/core/note_serializer.dart @@ -1,6 +1,8 @@ import 'package:gitjournal/utils/datetime.dart'; import 'package:gitjournal/settings.dart'; import 'package:flutter_emoji/flutter_emoji.dart'; +import 'package:gitjournal/utils/logger.dart'; +import 'package:yaml/yaml.dart'; import 'md_yaml_doc.dart'; import 'note.dart'; @@ -17,6 +19,7 @@ class NoteSerializationSettings { String createdKey = "created"; String titleKey = "title"; String typeKey = "type"; + String tagsKey = "tags"; } class NoteSerializer implements NoteSerializerInterface { @@ -54,6 +57,12 @@ class NoteSerializer implements NoteSerializerInterface { data.props.remove(settings.typeKey); } + if (note.tags.isEmpty) { + data.props.remove(settings.tagsKey); + } else { + data.props[settings.tagsKey] = note.tags; + } + data.body = emojiParser.unemojify(note.body); } @@ -95,5 +104,14 @@ class NoteSerializer implements NoteSerializerInterface { note.type = NoteType.Unknown; break; } + + try { + var tags = data.props[settings.tagsKey] as YamlList; + if (tags != null) { + note.tags = tags.map((t) => t.toString()).toList(); + } + } catch (e) { + Log.e("Note Decoding Failed: $e"); + } } } diff --git a/test/note_test.dart b/test/note_test.dart index 1d5330d8..f53a7a52 100644 --- a/test/note_test.dart +++ b/test/note_test.dart @@ -71,6 +71,44 @@ title: Foo mod: 2019-12-02T04:00:00+00:00 --- +Hello"""; + + var actualContent = File(notePath).readAsStringSync(); + expect(actualContent, equals(expectedContent)); + }); + + test('Should read and write tags', () async { + var content = """--- +title: Foo +modified: 2017-02-15T22:41:19+01:00 +tags: [A, B] +--- + +Hello"""; + + var notePath = p.join(tempDir.path, "note5.md"); + File(notePath).writeAsString(content); + + var parentFolder = NotesFolderFS(null, tempDir.path); + var note = Note(parentFolder, notePath); + await note.load(); + + expect(note.tags[0], 'A'); + expect(note.tags[1], 'B'); + expect(note.tags.length, 2); + + note.tags = [...note.tags]..add('C'); + note.tags.add('D'); + note.tags.remove('B'); + + await note.save(); + + var expectedContent = """--- +title: Foo +modified: 2017-02-15T22:41:19+01:00 +tags: [A, C, D] +--- + Hello"""; var actualContent = File(notePath).readAsStringSync();