mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-28 01:45:55 +08:00
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.
This commit is contained in:
@ -1,30 +1,38 @@
|
|||||||
import 'dart:convert';
|
|
||||||
|
|
||||||
import 'package:fimber/fimber.dart';
|
import 'package:fimber/fimber.dart';
|
||||||
import 'package:journal/note.dart';
|
|
||||||
import 'package:yaml/yaml.dart';
|
import 'package:yaml/yaml.dart';
|
||||||
|
|
||||||
abstract class NoteSerializer {
|
class NoteData {
|
||||||
String encode(Note note);
|
String body;
|
||||||
Note decode(String str);
|
Map<String, dynamic> 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 {
|
abstract class NoteSerializer {
|
||||||
@override
|
String encode(NoteData note);
|
||||||
Note decode(String str) {
|
NoteData decode(String str);
|
||||||
final json = JsonDecoder().convert(str);
|
|
||||||
return Note.fromJson(json);
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
String encode(Note note) {
|
|
||||||
return JsonEncoder().convert(note.toJson());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class MarkdownYAMLSerializer implements NoteSerializer {
|
class MarkdownYAMLSerializer implements NoteSerializer {
|
||||||
@override
|
@override
|
||||||
Note decode(String str) {
|
NoteData decode(String str) {
|
||||||
if (str.startsWith("---\n")) {
|
if (str.startsWith("---\n")) {
|
||||||
var parts = str.split("---\n");
|
var parts = str.split("---\n");
|
||||||
var map = <String, dynamic>{};
|
var map = <String, dynamic>{};
|
||||||
@ -41,27 +49,21 @@ class MarkdownYAMLSerializer implements NoteSerializer {
|
|||||||
Fimber.d(
|
Fimber.d(
|
||||||
'MarkdownYAMLSerializer::decode("$yamlText") -> ${err.toString()}');
|
'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 = <String, dynamic>{};
|
return NoteData(str, <String, dynamic>{});
|
||||||
map['body'] = str;
|
|
||||||
return Note.fromJson(map);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String encode(Note note) {
|
String encode(NoteData note) {
|
||||||
const serparator = '---\n';
|
const serparator = '---\n';
|
||||||
var str = "";
|
var str = "";
|
||||||
str += serparator;
|
str += serparator;
|
||||||
|
|
||||||
var metadata = note.toJson();
|
str += toYAML(note.props);
|
||||||
metadata.remove('body');
|
|
||||||
metadata.remove('filePath');
|
|
||||||
|
|
||||||
str += toYAML(metadata);
|
|
||||||
str += serparator;
|
str += serparator;
|
||||||
str += '\n';
|
str += '\n';
|
||||||
str += note.body;
|
str += note.body;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import 'package:journal/note.dart';
|
|
||||||
import 'package:journal/storage/serializers.dart';
|
import 'package:journal/storage/serializers.dart';
|
||||||
|
import 'package:journal/datetime_utils.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
DateTime nowWithoutMicro() {
|
DateTime nowWithoutMicro() {
|
||||||
@ -9,25 +9,14 @@ DateTime nowWithoutMicro() {
|
|||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
group('Serializers', () {
|
group('Serializers', () {
|
||||||
var note = Note(
|
var created = toIso8601WithTimezone(nowWithoutMicro());
|
||||||
filePath: "2", body: "This is the body", created: nowWithoutMicro());
|
var note = NoteData("This is the body", {"created": created});
|
||||||
|
|
||||||
test('JSON Serializer', () {
|
|
||||||
var serializer = JsonNoteSerializer();
|
|
||||||
var str = serializer.encode(note);
|
|
||||||
var note2 = serializer.decode(str);
|
|
||||||
|
|
||||||
expect(note2, note);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Markdown Serializer', () {
|
test('Markdown Serializer', () {
|
||||||
var serializer = MarkdownYAMLSerializer();
|
var serializer = MarkdownYAMLSerializer();
|
||||||
var str = serializer.encode(note);
|
var str = serializer.encode(note);
|
||||||
var note2 = serializer.decode(str);
|
var note2 = serializer.decode(str);
|
||||||
|
|
||||||
// The YAML seriazlier loses the fileName by design
|
|
||||||
note2.filePath = note.filePath;
|
|
||||||
|
|
||||||
expect(note2, note);
|
expect(note2, note);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user