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

Handles new comments events

This commit is contained in:
Pierre HUBERT 2020-04-18 16:35:53 +02:00
parent 02034acbbe
commit a60c1ed68c
4 changed files with 44 additions and 22 deletions

View File

@ -1,5 +1,6 @@
import 'dart:async'; import 'dart:async';
import 'package:comunic/models/comment.dart';
import 'package:event_bus/event_bus.dart'; import 'package:event_bus/event_bus.dart';
/// Events helper /// Events helper
@ -23,6 +24,13 @@ class NewNumberUnreadConversations {
NewNumberUnreadConversations(this.newNum); NewNumberUnreadConversations(this.newNum);
} }
/// New comment
class NewCommentEvent {
final Comment comment;
NewCommentEvent(this.comment);
}
class EventsHelper { class EventsHelper {
static EventBus _mgr = EventBus(); static EventBus _mgr = EventBus();

View File

@ -1,6 +1,7 @@
import 'dart:async'; import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'package:comunic/helpers/comments_helper.dart';
import 'package:comunic/helpers/events_helper.dart'; import 'package:comunic/helpers/events_helper.dart';
import 'package:comunic/models/api_request.dart'; import 'package:comunic/models/api_request.dart';
import 'package:comunic/models/config.dart'; import 'package:comunic/models/config.dart';
@ -39,8 +40,7 @@ class WebSocketHelper {
// Determine WebSocket URI // Determine WebSocket URI
final wsURL = final wsURL =
"${(config().apiServerSecure ? "wss" : "ws")}://${config() "${(config().apiServerSecure ? "wss" : "ws")}://${config().apiServerName}${config().apiServerUri}ws?token=$token";
.apiServerName}${config().apiServerUri}ws?token=$token";
final wsURI = Uri.parse(wsURL); final wsURI = Uri.parse(wsURL);
// Connect // Connect
@ -48,7 +48,7 @@ class WebSocketHelper {
_ws.stream.listen( _ws.stream.listen(
// When we got data // When we got data
(data) { (data) {
print("WS New data: $data"); print("WS New data: $data");
_processMessage(data.toString()); _processMessage(data.toString());
}, },
@ -110,16 +110,22 @@ class WebSocketHelper {
/// Process an unattended message /// Process an unattended message
static _processUnattendedMessage(WsMessage msg) { static _processUnattendedMessage(WsMessage msg) {
switch (msg.title) { switch (msg.title) {
// New number of notifications // New number of notifications
case "number_notifs": case "number_notifs":
EventsHelper.emit(NewNumberNotifsEvent(msg.data)); EventsHelper.emit(NewNumberNotifsEvent(msg.data));
break; break;
// New number of unread conversations // New number of unread conversations
case "number_unread_conversations": case "number_unread_conversations":
EventsHelper.emit(NewNumberUnreadConversations(msg.data)); EventsHelper.emit(NewNumberUnreadConversations(msg.data));
break; break;
// New comment
case "new_comment":
EventsHelper.emit(
NewCommentEvent(CommentsHelper.apiToComment(msg.data)));
break;
default: default:
throw Exception("Unknown message type: ${msg.title}"); throw Exception("Unknown message type: ${msg.title}");
} }

View File

@ -4,7 +4,6 @@ import 'package:comunic/enums/post_kind.dart';
import 'package:comunic/enums/post_visibility_level.dart'; import 'package:comunic/enums/post_visibility_level.dart';
import 'package:comunic/helpers/comments_helper.dart'; import 'package:comunic/helpers/comments_helper.dart';
import 'package:comunic/helpers/posts_helper.dart'; import 'package:comunic/helpers/posts_helper.dart';
import 'package:comunic/helpers/users_helper.dart';
import 'package:comunic/lists/groups_list.dart'; import 'package:comunic/lists/groups_list.dart';
import 'package:comunic/lists/users_list.dart'; import 'package:comunic/lists/users_list.dart';
import 'package:comunic/models/comment.dart'; import 'package:comunic/models/comment.dart';
@ -493,22 +492,6 @@ class _PostTileState extends State<PostTile> {
if (commentID < 1) throw new Exception("Comment ID is inferior to 1!"); if (commentID < 1) throw new Exception("Comment ID is inferior to 1!");
clearCommentForm(); clearCommentForm();
// Get and show new comment
final newComment = await _commentsHelper.getSingle(commentID);
if (newComment == null)
return showSimpleSnack(
context, tr("Could not retrieve created comment!"));
// Get information about the user who created the comment (if required)
if (!widget.usersInfo.hasUser(newComment.userID))
widget.usersInfo
.add(await UsersHelper().getSingleWithThrow(newComment.userID));
setState(() {
widget.post.comments.add(newComment);
});
} catch (e) { } catch (e) {
print(e); print(e);
showSimpleSnack(context, tr("Could not create comment!")); showSimpleSnack(context, tr("Could not create comment!"));

View File

@ -1,9 +1,11 @@
import 'package:comunic/helpers/events_helper.dart';
import 'package:comunic/helpers/groups_helper.dart'; import 'package:comunic/helpers/groups_helper.dart';
import 'package:comunic/helpers/posts_helper.dart'; import 'package:comunic/helpers/posts_helper.dart';
import 'package:comunic/helpers/users_helper.dart'; import 'package:comunic/helpers/users_helper.dart';
import 'package:comunic/lists/groups_list.dart'; import 'package:comunic/lists/groups_list.dart';
import 'package:comunic/lists/posts_list.dart'; import 'package:comunic/lists/posts_list.dart';
import 'package:comunic/lists/users_list.dart'; import 'package:comunic/lists/users_list.dart';
import 'package:comunic/models/comment.dart';
import 'package:comunic/models/post.dart'; import 'package:comunic/models/post.dart';
import 'package:comunic/ui/screens/conversation_screen.dart'; import 'package:comunic/ui/screens/conversation_screen.dart';
import 'package:comunic/ui/tiles/post_tile.dart'; import 'package:comunic/ui/tiles/post_tile.dart';
@ -65,6 +67,9 @@ class PostsListWidgetState extends SafeState<PostsListWidget> {
super.initState(); super.initState();
_scrollController = ScrollWatcher(onReachBottom: reachedPostsBottom); _scrollController = ScrollWatcher(onReachBottom: reachedPostsBottom);
// Register to events
this.listen<NewCommentEvent>((ev) => this._addComment(ev.comment));
} }
@override @override
@ -219,4 +224,24 @@ class PostsListWidgetState extends SafeState<PostsListWidget> {
void reachedPostsBottom() { void reachedPostsBottom() {
if (widget.getOlder != null) loadPostsList(getOlder: true); if (widget.getOlder != null) loadPostsList(getOlder: true);
} }
/// Add new comment
void _addComment(Comment c) async {
if (_list == null) return;
try {
final p = _list.singleWhere((p) => p.id == c.postID, orElse: () => null);
if (p == null) return;
if (!_users.hasUser(c.userID))
_users.add(await UsersHelper().getSingleWithThrow(c.userID));
setState(() {
p.comments.add(c);
});
} catch (e, stack) {
print("$e\n$stack");
}
}
} }