diff --git a/lib/file_storage.dart b/lib/file_storage.dart index 3e394e83..e1b1de6b 100644 --- a/lib/file_storage.dart +++ b/lib/file_storage.dart @@ -59,4 +59,25 @@ class FileStorage { return dir; } + + Future addNote(Note note) async { + final dir = await getDirectory(); + var filePath = p.join(dir.path, fileNameGenerator(note)); + + var file = new File(filePath); + var contents = noteSerializer.encode(note); + await file.writeAsString(contents); + + return true; + } + + Future removeNote(Note note) async { + final dir = await getDirectory(); + var filePath = p.join(dir.path, fileNameGenerator(note)); + + var file = new File(filePath); + await file.delete(); + + return true; + } } diff --git a/lib/state_container.dart b/lib/state_container.dart index 8844dbd2..e33b8e9c 100644 --- a/lib/state_container.dart +++ b/lib/state_container.dart @@ -56,36 +56,32 @@ class StateContainerState extends State { }); }).catchError((err) { setState(() { - print("Got Error"); + print("Load Notes Error:"); print(err); appState.isLoading = false; }); }); } - @override - void setState(VoidCallback fn) { - super.setState(fn); - - fileStorage.saveNotes(appState.notes); - } - void addNote(Note note) { setState(() { note.id = new Uuid().v4(); appState.notes.insert(0, note); + fileStorage.addNote(note); }); } void removeNote(Note note) { setState(() { appState.notes.remove(note); + fileStorage.removeNote(note); }); } void insertNote(int index, Note note) { setState(() { appState.notes.insert(index, note); + fileStorage.addNote(note); }); }