Note: Setting the 'created' property should update the NoteData

We shouldn't just do it when saving the note, this way when viewing the
raw editor, the created field is show for new notes.
This commit is contained in:
Vishesh Handa
2019-10-11 02:18:34 +02:00
parent 5b25fd56c1
commit 06824ccee3
2 changed files with 23 additions and 12 deletions

View File

@ -12,7 +12,7 @@ enum NoteLoadState {
class Note implements Comparable<Note> {
String filePath = "";
DateTime created;
DateTime _created;
NoteData _data = NoteData();
DateTime _fileLastModified;
@ -21,7 +21,21 @@ class Note implements Comparable<Note> {
var _serializer = MarkdownYAMLSerializer();
Note([this.filePath]) {
created = created ?? DateTime(0, 0, 0, 0, 0, 0, 0, 0);
_created = _created ?? DateTime(0, 0, 0, 0, 0, 0, 0, 0);
}
DateTime get created {
return _created;
}
set created(DateTime dt) {
_created = dt;
if (hasValidDate()) {
_data.props['created'] = toIso8601WithTimezone(created);
} else {
_data.props.remove('created');
}
}
String get body {
@ -42,24 +56,24 @@ class Note implements Comparable<Note> {
if (data.props.containsKey("created")) {
var createdStr = data.props['created'].toString();
try {
created = DateTime.parse(data.props['created']).toLocal();
_created = DateTime.parse(data.props['created']).toLocal();
} catch (ex) {
// Ignore it
}
if (created == null) {
if (_created == null) {
var regex = RegExp(
r"(\d{4})-(\d{2})-(\d{2})T(\d{2})\:(\d{2})\:(\d{2})\+(\d{2})\:(\d{2})");
if (regex.hasMatch(createdStr)) {
// FIXME: Handle the timezone!
createdStr = createdStr.substring(0, 19);
created = DateTime.parse(createdStr);
_created = DateTime.parse(createdStr);
}
}
}
if (created == null) {
created = DateTime(0, 0, 0, 0, 0, 0, 0, 0);
if (_created == null) {
_created = DateTime(0, 0, 0, 0, 0, 0, 0, 0);
}
}
@ -102,10 +116,6 @@ class Note implements Comparable<Note> {
assert(data.body != null);
assert(data.props != null);
if (hasValidDate()) {
data.props['created'] = toIso8601WithTimezone(created);
}
var file = File(filePath);
var contents = _serializer.encode(data);
await file.writeAsString(contents);

View File

@ -26,7 +26,8 @@ void main() {
n2Path = p.join(tempDir.path, "2.md");
var n1 = Note(n1Path);
n1.data = NoteData("test", props);
n1.body = "test";
n1.created = dt;
var n2 = Note(n2Path);
n2.data = NoteData("test2", props);