diff --git a/lib/helpers/events_helper.dart b/lib/helpers/events_helper.dart index aa87eff..0591109 100644 --- a/lib/helpers/events_helper.dart +++ b/lib/helpers/events_helper.dart @@ -70,6 +70,22 @@ class DeletedConversationMessageEvent { DeletedConversationMessageEvent(this.msg); } +/// User joined call event +class UserJoinedCallEvent { + final int callID; + final int userID; + + UserJoinedCallEvent(this.callID, this.userID); +} + +/// User left call event +class UserLeftCallEvent { + final int callID; + final int userID; + + UserLeftCallEvent(this.callID, this.userID); +} + class EventsHelper { static EventBus _mgr = EventBus(); diff --git a/lib/helpers/websocket_helper.dart b/lib/helpers/websocket_helper.dart index 82eb501..25ec323 100644 --- a/lib/helpers/websocket_helper.dart +++ b/lib/helpers/websocket_helper.dart @@ -161,6 +161,18 @@ class WebSocketHelper { ConversationsHelper.apiToConversationMessage(msg.data))); break; + // A user joined a call + case "user_joined_call": + EventsHelper.emit( + UserJoinedCallEvent(msg.data["callID"], msg.data["userID"])); + break; + + // A user left a call + case "user_left_call": + EventsHelper.emit( + UserLeftCallEvent(msg.data["callID"], msg.data["userID"])); + break; + default: throw Exception("Unknown message type: ${msg.title}"); } diff --git a/lib/models/call_member.dart b/lib/models/call_member.dart index e077507..96462ce 100644 --- a/lib/models/call_member.dart +++ b/lib/models/call_member.dart @@ -12,7 +12,7 @@ class CallMember { const CallMember({ @required this.id, - @required this.status, + this.status = MemberStatus.JOINED, }) : assert(id != null), assert(status != null); } diff --git a/lib/ui/screens/call_screen.dart b/lib/ui/screens/call_screen.dart index 22aa7e7..4a783ff 100644 --- a/lib/ui/screens/call_screen.dart +++ b/lib/ui/screens/call_screen.dart @@ -1,5 +1,6 @@ import 'package:comunic/helpers/calls_helper.dart'; import 'package:comunic/helpers/conversations_helper.dart'; +import 'package:comunic/helpers/events_helper.dart'; import 'package:comunic/helpers/users_helper.dart'; import 'package:comunic/lists/call_members_list.dart'; import 'package:comunic/lists/users_list.dart'; @@ -78,6 +79,15 @@ class _CallScreenState extends SafeState { _membersList = membersList; setState(() {}); + + // Register to events + this.listenChangeState((e) { + if (e.callID == convID) _membersList.add(CallMember(id: e.userID)); + }); + + this.listen((e) { + if (e.callID == convID) _removeMember(e.userID); + }); } catch (e, stack) { print("Could not initialize call! $e\n$stack"); setState(() => _error = true); @@ -93,6 +103,11 @@ class _CallScreenState extends SafeState { } } + void _removeMember(int memberID) { + _membersList.removeUser(memberID); + setState(() {}); + } + @override Widget build(BuildContext context) { return Scaffold(