Simplify code by using pathSpec

This commit is contained in:
Vishesh Handa
2020-03-21 00:52:54 +01:00
parent 613afee32e
commit a99eca9131
3 changed files with 20 additions and 15 deletions

View File

@ -94,29 +94,27 @@ class GitNoteRepository {
return NoteRepoResult(noteFilePath: newFullPath, error: false);
}
Future<NoteRepoResult> removeNote(String noteFilePath) async {
var pathSpec = noteFilePath.replaceFirst(gitDirPath, "").substring(1);
Future<NoteRepoResult> removeNote(Note note) async {
// We are not calling note.remove() as gitRm will also remove the file
await _gitRepo.rm(pathSpec);
var spec = note.pathSpec();
await _gitRepo.rm(spec);
await _gitRepo.commit(
message: "Removed Note " + pathSpec,
message: "Removed Note " + spec,
);
return NoteRepoResult(noteFilePath: noteFilePath, error: false);
return NoteRepoResult(noteFilePath: note.filePath, error: false);
}
Future<NoteRepoResult> removeFolder(String folderPath) async {
var pathSpec = folderPath.replaceFirst(gitDirPath, "").substring(1);
await _gitRepo.rm(pathSpec);
Future<NoteRepoResult> removeFolder(NotesFolderFS folder) async {
var spec = folder.pathSpec();
await _gitRepo.rm(spec);
await _gitRepo.commit(
message: "Removed Folder " + pathSpec,
message: "Removed Folder " + spec,
);
await Directory(folderPath).delete(recursive: true);
await Directory(folder.folderPath).delete(recursive: true);
return NoteRepoResult(noteFilePath: folderPath, error: false);
return NoteRepoResult(noteFilePath: folder.folderPath, error: false);
}
Future<NoteRepoResult> resetLastCommit() async {

View File

@ -259,4 +259,11 @@ class Note with NotesNotifier {
notifyModifiedListeners(this);
notifyListeners();
}
String pathSpec() {
if (parent == null) {
return fileName;
}
return p.join(parent.pathSpec(), fileName);
}
}

View File

@ -140,7 +140,7 @@ class StateContainer with ChangeNotifier {
Fimber.d("Removing Folder: " + folder.folderPath);
folder.parentFS.removeFolder(folder);
_gitRepo.removeFolder(folder.folderPath).then((NoteRepoResult _) {
_gitRepo.removeFolder(folder).then((NoteRepoResult _) {
_syncNotes();
});
});
@ -199,7 +199,7 @@ class StateContainer with ChangeNotifier {
return _opLock.synchronized(() async {
// FIXME: What if the Note hasn't yet been saved?
note.parent.remove(note);
_gitRepo.removeNote(note.filePath).then((NoteRepoResult _) async {
_gitRepo.removeNote(note).then((NoteRepoResult _) async {
// FIXME: Is there a way of figuring this amount dynamically?
// The '4 seconds' is taken from snack_bar.dart -> _kSnackBarDisplayDuration
// We wait an aritfical amount of time, so that the user has a change to undo