NoteBrowser: Add a button to rename notes

Fixes #23
This commit is contained in:
Vishesh Handa
2020-01-27 23:09:11 +01:00
parent 35f8559c74
commit f8e7773f16
4 changed files with 70 additions and 0 deletions

View File

@ -68,6 +68,19 @@ class GitNoteRepository {
return NoteRepoResult(noteFilePath: newFullPath, error: false);
}
Future<NoteRepoResult> renameNote(
String oldFullPath,
String newFullPath,
) async {
// FIXME: This is a hacky way of adding the changes, ideally we should be calling rm + add or something
await _gitRepo.add(".");
await _gitRepo.commit(
message: "Renamed Note",
);
return NoteRepoResult(noteFilePath: newFullPath, error: false);
}
Future<NoteRepoResult> removeNote(String noteFilePath) async {
var pathSpec = noteFilePath.replaceFirst(gitDirPath, "").substring(1);

View File

@ -155,6 +155,15 @@ class Note with ChangeNotifier implements Comparable<Note> {
await file.delete();
}
void rename(String newName) {
var parentDirName = p.dirname(filePath);
var newFilePath = p.join(parentDirName, newName);
File(filePath).renameSync(newFilePath);
_filePath = newFilePath;
notifyListeners();
}
@override
int get hashCode => _filePath.hashCode;

View File

@ -11,9 +11,12 @@ import 'package:gitjournal/state_container.dart';
import 'package:gitjournal/utils.dart';
import 'package:gitjournal/settings.dart';
import 'package:gitjournal/widgets/journal_editor_header.dart';
import 'package:gitjournal/widgets/rename_dialog.dart';
import 'journal_editor.dart';
enum NoteBrowserDropDownChoices { Rename }
class JournalBrowsingScreen extends StatefulWidget {
final List<Note> notes;
final int noteIndex;
@ -31,9 +34,11 @@ class JournalBrowsingScreen extends StatefulWidget {
class JournalBrowsingScreenState extends State<JournalBrowsingScreen> {
PageController pageController;
int currentPage;
JournalBrowsingScreenState({@required int noteIndex}) {
pageController = PageController(initialPage: noteIndex);
currentPage = noteIndex;
}
@override
@ -48,6 +53,11 @@ class JournalBrowsingScreenState extends State<JournalBrowsingScreen> {
note: widget.notes[pos],
);
},
onPageChanged: (int pageNum) {
setState(() {
currentPage = pageNum;
});
},
);
return Scaffold(
@ -66,6 +76,35 @@ class JournalBrowsingScreenState extends State<JournalBrowsingScreen> {
Share.share(note.body);
},
),
PopupMenuButton<NoteBrowserDropDownChoices>(
onSelected: (NoteBrowserDropDownChoices choice) async {
var note = widget.notes[currentPage];
switch (choice) {
case NoteBrowserDropDownChoices.Rename:
var fileName = await showDialog(
context: context,
builder: (_) => RenameDialog(
oldName: note.fileName,
inputDecoration: 'File Name',
dialogTitle: "Rename File",
),
);
if (fileName is String) {
final container = StateContainer.of(context);
container.renameNote(note, fileName);
}
break;
}
},
itemBuilder: (BuildContext context) =>
<PopupMenuEntry<NoteBrowserDropDownChoices>>[
const PopupMenuItem<NoteBrowserDropDownChoices>(
value: NoteBrowserDropDownChoices.Rename,
child: Text('Rename File'),
),
],
),
],
),
body: pageView,

View File

@ -150,6 +150,15 @@ class StateContainerState extends State<StateContainer> {
});
}
void renameNote(Note note, String newFileName) async {
var oldNotePath = note.filePath;
note.rename(newFileName);
_gitRepo.renameNote(oldNotePath, note.filePath).then((NoteRepoResult _) {
syncNotes();
});
}
void addNote(Note note) {
insertNote(0, note);
}