NoteMetaData: Allow tags key to be configured

Fixes #206
This commit is contained in:
Vishesh Handa
2020-08-06 10:55:41 +02:00
parent d091b771d7
commit 619023660f
4 changed files with 90 additions and 9 deletions

View File

@ -44,6 +44,7 @@ settings:
enableHeader: Enable YAML Header
modified: Modified Field
created: Created Field
tags: Tags Field
example:
title: Example Title
titleMetaData:
@ -52,6 +53,8 @@ settings:
fromYaml: From YAML 'title'
exampleTitle: Pigeons
exampleBody: I think they might be evil. Even more evil than penguins.
exampleTag1: Birds
exampleTag2: Evil
privacy: Privacy Policy
terms: Terms and Conditions
experimental:

View File

@ -21,7 +21,7 @@ class NoteSerializationSettings {
String createdKey = Settings.instance.yamlCreatedKey;
String titleKey = "title";
String typeKey = "type";
String tagsKey = "tags";
String tagsKey = Settings.instance.yamlTagsKey;
bool saveTitleAsH1 = Settings.instance.saveTitleInH1;
}
@ -155,14 +155,23 @@ class NoteSerializer implements NoteSerializerInterface {
}
try {
var tags = data.props[settings.tagsKey];
if (tags != null) {
if (tags is YamlList) {
note.tags = tags.map((t) => t.toString()).toSet();
} else if (tags is List) {
note.tags = tags.map((t) => t.toString()).toSet();
} else {
Log.e("Note Tags Decoding Failed: $tags");
var tagKeyOptions = [
"tags",
"categories",
];
for (var possibleKey in tagKeyOptions) {
var tags = data.props[possibleKey];
if (tags != null) {
if (tags is YamlList) {
note.tags = tags.map((t) => t.toString()).toSet();
} else if (tags is List) {
note.tags = tags.map((t) => t.toString()).toSet();
} else {
Log.e("Note Tags Decoding Failed: $tags");
}
settings.tagsKey = possibleKey;
break;
}
}
} catch (e) {

View File

@ -29,6 +29,10 @@ class _NoteMetadataSettingsScreenState
note.body = tr("settings.noteMetaData.exampleBody");
note.created = DateTime.now().add(const Duration(days: -1));
note.modified = DateTime.now();
note.tags = {
tr("settings.noteMetaData.exampleTag1"),
tr("settings.noteMetaData.exampleTag2"),
};
var body = Column(
children: <Widget>[
@ -94,6 +98,23 @@ class _NoteMetadataSettingsScreenState
enabled: Settings.instance.yamlHeaderEnabled,
),
),
ProOverlay(
child: ListPreference(
title: tr("settings.noteMetaData.tags"),
options: [
"tags",
"categories",
],
currentOption: Settings.instance.yamlTagsKey,
onChange: (String newVal) {
setState(() {
Settings.instance.yamlTagsKey = newVal;
Settings.instance.save();
});
},
enabled: Settings.instance.yamlHeaderEnabled,
),
),
ProOverlay(
child: ListPreference(
title: tr("settings.noteMetaData.titleMetaData.title"),
@ -192,7 +213,10 @@ class NoteInputExample extends StatelessWidget {
autofocus: false,
onChanged: () {},
),
Container(height: 8.0),
TagsWidget(note.tags),
],
crossAxisAlignment: CrossAxisAlignment.start,
),
),
_HeaderText(note.fileName, Alignment.topRight),
@ -225,3 +249,44 @@ class _HeaderText extends StatelessWidget {
);
}
}
class _Tag extends StatelessWidget {
final String text;
_Tag(this.text);
@override
Widget build(BuildContext context) {
var theme = Theme.of(context);
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(50),
color: theme.scaffoldBackgroundColor,
boxShadow: [
const BoxShadow(color: Colors.grey, spreadRadius: 1),
],
),
padding: const EdgeInsets.all(8.0),
child: Text(text, style: theme.textTheme.button),
);
}
}
class TagsWidget extends StatelessWidget {
final Set<String> tags;
TagsWidget(this.tags);
@override
Widget build(BuildContext context) {
return Wrap(
children: [
for (var tagText in tags) _Tag(tagText),
],
alignment: WrapAlignment.start,
spacing: 8.0,
runSpacing: 8.0,
);
}
}

View File

@ -22,6 +22,7 @@ class Settings {
String yamlModifiedKey = "modified";
String yamlCreatedKey = "created";
String yamlTagsKey = "tags";
bool yamlHeaderEnabled = true;
String defaultNewNoteFolderSpec = "";
@ -71,6 +72,7 @@ class Settings {
yamlModifiedKey = pref.getString("yamlModifiedKey") ?? yamlModifiedKey;
yamlCreatedKey = pref.getString("yamlCreatedKey") ?? yamlCreatedKey;
yamlTagsKey = pref.getString("yamlTagsKey") ?? yamlTagsKey;
yamlHeaderEnabled = pref.getBool("yamlHeaderEnabled") ?? yamlHeaderEnabled;
defaultNewNoteFolderSpec =
@ -146,6 +148,7 @@ class Settings {
pref, "yamlModifiedKey", yamlModifiedKey, defaultSet.yamlModifiedKey);
_setString(
pref, "yamlCreatedKey", yamlCreatedKey, defaultSet.yamlCreatedKey);
_setString(pref, "yamlTagsKey", yamlTagsKey, defaultSet.yamlTagsKey);
_setBool(pref, "yamlHeaderEnabled", yamlHeaderEnabled,
defaultSet.yamlHeaderEnabled);
_setString(pref, "defaultNewNoteFolderSpec", defaultNewNoteFolderSpec,
@ -232,6 +235,7 @@ class Settings {
"collectCrashReports": collectCrashReports.toString(),
"yamlModifiedKey": yamlModifiedKey,
"yamlCreatedKey": yamlCreatedKey,
"yamlTagsKey": yamlTagsKey,
"yamlHeaderEnabled": yamlHeaderEnabled.toString(),
"defaultNewNoteFolderSpec": defaultNewNoteFolderSpec,
"journalEditordefaultNewNoteFolderSpec":