From a99eca913127f1e14537607791904e824c6c5a7d Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Sat, 21 Mar 2020 00:52:54 +0100 Subject: [PATCH] Simplify code by using pathSpec --- lib/core/git_repo.dart | 24 +++++++++++------------- lib/core/note.dart | 7 +++++++ lib/state_container.dart | 4 ++-- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/lib/core/git_repo.dart b/lib/core/git_repo.dart index adce3dc1..98cff7b3 100644 --- a/lib/core/git_repo.dart +++ b/lib/core/git_repo.dart @@ -94,29 +94,27 @@ class GitNoteRepository { return NoteRepoResult(noteFilePath: newFullPath, error: false); } - Future removeNote(String noteFilePath) async { - var pathSpec = noteFilePath.replaceFirst(gitDirPath, "").substring(1); - + Future 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 removeFolder(String folderPath) async { - var pathSpec = folderPath.replaceFirst(gitDirPath, "").substring(1); - - await _gitRepo.rm(pathSpec); + Future 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 resetLastCommit() async { diff --git a/lib/core/note.dart b/lib/core/note.dart index c25ec169..f64e3bc0 100644 --- a/lib/core/note.dart +++ b/lib/core/note.dart @@ -259,4 +259,11 @@ class Note with NotesNotifier { notifyModifiedListeners(this); notifyListeners(); } + + String pathSpec() { + if (parent == null) { + return fileName; + } + return p.join(parent.pathSpec(), fileName); + } } diff --git a/lib/state_container.dart b/lib/state_container.dart index eb399ac1..3ec4a009 100644 --- a/lib/state_container.dart +++ b/lib/state_container.dart @@ -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