diff --git a/CHANGELOG.md b/CHANGELOG.md index 31fd8413..90b0ba67 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## Next Release + +* Feat: [Web] Add Twilio Device [DeviceState] accessor protecting un/registration. +* Docs: update CHANGELOG + ## 0.3.2+2 * Fix: [iOS] show caller number or interpretted client name for performStartCallAction diff --git a/lib/_internal/js/device/device.dart b/lib/_internal/js/device/device.dart index f545c918..f10ecf68 100644 --- a/lib/_internal/js/device/device.dart +++ b/lib/_internal/js/device/device.dart @@ -82,6 +82,11 @@ class Device extends Twilio { /// Documentation: https://www.twilio.com/docs/voice/sdks/javascript/twiliodevice#deviceupdateoptionsoptions @JS("updateOptions") external void updateOptions(DeviceOptions options); + + /// Get current call status, see [DeviceState] + /// Documentation: https://www.twilio.com/docs/voice/sdks/javascript/twiliodevice#devicestate + @JS("status") + external String state(); } /// Device options diff --git a/lib/_internal/js/device/device_status.dart b/lib/_internal/js/device/device_status.dart new file mode 100644 index 00000000..76a2e6df --- /dev/null +++ b/lib/_internal/js/device/device_status.dart @@ -0,0 +1,14 @@ +enum DeviceState { + destroyed, + unregistered, + registering, + registered, +} + +DeviceState parseDeviceState(String state) { + final lower = state.toLowerCase(); + return DeviceState.values.firstWhere( + (e) => e.name == lower, + orElse: () => DeviceState.unregistered, + ); +} diff --git a/lib/_internal/twilio_voice_web.dart b/lib/_internal/twilio_voice_web.dart index e85c273f..f8da2258 100644 --- a/lib/_internal/twilio_voice_web.dart +++ b/lib/_internal/twilio_voice_web.dart @@ -29,6 +29,7 @@ import 'package:web_callkit/web_callkit_web.dart'; import '../twilio_voice.dart'; import './js/js.dart' as twilio_js; import 'js/core/enums/device_sound_name.dart'; +import 'js/device/device_status.dart'; import 'js/utils/js_object_utils.dart'; import 'local_storage_web/local_storage_web.dart'; import 'method_channel/twilio_call_method_channel.dart'; @@ -310,7 +311,12 @@ class TwilioVoiceWeb extends MethodChannelTwilioVoice { @override Future unregister({String? accessToken}) async { if (device == null) { - return false; + return true; + } + final state = getDeviceState(device!); + if(state != DeviceState.registered) { + printDebug("Device is not registered, cannot unregister"); + return true; } try { device?.unregister(); @@ -551,6 +557,11 @@ class TwilioVoiceWeb extends MethodChannelTwilioVoice { sounds: js_util.jsify(_soundMap), ); } + + DeviceState getDeviceState(twilio_js.Device device) { + final status = device.state(); + return parseDeviceState(status); + } } class Call extends MethodChannelTwilioCall {