1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-25 22:39:22 +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!"; 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 /// Get the list of users ID present in this list
List<int> get usersID => List.generate(length, (i) => this[i].id); 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/enums/post_visibility_level.dart';
import 'package:comunic/helpers/comments_helper.dart'; import 'package:comunic/helpers/comments_helper.dart';
import 'package:comunic/helpers/posts_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/groups_list.dart';
import 'package:comunic/lists/users_list.dart'; import 'package:comunic/lists/users_list.dart';
import 'package:comunic/models/comment.dart'; import 'package:comunic/models/comment.dart';
@ -472,31 +473,40 @@ class _PostTileState extends State<PostTile> {
/// Submit comment entered by the user /// Submit comment entered by the user
Future<void> _submitComment() async { Future<void> _submitComment() async {
_sendingComment = true; try {
_sendingComment = true;
final commentID = await _commentsHelper.createComment(NewComment( final commentID = await _commentsHelper.createComment(NewComment(
postID: widget.post.id, postID: widget.post.id,
content: _commentController.text, content: _commentController.text,
image: _commentImage, image: _commentImage,
)); ));
_sendingComment = false; _sendingComment = false;
if (commentID < 1) if (commentID < 1) throw new Exception("Comment ID is inferior to 1!");
return showSimpleSnack(context, tr("Could not create comment!"));
clearCommentForm(); clearCommentForm();
// Get and show new comment // Get and show new comment
final newComment = await _commentsHelper.getSingle(commentID); final newComment = await _commentsHelper.getSingle(commentID);
if (newComment == null) if (newComment == null)
return showSimpleSnack( return showSimpleSnack(
context, tr("Could not retrieve created comment!")); context, tr("Could not retrieve created comment!"));
setState(() { // Get information about the user who created the comment (if required)
widget.post.comments.add(newComment); 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 /// Update comment content