From f8e7773f16ec414e08ed7d11536477b9c8c39e5c Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Mon, 27 Jan 2020 23:09:11 +0100 Subject: [PATCH] NoteBrowser: Add a button to rename notes Fixes #23 --- lib/core/git_repo.dart | 13 +++++++++++ lib/core/note.dart | 9 +++++++ lib/screens/journal_browsing.dart | 39 +++++++++++++++++++++++++++++++ lib/state_container.dart | 9 +++++++ 4 files changed, 70 insertions(+) diff --git a/lib/core/git_repo.dart b/lib/core/git_repo.dart index 25c4f675..5ec12ccb 100644 --- a/lib/core/git_repo.dart +++ b/lib/core/git_repo.dart @@ -68,6 +68,19 @@ class GitNoteRepository { return NoteRepoResult(noteFilePath: newFullPath, error: false); } + Future 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 removeNote(String noteFilePath) async { var pathSpec = noteFilePath.replaceFirst(gitDirPath, "").substring(1); diff --git a/lib/core/note.dart b/lib/core/note.dart index 76f50500..238ee032 100644 --- a/lib/core/note.dart +++ b/lib/core/note.dart @@ -155,6 +155,15 @@ class Note with ChangeNotifier implements Comparable { 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; diff --git a/lib/screens/journal_browsing.dart b/lib/screens/journal_browsing.dart index fdbb649b..a31f23d0 100644 --- a/lib/screens/journal_browsing.dart +++ b/lib/screens/journal_browsing.dart @@ -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 notes; final int noteIndex; @@ -31,9 +34,11 @@ class JournalBrowsingScreen extends StatefulWidget { class JournalBrowsingScreenState extends State { PageController pageController; + int currentPage; JournalBrowsingScreenState({@required int noteIndex}) { pageController = PageController(initialPage: noteIndex); + currentPage = noteIndex; } @override @@ -48,6 +53,11 @@ class JournalBrowsingScreenState extends State { note: widget.notes[pos], ); }, + onPageChanged: (int pageNum) { + setState(() { + currentPage = pageNum; + }); + }, ); return Scaffold( @@ -66,6 +76,35 @@ class JournalBrowsingScreenState extends State { Share.share(note.body); }, ), + PopupMenuButton( + 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) => + >[ + const PopupMenuItem( + value: NoteBrowserDropDownChoices.Rename, + child: Text('Rename File'), + ), + ], + ), ], ), body: pageView, diff --git a/lib/state_container.dart b/lib/state_container.dart index 3e42aae7..8c86c3d9 100644 --- a/lib/state_container.dart +++ b/lib/state_container.dart @@ -150,6 +150,15 @@ class StateContainerState extends State { }); } + 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); }