From dab4e7bde14071fd3b30e602286821ea31447756 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 18 Apr 2020 16:57:00 +0200 Subject: [PATCH] Handles comment deletion events --- lib/helpers/events_helper.dart | 8 ++++++++ lib/helpers/websocket_helper.dart | 5 +++++ lib/ui/tiles/post_tile.dart | 5 ----- lib/ui/widgets/posts_list_widget.dart | 9 +++++++++ 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/helpers/events_helper.dart b/lib/helpers/events_helper.dart index a77247f..fd5a7c1 100644 --- a/lib/helpers/events_helper.dart +++ b/lib/helpers/events_helper.dart @@ -38,6 +38,14 @@ class UpdatedCommentEvent { UpdatedCommentEvent(this.comment); } +/// Deleted comment +class DeletedCommentEvent { + final int commentID; + + DeletedCommentEvent(this.commentID); +} + + class EventsHelper { static EventBus _mgr = EventBus(); diff --git a/lib/helpers/websocket_helper.dart b/lib/helpers/websocket_helper.dart index 01edb5f..a69c9f0 100644 --- a/lib/helpers/websocket_helper.dart +++ b/lib/helpers/websocket_helper.dart @@ -132,6 +132,11 @@ class WebSocketHelper { UpdatedCommentEvent(CommentsHelper.apiToComment(msg.data))); break; + // Deleted comment + case "comment_deleted": + EventsHelper.emit(DeletedCommentEvent(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 4434546..b2f00e2 100644 --- a/lib/ui/tiles/post_tile.dart +++ b/lib/ui/tiles/post_tile.dart @@ -523,11 +523,6 @@ class _PostTileState extends State { showSimpleSnack(context, tr("Could not delete the comment!")); return; } - - // Remove the comment from the list - setState(() { - widget.post.comments.remove(comment); - }); } /// Method called each time the user has selected an option diff --git a/lib/ui/widgets/posts_list_widget.dart b/lib/ui/widgets/posts_list_widget.dart index dd338e4..d17a9d5 100644 --- a/lib/ui/widgets/posts_list_widget.dart +++ b/lib/ui/widgets/posts_list_widget.dart @@ -72,6 +72,8 @@ class PostsListWidgetState extends SafeState { this.listen((ev) => this._addComment(ev.comment)); this.listenChangeState( (ev) => this._updateComment(ev.comment)); + this.listenChangeState( + (ev) => this._removeComment(ev.commentID)); } @override @@ -263,4 +265,11 @@ class PostsListWidgetState extends SafeState { print("$e\n$stack"); } } + + /// Remove a comment from the list + void _removeComment(int commentID) { + if (_list == null) return; + + _list.forEach((p) => p.comments.removeWhere((c) => c.id == commentID)); + } }