mirror of
https://gitlab.com/comunic/comunicmobile
synced 2025-06-19 00:05:16 +00:00
Can send comments
This commit is contained in:
@ -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
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user