@vgroup/dialbox 0.0.53 → 0.0.55

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.
@@ -1315,33 +1315,67 @@ class TwilioService {
1315
1315
  this.isAvailableNumber = new BehaviorSubject(false);
1316
1316
  this.callerIdList = new BehaviorSubject([]);
1317
1317
  this.triggerSMSReload = new BehaviorSubject(false);
1318
+ this.isInitialized = false;
1319
+ this.initializationInProgress = false;
1320
+ // Initialize when token is available
1318
1321
  this.initializeTwilioDevice();
1319
1322
  }
1320
1323
  initializeTwilioDevice() {
1321
- if (this.token) {
1322
- this.extensionService.getIncomingCallToken().subscribe((data) => {
1323
- this.incomingCallToken = data.token;
1324
- localStorage.setItem('in-token', data.token);
1325
- this.device = new Device(this.incomingCallToken, {
1326
- allowIncomingWhileBusy: true,
1327
- // Add any other necessary options
1328
- });
1329
- this.device.register();
1330
- this.device.on('incoming', (call) => {
1331
- this.currentCall.next(call);
1332
- this.callType.next('INCOMING');
1333
- this.currentCallState.next('incoming');
1334
- });
1335
- this.device.on('error', (error) => {
1336
- console.error('Twilio Device Error:', error);
1337
- // Add error handling and reconnection logic
1338
- });
1339
- });
1324
+ if (this.initializationInProgress || !this.token)
1325
+ return;
1326
+ this.initializationInProgress = true;
1327
+ this.extensionService.getIncomingCallToken().subscribe({
1328
+ next: (data) => {
1329
+ if (data?.token) {
1330
+ this.setupDevice(data.token);
1331
+ this.isInitialized = true;
1332
+ }
1333
+ },
1334
+ error: (error) => {
1335
+ console.error('Error initializing Twilio:', error);
1336
+ this.initializationInProgress = false;
1337
+ // Retry after delay
1338
+ setTimeout(() => this.initializeTwilioDevice(), 5000);
1339
+ }
1340
+ });
1341
+ }
1342
+ setupDevice(token) {
1343
+ // Clean up existing device if any
1344
+ if (this.device) {
1345
+ this.device.destroy();
1340
1346
  }
1347
+ this.incomingCallToken = token;
1348
+ localStorage.setItem('in-token', token);
1349
+ this.device = new Device(token, {
1350
+ allowIncomingWhileBusy: true,
1351
+ closeProtection: true
1352
+ });
1353
+ // Set up event listeners
1354
+ this.device.on('registered', () => {
1355
+ console.log('Twilio Device registered successfully');
1356
+ this.initializationInProgress = false;
1357
+ });
1358
+ this.device.on('incoming', (call) => {
1359
+ console.log('Incoming call received');
1360
+ this.currentCall.next(call);
1361
+ this.callType.next('INCOMING');
1362
+ this.currentCallState.next('incoming');
1363
+ });
1364
+ this.device.on('error', (error) => {
1365
+ console.error('Twilio Device Error:', error);
1366
+ this.isInitialized = false;
1367
+ this.initializationInProgress = false;
1368
+ // Attempt to reinitialize after error
1369
+ setTimeout(() => this.initializeTwilioDevice(), 5000);
1370
+ });
1371
+ this.device.on('unregistered', () => {
1372
+ console.log('Twilio Device unregistered');
1373
+ this.isInitialized = false;
1374
+ // Attempt to re-register
1375
+ this.initializeTwilioDevice();
1376
+ });
1377
+ this.device.register();
1341
1378
  }
1342
- // onIncomingCall(){
1343
- // this.device.on('incoming', (call:any) => {
1344
- // console.log(call);
1345
1379
  // //call.accept();
1346
1380
  // });
1347
1381
  // }
@@ -2129,6 +2163,29 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.10", ngImpo
2129
2163
  }] } });
2130
2164
 
2131
2165
  class DialboxComponent {
2166
+ set isDialpadHidden(value) {
2167
+ this._isDialpadHidden = value;
2168
+ if (!value && !this.isInitialized) {
2169
+ this.initializeTwilio();
2170
+ }
2171
+ }
2172
+ get isDialpadHidden() {
2173
+ return this._isDialpadHidden;
2174
+ }
2175
+ initializeTwilio() {
2176
+ if (this.isInitialized || !this.token)
2177
+ return;
2178
+ this.twilioService.initializeTwilioDevice();
2179
+ this.isInitialized = true;
2180
+ // Subscribe to incoming calls
2181
+ const callSub = this.twilioService.currentCall.subscribe(call => {
2182
+ if (call) {
2183
+ this.isCallInProgress = true;
2184
+ this.isDialpadHidden = false; // Show dialpad on incoming call
2185
+ }
2186
+ });
2187
+ this.subscriptions.add(callSub);
2188
+ }
2132
2189
  constructor(twilioService, extService, dialog, ipService, extensionService, router) {
2133
2190
  this.twilioService = twilioService;
2134
2191
  this.extService = extService;
@@ -2136,7 +2193,7 @@ class DialboxComponent {
2136
2193
  this.ipService = ipService;
2137
2194
  this.extensionService = extensionService;
2138
2195
  this.router = router;
2139
- this.isDialpadHidden = true;
2196
+ this._isDialpadHidden = true;
2140
2197
  this.closeDialpadEvent = new EventEmitter();
2141
2198
  this.callInitiated = new EventEmitter();
2142
2199
  this.endCallEvent = new EventEmitter();
@@ -2182,14 +2239,8 @@ class DialboxComponent {
2182
2239
  this.subscriptions = new Subscription();
2183
2240
  this.shakeDedicatedBtn = false;
2184
2241
  this.isSmartDialCall = false;
2185
- this.isTwilioInitialized = false;
2242
+ this.isInitialized = false;
2186
2243
  this.isMinimised = false;
2187
- // Subscribe to incoming calls
2188
- this.subscriptions.add(this.twilioService.currentCall.subscribe(call => {
2189
- if (call) {
2190
- this.handleIncomingCall(call);
2191
- }
2192
- }));
2193
2244
  }
2194
2245
  ngOnInit() {
2195
2246
  try {
@@ -2199,10 +2250,10 @@ class DialboxComponent {
2199
2250
  this.getUserCallSetting();
2200
2251
  // Subscribe to dial number events
2201
2252
  const sub1 = this.twilioService.dialNumberFromOtherModule.subscribe((contact) => {
2202
- if (contact.number) {
2253
+ if (contact?.number) {
2203
2254
  this.isSmartDialCall = false;
2204
2255
  if (contact.isDialFromHistory) {
2205
- //handle dialing from history page
2256
+ // Handle dialing from history page
2206
2257
  if (contact.callerId == 'smartDialing') {
2207
2258
  this.selectedCallerId = { number: contact.from };
2208
2259
  this.isSmartDialCall = true;
@@ -2316,47 +2367,12 @@ class DialboxComponent {
2316
2367
  this.registerDragElement();
2317
2368
  }
2318
2369
  ngOnChanges(changes) {
2319
- if (changes['isDialpadHidden']) {
2320
- if (!changes['isDialpadHidden'].firstChange && !this.isDialpadHidden) {
2321
- // Re-initialize Twilio when dialpad becomes visible
2322
- this.initializeTwilio();
2323
- }
2324
- if (!this.isDialpadHidden) {
2325
- this.getContactList();
2326
- this.getUserCallSetting();
2327
- setTimeout(() => {
2328
- if (this.dialInputElement?.nativeElement) {
2329
- this.dialInputElement.nativeElement.focus();
2330
- }
2331
- }, 0);
2332
- }
2333
- }
2334
- }
2335
- initializeTwilio() {
2336
- if (this.isTwilioInitialized || !this.token)
2337
- return;
2338
- this.isTwilioInitialized = true;
2339
- this.twilioService.initializeTwilioDevice();
2340
- }
2341
- handleIncomingCall(call) {
2342
- try {
2343
- if (!call)
2344
- return;
2345
- this.incomingCallInitiated.emit();
2346
- this.newIncomingCallData = call;
2347
- // Add to incoming calls list if not already present
2348
- const existingCall = this.incomingCallsList.find((c) => c.parameters.CallSid === call.parameters.CallSid);
2349
- if (!existingCall) {
2350
- this.incomingCallsList.unshift(call);
2351
- this.incomingCallsNewInfoEvent.emit(this.incomingCallsList);
2352
- }
2353
- // Show the dialpad if it's hidden
2354
- if (this.isDialpadHidden) {
2355
- this.isDialpadHidden = false;
2356
- }
2357
- }
2358
- catch (error) {
2359
- console.error('Error handling incoming call:', error);
2370
+ if (changes['isDialpadHidden'] && !this.isDialpadHidden) {
2371
+ this.getContactList();
2372
+ this.getUserCallSetting();
2373
+ setTimeout(() => {
2374
+ this.dialInputElement.nativeElement.focus();
2375
+ }, 0);
2360
2376
  }
2361
2377
  }
2362
2378
  registerDragElement() {
@@ -2566,232 +2582,6 @@ class DialboxComponent {
2566
2582
  this.endCallEvent.emit();
2567
2583
  }
2568
2584
  }
2569
- // async initiateCall() {
2570
- // try {
2571
- // console.log('Initiating call with number:', this.dialedNumber);
2572
- // if (!this.dialedNumber && this.lastDialed) {
2573
- // console.log('Using last dialed number:', this.lastDialed.number);
2574
- // this.sanitizedNum = this.lastDialed.number;
2575
- // }
2576
- // const isInvalid = await this.isInvalidNumber();
2577
- // if (isInvalid) {
2578
- // console.error('Invalid number format');
2579
- // return false;
2580
- // }
2581
- // this.saveLastDialed();
2582
- // this.isSavedContactDialled();
2583
- // // Check payment status
2584
- // this.isPaymentDue = localStorage.getItem('paymentDue') === 'true';
2585
- // if (this.isPaymentDue) {
2586
- // console.warn('Payment is due');
2587
- // swal('Warning', 'Please note that your payment is due. To continue using our services, kindly subscribe to avoid interruptions.');
2588
- // return false;
2589
- // }
2590
- // // Check if dialing own number
2591
- // if (this.sanitizedNum === localStorage.getItem('twilioNumber')) {
2592
- // console.error('Attempted to dial own number');
2593
- // swal('Error', 'You cannot dial your own number');
2594
- // return false;
2595
- // }
2596
- // // Check microphone permissions
2597
- // const hasPermission = await this.checkMicrophonePermission();
2598
- // if (!hasPermission) {
2599
- // console.warn('Microphone permission not granted');
2600
- // await this.askForMicrophonePermission();
2601
- // return false;
2602
- // }
2603
- // if (!this.selectedCallerId) {
2604
- // console.error('No caller ID selected');
2605
- // this.shakeDedicatedBtn = true;
2606
- // this.showDialAlert('Please select a C2C number to call from');
2607
- // setTimeout(() => {
2608
- // this.shakeDedicatedBtn = false;
2609
- // }, 3000);
2610
- // return false;
2611
- // }
2612
- // console.log('Getting number with country code...');
2613
- // this.callData.displayNum = '';
2614
- // try {
2615
- // await this.getToNumber(this.sanitizedNum);
2616
- // } catch (error) {
2617
- // console.error('Error getting number with country code:', error);
2618
- // this.showDialAlert('Error processing number. Please try again.');
2619
- // return false;
2620
- // }
2621
- // if (this.terminateCall) {
2622
- // console.log('Call terminated by user');
2623
- // this.terminateCall = false;
2624
- // return false;
2625
- // }
2626
- // // Prepare call data
2627
- // this.callData = {
2628
- // ...this.callData,
2629
- // phone: this.sanitizedNum,
2630
- // isIncomingCall: false,
2631
- // dial: true,
2632
- // from: this.isSmartDialCall ? this.callData.from : this.selectedCallerId.number,
2633
- // timestamp: new Date().toISOString()
2634
- // };
2635
- // console.log('Initiating call with data:', this.callData);
2636
- // this.isCallInProgress = true;
2637
- // this.callInitiated.emit({ ...this.callData });
2638
- // return true;
2639
- // } catch (error) {
2640
- // console.error('Error in initiateCall:', error);
2641
- // this.showDialAlert('Failed to initiate call. Please try again.');
2642
- // this.isCallInProgress = false;
2643
- // return false;
2644
- // }
2645
- // //this.clearAllDialed();
2646
- // // } else {
2647
- // // swal('Error', 'Trial period is over. Please setup payment method to continue services')
2648
- // // }
2649
- // }
2650
- // async initiateCall() {
2651
- // if (!this.dialedNumber && this.lastDialed) {
2652
- // this.sanitizedNum = this.lastDialed.number;
2653
- // }
2654
- // const isInvalid = await this.isInvalidNumber();
2655
- // if (isInvalid) {
2656
- // return false;
2657
- // }
2658
- // this.saveLastDialed();
2659
- // this.isSavedContactDialled();
2660
- // //let isCallerIdSet = await this.isCallerIdSet();
2661
- // this.isPaymentDue = localStorage.getItem('paymentDue') == 'false' ? false : true;
2662
- // if (this.isPaymentDue) {
2663
- // swal('Warning', 'Please note that your payment is due, To continue on your services kindly subscribe to use uninterrupted services.');
2664
- // return;
2665
- // }
2666
- // this.isTrialPeriodOver = localStorage.getItem('trialOver') == 'false' ? false : true;
2667
- // // if (!this.isTrialPeriodOver) {
2668
- // if (this.sanitizedNum == localStorage.getItem('twilioNumber')) {
2669
- // swal('Error', 'You can not dial this number');
2670
- // return;
2671
- // }
2672
- // const hasPermission = await this.checkMicrophonePermission();
2673
- // if (hasPermission) {
2674
- // if (this.selectedCallerId) {
2675
- // //clear displayNum if value is binded from previous call
2676
- // this.callData.displayNum = '';
2677
- // // get number to be dialled from backend
2678
- // await this.getToNumber(this.sanitizedNum);
2679
- // if (this.terminateCall) {
2680
- // this.terminateCall = false;
2681
- // return;
2682
- // }
2683
- // this.callData.phone = this.sanitizedNum;
2684
- // this.callData.isIncomingCall = false;
2685
- // this.callData.dial = true;
2686
- // if (!this.isSmartDialCall) {
2687
- // this.callData.from = this.selectedCallerId.number;
2688
- // }
2689
- // this.isCallInProgress = true;
2690
- // this.callData = {
2691
- // ...this.callData,
2692
- // phone: this.sanitizedNum,
2693
- // isIncomingCall: false,
2694
- // dial: true,
2695
- // from: this.isSmartDialCall ? this.callData.from : this.selectedCallerId.number,
2696
- // timestamp: new Date().toISOString()
2697
- // };
2698
- // console.log('Initiating call with data:', this.callData);
2699
- // this.isCallInProgress = true;
2700
- // this.callInitiated.emit({ ...this.callData });
2701
- // return true;
2702
- // } else {
2703
- // this.shakeDedicatedBtn = true;
2704
- // this.showDialAlert('Select a C2C number to call');
2705
- // setTimeout(() => {
2706
- // this.shakeDedicatedBtn = false;
2707
- // }, 3000);
2708
- // return false;
2709
- // }
2710
- // //this.callingOpenEvent.emit({ phone: this.dialedNumber });
2711
- // } else {
2712
- // await this.askForMicrophonePermission();
2713
- // }
2714
- // //this.clearAllDialed();
2715
- // // } else {
2716
- // // swal('Error', 'Trial period is over. Please setup payment method to continue services')
2717
- // // }
2718
- // }
2719
- // async initiateCall() {
2720
- // try{
2721
- // if (!this.dialedNumber && this.lastDialed) {
2722
- // this.sanitizedNum = this.lastDialed.number;
2723
- // }
2724
- // const isInvalid = await this.isInvalidNumber();
2725
- // if (isInvalid) {
2726
- // return false;
2727
- // }
2728
- // this.saveLastDialed();
2729
- // this.isSavedContactDialled();
2730
- // //let isCallerIdSet = await this.isCallerIdSet();
2731
- // this.isPaymentDue = localStorage.getItem('paymentDue') == 'false' ? false : true;
2732
- // if (this.isPaymentDue) {
2733
- // swal('Warning', 'Please note that your payment is due, To continue on your services kindly subscribe to use uninterrupted services.');
2734
- // return false;
2735
- // }
2736
- // this.isTrialPeriodOver = localStorage.getItem('trialOver') == 'false' ? false : true;
2737
- // // if (!this.isTrialPeriodOver) {
2738
- // if (this.sanitizedNum == localStorage.getItem('twilioNumber')) {
2739
- // swal('Error', 'You can not dial this number');
2740
- // return false;
2741
- // }
2742
- // const hasPermission = await this.checkMicrophonePermission();
2743
- // if (hasPermission) {
2744
- // if (this.selectedCallerId) {
2745
- // //clear displayNum if value is binded from previous call
2746
- // this.callData.displayNum = '';
2747
- // // get number to be dialled from backend
2748
- // await this.getToNumber(this.sanitizedNum);
2749
- // if (this.terminateCall) {
2750
- // this.terminateCall = false;
2751
- // return;
2752
- // }
2753
- // this.callData.phone = this.sanitizedNum;
2754
- // this.callData.isIncomingCall = false;
2755
- // this.callData.dial = true;
2756
- // if (!this.isSmartDialCall) {
2757
- // this.callData.from = this.selectedCallerId.number;
2758
- // }
2759
- // this.isCallInProgress = true;
2760
- // this.callData = {
2761
- // ...this.callData,
2762
- // phone: this.sanitizedNum,
2763
- // isIncomingCall: false,
2764
- // dial: true,
2765
- // from: this.isSmartDialCall ? this.callData.from : this.selectedCallerId.number,
2766
- // timestamp: new Date().toISOString()
2767
- // };
2768
- // console.log('Initiating call with data:', this.callData);
2769
- // this.isCallInProgress = true;
2770
- // this.callInitiated.emit({ ...this.callData });
2771
- // return true;
2772
- // } else {
2773
- // this.shakeDedicatedBtn = true;
2774
- // this.showDialAlert('Select a C2C number to call');
2775
- // setTimeout(() => {
2776
- // this.shakeDedicatedBtn = false;
2777
- // }, 3000);
2778
- // return false;
2779
- // }
2780
- // //this.callingOpenEvent.emit({ phone: this.dialedNumber });
2781
- // } else {
2782
- // await this.askForMicrophonePermission();
2783
- // }
2784
- // //this.clearAllDialed();
2785
- // // } else {
2786
- // // swal('Error', 'Trial period is over. Please setup payment method to continue services')
2787
- // // }
2788
- // }catch(e){
2789
- // console.error('Error in initiateCall:', e);
2790
- // this.showDialAlert('Failed to initiate call. Please try again.');
2791
- // this.isCallInProgress = false;
2792
- // return false;
2793
- // }
2794
- // }
2795
2585
  async initiateCall() {
2796
2586
  try {
2797
2587
  if (!this.dialedNumber && this.lastDialed) {