diff --git a/lib/helpers/events_helper.dart b/lib/helpers/events_helper.dart index fbbe5c1..a77247f 100644 --- a/lib/helpers/events_helper.dart +++ b/lib/helpers/events_helper.dart @@ -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(); diff --git a/lib/helpers/websocket_helper.dart b/lib/helpers/websocket_helper.dart index f27864b..01edb5f 100644 --- a/lib/helpers/websocket_helper.dart +++ b/lib/helpers/websocket_helper.dart @@ -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}"); } diff --git a/lib/ui/tiles/post_tile.dart b/lib/ui/tiles/post_tile.dart index 18b4285..4434546 100644 --- a/lib/ui/tiles/post_tile.dart +++ b/lib/ui/tiles/post_tile.dart @@ -510,10 +510,6 @@ class _PostTileState extends State { 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 { 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"), ); diff --git a/lib/ui/widgets/posts_list_widget.dart b/lib/ui/widgets/posts_list_widget.dart index b3a5edc..dd338e4 100644 --- a/lib/ui/widgets/posts_list_widget.dart +++ b/lib/ui/widgets/posts_list_widget.dart @@ -70,6 +70,8 @@ class PostsListWidgetState extends SafeState { // Register to events this.listen((ev) => this._addComment(ev.comment)); + this.listenChangeState( + (ev) => this._updateComment(ev.comment)); } @override @@ -244,4 +246,21 @@ class PostsListWidgetState extends SafeState { 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"); + } + } }