mirror of
				https://gitlab.com/comunic/comunicmobile
				synced 2025-11-04 04:04:18 +00:00 
			
		
		
		
	Register to comment udpate events
This commit is contained in:
		@@ -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();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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}");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user