From f222c589a93bbbf11d886f413ecd53402dd60e67 Mon Sep 17 00:00:00 2001 From: Sikander Saleem Date: Sun, 27 Sep 2026 15:27:25 +0300 Subject: [PATCH] improvements --- .../chat/calling/audio_call_page.dart | 6 +- .../cx_module/chat/calling/call_provider.dart | 64 +++++++++---------- lib/new_views/pages/land_page/land_page.dart | 2 +- 3 files changed, 38 insertions(+), 34 deletions(-) diff --git a/lib/modules/cx_module/chat/calling/audio_call_page.dart b/lib/modules/cx_module/chat/calling/audio_call_page.dart index 8373f1ab..e970a1c6 100644 --- a/lib/modules/cx_module/chat/calling/audio_call_page.dart +++ b/lib/modules/cx_module/chat/calling/audio_call_page.dart @@ -60,7 +60,11 @@ class _AudioCallPageState extends State { callPro.initAudioCallListeners(); if (widget.isAccept) { - callPro.acceptCall(widget.sender.employeeNumber ?? "", widget.recipient.employeeNumber ?? "", false, moduleId: widget.moduleId); + callPro.acceptCall(widget.sender.employeeNumber ?? "", widget.recipient.employeeNumber ?? "", false, moduleId: widget.moduleId,onSuccess: (String msg) { + _closeCall(); + }, onFailed: (String msg) { + _showFailure(msg, title: "Failed"); + }); } else { callPro.startCall(widget.sender.employeeNumber ?? "", widget.recipient.employeeNumber ?? "", false, widget.moduleId, widget.referenceID, onSuccess: (String msg) { _closeCall(); diff --git a/lib/modules/cx_module/chat/calling/call_provider.dart b/lib/modules/cx_module/chat/calling/call_provider.dart index 32a1b36f..d3397a8a 100644 --- a/lib/modules/cx_module/chat/calling/call_provider.dart +++ b/lib/modules/cx_module/chat/calling/call_provider.dart @@ -98,10 +98,7 @@ class CallProvider with ChangeNotifier, DiagnosticableTreeMixin { requestTimeout: 30000, ); - return HubConnectionBuilder() - .withUrl(hubUrl, options: httpOp) - .withAutomaticReconnect(retryDelays: [2000, 5000, 10000, 20000]) - .build(); + return HubConnectionBuilder().withUrl(hubUrl, options: httpOp).withAutomaticReconnect(retryDelays: [2000, 5000, 10000, 20000]).build(); } /// Writes the port into the url *textually*. @@ -123,38 +120,38 @@ class CallProvider with ChangeNotifier, DiagnosticableTreeMixin { Future buildHubConnection(String employeeNumber, {HubConnection? hubC}) async { _setStatus("Connecting..."); try { - if (hubC != null) { - final bool reused = await _adoptExistingConnection(hubC); - if (reused) return true; - if (kDebugMode) { - print('ℹ️ Shared chat hub not usable (state: ${hubC.state}), opening a dedicated call connection.'); - } + if (hubC != null) { + final bool reused = await _adoptExistingConnection(hubC); + if (reused) return true; + if (kDebugMode) { + print('ℹ️ Shared chat hub not usable (state: ${hubC.state}), opening a dedicated call connection.'); } - await _disposeConnection(); + } + await _disposeConnection(); - final http.Response response = await ApiClient().postJsonForResponse( - URLs.chatSdkToken, - {"apiKey": URLs.chatApiKey, "employeeNumber": employeeNumber, "voIPToken": null}, - ); - if (response.statusCode != 200) { - if (kDebugMode) { - print('⚠️ Call token request failed: HTTP ${response.statusCode}'); - } - return false; + final http.Response response = await ApiClient().postJsonForResponse( + URLs.chatSdkToken, + {"apiKey": URLs.chatApiKey, "employeeNumber": employeeNumber, "voIPToken": null}, + ); + if (response.statusCode != 200) { + if (kDebugMode) { + print('⚠️ Call token request failed: HTTP ${response.statusCode}'); } + return false; + } - final ChatLoginResponse chatResponse = ChatLoginResponse.fromJson(jsonDecode(response.body)); - if (chatResponse.token == null || chatResponse.token!.isEmpty) { - if (kDebugMode) { - print('⚠️ Call token request returned an empty token.'); - } - return false; + final ChatLoginResponse chatResponse = ChatLoginResponse.fromJson(jsonDecode(response.body)); + if (chatResponse.token == null || chatResponse.token!.isEmpty) { + if (kDebugMode) { + print('⚠️ Call token request returned an empty token.'); } + return false; + } - callHubConnection = await getHubConnection(chatResponse.token!); - _ownsConnection = true; - await callHubConnection!.start(); - return callHubConnection?.state == HubConnectionState.Connected; + callHubConnection = await getHubConnection(chatResponse.token!); + _ownsConnection = true; + await callHubConnection!.start(); + return callHubConnection?.state == HubConnectionState.Connected; } catch (e) { if (kDebugMode) { print('⚠️ Error building call SignalR connection: $e'); @@ -262,7 +259,6 @@ class CallProvider with ChangeNotifier, DiagnosticableTreeMixin { }; Future _createPeerConnection() async { - Map configuration = { "iceServers": [ {"urls": 'stun:15.185.116.59:3478'}, @@ -364,11 +360,14 @@ class CallProvider with ChangeNotifier, DiagnosticableTreeMixin { } } - void acceptCall(String senderN, String recipientN, bool isVideo, {required int moduleId}) async { + void acceptCall(String senderN, String recipientN, bool isVideo, {required int moduleId, Function(String message)? onFailed, Function(String message)? onSuccess}) async { this.senderN = senderN; this.recipientN = recipientN; + this.onFailed = onFailed; + this.onSuccess = onSuccess; try { await invoke(invokeMethod: "AnswerCallAsync", currentUserID: senderN, targetUserID: recipientN, isVideoCall: isVideo, data: [this.moduleId]); + _setStatus("Connected"); } catch (e) { if (kDebugMode) { print('⚠️ AnswerCallAsync failed: $e'); @@ -552,6 +551,7 @@ class CallProvider with ChangeNotifier, DiagnosticableTreeMixin { } else if (params[0] == 'USER_OFFLINE') { onFailed?.call("The user is currently offline"); } else { + _setStatus("Call Ended"); onSuccess?.call(""); } } diff --git a/lib/new_views/pages/land_page/land_page.dart b/lib/new_views/pages/land_page/land_page.dart index de170023..d8adcc3d 100644 --- a/lib/new_views/pages/land_page/land_page.dart +++ b/lib/new_views/pages/land_page/land_page.dart @@ -230,7 +230,7 @@ class _LandPageState extends State { case CallEventActionDidUpdateDevicePushTokenVoip(): // TODO: Handle this case. throw UnimplementedError(); - case CallEventActionCallIncoming(): + case CallEventActionCallIncoming(:final callKitParams): // TODO: Handle this case. throw UnimplementedError(); case CallEventActionCallStart():