Add/Remove individual notes from disk

Instead of rewriting all of them on save.
This commit is contained in:
Vishesh Handa
2018-06-03 19:34:46 +02:00
parent fd98ef1048
commit 69998c37f9
2 changed files with 25 additions and 8 deletions

View File

@ -59,4 +59,25 @@ class FileStorage {
return dir;
}
Future<bool> 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<bool> 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;
}
}

View File

@ -56,36 +56,32 @@ class StateContainerState extends State<StateContainer> {
});
}).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);
});
}