From 6d73d38bf86eed15ecc1d2a35bd5cac3fa2cf4b5 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 18 May 2019 16:48:19 +0200 Subject: [PATCH] Can delete comments --- lib/helpers/comments_helper.dart | 9 +++++++++ lib/models/comment.dart | 3 +++ lib/ui/tiles/comment_tile.dart | 34 +++++++++++++++++++++++++++++--- lib/ui/tiles/post_tile.dart | 21 +++++++++++++++++++- lib/utils/ui_utils.dart | 2 +- 5 files changed, 64 insertions(+), 5 deletions(-) diff --git a/lib/helpers/comments_helper.dart b/lib/helpers/comments_helper.dart index 4b62a12..ae23c74 100644 --- a/lib/helpers/comments_helper.dart +++ b/lib/helpers/comments_helper.dart @@ -37,6 +37,15 @@ class CommentsHelper { return apiToComment(response.getObject()); } + /// Attempt to delete a comment + Future delete(int id) async { + return (await APIRequest( + uri: "comments/delete", + needLogin: true, + args: {"commentID": id.toString()}, + ).exec()).code == 200; + } + /// Turn an API entry into a [Comment] object static Comment apiToComment(Map entry) { return Comment( diff --git a/lib/models/comment.dart b/lib/models/comment.dart index 688be60..f267388 100644 --- a/lib/models/comment.dart +++ b/lib/models/comment.dart @@ -1,5 +1,6 @@ import 'package:comunic/models/like_element.dart'; import 'package:meta/meta.dart'; +import 'package:comunic/utils/account_utils.dart' as account; /// Comments /// @@ -37,4 +38,6 @@ class Comment implements LikeElement { bool get hasContent => content != null && content.length > 0; bool get hasImage => imageURL != null; + + bool get isOwner => userID == account.userID(); } diff --git a/lib/ui/tiles/comment_tile.dart b/lib/ui/tiles/comment_tile.dart index ffbe849..0b257f7 100644 --- a/lib/ui/tiles/comment_tile.dart +++ b/lib/ui/tiles/comment_tile.dart @@ -9,16 +9,20 @@ import 'package:flutter/material.dart'; /// /// @author Pierre HUBERT +enum _CommentAction { DELETE } + class CommentTile extends StatelessWidget { final Comment comment; final User user; final void Function(Comment) onUpdateLike; + final void Function(Comment) onDeleteComment; const CommentTile({ Key key, @required this.comment, @required this.user, @required this.onUpdateLike, + @required this.onDeleteComment, }) : assert(comment != null), assert(user != null), assert(onUpdateLike != null), @@ -27,9 +31,7 @@ class CommentTile extends StatelessWidget { @override Widget build(BuildContext context) { return ListTile( - leading: AccountImageWidget( - user: user, - ), + leading: _buildAccountImageWidget(), title: Text( user.displayName, ), @@ -37,6 +39,22 @@ class CommentTile extends StatelessWidget { ); } + Widget _buildAccountImageWidget() { + return PopupMenuButton<_CommentAction>( + child: AccountImageWidget( + user: user, + ), + onSelected: _selectedMenuOption, + itemBuilder: (c) => [ + PopupMenuItem( + enabled: comment.isOwner, + child: Text("Delete"), + value: _CommentAction.DELETE, + ) + ], + ); + } + Widget _buildCommentContent() { return Column( crossAxisAlignment: CrossAxisAlignment.stretch, @@ -106,4 +124,14 @@ class CommentTile extends StatelessWidget { ), ); } + + /// A menu option has been selected + void _selectedMenuOption(_CommentAction value) { + switch (value) { + // Delete comment + case _CommentAction.DELETE: + onDeleteComment(comment); + break; + } + } } diff --git a/lib/ui/tiles/post_tile.dart b/lib/ui/tiles/post_tile.dart index afa7be8..e6e97b9 100644 --- a/lib/ui/tiles/post_tile.dart +++ b/lib/ui/tiles/post_tile.dart @@ -195,6 +195,7 @@ class _PostTileState extends State { comment: widget.post.comments[num], user: widget.usersInfo.getUser(widget.post.comments[num].userID), onUpdateLike: _updateCommentLike, + onDeleteComment: _deleteComment, ), ); @@ -290,7 +291,7 @@ class _PostTileState extends State { Future _pickImageForComment() async { // Ask the user to confirm image removal if there is already one selected if (_hasImage) { - if (await askUserConfirmation( + if (await showConfirmDialog( context: context, title: tr("Remove selected image"), message: tr("Do you want to unselected currently selected image ?"), @@ -364,4 +365,22 @@ class _PostTileState extends State { element.userLike ? element.likes++ : element.likes--; }); } + + /// Process the deletion of a user + Future _deleteComment(Comment comment) async { + if (!await showConfirmDialog( + context: context, + message: tr("Do you really want to delete this comment ?"), + title: tr("Delete comment"))) return; + + if (!await _commentsHelper.delete(comment.id)) { + showSimpleSnack(context, tr("Could not delete the comment!")); + return; + } + + // Remove the comment from the list + setState(() { + widget.post.comments.remove(comment); + }); + } } diff --git a/lib/utils/ui_utils.dart b/lib/utils/ui_utils.dart index 6165d76..59117ae 100644 --- a/lib/utils/ui_utils.dart +++ b/lib/utils/ui_utils.dart @@ -122,7 +122,7 @@ Future askUserString({ /// Show an alert dialog to get user confirmation for something /// /// Return value of this function is never null -Future askUserConfirmation({ +Future showConfirmDialog({ @required BuildContext context, String title, @required String message,