mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-30 03:19:11 +08:00
FileStorage: Use the YAML Markdown serializer
Instead of saving stuff in json.
This commit is contained in:
@ -1,16 +1,20 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:journal/serializers.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
|
||||
import './note.dart';
|
||||
|
||||
class FileStorage {
|
||||
final Future<Directory> Function() getDirectory;
|
||||
final NoteSerializer noteSerializer;
|
||||
|
||||
const FileStorage({@required this.getDirectory});
|
||||
const FileStorage({
|
||||
@required this.getDirectory,
|
||||
@required this.noteSerializer,
|
||||
});
|
||||
|
||||
Future<List<Note>> loadNotes() async {
|
||||
final dir = await getDirectory();
|
||||
@ -31,8 +35,7 @@ class FileStorage {
|
||||
}
|
||||
var file = entity as File;
|
||||
final string = await file.readAsString();
|
||||
final json = JsonDecoder().convert(string);
|
||||
return new Note.fromJson(json);
|
||||
return noteSerializer.decode(string);
|
||||
}
|
||||
|
||||
Future<Directory> saveNotes(List<Note> notes) async {
|
||||
@ -44,7 +47,7 @@ class FileStorage {
|
||||
var filePath = p.join(dir.path, note.id);
|
||||
|
||||
var file = new File(filePath);
|
||||
var contents = JsonEncoder().convert(note.toJson());
|
||||
var contents = noteSerializer.encode(note);
|
||||
await file.writeAsString(contents);
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,7 @@ import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:journal/serializers.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
import 'package:uuid/uuid.dart';
|
||||
@ -19,14 +20,10 @@ Future<Directory> getNotesDir() async {
|
||||
}
|
||||
|
||||
class StateContainer extends StatefulWidget {
|
||||
final FileStorage fileStorage;
|
||||
final Widget child;
|
||||
|
||||
StateContainer({
|
||||
@required this.child,
|
||||
this.fileStorage = const FileStorage(
|
||||
getDirectory: getNotesDir,
|
||||
),
|
||||
});
|
||||
|
||||
static StateContainerState of(BuildContext context) {
|
||||
@ -43,12 +40,18 @@ class StateContainer extends StatefulWidget {
|
||||
|
||||
class StateContainerState extends State<StateContainer> {
|
||||
AppState appState = AppState.loading();
|
||||
FileStorage fileStorage;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
widget.fileStorage.loadNotes().then((loadedNotes) {
|
||||
fileStorage = new FileStorage(
|
||||
getDirectory: getNotesDir,
|
||||
noteSerializer: new MarkdownYAMLSerializer(),
|
||||
);
|
||||
|
||||
fileStorage.loadNotes().then((loadedNotes) {
|
||||
setState(() {
|
||||
appState = AppState(notes: loadedNotes);
|
||||
});
|
||||
@ -65,7 +68,7 @@ class StateContainerState extends State<StateContainer> {
|
||||
void setState(VoidCallback fn) {
|
||||
super.setState(fn);
|
||||
|
||||
widget.fileStorage.saveNotes(appState.notes);
|
||||
fileStorage.saveNotes(appState.notes);
|
||||
}
|
||||
|
||||
void addNote(Note note) {
|
||||
|
Reference in New Issue
Block a user