From 7a627bbb427e1dcc8641abde13de8a8627e77a8e Mon Sep 17 00:00:00 2001 From: shilangyu Date: Sat, 17 Apr 2021 22:45:18 +0200 Subject: [PATCH 1/2] Add comment editing --- lib/widgets/comment.dart | 25 ++++++++++++++++++++ lib/widgets/write_comment.dart | 42 +++++++++++++++++++++++++--------- 2 files changed, 56 insertions(+), 11 deletions(-) diff --git a/lib/widgets/comment.dart b/lib/widgets/comment.dart index cbe5b58..5c2bcda 100644 --- a/lib/widgets/comment.dart +++ b/lib/widgets/comment.dart @@ -130,6 +130,25 @@ class CommentWidget extends HookWidget { ); } + handleEdit() async { + final editedComment = await showCupertinoModalPopup( + context: context, + builder: (_) => WriteComment.edit( + comment: comment.comment, + post: comment.post, + ), + ); + + if (editedComment != null) { + commentTree.comment = editedComment; + // TODO: workaround to force this widget to rebuild + // TODO: These problems should go away once we move to an actual state management lib + // TODO: work being done here should also help: https://github.com/krawieck/lemmur/tree/fix/better-local-updates + (context as Element).markNeedsBuild(); + Navigator.of(context).pop(); + } + } + void _openMoreMenu(BuildContext context) { pop() => Navigator.of(context).pop(); @@ -174,6 +193,12 @@ class CommentWidget extends HookWidget { pop(); }, ), + if (isMine) + ListTile( + leading: const Icon(Icons.edit), + title: const Text('Edit'), + onTap: handleEdit, + ), if (isMine) ListTile( leading: Icon(isDeleted.value ? Icons.restore : Icons.delete), diff --git a/lib/widgets/write_comment.dart b/lib/widgets/write_comment.dart index 1669b92..8991940 100644 --- a/lib/widgets/write_comment.dart +++ b/lib/widgets/write_comment.dart @@ -8,22 +8,30 @@ import '../l10n/l10n.dart'; import 'markdown_mode_icon.dart'; import 'markdown_text.dart'; -/// Modal for writing a comment to a given post/comment (aka reply) +/// Modal for writing/editing a comment to a given post/comment (aka reply) /// on submit pops the navigator stack with a [CommentView] /// or `null` if cancelled class WriteComment extends HookWidget { final Post post; final Comment? comment; + final bool _isEdit; - const WriteComment.toPost(this.post) : comment = null; + const WriteComment.toPost(this.post) + : comment = null, + _isEdit = false; const WriteComment.toComment({ required Comment this.comment, required this.post, - }); + }) : _isEdit = false; + const WriteComment.edit({ + required Comment this.comment, + required this.post, + }) : _isEdit = true; @override Widget build(BuildContext context) { - final controller = useTextEditingController(); + final controller = + useTextEditingController(text: _isEdit ? comment?.content : null); final showFancy = useState(false); final delayed = useDelayedLoading(); final loggedInAction = useLoggedInAction(post.instanceHost); @@ -56,12 +64,22 @@ class WriteComment extends HookWidget { delayed.start(); try { - final res = await api.run(CreateComment( - content: controller.text, - postId: post.id, - parentId: comment?.id, - auth: token.raw, - )); + final res = await () { + if (_isEdit) { + return api.run(EditComment( + commentId: comment!.id, + content: controller.text, + auth: token.raw, + )); + } else { + return api.run(CreateComment( + content: controller.text, + postId: post.id, + parentId: comment?.id, + auth: token.raw, + )); + } + }(); Navigator.of(context).pop(res.commentView); // ignore: avoid_catches_without_on_clauses } catch (e) { @@ -120,7 +138,9 @@ class WriteComment extends HookWidget { delayed.pending ? () {} : loggedInAction(handleSubmit), child: delayed.loading ? const CircularProgressIndicator() - : Text(L10n.of(context)!.post), + : Text(_isEdit + ? L10n.of(context)!.edit + : L10n.of(context)!.post), ) ], ), From 353d3a899b178e6ca78562c4eb0aa5fa599f93e0 Mon Sep 17 00:00:00 2001 From: shilangyu Date: Sat, 17 Apr 2021 22:51:38 +0200 Subject: [PATCH 2/2] Update changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 06d7a87..5f9fb74 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## Unreleased + +### Added + +- Editing comments + ## v0.4.2 - 2021-04-12 ### Changed