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); NewCommentEvent(this.comment);
} }
/// Updated comment
class UpdatedCommentEvent {
final Comment comment;
UpdatedCommentEvent(this.comment);
}
class EventsHelper { class EventsHelper {
static EventBus _mgr = EventBus(); static EventBus _mgr = EventBus();

View File

@ -126,6 +126,12 @@ class WebSocketHelper {
NewCommentEvent(CommentsHelper.apiToComment(msg.data))); NewCommentEvent(CommentsHelper.apiToComment(msg.data)));
break; break;
// Updated comment
case "comment_updated":
EventsHelper.emit(
UpdatedCommentEvent(CommentsHelper.apiToComment(msg.data)));
break;
default: default:
throw Exception("Unknown message type: ${msg.title}"); 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))) if (!(await _commentsHelper.updateContent(comment.id, newContent)))
return showSimpleSnack(context, tr("Could not update comment content!")); return showSimpleSnack(context, tr("Could not update comment content!"));
setState(() {
comment.content.content = newContent;
});
} }
/// Process the deletion of a user /// Process the deletion of a user
@ -575,7 +571,7 @@ class _PostTileState extends State<PostTile> {
title: tr("Update post content"), title: tr("Update post content"),
message: tr("Please enter message content: "), message: tr("Please enter message content: "),
defaultValue: defaultValue:
widget.post.content.isNull == null ? "" : widget.post.content.content, widget.post.content.isNull == null ? "" : widget.post.content.content,
hint: tr("Post content"), hint: tr("Post content"),
); );

View File

@ -70,6 +70,8 @@ class PostsListWidgetState extends SafeState<PostsListWidget> {
// Register to events // Register to events
this.listen<NewCommentEvent>((ev) => this._addComment(ev.comment)); this.listen<NewCommentEvent>((ev) => this._addComment(ev.comment));
this.listenChangeState<UpdatedCommentEvent>(
(ev) => this._updateComment(ev.comment));
} }
@override @override
@ -244,4 +246,21 @@ class PostsListWidgetState extends SafeState<PostsListWidget> {
print("$e\n$stack"); 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");
}
}
} }