Skip to content

Commit 3208d02

Browse files
authored
feat: ringing call cancelation (#544)
* ringing call cancelation * tweak
1 parent c6045e4 commit 3208d02

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

packages/stream_video/lib/src/utils/subscriptions.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ class Subscriptions {
88
_subscriptions[id] = subscription;
99
}
1010

11+
void addIfAbsent(int id, StreamSubscription<dynamic> Function() ifAbsent) {
12+
if (_subscriptions.containsKey(id)) return;
13+
14+
_subscriptions[id] = ifAbsent();
15+
}
16+
1117
void cancel(int id) {
1218
_subscriptions[id]?.cancel();
1319
_subscriptions.remove(id);

packages/stream_video_push_notification/lib/src/stream_video_push_notification.dart

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ import 'stream_video_push_params.dart';
1212

1313
part '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.
1621
class 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

Comments
 (0)