Note: Make tags a set instead of a list

This way we don't need to check for duplicates
This commit is contained in:
Vishesh Handa
2020-05-12 23:33:57 +02:00
parent e7cae7b07a
commit 52fd859990
3 changed files with 8 additions and 8 deletions

View File

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

View File

@ -60,7 +60,7 @@ class NoteSerializer implements NoteSerializerInterface {
if (note.tags.isEmpty) {
data.props.remove(settings.tagsKey);
} else {
data.props[settings.tagsKey] = note.tags;
data.props[settings.tagsKey] = note.tags.toList();
}
data.body = emojiParser.unemojify(note.body);
@ -108,7 +108,7 @@ class NoteSerializer implements NoteSerializerInterface {
try {
var tags = data.props[settings.tagsKey] as YamlList;
if (tags != null) {
note.tags = tags.map((t) => t.toString()).toList();
note.tags = tags.map((t) => t.toString()).toSet();
}
} catch (e) {
Log.e("Note Decoding Failed: $e");

View File

@ -93,11 +93,11 @@ Hello""";
var note = Note(parentFolder, notePath);
await note.load();
expect(note.tags[0], 'A');
expect(note.tags[1], 'B');
expect(note.tags.contains('A'), true);
expect(note.tags.contains('B'), true);
expect(note.tags.length, 2);
note.tags = [...note.tags]..add('C');
note.tags = {...note.tags}..add('C');
note.tags.add('D');
note.tags.remove('B');