From 67b6764f096f6f1826633e715b2a9948cdd1a270 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 18 May 2019 18:48:12 +0200 Subject: [PATCH] Can update comments content --- lib/helpers/comments_helper.dart | 19 +++++++++++++++---- lib/models/api_response.dart | 3 +++ lib/models/comment.dart | 2 +- lib/ui/tiles/comment_tile.dart | 23 ++++++++++++++++++++--- lib/ui/tiles/post_tile.dart | 19 +++++++++++++++++++ lib/utils/ui_utils.dart | 2 ++ 6 files changed, 60 insertions(+), 8 deletions(-) diff --git a/lib/helpers/comments_helper.dart b/lib/helpers/comments_helper.dart index ae23c74..dd9ba7c 100644 --- a/lib/helpers/comments_helper.dart +++ b/lib/helpers/comments_helper.dart @@ -37,13 +37,24 @@ class CommentsHelper { return apiToComment(response.getObject()); } + /// Update comment content + Future updateContent(int id, String newContent) async { + return (await APIRequest(uri: "comments/edit", needLogin: true, args: { + "commentID": id.toString(), + "content": newContent, + }).exec()) + .isOK; + } + /// 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; + uri: "comments/delete", + needLogin: true, + args: {"commentID": id.toString()}, + ).exec()) + .code == + 200; } /// Turn an API entry into a [Comment] object diff --git a/lib/models/api_response.dart b/lib/models/api_response.dart index 227f207..1ca8923 100644 --- a/lib/models/api_response.dart +++ b/lib/models/api_response.dart @@ -13,4 +13,7 @@ class APIResponse { List getArray() => jsonDecode(this.content); Map getObject() => jsonDecode(this.content); + + /// Check if the request is successful or not + bool get isOK => code == 200; } diff --git a/lib/models/comment.dart b/lib/models/comment.dart index f267388..40b7146 100644 --- a/lib/models/comment.dart +++ b/lib/models/comment.dart @@ -13,7 +13,7 @@ class Comment implements LikeElement { final int userID; final int postID; final int timeSent; - final String content; + String content; final String imageURL; int likes; bool userLike; diff --git a/lib/ui/tiles/comment_tile.dart b/lib/ui/tiles/comment_tile.dart index 0b257f7..5505de0 100644 --- a/lib/ui/tiles/comment_tile.dart +++ b/lib/ui/tiles/comment_tile.dart @@ -9,12 +9,13 @@ import 'package:flutter/material.dart'; /// /// @author Pierre HUBERT -enum _CommentAction { DELETE } +enum _CommentAction { DELETE, UPDATE } class CommentTile extends StatelessWidget { final Comment comment; final User user; final void Function(Comment) onUpdateLike; + final void Function(Comment) onUpdateComment; final void Function(Comment) onDeleteComment; const CommentTile({ @@ -22,10 +23,13 @@ class CommentTile extends StatelessWidget { @required this.comment, @required this.user, @required this.onUpdateLike, + @required this.onUpdateComment, @required this.onDeleteComment, }) : assert(comment != null), assert(user != null), assert(onUpdateLike != null), + assert(onUpdateComment != null), + assert(onDeleteComment != null), super(key: key); @override @@ -46,11 +50,19 @@ class CommentTile extends StatelessWidget { ), onSelected: _selectedMenuOption, itemBuilder: (c) => [ + // Update comment content PopupMenuItem( enabled: comment.isOwner, - child: Text("Delete"), + child: Text(tr("Update")), + value: _CommentAction.UPDATE, + ), + + // Delete comment + PopupMenuItem( + enabled: comment.isOwner, + child: Text(tr("Delete")), value: _CommentAction.DELETE, - ) + ), ], ); } @@ -128,6 +140,11 @@ class CommentTile extends StatelessWidget { /// A menu option has been selected void _selectedMenuOption(_CommentAction value) { switch (value) { + // Update comment content + case _CommentAction.UPDATE: + onUpdateComment(comment); + break; + // Delete comment case _CommentAction.DELETE: onDeleteComment(comment); diff --git a/lib/ui/tiles/post_tile.dart b/lib/ui/tiles/post_tile.dart index e6e97b9..719fc76 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, + onUpdateComment: _updateCommentContent, onDeleteComment: _deleteComment, ), ); @@ -366,6 +367,24 @@ class _PostTileState extends State { }); } + /// Update comment content + Future _updateCommentContent(Comment comment) async { + final newContent = await askUserString( + context: context, + title: tr("Update comment content"), + message: tr("New content:"), + defaultValue: comment.content, + hint: tr("New content..."), + ); + + if (!(await _commentsHelper.updateContent(comment.id, newContent))) + return showSimpleSnack(context, tr("Could not update comment content!")); + + setState(() { + comment.content = newContent; + }); + } + /// Process the deletion of a user Future _deleteComment(Comment comment) async { if (!await showConfirmDialog( diff --git a/lib/utils/ui_utils.dart b/lib/utils/ui_utils.dart index 59117ae..4a808f6 100644 --- a/lib/utils/ui_utils.dart +++ b/lib/utils/ui_utils.dart @@ -68,6 +68,8 @@ void showSimpleSnack(BuildContext context, String message) { } /// Show an alert dialog to ask the user to enter a string +/// +/// Returns entered string if the dialog is confirmed, null else Future askUserString({ @required BuildContext context, @required String title,