From ef2ad7466f755b2e39b3a2c9ce8c68e0a4806cb3 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Thu, 26 Sep 2019 15:51:16 +0200 Subject: [PATCH] Port Serializers to handle NoteData This completely breaks the app, as it is part of a large refactor to make Note loading asyncrhnous and not block. Additionally this removed JSON serialization for Notes as that isn't something we care about any more. --- lib/storage/serializers.dart | 60 +++++++++++++++++++----------------- test/serializers_test.dart | 17 ++-------- 2 files changed, 34 insertions(+), 43 deletions(-) diff --git a/lib/storage/serializers.dart b/lib/storage/serializers.dart index d569685a..970a1d9b 100644 --- a/lib/storage/serializers.dart +++ b/lib/storage/serializers.dart @@ -1,30 +1,38 @@ -import 'dart:convert'; - import 'package:fimber/fimber.dart'; -import 'package:journal/note.dart'; import 'package:yaml/yaml.dart'; -abstract class NoteSerializer { - String encode(Note note); - Note decode(String str); +class NoteData { + String body; + Map props = {}; + + NoteData(this.body, this.props); + + @override + int get hashCode => body.hashCode ^ props.hashCode; + + @override + bool operator ==(Object other) => + identical(this, other) || + other is NoteData && + runtimeType == other.runtimeType && + body == other.body && + _equalMaps(props, other.props); + + static bool _equalMaps(Map a, Map b) { + if (a.length != b.length) return false; + return a.keys + .every((dynamic key) => b.containsKey(key) && a[key] == b[key]); + } } -class JsonNoteSerializer implements NoteSerializer { - @override - Note decode(String str) { - final json = JsonDecoder().convert(str); - return Note.fromJson(json); - } - - @override - String encode(Note note) { - return JsonEncoder().convert(note.toJson()); - } +abstract class NoteSerializer { + String encode(NoteData note); + NoteData decode(String str); } class MarkdownYAMLSerializer implements NoteSerializer { @override - Note decode(String str) { + NoteData decode(String str) { if (str.startsWith("---\n")) { var parts = str.split("---\n"); var map = {}; @@ -41,27 +49,21 @@ class MarkdownYAMLSerializer implements NoteSerializer { Fimber.d( 'MarkdownYAMLSerializer::decode("$yamlText") -> ${err.toString()}'); } - map['body'] = parts[2].trimLeft(); + var body = parts[2].trimLeft(); - return Note.fromJson(map); + return NoteData(body, map); } - var map = {}; - map['body'] = str; - return Note.fromJson(map); + return NoteData(str, {}); } @override - String encode(Note note) { + String encode(NoteData note) { const serparator = '---\n'; var str = ""; str += serparator; - var metadata = note.toJson(); - metadata.remove('body'); - metadata.remove('filePath'); - - str += toYAML(metadata); + str += toYAML(note.props); str += serparator; str += '\n'; str += note.body; diff --git a/test/serializers_test.dart b/test/serializers_test.dart index 39e650a6..8b65087c 100644 --- a/test/serializers_test.dart +++ b/test/serializers_test.dart @@ -1,5 +1,5 @@ -import 'package:journal/note.dart'; import 'package:journal/storage/serializers.dart'; +import 'package:journal/datetime_utils.dart'; import 'package:test/test.dart'; DateTime nowWithoutMicro() { @@ -9,25 +9,14 @@ DateTime nowWithoutMicro() { void main() { group('Serializers', () { - var note = Note( - filePath: "2", body: "This is the body", created: nowWithoutMicro()); - - test('JSON Serializer', () { - var serializer = JsonNoteSerializer(); - var str = serializer.encode(note); - var note2 = serializer.decode(str); - - expect(note2, note); - }); + var created = toIso8601WithTimezone(nowWithoutMicro()); + var note = NoteData("This is the body", {"created": created}); test('Markdown Serializer', () { var serializer = MarkdownYAMLSerializer(); var str = serializer.encode(note); var note2 = serializer.decode(str); - // The YAML seriazlier loses the fileName by design - note2.filePath = note.filePath; - expect(note2, note); });