Skip to content

Commit 27673d5

Browse files
authored
fix(ui): extract callkit manager to make it independent of flutter initialization (#1140)
* extract callkit manager to make it independent of flutter initialization * changelog
1 parent e3e80cd commit 27673d5

File tree

4 files changed

+52
-23
lines changed

4 files changed

+52
-23
lines changed

packages/stream_video_flutter/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## Upcoming
2+
3+
🐞 Fixed
4+
* [iOS/macOS] Fixed crash when VoIP push is received before Flutter fully initializes from the terminated state.
5+
16
## 1.2.0
27

38
### ✅ Added

packages/stream_video_push_notification/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## Upcoming
2+
3+
🐞 Fixed
4+
* [iOS/macOS] Fixed crash when VoIP push is received before Flutter fully initializes from the terminated state.
5+
16
## 1.2.0
27
* Sync version with `stream_video_flutter` 1.2.0
38

packages/stream_video_push_notification/ios/stream_video_push_notification/Sources/stream_video_push_notification/StreamVideoCallkitManager.swift

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import UIKit
66
@available(iOS 10.0, *)
77
public class StreamVideoCallkitManager: NSObject, CXProviderDelegate {
88

9-
@objc public private(set) static var sharedInstance: StreamVideoCallkitManager!
9+
@objc public static let shared = StreamVideoCallkitManager()
1010

11-
private var eventHandler: EventCallbackHandler
11+
private var eventHandler: EventCallbackHandler?
12+
private var pendingEvents: [(event: String, body: [String: Any?]?)] = []
1213

1314
private var callController: StreamCallKitCallController
1415

@@ -21,25 +22,44 @@ public class StreamVideoCallkitManager: NSObject, CXProviderDelegate {
2122
private var isFromPushKit: Bool = false
2223
private var silenceEvents: Bool = false
2324

24-
public init(
25-
eventHandler: EventCallbackHandler
26-
) {
27-
self.eventHandler = eventHandler
25+
private override init() {
2826
callController = StreamCallKitCallController()
27+
super.init()
28+
}
29+
30+
public func setEventHandler(_ handler: EventCallbackHandler) {
31+
self.eventHandler = handler
32+
flushPendingEvents()
33+
}
34+
35+
private func flushPendingEvents() {
36+
guard let eventHandler = eventHandler else { return }
37+
38+
for pendingEvent in pendingEvents {
39+
eventHandler.send(pendingEvent.event, pendingEvent.body ?? [:])
40+
}
41+
42+
pendingEvents.removeAll()
2943
}
3044

3145
private func sendEvent(_ event: String, _ body: [String: Any?]?) {
3246
if silenceEvents {
3347
print(event, " silenced")
3448
return
35-
} else {
36-
eventHandler.send(event, body ?? [:])
3749
}
3850

51+
guard let eventHandler = eventHandler else {
52+
pendingEvents.append((event: event, body: body))
53+
return
54+
}
55+
56+
eventHandler.send(event, body ?? [:])
3957
}
4058

4159
@objc public func sendEventCustom(_ event: String, body: NSDictionary?) {
42-
eventHandler.send(event, body ?? [:])
60+
// Convert NSDictionary to [String: Any?] for queueing if needed
61+
let bodyDict = body as? [String: Any?]
62+
sendEvent(event, bodyDict)
4363
}
4464

4565
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {

packages/stream_video_push_notification/ios/stream_video_push_notification/Sources/stream_video_push_notification/StreamVideoPushNotificationPlugin.swift

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ import Flutter
22
import UIKit
33

44
public class StreamVideoPushNotificationPlugin: NSObject, FlutterPlugin {
5-
private let devicePushTokenVoIP = "DevicePushTokenVoIP"
5+
private static let devicePushTokenVoIPKey = "DevicePushTokenVoIP"
66

77
let persistentState: UserDefaults = UserDefaults.standard
88

9-
@objc public private(set) static var sharedInstance: StreamVideoPushNotificationPlugin!
9+
@objc public private(set) static var sharedInstance: StreamVideoPushNotificationPlugin?
1010

1111
private var callKitManager: StreamVideoCallkitManager
1212

13-
public init(callKitManager: StreamVideoCallkitManager) {
14-
self.callKitManager = callKitManager
13+
private override init() {
14+
self.callKitManager = StreamVideoCallkitManager.shared
1515
super.init()
1616
}
1717

@@ -24,10 +24,10 @@ public class StreamVideoPushNotificationPlugin: NSObject, FlutterPlugin {
2424
let eventsHandler = EventCallbackHandler()
2525
eventChannel.setStreamHandler(eventsHandler)
2626

27-
let callKitManager = StreamVideoCallkitManager(eventHandler: eventsHandler)
28-
sharedInstance = StreamVideoPushNotificationPlugin(callKitManager: callKitManager)
27+
StreamVideoCallkitManager.shared.setEventHandler(eventsHandler)
2928

30-
registrar.addMethodCallDelegate(sharedInstance, channel: mainChannel)
29+
sharedInstance = StreamVideoPushNotificationPlugin()
30+
registrar.addMethodCallDelegate(sharedInstance!, channel: mainChannel)
3131

3232
StreamVideoPKDelegateManager.shared.initChannel(mainChannel: mainChannel)
3333
}
@@ -59,35 +59,34 @@ public class StreamVideoPushNotificationPlugin: NSObject, FlutterPlugin {
5959
}
6060

6161
@objc public static func setDevicePushTokenVoIP(deviceToken: String) {
62-
sharedInstance.setDevicePushTokenVoIP(deviceToken: deviceToken)
63-
//TODO: send event? //ACTION_DID_UPDATE_DEVICE_PUSH_TOKEN_VOIP
62+
sharedInstance?.setDevicePushTokenVoIP(deviceToken: deviceToken)
6463
}
6564

6665
@objc public static func startOutgoingCall(
6766
data: CallData,
6867
fromPushKit: Bool
6968
) {
70-
sharedInstance.callKitManager.startCall(data, fromPushKit: fromPushKit)
69+
StreamVideoCallkitManager.shared.startCall(data, fromPushKit: fromPushKit)
7170
}
7271

7372
@objc public static func showIncomingCall(
7473
data: CallData,
7574
fromPushKit: Bool
7675
) {
77-
sharedInstance.callKitManager.showIncomingCall(
76+
StreamVideoCallkitManager.shared.showIncomingCall(
7877
data, fromPushKit: fromPushKit)
7978
}
8079

8180
@objc public static func activeCalls() -> [[String: Any]]? {
82-
sharedInstance.callKitManager.activeCalls()
81+
return StreamVideoCallkitManager.shared.activeCalls()
8382
}
8483

8584
@objc public func setDevicePushTokenVoIP(deviceToken: String) {
86-
persistentState.set(deviceToken, forKey: devicePushTokenVoIP)
85+
persistentState.set(deviceToken, forKey: Self.devicePushTokenVoIPKey)
8786
}
8887

8988
@objc public func getDevicePushTokenVoIP() -> String {
90-
return persistentState.string(forKey: devicePushTokenVoIP) ?? ""
89+
return persistentState.string(forKey: Self.devicePushTokenVoIPKey) ?? ""
9190
}
9291
}
9392

0 commit comments

Comments
 (0)