MrHertal / react-native-twilio-phone

Twilio Voice React Native module.
MIT License
154 stars 67 forks source link

Cancel call functionality? #28

Closed josaric closed 3 years ago

josaric commented 3 years ago

Hi, when I click start call using this function

await RNTwilioPhone.startCall('+00123456789');

it takes some time to fetch the token, initialize the call a start riniging. Is there a possibility to cancel the call before it even started ringing?

Thanks

MrHertal commented 3 years ago

Hi,

Once TwilioPhone.startCall is fired, call is started and you cannot cancel it before CallRinging event is dispatched.

You can only cancel right after the fetchToken. You will have to make your own copy of RNTwilioPhone.ts and override the startCall method.

josaric commented 3 years ago

For everyone with this issue I recommend to do next:

setState in constructor: callStartedRinging: FALSE

if user clicks hangUp function and call is not established, put callStartedRinging state to TRUE

and then check inside twilioPhoneEmitter event CallRinging or CallConnected if callStartedRinging state is TRUE and just disconnect call with TwilioPhone.disconnectCall(callSid)

constructor(props) {
        super(props);
        this.state = {
            callStartedRinging: false,
            callSid: null,
            established: false
        }
}

let self = this;
twilioPhoneEmitter.addListener('CallConnected', (data) => {
            // disconnects call on connected
            if(self.state.callRingingClose) {
                TwilioPhone.disconnectCall(data.callSid);
                TwilioPhone.endCall(data.callSid);
            } else {
                self.setState({established: true, callSid: data.callSid});
            }
        });

twilioPhoneEmitter.addListener('CallRinging', (data) => {
           // disconnects call on ringing
            if(self.state.callRingingClose) {
                TwilioPhone.disconnectCall(data.callSid);
                TwilioPhone.endCall(data.callSid);
            }
        });