mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-28 09:47:35 +08:00
Add/Remove individual notes from disk
Instead of rewriting all of them on save.
This commit is contained in:
@ -59,4 +59,25 @@ class FileStorage {
|
|||||||
|
|
||||||
return dir;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -56,36 +56,32 @@ class StateContainerState extends State<StateContainer> {
|
|||||||
});
|
});
|
||||||
}).catchError((err) {
|
}).catchError((err) {
|
||||||
setState(() {
|
setState(() {
|
||||||
print("Got Error");
|
print("Load Notes Error:");
|
||||||
print(err);
|
print(err);
|
||||||
appState.isLoading = false;
|
appState.isLoading = false;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
|
||||||
void setState(VoidCallback fn) {
|
|
||||||
super.setState(fn);
|
|
||||||
|
|
||||||
fileStorage.saveNotes(appState.notes);
|
|
||||||
}
|
|
||||||
|
|
||||||
void addNote(Note note) {
|
void addNote(Note note) {
|
||||||
setState(() {
|
setState(() {
|
||||||
note.id = new Uuid().v4();
|
note.id = new Uuid().v4();
|
||||||
appState.notes.insert(0, note);
|
appState.notes.insert(0, note);
|
||||||
|
fileStorage.addNote(note);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void removeNote(Note note) {
|
void removeNote(Note note) {
|
||||||
setState(() {
|
setState(() {
|
||||||
appState.notes.remove(note);
|
appState.notes.remove(note);
|
||||||
|
fileStorage.removeNote(note);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void insertNote(int index, Note note) {
|
void insertNote(int index, Note note) {
|
||||||
setState(() {
|
setState(() {
|
||||||
appState.notes.insert(index, note);
|
appState.notes.insert(index, note);
|
||||||
|
fileStorage.addNote(note);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user