mirror of
https://gitlab.com/comunic/comunicmobile
synced 2024-11-22 12:59:21 +00:00
Register to comment udpate events
This commit is contained in:
parent
a60c1ed68c
commit
6b08b62832
@ -31,6 +31,13 @@ class NewCommentEvent {
|
||||
NewCommentEvent(this.comment);
|
||||
}
|
||||
|
||||
/// Updated comment
|
||||
class UpdatedCommentEvent {
|
||||
final Comment comment;
|
||||
|
||||
UpdatedCommentEvent(this.comment);
|
||||
}
|
||||
|
||||
class EventsHelper {
|
||||
static EventBus _mgr = EventBus();
|
||||
|
||||
|
@ -126,6 +126,12 @@ class WebSocketHelper {
|
||||
NewCommentEvent(CommentsHelper.apiToComment(msg.data)));
|
||||
break;
|
||||
|
||||
// Updated comment
|
||||
case "comment_updated":
|
||||
EventsHelper.emit(
|
||||
UpdatedCommentEvent(CommentsHelper.apiToComment(msg.data)));
|
||||
break;
|
||||
|
||||
default:
|
||||
throw Exception("Unknown message type: ${msg.title}");
|
||||
}
|
||||
|
@ -510,10 +510,6 @@ class _PostTileState extends State<PostTile> {
|
||||
|
||||
if (!(await _commentsHelper.updateContent(comment.id, newContent)))
|
||||
return showSimpleSnack(context, tr("Could not update comment content!"));
|
||||
|
||||
setState(() {
|
||||
comment.content.content = newContent;
|
||||
});
|
||||
}
|
||||
|
||||
/// Process the deletion of a user
|
||||
@ -575,7 +571,7 @@ class _PostTileState extends State<PostTile> {
|
||||
title: tr("Update post content"),
|
||||
message: tr("Please enter message content: "),
|
||||
defaultValue:
|
||||
widget.post.content.isNull == null ? "" : widget.post.content.content,
|
||||
widget.post.content.isNull == null ? "" : widget.post.content.content,
|
||||
hint: tr("Post content"),
|
||||
);
|
||||
|
||||
|
@ -70,6 +70,8 @@ class PostsListWidgetState extends SafeState<PostsListWidget> {
|
||||
|
||||
// Register to events
|
||||
this.listen<NewCommentEvent>((ev) => this._addComment(ev.comment));
|
||||
this.listenChangeState<UpdatedCommentEvent>(
|
||||
(ev) => this._updateComment(ev.comment));
|
||||
}
|
||||
|
||||
@override
|
||||
@ -244,4 +246,21 @@ class PostsListWidgetState extends SafeState<PostsListWidget> {
|
||||
print("$e\n$stack");
|
||||
}
|
||||
}
|
||||
|
||||
/// Update a comment
|
||||
void _updateComment(Comment c) {
|
||||
if (_list == null) return;
|
||||
|
||||
try {
|
||||
final p = _list.singleWhere((p) => p.id == c.postID, orElse: () => null);
|
||||
|
||||
if (p == null) return;
|
||||
|
||||
final index = p.comments.indexWhere((d) => d.id == c.id);
|
||||
|
||||
if (index > -1) p.comments[index] = c;
|
||||
} catch (e, stack) {
|
||||
print("$e\n$stack");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user