Allow Folders to be deleted

For safety we're only allowing non-empty folders to be deleted. Ideally,
we should have some kind of undo button. Or some kind of history - since
we do have that with git.

Related to #18
This commit is contained in:
Vishesh Handa
2019-12-11 21:40:44 +01:00
parent 453a6c518d
commit abb6029798
4 changed files with 87 additions and 1 deletions

View File

@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:io';
import 'package:fimber/fimber.dart';
import 'package:flutter/foundation.dart';
@ -86,6 +87,20 @@ class GitNoteRepository {
return NoteRepoResult(noteFilePath: noteFilePath, error: false);
}
Future<NoteRepoResult> removeFolder(String folderPath) async {
var gitDir = p.join(baseDirectory, dirName);
var pathSpec = folderPath.replaceFirst(gitDir, "").substring(1);
await _gitRepo.rm(pathSpec);
await _gitRepo.commit(
message: "Removed Folder " + pathSpec,
);
await Directory(folderPath).delete(recursive: true);
return NoteRepoResult(noteFilePath: folderPath, error: false);
}
Future<NoteRepoResult> resetLastCommit() async {
await _gitRepo.resetLast();
return NoteRepoResult(error: false);

View File

@ -48,6 +48,23 @@ class NotesFolder with ChangeNotifier {
return _entities.firstWhere((e) => e.isNote, orElse: () => null) != null;
}
bool get hasNotesRecursive {
bool has = hasNotes;
if (has) return true;
for (var i = 0; i < _entities.length; i++) {
var e = _entities[i];
if (e.isNote) continue;
has = has || e.folder.hasNotes;
if (has) {
return true;
}
}
return has;
}
int get numberOfNotes {
int i = 0;
_entities.forEach((e) {
@ -231,6 +248,21 @@ class NotesFolder with ChangeNotifier {
notifyListeners();
}
void removeFolder(NotesFolder folder) {
folder.removeListener(_entityChanged);
var i = _entities.indexWhere((e) {
if (e.isNote) return false;
return e.folder.folderPath == folder.folderPath;
});
assert(i != -1);
_entities.removeAt(i);
_entityMap.remove(folder.folderPath);
notifyListeners();
}
void rename(String newName) {
var dir = Directory(folderPath);
var parentDirName = dirname(folderPath);

View File

@ -57,7 +57,11 @@ class _FolderListingScreenState extends State<FolderListingScreen> {
const PopupMenuItem<String>(
child: Text("Create Sub-Folder"),
value: "Create",
)
),
const PopupMenuItem<String>(
child: Text("Delete Folder"),
value: "Delete",
),
];
},
onSelected: (String value) async {
@ -79,6 +83,16 @@ class _FolderListingScreenState extends State<FolderListingScreen> {
final container = StateContainer.of(context);
container.createFolder(selectedFolder, folderName);
}
} else if (value == "Delete") {
if (selectedFolder.hasNotesRecursive) {
await showDialog(
context: context,
builder: (_) => FolderErrorDialog(),
);
} else {
final container = StateContainer.of(context);
container.removeFolder(selectedFolder);
}
}
_folderTreeViewKey.currentState.resetSelection();
@ -254,3 +268,19 @@ class _RenameFolderDialogState extends State<RenameFolderDialog> {
super.dispose();
}
}
class FolderErrorDialog extends StatelessWidget {
@override
Widget build(BuildContext context) {
return AlertDialog(
title: const Text("Error"),
content: const Text("Cannot delete a Folder which contains notes"),
actions: <Widget>[
FlatButton(
child: const Text("Ok"),
onPressed: () => Navigator.of(context).pop(),
),
],
);
}
}

View File

@ -111,6 +111,15 @@ class StateContainerState extends State<StateContainer> {
});
}
void removeFolder(NotesFolder folder) {
Fimber.d("Removing Folder: " + folder.folderPath);
folder.parent.removeFolder(folder);
_gitRepo.removeFolder(folder.folderPath).then((NoteRepoResult _) {
syncNotes();
});
}
void renameFolder(NotesFolder folder, String newFolderName) async {
var oldFolderPath = folder.folderPath;
folder.rename(newFolderName);