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

Fix issue: when creating a comment on a list of posts where user never appeared before

This commit is contained in:
Pierre HUBERT 2020-04-16 08:34:39 +02:00
parent f0a23bcb47
commit cf5b1180a9
2 changed files with 31 additions and 18 deletions

View File

@ -31,6 +31,9 @@ class UsersList extends ListBase<User> {
throw "User not found in the list!";
}
/// Check if the user is included in this list or not
bool hasUser(int userID) => any((f) => f.id == userID);
/// Get the list of users ID present in this list
List<int> get usersID => List.generate(length, (i) => this[i].id);
}

View File

@ -4,6 +4,7 @@ import 'package:comunic/enums/post_kind.dart';
import 'package:comunic/enums/post_visibility_level.dart';
import 'package:comunic/helpers/comments_helper.dart';
import 'package:comunic/helpers/posts_helper.dart';
import 'package:comunic/helpers/users_helper.dart';
import 'package:comunic/lists/groups_list.dart';
import 'package:comunic/lists/users_list.dart';
import 'package:comunic/models/comment.dart';
@ -472,31 +473,40 @@ class _PostTileState extends State<PostTile> {
/// Submit comment entered by the user
Future<void> _submitComment() async {
_sendingComment = true;
try {
_sendingComment = true;
final commentID = await _commentsHelper.createComment(NewComment(
postID: widget.post.id,
content: _commentController.text,
image: _commentImage,
));
final commentID = await _commentsHelper.createComment(NewComment(
postID: widget.post.id,
content: _commentController.text,
image: _commentImage,
));
_sendingComment = false;
_sendingComment = false;
if (commentID < 1)
return showSimpleSnack(context, tr("Could not create comment!"));
if (commentID < 1) throw new Exception("Comment ID is inferior to 1!");
clearCommentForm();
clearCommentForm();
// Get and show new comment
final newComment = await _commentsHelper.getSingle(commentID);
// Get and show new comment
final newComment = await _commentsHelper.getSingle(commentID);
if (newComment == null)
return showSimpleSnack(
context, tr("Could not retrieve created comment!"));
if (newComment == null)
return showSimpleSnack(
context, tr("Could not retrieve created comment!"));
setState(() {
widget.post.comments.add(newComment);
});
// Get information about the user who created the comment (if required)
if (!widget.usersInfo.hasUser(newComment.userID))
widget.usersInfo
.add(await UsersHelper().getSingleWithThrow(newComment.userID));
setState(() {
widget.post.comments.add(newComment);
});
} catch (e) {
print(e);
showSimpleSnack(context, tr("Could not create comment!"));
}
}
/// Update comment content