@vgroup/dialbox 0.0.54 → 0.0.56

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,19 +1315,55 @@ class TwilioService {
1315
1315
  this.isAvailableNumber = new BehaviorSubject(false);
1316
1316
  this.callerIdList = new BehaviorSubject([]);
1317
1317
  this.triggerSMSReload = new BehaviorSubject(false);
1318
- this.initializeTwilioDevice();
1318
+ this.isInitialized = false;
1319
+ this.initializationInProgress = false;
1320
+ // Initialize when token is available
1321
+ if (this.token) {
1322
+ this.initializeTwilioDevice();
1323
+ }
1324
+ else {
1325
+ // If token is not available, try to get it from localStorage
1326
+ const token = localStorage.getItem('ext_token');
1327
+ if (token) {
1328
+ this.token = token;
1329
+ this.initializeTwilioDevice();
1330
+ }
1331
+ }
1319
1332
  }
1320
- setupDeviceListeners() {
1321
- if (!this.device)
1333
+ initializeTwilioDevice() {
1334
+ if (this.initializationInProgress || !this.token)
1322
1335
  return;
1323
- // Remove any existing listeners first
1324
- this.device.off('incoming');
1325
- this.device.off('error');
1326
- this.device.off('registered');
1327
- this.device.off('unregistered');
1328
- // Set up new listeners
1336
+ this.initializationInProgress = true;
1337
+ this.extensionService.getIncomingCallToken().subscribe({
1338
+ next: (data) => {
1339
+ if (data?.token) {
1340
+ this.setupDevice(data.token);
1341
+ this.isInitialized = true;
1342
+ }
1343
+ },
1344
+ error: (error) => {
1345
+ console.error('Error initializing Twilio:', error);
1346
+ this.initializationInProgress = false;
1347
+ // Retry after delay
1348
+ setTimeout(() => this.initializeTwilioDevice(), 5000);
1349
+ }
1350
+ });
1351
+ }
1352
+ setupDevice(token) {
1353
+ // Clean up existing device if any
1354
+ if (this.device) {
1355
+ this.device.destroy();
1356
+ }
1357
+ this.incomingCallToken = token;
1358
+ localStorage.setItem('in-token', token);
1359
+ this.device = new Device(token, {
1360
+ allowIncomingWhileBusy: true,
1361
+ closeProtection: true
1362
+ });
1363
+ // Set up event listeners
1329
1364
  this.device.on('registered', () => {
1330
- console.log('Twilio Device registered');
1365
+ console.log('Twilio Device registered successfully');
1366
+ this.initializationInProgress = false;
1331
1367
  });
1332
1368
  this.device.on('incoming', (call) => {
1333
1369
  console.log('Incoming call received');
@@ -1337,40 +1373,19 @@ class TwilioService {
1337
1373
  });
1338
1374
  this.device.on('error', (error) => {
1339
1375
  console.error('Twilio Device Error:', error);
1376
+ this.isInitialized = false;
1377
+ this.initializationInProgress = false;
1378
+ // Attempt to reinitialize after error
1379
+ setTimeout(() => this.initializeTwilioDevice(), 5000);
1340
1380
  });
1381
+ this.device.on('unregistered', () => {
1382
+ console.log('Twilio Device unregistered');
1383
+ this.isInitialized = false;
1384
+ // Attempt to re-register
1385
+ this.initializeTwilioDevice();
1386
+ });
1387
+ this.device.register();
1341
1388
  }
1342
- initializeTwilioDevice() {
1343
- if (this.device) {
1344
- // If device already exists, just re-register
1345
- this.device.register();
1346
- return;
1347
- }
1348
- if (this.token) {
1349
- this.extensionService.getIncomingCallToken().subscribe({
1350
- next: (data) => {
1351
- this.incomingCallToken = data.token;
1352
- localStorage.setItem('in-token', data.token);
1353
- // Destroy existing device if any
1354
- if (this.device) {
1355
- this.device.destroy();
1356
- }
1357
- this.device = new Device(this.incomingCallToken, {
1358
- allowIncomingWhileBusy: true,
1359
- closeProtection: true
1360
- });
1361
- this.setupDeviceListeners();
1362
- this.device.register();
1363
- },
1364
- error: (error) => {
1365
- console.error('Error getting Twilio token:', error);
1366
- // Add retry logic here if needed
1367
- }
1368
- });
1369
- }
1370
- }
1371
- // onIncomingCall(){
1372
- // this.device.on('incoming', (call:any) => {
1373
- // console.log(call);
1374
1389
  // //call.accept();
1375
1390
  // });
1376
1391
  // }
@@ -2158,6 +2173,16 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.10", ngImpo
2158
2173
  }] } });
2159
2174
 
2160
2175
  class DialboxComponent {
2176
+ set isDialpadHidden(value) {
2177
+ this._isDialpadHidden = value;
2178
+ if (!value) {
2179
+ // When dialpad becomes visible, ensure Twilio is initialized
2180
+ this.initializeTwilio();
2181
+ }
2182
+ }
2183
+ get isDialpadHidden() {
2184
+ return this._isDialpadHidden;
2185
+ }
2161
2186
  constructor(twilioService, extService, dialog, ipService, extensionService, router) {
2162
2187
  this.twilioService = twilioService;
2163
2188
  this.extService = extService;
@@ -2165,7 +2190,7 @@ class DialboxComponent {
2165
2190
  this.ipService = ipService;
2166
2191
  this.extensionService = extensionService;
2167
2192
  this.router = router;
2168
- this.isDialpadHidden = true;
2193
+ this._isDialpadHidden = true;
2169
2194
  this.closeDialpadEvent = new EventEmitter();
2170
2195
  this.callInitiated = new EventEmitter();
2171
2196
  this.endCallEvent = new EventEmitter();
@@ -2211,19 +2236,35 @@ class DialboxComponent {
2211
2236
  this.subscriptions = new Subscription();
2212
2237
  this.shakeDedicatedBtn = false;
2213
2238
  this.isSmartDialCall = false;
2214
- this.isTwilioInitialized = false;
2239
+ this.isInitialized = false;
2215
2240
  this.isMinimised = false;
2216
- // Subscribe to incoming calls
2217
- this.subscriptions.add(this.twilioService.currentCall.subscribe(call => {
2241
+ // Initialize if dialpad is visible by default
2242
+ if (!this.isDialpadHidden) {
2243
+ this.initializeTwilio();
2244
+ }
2245
+ }
2246
+ initializeTwilio() {
2247
+ if (this.isInitialized)
2248
+ return;
2249
+ this.token = localStorage.getItem('ext_token') || '';
2250
+ if (!this.token) {
2251
+ console.error('No authentication token found');
2252
+ return;
2253
+ }
2254
+ this.isInitialized = true;
2255
+ // Initialize Twilio service
2256
+ this.twilioService.initializeTwilioDevice();
2257
+ // Subscribe to incoming calls to show dialpad when call comes in
2258
+ const callSub = this.twilioService.currentCall.subscribe(call => {
2218
2259
  if (call) {
2219
- this.handleIncomingCall(call);
2260
+ this.isCallInProgress = true;
2261
+ this._isDialpadHidden = false; // Show dialpad on incoming call
2220
2262
  }
2221
- }));
2263
+ });
2264
+ this.subscriptions.add(callSub);
2222
2265
  }
2223
2266
  ngOnInit() {
2224
2267
  try {
2225
- this.token = localStorage.getItem('ext_token') || '';
2226
- this.initializeTwilio();
2227
2268
  this.getContactList();
2228
2269
  this.getUserCallSetting();
2229
2270
  // Subscribe to dial number events
@@ -2345,56 +2386,12 @@ class DialboxComponent {
2345
2386
  this.registerDragElement();
2346
2387
  }
2347
2388
  ngOnChanges(changes) {
2348
- if (changes['isDialpadHidden']) {
2349
- if (!changes['isDialpadHidden'].firstChange && !this.isDialpadHidden) {
2350
- // Re-initialize Twilio when dialpad becomes visible
2351
- this.initializeTwilio();
2352
- }
2353
- if (!this.isDialpadHidden) {
2354
- this.getContactList();
2355
- this.getUserCallSetting();
2356
- setTimeout(() => {
2357
- if (this.dialInputElement?.nativeElement) {
2358
- this.dialInputElement.nativeElement.focus();
2359
- }
2360
- }, 0);
2361
- }
2362
- }
2363
- }
2364
- initializeTwilio() {
2365
- if (this.isTwilioInitialized || !this.token)
2366
- return;
2367
- this.isTwilioInitialized = true;
2368
- // Clear any existing device first
2369
- if (this.twilioService.device) {
2370
- this.twilioService.device.destroy();
2371
- this.twilioService.device = null;
2372
- }
2373
- // Initialize new device
2374
- this.twilioService.initializeTwilioDevice();
2375
- // Reset the current call state
2376
- this.twilioService.currentCall.next(null);
2377
- this.twilioService.currentCallState.next('none');
2378
- }
2379
- handleIncomingCall(call) {
2380
- try {
2381
- if (!call)
2382
- return;
2383
- this.incomingCallInitiated.emit();
2384
- this.newIncomingCallData = call;
2385
- // Add to incoming calls list if not already present
2386
- const existingCall = this.incomingCallsList.find((c) => c.parameters.CallSid === call.parameters.CallSid);
2387
- if (!existingCall) {
2388
- this.incomingCallsList.unshift(call);
2389
- this.incomingCallsNewInfoEvent.emit(this.incomingCallsList);
2390
- }
2391
- // Show the dialpad if it's hidden
2392
- if (this.isDialpadHidden) {
2393
- this.isDialpadHidden = false;
2394
- }
2395
- }
2396
- catch (error) {
2397
- console.error('Error handling incoming call:', error);
2389
+ if (changes['isDialpadHidden'] && !this.isDialpadHidden) {
2390
+ this.getContactList();
2391
+ this.getUserCallSetting();
2392
+ setTimeout(() => {
2393
+ this.dialInputElement.nativeElement.focus();
2394
+ }, 0);
2398
2395
  }
2399
2396
  }
2400
2397
  registerDragElement() {
@@ -3122,22 +3119,23 @@ class DialboxComponent {
3122
3119
  }
3123
3120
  ngOnDestroy() {
3124
3121
  try {
3122
+ console.log('Cleaning up C2cDialpadComponent');
3123
+ // Unsubscribe from all subscriptions
3124
+ if (this.subscriptions) {
3125
+ this.subscriptions.unsubscribe();
3126
+ }
3127
+ // Clean up Twilio device when component is destroyed
3128
+ if (this.twilioService['device']) {
3129
+ this.twilioService['device'].destroy();
3130
+ }
3131
+ // End any active call
3132
+ if (this.isCallInProgress) {
3133
+ this.endCall();
3134
+ }
3125
3135
  // Clear any timeouts or intervals if they exist
3126
3136
  if (this.toastTimeout) {
3127
3137
  clearTimeout(this.toastTimeout);
3128
3138
  }
3129
- // Unsubscribe from all subscriptions
3130
- this.subscriptions.unsubscribe();
3131
- // Clean up Twilio device if it exists
3132
- if (this.twilioService.device) {
3133
- try {
3134
- this.twilioService.device.destroy();
3135
- this.twilioService.device = null;
3136
- }
3137
- catch (error) {
3138
- console.error('Error cleaning up Twilio device:', error);
3139
- }
3140
- }
3141
3139
  console.log('C2cDialpadComponent cleanup complete');
3142
3140
  }
3143
3141
  catch (error) {