@@ -12,6 +12,11 @@ import 'stream_video_push_params.dart';
1212
1313part 'stream_video_push_provider.dart' ;
1414
15+ const _idToken = 1 ;
16+ const _idCallIncoming = 2 ;
17+ const _idCallEnded = 3 ;
18+ const _idCallAcceptDecline = 4 ;
19+
1520/// Implementation of [PushNotificationManager] for Stream Video.
1621class StreamVideoPushNotificationManager implements PushNotificationManager {
1722 /// Factory for creating a new instance of [StreamVideoPushNotificationManager] .
@@ -45,7 +50,33 @@ class StreamVideoPushNotificationManager implements PushNotificationManager {
4550 required this .androidPushProvider,
4651 required this .pushParams,
4752 this .callerCustomizationCallback,
48- }) : _client = client;
53+ }) : _client = client {
54+ _subscriptions.add (
55+ _idCallIncoming,
56+ onCallEvent.whereType <ActionCallIncoming >().listen (
57+ (_) {
58+ _subscriptions.add (
59+ _idCallEnded,
60+ client.events.on < CoordinatorCallEndedEvent > (
61+ (_) {
62+ endAllCalls ();
63+ },
64+ ),
65+ );
66+ },
67+ ),
68+ );
69+
70+ _subscriptions.add (
71+ _idCallAcceptDecline,
72+ onCallEvent.whereType <ActionCallAccept >().map ((_) => null ).mergeWith (
73+ [onCallEvent.whereType <ActionCallDecline >().map ((_) => null )]).listen (
74+ (_) {
75+ _subscriptions.cancel (_idCallEnded);
76+ },
77+ ),
78+ );
79+ }
4980
5081 final CoordinatorClient _client;
5182 final StreamVideoPushProvider iosPushProvider;
@@ -55,7 +86,7 @@ class StreamVideoPushNotificationManager implements PushNotificationManager {
5586
5687 final _logger = taggedLogger (tag: 'SV:PNManager' );
5788
58- StreamSubscription < String > ? _tokenSubscription ;
89+ final Subscriptions _subscriptions = Subscriptions () ;
5990
6091 @override
6192 void registerDevice () {
@@ -79,7 +110,8 @@ class StreamVideoPushNotificationManager implements PushNotificationManager {
79110 );
80111 }
81112
82- _tokenSubscription ?? = pushProvider.onTokenRefresh.listen (registerDevice);
113+ _subscriptions.addIfAbsent (
114+ _idToken, () => pushProvider.onTokenRefresh.listen (registerDevice));
83115 }
84116
85117 @override
@@ -88,9 +120,7 @@ class StreamVideoPushNotificationManager implements PushNotificationManager {
88120 if (token == null ) return ;
89121
90122 _client.deleteDevice (id: token);
91-
92- _tokenSubscription? .cancel ();
93- _tokenSubscription = null ;
123+ _subscriptions.cancel (_idToken);
94124 }
95125
96126 @override
@@ -228,8 +258,7 @@ class StreamVideoPushNotificationManager implements PushNotificationManager {
228258
229259 @override
230260 Future <void > dispose () async {
231- _tokenSubscription? .cancel ();
232- _tokenSubscription = null ;
261+ _subscriptions.cancelAll ();
233262 }
234263}
235264
0 commit comments