|
2 | 2 |
|
3 | 3 | import { |
4 | 4 | NativeModules, |
5 | | - NativeEventEmitter, |
6 | 5 | Platform, |
7 | 6 | } from 'react-native'; |
8 | 7 |
|
| 8 | +import { listeners } from './actions' |
| 9 | + |
9 | 10 | const _RNCallKit = NativeModules.RNCallKit; |
10 | | -const _RNCallKitEmitter = new NativeEventEmitter(_RNCallKit); |
11 | 11 |
|
12 | 12 | const _callkitEventHandlers = new Map(); |
13 | 13 |
|
14 | | -const RNCallKitDidReceiveStartCallAction = 'RNCallKitDidReceiveStartCallAction'; |
15 | | -const RNCallKitPerformAnswerCallAction = 'RNCallKitPerformAnswerCallAction'; |
16 | | -const RNCallKitPerformEndCallAction = 'RNCallKitPerformEndCallAction'; |
17 | | -const RNCallKitDidActivateAudioSession = 'RNCallKitDidActivateAudioSession'; |
18 | | -const RNCallKitDidDisplayIncomingCall = 'RNCallKitDidDisplayIncomingCall'; |
19 | | -const RNCallKitDidPerformSetMutedCallAction = 'RNCallKitDidPerformSetMutedCallAction'; |
20 | | - |
21 | 14 | export default class RNCallKit { |
| 15 | + |
22 | 16 | static addEventListener(type, handler) { |
23 | 17 | if (Platform.OS !== 'ios') return; |
24 | | - var listener; |
25 | | - if (type === 'didReceiveStartCallAction') { |
26 | | - listener = _RNCallKitEmitter.addListener( |
27 | | - RNCallKitDidReceiveStartCallAction, |
28 | | - (data) => { handler(data);} |
29 | | - ); |
30 | | - _RNCallKit._startCallActionEventListenerAdded(); |
31 | | - } else if (type === 'answerCall') { |
32 | | - listener = _RNCallKitEmitter.addListener( |
33 | | - RNCallKitPerformAnswerCallAction, |
34 | | - (data) => { handler(data);} |
35 | | - ); |
36 | | - } else if (type === 'endCall') { |
37 | | - listener = _RNCallKitEmitter.addListener( |
38 | | - RNCallKitPerformEndCallAction, |
39 | | - (data) => { handler(data); } |
40 | | - ); |
41 | | - } else if (type === 'didActivateAudioSession') { |
42 | | - listener = _RNCallKitEmitter.addListener( |
43 | | - RNCallKitDidActivateAudioSession, |
44 | | - () => { handler(); } |
45 | | - ); |
46 | | - } else if (type === 'didDisplayIncomingCall') { |
47 | | - listener = _RNCallKitEmitter.addListener( |
48 | | - RNCallKitDidDisplayIncomingCall, |
49 | | - (data) => { handler(data.error); } |
50 | | - ); |
51 | | - } else if (type === 'didPerformSetMutedCallAction') { |
52 | | - listener = _RNCallKitEmitter.addListener( |
53 | | - RNCallKitDidPerformSetMutedCallAction, |
54 | | - (data) => { handler(data.muted); } |
55 | | - ); |
56 | | - } |
57 | | - |
| 18 | + const listener = listeners[type](handler) |
58 | 19 | _callkitEventHandlers.set(handler, listener); |
59 | 20 | } |
60 | 21 |
|
|
0 commit comments