Note: Allow extra yaml metadata to be set

This way if there is existing metadata, we aren't removing it when
writing the notes. The order isn't preserved, but meh, that's fine for
now.
This commit is contained in:
Vishesh Handa
2019-01-15 23:27:32 +01:00
parent 4ba27e9b16
commit a5ac547ecc

View File

@ -9,12 +9,15 @@ class Note implements Comparable {
DateTime created; DateTime created;
String body; String body;
Note({this.created, this.body, this.id}); Map<String, dynamic> extraProperties = new Map<String, dynamic>();
Note({this.created, this.body, this.id, this.extraProperties});
factory Note.fromJson(Map<String, dynamic> json) { factory Note.fromJson(Map<String, dynamic> json) {
String id; String id;
if (json.containsKey("id")) { if (json.containsKey("id")) {
id = json["id"].toString(); id = json["id"].toString();
json.remove("id");
} }
DateTime created; DateTime created;
@ -36,8 +39,11 @@ class Note implements Comparable {
// FIXME: Get created from file system or from git! // FIXME: Get created from file system or from git!
if (created == null) { if (created == null) {
// FIXME: make this 0
created = DateTime.now(); created = DateTime.now();
} }
json.remove("created");
} }
if (id == null && created != null) { if (id == null && created != null) {
@ -47,25 +53,29 @@ class Note implements Comparable {
String body = ""; String body = "";
if (json.containsKey("body")) { if (json.containsKey("body")) {
body = json['body']; body = json['body'];
json.remove("body");
} }
return new Note( return new Note(
id: id, id: id,
created: created, created: created,
body: body, body: body,
extraProperties: json,
); );
} }
Map<String, dynamic> toJson() { Map<String, dynamic> toJson() {
return { var json = Map<String, dynamic>.from(extraProperties);
"created": toIso8601WithTimezone(created), json['created'] = toIso8601WithTimezone(created);
"body": body, json['body'] = body;
"id": id, json['id'] = id;
};
return json;
} }
@override @override
int get hashCode => id.hashCode ^ created.hashCode ^ body.hashCode; int get hashCode =>
id.hashCode ^ created.hashCode ^ body.hashCode ^ extraProperties.hashCode;
@override @override
bool operator ==(Object other) => bool operator ==(Object other) =>
@ -74,11 +84,12 @@ class Note implements Comparable {
runtimeType == other.runtimeType && runtimeType == other.runtimeType &&
id == other.id && id == other.id &&
body == other.body && body == other.body &&
created == other.created; created == other.created &&
extraProperties == other.extraProperties;
@override @override
String toString() { String toString() {
return 'Note{id: $id, body: $body, created: $created}'; return 'Note{id: $id, body: $body, created: $created, extraProperties: $extraProperties}';
} }
@override @override