1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 21:09:21 +00:00

Register to comment udpate events

This commit is contained in:
Pierre HUBERT 2020-04-18 16:46:55 +02:00
parent a60c1ed68c
commit 6b08b62832
4 changed files with 33 additions and 5 deletions

View File

@ -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();

View File

@ -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}");
}

View File

@ -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

View File

@ -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");
}
}
}