Browse Source

feat(ios) added ability to use an external CXProvider and CXCallController

j8
sorinant 4 years ago
parent
commit
e261bb5616
No account linked to committer's email address
1 changed files with 55 additions and 18 deletions
  1. 55
    18
      ios/sdk/src/callkit/JMCallKitProxy.swift

+ 55
- 18
ios/sdk/src/callkit/JMCallKitProxy.swift View File

18
 import CallKit
18
 import CallKit
19
 import Foundation
19
 import Foundation
20
 
20
 
21
+public protocol CXProviderProtocol: class {
22
+    var configuration: CXProviderConfiguration { get set }
23
+    func setDelegate(_ delegate: CXProviderDelegate?, queue: DispatchQueue?)
24
+    func reportNewIncomingCall(with UUID: UUID, update: CXCallUpdate, completion: @escaping (Error?) -> Void)
25
+    func reportCall(with UUID: UUID, updated update: CXCallUpdate)
26
+    func reportCall(with UUID: UUID, endedAt dateEnded: Date?, reason endedReason: CXCallEndedReason)
27
+    func reportOutgoingCall(with UUID: UUID, startedConnectingAt dateStartedConnecting: Date?)
28
+    func reportOutgoingCall(with UUID: UUID, connectedAt dateConnected: Date?)
29
+    func invalidate()
30
+}
31
+
32
+public protocol CXCallControllerProtocol: class {
33
+    var calls: [CXCall] { get }
34
+    func request(_ transaction: CXTransaction, completion: @escaping (Error?) -> Swift.Void)
35
+}
36
+
37
+extension CXProvider: CXProviderProtocol {}
38
+extension CXCallController: CXCallControllerProtocol {
39
+    public var calls: [CXCall] {
40
+        return callObserver.calls
41
+    }
42
+}
43
+
21
 /// JitsiMeet CallKit proxy
44
 /// JitsiMeet CallKit proxy
22
 // NOTE: The methods this class exposes are meant to be called in the UI thread.
45
 // NOTE: The methods this class exposes are meant to be called in the UI thread.
23
 // All delegate methods called by JMCallKitEmitter will be called in the UI thread.
46
 // All delegate methods called by JMCallKitEmitter will be called in the UI thread.
26
     private override init() {}
49
     private override init() {}
27
 
50
 
28
     // MARK: - CallKit proxy
51
     // MARK: - CallKit proxy
52
+    
53
+    public static var callKitProvider: CXProviderProtocol?
54
+    public static var callKitCallController: CXCallControllerProtocol?
29
 
55
 
30
-    private static var provider: CXProvider = {
31
-        let configuration = CXProviderConfiguration(localizedName: "")
32
-        return CXProvider(configuration: configuration)
33
-    }()
56
+    private static var provider: CXProviderProtocol {
57
+        callKitProvider ?? defaultProvider
58
+    }
59
+    
60
+    private static var callController: CXCallControllerProtocol {
61
+        callKitCallController ?? defaultCallController
62
+    }
34
 
63
 
35
     private static var providerConfiguration: CXProviderConfiguration? {
64
     private static var providerConfiguration: CXProviderConfiguration? {
36
         didSet {
65
         didSet {
39
             provider.setDelegate(emitter, queue: nil)
68
             provider.setDelegate(emitter, queue: nil)
40
         }
69
         }
41
     }
70
     }
42
-
43
-    private static let callController: CXCallController = {
71
+    
72
+    private static let defaultCallController: CXCallController = {
44
         return CXCallController()
73
         return CXCallController()
45
     }()
74
     }()
75
+    
76
+    private static var defaultProvider: CXProvider = {
77
+        let configuration = CXProviderConfiguration(localizedName: "")
78
+        return CXProvider(configuration: configuration)
79
+    }()
46
 
80
 
47
     private static let emitter: JMCallKitEmitter = {
81
     private static let emitter: JMCallKitEmitter = {
48
         return JMCallKitEmitter()
82
         return JMCallKitEmitter()
52
     /// Defaults to enabled, set to false when you don't want to use CallKit.
86
     /// Defaults to enabled, set to false when you don't want to use CallKit.
53
     @objc public static var enabled: Bool = true {
87
     @objc public static var enabled: Bool = true {
54
         didSet {
88
         didSet {
55
-            provider.invalidate()
89
+            if callKitProvider == nil {
90
+                provider.invalidate()
91
+            }
92
+            
56
             if enabled {
93
             if enabled {
57
-                guard isProviderConfigured() else  { return; }
58
-                provider = CXProvider(configuration: providerConfiguration!)
94
+                guard isProviderConfigured() else  { return }
95
+                if callKitProvider == nil {
96
+                    defaultProvider = CXProvider(configuration: providerConfiguration!)
97
+                }
98
+                
59
                 provider.setDelegate(emitter, queue: nil)
99
                 provider.setDelegate(emitter, queue: nil)
60
             } else {
100
             } else {
61
                 provider.setDelegate(nil, queue: nil)
101
                 provider.setDelegate(nil, queue: nil)
92
     }
132
     }
93
 
133
 
94
     @objc public static func hasActiveCallForUUID(_ callUUID: String) -> Bool {
134
     @objc public static func hasActiveCallForUUID(_ callUUID: String) -> Bool {
95
-        let activeCallForUUID = callController.callObserver.calls.first {
135
+        let activeCallForUUID = callController.calls.first {
96
             $0.uuid == UUID(uuidString: callUUID)
136
             $0.uuid == UUID(uuidString: callUUID)
97
         }
137
         }
98
         guard activeCallForUUID != nil else { return false }
138
         guard activeCallForUUID != nil else { return false }
99
         return true
139
         return true
100
     }
140
     }
101
 
141
 
102
-    @objc public static func reportNewIncomingCall(
103
-            UUID: UUID,
104
-            handle: String?,
105
-            displayName: String?,
106
-            hasVideo: Bool,
107
-            completion: @escaping (Error?) -> Void) {
142
+    @objc public static func reportNewIncomingCall(UUID: UUID,
143
+                                                   handle: String?,
144
+                                                   displayName: String?,
145
+                                                   hasVideo: Bool,
146
+                                                   completion: @escaping (Error?) -> Void) {
108
         guard enabled else { return }
147
         guard enabled else { return }
109
 
148
 
110
         let callUpdate = makeCXUpdate(handle: handle,
149
         let callUpdate = makeCXUpdate(handle: handle,
132
             endedAt dateEnded: Date?,
171
             endedAt dateEnded: Date?,
133
             reason endedReason: CXCallEndedReason) {
172
             reason endedReason: CXCallEndedReason) {
134
         guard enabled else { return }
173
         guard enabled else { return }
135
-
136
         provider.reportCall(with: UUID,
174
         provider.reportCall(with: UUID,
137
                             endedAt: dateEnded,
175
                             endedAt: dateEnded,
138
                             reason: endedReason)
176
                             reason: endedReason)
142
             with UUID: UUID,
180
             with UUID: UUID,
143
             startedConnectingAt dateStartedConnecting: Date?) {
181
             startedConnectingAt dateStartedConnecting: Date?) {
144
         guard enabled else { return }
182
         guard enabled else { return }
145
-
146
         provider.reportOutgoingCall(with: UUID,
183
         provider.reportOutgoingCall(with: UUID,
147
                                     startedConnectingAt: dateStartedConnecting)
184
                                     startedConnectingAt: dateStartedConnecting)
148
     }
185
     }

Loading…
Cancel
Save