Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 5 additions & 0 deletions lib/_internal/js/device/device.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions lib/_internal/js/device/device_status.dart
Original file line number Diff line number Diff line change
@@ -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,
);
}
13 changes: 12 additions & 1 deletion lib/_internal/twilio_voice_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -310,7 +311,12 @@ class TwilioVoiceWeb extends MethodChannelTwilioVoice {
@override
Future<bool?> 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();
Expand Down Expand Up @@ -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 {
Expand Down