Skip to content

Commit d39004a

Browse files
authored
Merge pull request #303 from cybex-dev/feat_web_twilio_device_state
Feat: add internal Twilio Device registration state
2 parents 028d7aa + c83ef40 commit d39004a

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## Next Release
2+
3+
* Feat: [Web] Add Twilio Device [DeviceState] accessor protecting un/registration.
4+
* Docs: update CHANGELOG
5+
16
## 0.3.2+2
27

38
* Fix: [iOS] show caller number or interpretted client name for performStartCallAction

lib/_internal/js/device/device.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ class Device extends Twilio {
8282
/// Documentation: https://www.twilio.com/docs/voice/sdks/javascript/twiliodevice#deviceupdateoptionsoptions
8383
@JS("updateOptions")
8484
external void updateOptions(DeviceOptions options);
85+
86+
/// Get current call status, see [DeviceState]
87+
/// Documentation: https://www.twilio.com/docs/voice/sdks/javascript/twiliodevice#devicestate
88+
@JS("status")
89+
external String state();
8590
}
8691

8792
/// Device options
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
enum DeviceState {
2+
destroyed,
3+
unregistered,
4+
registering,
5+
registered,
6+
}
7+
8+
DeviceState parseDeviceState(String state) {
9+
final lower = state.toLowerCase();
10+
return DeviceState.values.firstWhere(
11+
(e) => e.name == lower,
12+
orElse: () => DeviceState.unregistered,
13+
);
14+
}

lib/_internal/twilio_voice_web.dart

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import 'package:web_callkit/web_callkit_web.dart';
2929
import '../twilio_voice.dart';
3030
import './js/js.dart' as twilio_js;
3131
import 'js/core/enums/device_sound_name.dart';
32+
import 'js/device/device_status.dart';
3233
import 'js/utils/js_object_utils.dart';
3334
import 'local_storage_web/local_storage_web.dart';
3435
import 'method_channel/twilio_call_method_channel.dart';
@@ -310,7 +311,12 @@ class TwilioVoiceWeb extends MethodChannelTwilioVoice {
310311
@override
311312
Future<bool?> unregister({String? accessToken}) async {
312313
if (device == null) {
313-
return false;
314+
return true;
315+
}
316+
final state = getDeviceState(device!);
317+
if(state != DeviceState.registered) {
318+
printDebug("Device is not registered, cannot unregister");
319+
return true;
314320
}
315321
try {
316322
device?.unregister();
@@ -551,6 +557,11 @@ class TwilioVoiceWeb extends MethodChannelTwilioVoice {
551557
sounds: js_util.jsify(_soundMap),
552558
);
553559
}
560+
561+
DeviceState getDeviceState(twilio_js.Device device) {
562+
final status = device.state();
563+
return parseDeviceState(status);
564+
}
554565
}
555566

556567
class Call extends MethodChannelTwilioCall {

0 commit comments

Comments
 (0)