1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-12-26 04:48:51 +00:00

Can send comments

This commit is contained in:
Pierre HUBERT 2019-05-18 14:58:35 +02:00
parent c267940ce6
commit 1f899f7594
3 changed files with 82 additions and 8 deletions

View File

@ -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<int> 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<String, dynamic> entry) {
return Comment(

View File

@ -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;
}

View File

@ -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<PostTile> {
// 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<PostTile> {
// 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<PostTile> {
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<PostTile> {
);
}
/// Clear comments submitting form
void clearCommentForm() {
setState(() {
_commentController.text = "";
_commentImage = null;
});
}
/// Pick an image
Future<void> _pickImage() async {
// Ask the user to confirm image removal if there is already one selected
@ -291,5 +305,22 @@ class _PostTileState extends State<PostTile> {
}
/// Submit comment entered by the user
Future<bool> _submitComment() async {}
Future<void> _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
}
}