mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-07-18 03:10:28 +08:00
118 lines
3.1 KiB
Dart
118 lines
3.1 KiB
Dart
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';
|
|
|
|
abstract class NoteSerializerInterface {
|
|
void encode(Note note, MdYamlDoc data);
|
|
void decode(MdYamlDoc data, Note note);
|
|
}
|
|
|
|
var emojiParser = EmojiParser();
|
|
|
|
class NoteSerializationSettings {
|
|
String modifiedKey = Settings.instance.yamlModifiedKey;
|
|
String createdKey = "created";
|
|
String titleKey = "title";
|
|
String typeKey = "type";
|
|
String tagsKey = "tags";
|
|
}
|
|
|
|
class NoteSerializer implements NoteSerializerInterface {
|
|
var settings = NoteSerializationSettings();
|
|
|
|
@override
|
|
void encode(Note note, MdYamlDoc data) {
|
|
if (note.created != null) {
|
|
data.props[settings.createdKey] = toIso8601WithTimezone(note.created);
|
|
} else {
|
|
data.props.remove(settings.createdKey);
|
|
}
|
|
|
|
if (note.modified != null) {
|
|
data.props[settings.modifiedKey] = toIso8601WithTimezone(note.modified);
|
|
} else {
|
|
data.props.remove(settings.modifiedKey);
|
|
}
|
|
|
|
if (note.title != null) {
|
|
var title = note.title.trim();
|
|
if (title.isNotEmpty) {
|
|
data.props[settings.titleKey] = emojiParser.unemojify(note.title);
|
|
} else {
|
|
data.props.remove(settings.titleKey);
|
|
}
|
|
} else {
|
|
data.props.remove(settings.titleKey);
|
|
}
|
|
|
|
if (note.type != NoteType.Unknown) {
|
|
var type = note.type.toString().substring(9); // Remove "NoteType."
|
|
data.props[settings.typeKey] = type;
|
|
} else {
|
|
data.props.remove(settings.typeKey);
|
|
}
|
|
|
|
if (note.tags.isEmpty) {
|
|
data.props.remove(settings.tagsKey);
|
|
} else {
|
|
data.props[settings.tagsKey] = note.tags.toList();
|
|
}
|
|
|
|
data.body = emojiParser.unemojify(note.body);
|
|
}
|
|
|
|
@override
|
|
void decode(MdYamlDoc data, Note note) {
|
|
var modifiedKeyOptions = [
|
|
"modified",
|
|
"mod",
|
|
"lastModified",
|
|
"lastMod",
|
|
"lastmodified",
|
|
"lastmod",
|
|
];
|
|
for (var i = 0; i < modifiedKeyOptions.length; i++) {
|
|
var possibleKey = modifiedKeyOptions[i];
|
|
var modifiedVal = data.props[possibleKey];
|
|
if (modifiedVal != null) {
|
|
note.modified = parseDateTime(modifiedVal.toString());
|
|
settings.modifiedKey = possibleKey;
|
|
break;
|
|
}
|
|
}
|
|
|
|
note.body = emojiParser.emojify(data.body);
|
|
note.created = parseDateTime(data.props[settings.createdKey]?.toString());
|
|
|
|
var title = data.props[settings.titleKey]?.toString() ?? "";
|
|
note.title = emojiParser.emojify(title);
|
|
|
|
var type = data.props[settings.typeKey];
|
|
switch (type) {
|
|
case "Checklist":
|
|
note.type = NoteType.Checklist;
|
|
break;
|
|
case "Journal":
|
|
note.type = NoteType.Journal;
|
|
break;
|
|
default:
|
|
note.type = NoteType.Unknown;
|
|
break;
|
|
}
|
|
|
|
try {
|
|
var tags = data.props[settings.tagsKey] as YamlList;
|
|
if (tags != null) {
|
|
note.tags = tags.map((t) => t.toString()).toSet();
|
|
}
|
|
} catch (e) {
|
|
Log.e("Note Decoding Failed: $e");
|
|
}
|
|
}
|
|
}
|