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

View File

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

View File

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