@vgroup/dialbox 0.0.63 → 0.0.64

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1301,13 +1301,22 @@ class NotificationService {
1301
1301
  if ('serviceWorker' in navigator) {
1302
1302
  navigator.serviceWorker.addEventListener('message', this.handleServiceWorkerMessage.bind(this));
1303
1303
  }
1304
+ // Listen for incoming calls from Twilio service
1305
+ this.twilioService.currentCall.subscribe((call) => {
1306
+ if (call) {
1307
+ this.showNotification(call);
1308
+ }
1309
+ });
1304
1310
  }
1305
1311
  showNotification(callData) {
1306
- this.twilioAuthId = callData.customParameters.get('twilioAuthId');
1312
+ if (!callData)
1313
+ return;
1307
1314
  this.twilioCallData = callData;
1308
- const incomingNumber = callData.parameters.From;
1309
- const callerName = callData.customParameters.get('name');
1310
- const callerImage = callData.customParameters.get('image');
1315
+ const incomingNumber = callData.parameters?.From;
1316
+ const customParams = callData.customParameters || new Map();
1317
+ const callerName = customParams.get('name') || incomingNumber;
1318
+ const callerImage = customParams.get('image') || '';
1319
+ this.twilioAuthId = customParams.get('twilioAuthId');
1311
1320
  if (('serviceWorker' in navigator)) {
1312
1321
  navigator.serviceWorker.ready.then((registration) => {
1313
1322
  const options = {
@@ -1392,26 +1401,82 @@ class TwilioService {
1392
1401
  initializeTwilioDevice() {
1393
1402
  if (this.token) {
1394
1403
  this.extensionService.getIncomingCallToken().subscribe((data) => {
1395
- this.incomingCallToken = data.token;
1396
- localStorage.setItem('in-token', data.token);
1397
- this.device = new Device(this.incomingCallToken, {
1398
- allowIncomingWhileBusy: true,
1399
- // Add any other necessary options
1400
- });
1401
- this.device.register();
1402
- this.device.on('incoming', (call) => {
1403
- this.currentCall.next(call);
1404
- this.callType.next('INCOMING');
1405
- this.currentCallState.next('incoming');
1406
- this.notificationSerivce.showNotification(call);
1407
- });
1408
- this.device.on('error', (error) => {
1409
- console.error('Twilio Device Error:', error);
1410
- // Add error handling and reconnection logic
1411
- });
1404
+ try {
1405
+ this.incomingCallToken = data.token;
1406
+ localStorage.setItem('in-token', data.token);
1407
+ // Destroy existing device if any
1408
+ if (this.device) {
1409
+ this.device.destroy();
1410
+ }
1411
+ this.device = new Device(this.incomingCallToken, {
1412
+ allowIncomingWhileBusy: true,
1413
+ closeProtection: true
1414
+ });
1415
+ // Register the device
1416
+ this.device.register();
1417
+ // Set up device event listeners
1418
+ this.setupDeviceListeners();
1419
+ console.log('Twilio device initialized and registered');
1420
+ }
1421
+ catch (error) {
1422
+ console.error('Error initializing Twilio device:', error);
1423
+ }
1424
+ }, (error) => {
1425
+ console.error('Error getting Twilio token:', error);
1412
1426
  });
1413
1427
  }
1414
1428
  }
1429
+ setupDeviceListeners() {
1430
+ if (!this.device)
1431
+ return;
1432
+ // Incoming call handler
1433
+ this.device.on('incoming', (call) => {
1434
+ console.log('Incoming call from:', call.parameters['From']);
1435
+ // Update call state
1436
+ this.currentCall.next(call);
1437
+ this.callType.next('INCOMING');
1438
+ this.currentCallState.next('incoming');
1439
+ // Show notification and open dialpad
1440
+ this.notificationSerivce.showNotification(call);
1441
+ this.openInProgressDialpad.next(true);
1442
+ // Set up call event listeners
1443
+ call.on('accept', () => {
1444
+ console.log('Call accepted');
1445
+ this.currentCallState.next('in-progress');
1446
+ this.isIncomingCallPicked.next(true);
1447
+ });
1448
+ call.on('disconnect', () => {
1449
+ console.log('Call disconnected');
1450
+ this.currentCallState.next('none');
1451
+ this.currentCall.next(null);
1452
+ this.isIncomingCallPicked.next(false);
1453
+ this.openInProgressDialpad.next(false);
1454
+ });
1455
+ call.on('cancel', () => {
1456
+ console.log('Call cancelled');
1457
+ this.currentCallState.next('none');
1458
+ this.currentCall.next(null);
1459
+ this.isIncomingCallPicked.next(false);
1460
+ this.openInProgressDialpad.next(false);
1461
+ });
1462
+ });
1463
+ // Error handling
1464
+ this.device.on('error', (error) => {
1465
+ console.error('Twilio Device Error:', error);
1466
+ // Attempt to reinitialize on error
1467
+ if (error.code === 31201) { // Invalid token error
1468
+ console.log('Attempting to refresh Twilio token...');
1469
+ this.initializeTwilioDevice();
1470
+ }
1471
+ });
1472
+ // Device ready
1473
+ this.device.on('registered', () => {
1474
+ console.log('Twilio device registered and ready');
1475
+ });
1476
+ this.device.on('unregistered', () => {
1477
+ console.log('Twilio device unregistered');
1478
+ });
1479
+ }
1415
1480
  // onIncomingCall(){
1416
1481
  // this.device.on('incoming', (call:any) => {
1417
1482
  // console.log(call);
@@ -3177,12 +3242,18 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.10", ngImpo
3177
3242
  }] }]; } });
3178
3243
 
3179
3244
  class AppInitializerService {
3180
- constructor(twilioService, extensionService, router, platformId) {
3181
- this.twilioService = twilioService;
3245
+ constructor(injector, extensionService, router, platformId) {
3246
+ this.injector = injector;
3182
3247
  this.extensionService = extensionService;
3183
3248
  this.router = router;
3184
3249
  this.platformId = platformId;
3185
3250
  }
3251
+ getTwilioService() {
3252
+ if (!this.twilioService) {
3253
+ this.twilioService = this.injector.get(TwilioService);
3254
+ }
3255
+ return this.twilioService;
3256
+ }
3186
3257
  initializeApp() {
3187
3258
  return new Promise((resolve) => {
3188
3259
  if (isPlatformBrowser(this.platformId)) {
@@ -3210,7 +3281,13 @@ class AppInitializerService {
3210
3281
  async initializeTwilio() {
3211
3282
  const token = localStorage.getItem('ext_token');
3212
3283
  if (token) {
3213
- await this.twilioService.initializeTwilioDevice();
3284
+ try {
3285
+ const twilioService = this.getTwilioService();
3286
+ await twilioService.initializeTwilioDevice();
3287
+ }
3288
+ catch (error) {
3289
+ console.error('Error initializing Twilio:', error);
3290
+ }
3214
3291
  }
3215
3292
  }
3216
3293
  async fetchInitialData() {
@@ -3234,14 +3311,14 @@ class AppInitializerService {
3234
3311
  }
3235
3312
  }
3236
3313
  }
3237
- AppInitializerService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.10", ngImport: i0, type: AppInitializerService, deps: [{ token: TwilioService }, { token: ExtensionService }, { token: i5.Router }, { token: PLATFORM_ID }], target: i0.ɵɵFactoryTarget.Injectable });
3314
+ AppInitializerService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.10", ngImport: i0, type: AppInitializerService, deps: [{ token: i0.Injector }, { token: ExtensionService }, { token: i5.Router }, { token: PLATFORM_ID }], target: i0.ɵɵFactoryTarget.Injectable });
3238
3315
  AppInitializerService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.2.10", ngImport: i0, type: AppInitializerService, providedIn: 'root' });
3239
3316
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.10", ngImport: i0, type: AppInitializerService, decorators: [{
3240
3317
  type: Injectable,
3241
3318
  args: [{
3242
3319
  providedIn: 'root'
3243
3320
  }]
3244
- }], ctorParameters: function () { return [{ type: TwilioService }, { type: ExtensionService }, { type: i5.Router }, { type: Object, decorators: [{
3321
+ }], ctorParameters: function () { return [{ type: i0.Injector }, { type: ExtensionService }, { type: i5.Router }, { type: Object, decorators: [{
3245
3322
  type: Inject,
3246
3323
  args: [PLATFORM_ID]
3247
3324
  }] }]; } });