From 1f899f7594923162a49d7dc95b19c4f19a7d9a97 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 18 May 2019 14:58:35 +0200 Subject: [PATCH] Can send comments --- lib/helpers/comments_helper.dart | 20 ++++++++++++++ lib/models/new_comment.dart | 23 ++++++++++++++++ lib/ui/tiles/post_tile.dart | 47 ++++++++++++++++++++++++++------ 3 files changed, 82 insertions(+), 8 deletions(-) create mode 100644 lib/models/new_comment.dart diff --git a/lib/helpers/comments_helper.dart b/lib/helpers/comments_helper.dart index 135a13c..b0594f6 100644 --- a/lib/helpers/comments_helper.dart +++ b/lib/helpers/comments_helper.dart @@ -1,10 +1,30 @@ +import 'package:comunic/models/api_request.dart'; import 'package:comunic/models/comment.dart'; +import 'package:comunic/models/new_comment.dart'; /// Comments helper /// /// @author Pierre HUBERT class CommentsHelper { + /// Send a new comment + /// + /// Returns 0 or below in case of failure, the ID of the comment else + Future createComment(NewComment comment) async { + final request = APIRequest(uri: "comments/create", needLogin: true, args: { + "postID": comment.postID.toString(), + "content": comment.hasContent ? comment.content : "", + }); + + if (comment.hasImage) request.addFile("image", comment.image); + + final response = await request.execWithFiles(); + + if (response.code != 200) return -1; + + return response.getObject()["commentID"]; + } + /// Turn an API entry into a [Comment] object static Comment apiToComment(Map entry) { return Comment( diff --git a/lib/models/new_comment.dart b/lib/models/new_comment.dart new file mode 100644 index 0000000..f88d747 --- /dev/null +++ b/lib/models/new_comment.dart @@ -0,0 +1,23 @@ +import 'dart:io'; + +import 'package:meta/meta.dart'; + +/// New comment information +/// +/// @author Pierre HUBERT + +class NewComment { + final int postID; + final String content; + final File image; + + const NewComment({ + @required this.postID, + @required this.content, + @required this.image, + }) : assert(postID != null); + + bool get hasContent => content != null && content.length > 0; + + bool get hasImage => image != null; +} diff --git a/lib/ui/tiles/post_tile.dart b/lib/ui/tiles/post_tile.dart index 83d2cff..d216803 100644 --- a/lib/ui/tiles/post_tile.dart +++ b/lib/ui/tiles/post_tile.dart @@ -1,7 +1,9 @@ import 'dart:io'; import 'package:comunic/enums/post_kind.dart'; +import 'package:comunic/helpers/comments_helper.dart'; import 'package:comunic/lists/users_list.dart'; +import 'package:comunic/models/new_comment.dart'; import 'package:comunic/models/post.dart'; import 'package:comunic/models/user.dart'; import 'package:comunic/ui/tiles/comment_tile.dart'; @@ -44,15 +46,20 @@ class PostTile extends StatefulWidget { class _PostTileState extends State { // Class members - TextEditingController _controller = TextEditingController(); + TextEditingController _commentController = TextEditingController(); File _commentImage; + bool _submitting = false; User get _user => widget.usersInfo.getUser(widget.post.userID); - bool get _commentValid => _controller.text.toString().length > 3; + bool get _commentValid => _commentController.text.toString().length > 3; bool get _hasImage => _commentImage != null; + bool get _canSubmitComment => !_submitting && (_commentValid || _hasImage); + + set _sendingComment(bool sending) => setState(() => _submitting = sending); + Widget _buildHeaderRow() { // Header row return Row( @@ -211,9 +218,9 @@ class _PostTileState extends State { // Comment input Expanded( child: TextField( - controller: _controller, + controller: _commentController, onChanged: (s) => setState(() {}), - onSubmitted: _commentValid ? (s) => _submitComment() : null, + onSubmitted: _canSubmitComment ? (s) => _submitComment() : null, decoration: InputDecoration( hintText: tr("New comment..."), hintStyle: TextStyle(color: Colors.grey, fontSize: 12), @@ -253,11 +260,10 @@ class _PostTileState extends State { width: 40, child: FlatButton( padding: EdgeInsets.only(), - onPressed: - _commentValid || _hasImage ? () => _submitComment() : null, + onPressed: _canSubmitComment ? () => _submitComment() : null, child: Icon( Icons.send, - color: _commentValid || _hasImage ? Colors.blue : Colors.grey, + color: _canSubmitComment ? Colors.blue : Colors.grey, ), ), ), @@ -266,6 +272,14 @@ class _PostTileState extends State { ); } + /// Clear comments submitting form + void clearCommentForm() { + setState(() { + _commentController.text = ""; + _commentImage = null; + }); + } + /// Pick an image Future _pickImage() async { // Ask the user to confirm image removal if there is already one selected @@ -291,5 +305,22 @@ class _PostTileState extends State { } /// Submit comment entered by the user - Future _submitComment() async {} + Future _submitComment() async { + _sendingComment = true; + + final commentID = await CommentsHelper().createComment(NewComment( + postID: widget.post.id, + content: _commentController.text, + image: _commentImage, + )); + + _sendingComment = false; + + if (commentID < 1) + return showSimpleSnack(context, tr("Could not create comment!")); + + clearCommentForm(); + + // Get and show new comment + } }