mirror of
https://gitlab.com/comunic/comunicmobile
synced 2025-06-19 08:15:16 +00:00
Handles new comments events
This commit is contained in:
@ -1,9 +1,11 @@
|
||||
import 'package:comunic/helpers/events_helper.dart';
|
||||
import 'package:comunic/helpers/groups_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/posts_list.dart';
|
||||
import 'package:comunic/lists/users_list.dart';
|
||||
import 'package:comunic/models/comment.dart';
|
||||
import 'package:comunic/models/post.dart';
|
||||
import 'package:comunic/ui/screens/conversation_screen.dart';
|
||||
import 'package:comunic/ui/tiles/post_tile.dart';
|
||||
@ -65,6 +67,9 @@ class PostsListWidgetState extends SafeState<PostsListWidget> {
|
||||
super.initState();
|
||||
|
||||
_scrollController = ScrollWatcher(onReachBottom: reachedPostsBottom);
|
||||
|
||||
// Register to events
|
||||
this.listen<NewCommentEvent>((ev) => this._addComment(ev.comment));
|
||||
}
|
||||
|
||||
@override
|
||||
@ -219,4 +224,24 @@ class PostsListWidgetState extends SafeState<PostsListWidget> {
|
||||
void reachedPostsBottom() {
|
||||
if (widget.getOlder != null) loadPostsList(getOlder: true);
|
||||
}
|
||||
|
||||
/// Add new comment
|
||||
void _addComment(Comment c) async {
|
||||
if (_list == null) return;
|
||||
|
||||
try {
|
||||
final p = _list.singleWhere((p) => p.id == c.postID, orElse: () => null);
|
||||
|
||||
if (p == null) return;
|
||||
|
||||
if (!_users.hasUser(c.userID))
|
||||
_users.add(await UsersHelper().getSingleWithThrow(c.userID));
|
||||
|
||||
setState(() {
|
||||
p.comments.add(c);
|
||||
});
|
||||
} catch (e, stack) {
|
||||
print("$e\n$stack");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user