Skip to content

Commit

Permalink
FLUT-284: Added onPeerListUpdate callback in preview (#1762)
Browse files Browse the repository at this point in the history
* Added onPeerListUpdate callback in preview

* 🤖 Automated Format and Fix

* Merged branch

* 🤖 Automated Format and Fix

---------

Co-authored-by: Decoder07 <[email protected]>
  • Loading branch information
Decoder07 and Decoder07 committed Jul 29, 2024
1 parent 3ef492b commit 9a8860d
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/hms_room_kit/lib/src/preview/preview_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,11 @@ class PreviewStore extends ChangeNotifier
}
notifyListeners();
}

@override
void onPeerListUpdate(
{required List<HMSPeer> addedPeers,
required List<HMSPeer> removedPeers}) {
log("onPeerListUpdate -> addedPeers: $addedPeers removedPeers: $removedPeers");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1516,6 +1516,34 @@ class HmssdkFlutterPlugin :
}
}
}

override fun peerListUpdated(addedPeers: ArrayList<HMSPeer>?, removedPeers: ArrayList<HMSPeer>?){
val args = HashMap<String, Any?>()
args["event_name"] = "on_peer_list_update"
val parameters = HashMap<String, Any?>()
val peersAdded = ArrayList<HashMap<String, Any?>?>()
val peersRemoved = ArrayList<HashMap<String, Any?>?>()
/**
* Here we add peers to the list after parsing the
* peer object
*/
addedPeers?.forEach { peer ->
peersAdded.add(HMSPeerExtension.toDictionary(peer))
}

removedPeers?.forEach { peer ->
peersRemoved.add(HMSPeerExtension.toDictionary(peer))
}

parameters["added_peers"] = peersAdded
parameters["removed_peers"] = peersRemoved

args["data"] = parameters

CoroutineScope(Dispatchers.Main).launch {
previewSink?.success(args)
}
}
}

/***
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1276,6 +1276,7 @@ public class SwiftHmssdkFlutterPlugin: NSObject, FlutterPlugin, HMSUpdateListene
previewEnded = false
previewSink?(data)
}

var previewEnded = false
public func on(join room: HMSRoom) {
previewEnded = true
Expand Down Expand Up @@ -1496,7 +1497,11 @@ public class SwiftHmssdkFlutterPlugin: NSObject, FlutterPlugin, HMSUpdateListene

let data = ["event_name": "on_peer_list_update", "data": parameters] as [String: Any]

eventSink?(data)
if(previewEnded){
eventSink?(data)
}else{
previewSink?(data)
}
}

// MARK: - RTC Stats Listeners
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ enum HMSPreviewUpdateListenerMethod {
onPeerUpdate,
onRoomUpdate,
onAudioDeviceChanged,
onPeerListUpdate,
unknown
}

Expand All @@ -21,6 +22,8 @@ extension HMSPreviewUpdateListenerMethodValues
return HMSPreviewUpdateListenerMethod.onRoomUpdate;
case 'on_audio_device_changed':
return HMSPreviewUpdateListenerMethod.onAudioDeviceChanged;
case 'on_peer_list_update':
return HMSPreviewUpdateListenerMethod.onPeerListUpdate;
default:
return HMSPreviewUpdateListenerMethod.unknown;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,13 @@ abstract class HMSPreviewListener {
void onAudioDeviceChanged(
{HMSAudioDevice? currentAudioDevice,
List<HMSAudioDevice>? availableAudioDevice});

///Upon joining a room with existing peers, onPeerListUpdated will be called with the list of peers present
///in the room instead of getting onPeerUpdate for each peer in the room.
///Subsequent peer joins/leaves would be notified via both onPeerUpdate and onPeerListUpdated
/// - Parameters:
/// - addedPeers: List of peers who joined the room
/// - removedPeers: List of peers who left the room
void onPeerListUpdate(
{required List<HMSPeer> addedPeers, required List<HMSPeer> removedPeers});
}
27 changes: 27 additions & 0 deletions packages/hmssdk_flutter/lib/src/service/platform_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,27 @@ abstract class PlatformService {
: null
});
break;
case HMSPreviewUpdateListenerMethod.onPeerListUpdate:
List<HMSPeer> addedPeers = [];
List<HMSPeer> removedPeers = [];

if (event.data.containsKey("added_peers") &&
event.data["added_peers"] != null) {
for (var peer in event.data["added_peers"]) {
addedPeers.add(HMSPeer.fromMap(peer));
}
}

if (event.data.containsKey("removed_peers") &&
event.data["removed_peers"] != null) {
for (var peer in event.data["removed_peers"]) {
removedPeers.add(HMSPeer.fromMap(peer));
}
}

notifyPreviewListeners(method,
{"added_peers": addedPeers, "removed_peers": removedPeers});
break;
}
});

Expand Down Expand Up @@ -614,6 +635,12 @@ abstract class PlatformService {
availableAudioDevice: arguments["available_audio_device"]);
});
break;
case HMSPreviewUpdateListenerMethod.onPeerListUpdate:
previewListeners.forEach((e) {
e.onPeerListUpdate(
addedPeers: arguments["added_peers"],
removedPeers: arguments["removed_peers"]);
});
}
}

Expand Down

0 comments on commit 9a8860d

Please sign in to comment.