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.
This commit is contained in:
Vishesh Handa
2020-05-12 22:53:33 +02:00
parent faf561f104
commit 59ab66844f
3 changed files with 68 additions and 0 deletions

View File

@ -36,6 +36,7 @@ class Note with NotesNotifier {
DateTime _modified; DateTime _modified;
String _body = ""; String _body = "";
NoteType _type = NoteType.Unknown; NoteType _type = NoteType.Unknown;
List<String> _tags = [];
MdYamlDoc _data = MdYamlDoc(); MdYamlDoc _data = MdYamlDoc();
NoteSerializer noteSerializer = NoteSerializer(); NoteSerializer noteSerializer = NoteSerializer();
@ -124,6 +125,17 @@ class Note with NotesNotifier {
_notifyModified(); _notifyModified();
} }
List<String> get tags {
return _tags;
}
set tags(List<String> tags) {
if (!canHaveMetadata) return;
_tags = tags;
_notifyModified();
}
bool get canHaveMetadata { bool get canHaveMetadata {
return Settings.instance.yamlHeaderEnabled; return Settings.instance.yamlHeaderEnabled;
} }

View File

@ -1,6 +1,8 @@
import 'package:gitjournal/utils/datetime.dart'; import 'package:gitjournal/utils/datetime.dart';
import 'package:gitjournal/settings.dart'; import 'package:gitjournal/settings.dart';
import 'package:flutter_emoji/flutter_emoji.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 'md_yaml_doc.dart';
import 'note.dart'; import 'note.dart';
@ -17,6 +19,7 @@ class NoteSerializationSettings {
String createdKey = "created"; String createdKey = "created";
String titleKey = "title"; String titleKey = "title";
String typeKey = "type"; String typeKey = "type";
String tagsKey = "tags";
} }
class NoteSerializer implements NoteSerializerInterface { class NoteSerializer implements NoteSerializerInterface {
@ -54,6 +57,12 @@ class NoteSerializer implements NoteSerializerInterface {
data.props.remove(settings.typeKey); 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); data.body = emojiParser.unemojify(note.body);
} }
@ -95,5 +104,14 @@ class NoteSerializer implements NoteSerializerInterface {
note.type = NoteType.Unknown; note.type = NoteType.Unknown;
break; 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");
}
} }
} }

View File

@ -71,6 +71,44 @@ title: Foo
mod: 2019-12-02T04:00:00+00:00 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"""; Hello""";
var actualContent = File(notePath).readAsStringSync(); var actualContent = File(notePath).readAsStringSync();