import 'dart:io'; import 'package:comunic/enums/likes_type.dart'; import 'package:comunic/enums/post_kind.dart'; import 'package:comunic/helpers/comments_helper.dart'; import 'package:comunic/helpers/likes_helper.dart'; import 'package:comunic/lists/users_list.dart'; import 'package:comunic/models/comment.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'; import 'package:comunic/ui/widgets/account_image_widget.dart'; import 'package:comunic/ui/widgets/network_image_widget.dart'; import 'package:comunic/utils/date_utils.dart'; import 'package:comunic/utils/files_utils.dart'; import 'package:comunic/utils/intl_utils.dart'; import 'package:comunic/utils/ui_utils.dart'; import 'package:flutter/material.dart'; /// Single posts tile /// /// @author Pierre HUBERT /// User style const TextStyle _userNameStyle = TextStyle( color: Color.fromRGBO(0x72, 0xaf, 0xd2, 1.0), //#72afd2 fontWeight: FontWeight.w600, fontSize: 16.0); class PostTile extends StatefulWidget { final Post post; final UsersList usersInfo; const PostTile({ Key key, @required this.post, @required this.usersInfo, }) : assert(post != null), assert(usersInfo != null), super(key: key); @override State createState() => _PostTileState(); } class _PostTileState extends State { // Helpers final _likesHelper = LikesHelper(); final _commentsHelper = CommentsHelper(); // Class members TextEditingController _commentController = TextEditingController(); File _commentImage; bool _submitting = false; User get _user => widget.usersInfo.getUser(widget.post.userID); 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( children: [ // User account image Padding( padding: const EdgeInsets.only(right: 8.0, left: 8.0), child: AccountImageWidget(user: _user), ), // Column with user name + post target Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( _user.displayName, style: _userNameStyle, ), Text(diffTimeFromNowToStr(widget.post.timeSent)), ], ), ), PopupMenuButton( itemBuilder: (c) => [], ) ], ); } Widget _buildContentRow() { Widget postContent; switch (widget.post.kind) { case PostKind.IMAGE: postContent = _buildPostImage(); break; default: } return Column( children: [ // Post "rich" content Container(child: postContent), // Post text Container( child: widget.post.hasContent ? Text(widget.post.content) : null), ], ); } Widget _buildButtonsArea() { return Padding( padding: const EdgeInsets.only(top: 16.0, left: 8.0, right: 8.0, bottom: 16.0), child: Column( children: [ // Like button Center( child: InkWell( onTap: () => _updateLike(), child: Row( crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center, children: [ Padding( padding: const EdgeInsets.only(left: 8.0, right: 8.0), child: Icon( Icons.thumb_up, color: widget.post.userLikes ? Colors.blue : null, ), ), Text(widget.post.likes < 2 ? tr("%num% like", args: {"num": widget.post.likes.toString()}) : tr("%num% likes", args: {"num": widget.post.likes.toString()})) ], ), ), ), ], ), ); } @override Widget build(BuildContext context) { return Card( elevation: 1.0, child: Column( children: [ Padding( padding: const EdgeInsets.all(8.0), child: Column( children: [ _buildHeaderRow(), _buildContentRow(), _buildButtonsArea(), ], ), ), Container( child: widget.post.hasComments ? _buildComments() : null, ), ], ), ); } Widget _buildPostImage() { return NetworkImageWidget( url: widget.post.fileURL, allowFullScreen: true, roundedEdges: false, ); } /// Build the list of comments Widget _buildComments() { assert(widget.post.hasComments); final comments = List.generate( widget.post.comments.length, (num) => CommentTile( comment: widget.post.comments[num], user: widget.usersInfo.getUser(widget.post.comments[num].userID), onUpdateLike: _updateCommentLike, ), ); // Add comments form comments.add(_buildCommentsForm()); return Container( color: Colors.grey[300], child: Padding( padding: const EdgeInsets.only(top: 8.0, bottom: 8.0), child: Column( children: comments, ), ), ); } /// Build comments form Widget _buildCommentsForm() { return Padding( padding: EdgeInsets.only( left: 8.0, right: 8.0, top: (widget.post.comments.length > 0 ? 8.0 : 0)), child: Row( children: [ // Comment input Expanded( child: TextField( controller: _commentController, onChanged: (s) => setState(() {}), onSubmitted: _canSubmitComment ? (s) => _submitComment() : null, decoration: InputDecoration( hintText: tr("New comment..."), hintStyle: TextStyle(color: Colors.grey, fontSize: 12), fillColor: Colors.white, filled: true, border: OutlineInputBorder( borderSide: BorderSide(width: 0.5, color: Colors.grey), borderRadius: BorderRadius.only( topLeft: Radius.circular(3), bottomLeft: Radius.circular(3), ), gapPadding: 1), contentPadding: EdgeInsets.only( left: 10, right: 10, bottom: 5, top: 5, )), ), ), // Image button Container( width: 30, child: FlatButton( padding: EdgeInsets.only(), onPressed: _pickImageForComment, child: Icon( Icons.image, color: _hasImage ? Colors.blue : Colors.grey, ), ), ), // Submit button Container( width: 40, child: FlatButton( padding: EdgeInsets.only(), onPressed: _canSubmitComment ? () => _submitComment() : null, child: Icon( Icons.send, color: _canSubmitComment ? Colors.blue : Colors.grey, ), ), ), ], ), ); } /// Clear comments submitting form void clearCommentForm() { setState(() { _commentController.text = ""; _commentImage = null; }); } /// Pick an image Future _pickImageForComment() async { // Ask the user to confirm image removal if there is already one selected if (_hasImage) { if (await askUserConfirmation( context: context, title: tr("Remove selected image"), message: tr("Do you want to unselected currently selected image ?"), )) { setState(() { _commentImage = null; }); } return; } // Pick a new image final newImage = await pickImage(context); setState(() { _commentImage = newImage; }); } /// Submit comment entered by the user 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 final newComment = await _commentsHelper.getSingle(commentID); if (newComment == null) return showSimpleSnack( context, tr("Could not retrieve created comment!")); setState(() { widget.post.comments.add(newComment); }); } /// Update like status Future _updateLike() async { // Update liking status _likesHelper.setLiking( type: LikesType.POST, like: !widget.post.userLikes, id: widget.post.id, ); // Save new like status setState(() { widget.post.userLikes = !widget.post.userLikes; widget.post.userLikes ? widget.post.likes++ : widget.post.likes--; }); } /// Invert comment like status Future _updateCommentLike(Comment comment) async { // Update liking status _likesHelper.setLiking( type: LikesType.COMMENT, like: !comment.userLike, id: comment.id, ); // Save new like status setState(() { comment.userLike = !comment.userLike; comment.userLike ? comment.likes++ : comment.likes--; }); } }