@ -1,3 +1,4 @@
import ' dart:async ' ;
import ' dart:convert ' ;
import ' dart:io ' ;
@ -6,21 +7,21 @@ import 'package:diplomaticquarterapp/core/service/base_service.dart';
import ' package:diplomaticquarterapp/locator.dart ' ;
import ' package:diplomaticquarterapp/routes.dart ' ;
import ' package:diplomaticquarterapp/uitl/navigation_service.dart ' ;
import ' package:diplomaticquarterapp/uitl/utils.dart ' ;
import ' package:diplomaticquarterapp/voipcall/call/chat_incoming_call_screen.dart ' ;
import ' package:diplomaticquarterapp/voipcall/consts.dart ' ;
import ' package:diplomaticquarterapp/voipcall/model/call.dart ' ;
import ' package:diplomaticquarterapp/voipcall/model/get_single_user_chat_call.dart ' ;
import ' package:diplomaticquarterapp/voipcall/model/get_user_login_token.dart ' ;
import ' package:diplomaticquarterapp/voipcall/model/get_user_login_token.dart ' as getUserToken ;
import ' package:diplomaticquarterapp/voipcall/model/webrtc_payloads.dart ' ;
import ' package:flutter/foundation.dart ' ;
import ' package:flutter/material.dart ' ;
import ' package:flutter_callkit_incoming/flutter_callkit_incoming.dart ' ;
import ' package:flutter_webrtc/flutter_webrtc.dart ' ;
import ' package:http/http.dart ' as http ;
import ' package:http/http.dart ' ;
import ' package:just_audio/just_audio.dart ' ;
import ' package:signalr_netcore/http_connection_options.dart ' ;
import ' package:signalr_netcore/hub_connection.dart ' ;
import ' package:signalr_netcore/hub_connection_builder.dart ' ;
import ' package:logging/logging.dart ' ;
import ' package:signalr_netcore/signalr_client.dart ' ;
class ChatCallProvider with ChangeNotifier , DiagnosticableTreeMixin {
/ / / / / / / / / / / / / / / / / / / / / Web RTC Video Calling / / / / / / / / / / / / / / / / / / / / / /
@ -39,6 +40,7 @@ class ChatCallProvider with ChangeNotifier, DiagnosticableTreeMixin {
bool isLoudSpeaker = false ;
bool isCamOff = false ;
bool isCallEnded = false ;
/ / This need to Be Changed to dynamic
bool isVideoCall = true ;
bool isAudioCall = false ;
@ -54,48 +56,76 @@ class ChatCallProvider with ChangeNotifier, DiagnosticableTreeMixin {
List < MediaDeviceInfo > devices = [ ] ;
Future < void > buildHubConnection ( ) async {
try {
chatHubConnection = await getHubConnection ( ) ;
await chatHubConnection ! . start ( ) ;
} catch ( e ) {
print ( e . toString ( ) ) ;
Utils . showErrorToast ( e . toString ( ) ) ;
}
if ( kDebugMode ) {
print ( " Hub Conn: Startedddddddd " ) ;
}
/ / chatHubConnection . on ( " OnDeliveredChatUserAsync " , onMsgReceived ) ;
/ / chatHubConnection . on ( " OnGetChatConversationCount " , onNewChatConversion ) ;
/ / chatHubConnection . on ( " OnDeliveredGroupChatHistoryAsync " , onGroupMsgReceived ) ;
registerCallListners ( ) ;
}
/ /
/ / / / If you want only to log out the message for the higer level hub protocol:
/ / final hubProtLogger = Logger ( " SignalR - hub " ) ;
/ / final transportProtLogger = Logger ( " SignalR - transport " ) ;
/ / late Logger _logger ;
/ / late StreamSubscription < LogRecord > _logMessagesSub ;
/ /
/ / void _handleLogMessage ( LogRecord msg ) {
/ / print ( msg . message ) ;
/ / }
/ /
/ / Future < void > buildHubConnection ( ) async {
/ / try {
/ / Logger . root . level = Level . ALL ;
/ / _logMessagesSub = Logger . root . onRecord . listen ( _handleLogMessage ) ;
/ / _logger = Logger ( " ChatPageViewModel " ) ;
/ /
/ / / / chatHubConnection = await getHubConnection ( ) ;
/ / String to = " ytvF4zQfUrJHOUfpE7UE1Q " ;
/ / HttpConnectionOptions httpOp = HttpConnectionOptions ( skipNegotiation: false , logMessageContent: true , logger: transportProtLogger ) ;
/ / / / hub = HubConnectionBuilder ( )
/ / / / . withUrl ( ApiConsts . chatHubConnectionUrl + " ?UserId= ${ AppState ( ) . chatDetails ! . response ! . id } &source=Desktop&access_token= ${ AppState ( ) . chatDetails ! . response ! . token } " , options: httpOp )
/ / / / . withAutomaticReconnect ( retryDelays: < int > [ 2000 , 5000 , 10000 , 20000 ] ) . build ( ) ;
/ / var urlWithParams = " https://ms.hmg.com/livecare/liveCareHub?UserID=4767770&ProjectId=12&IsAdmin=false&To= $ to " ;
/ / print ( " ChatHubURL:::: " + urlWithParams ) ;
/ / chatHubConnection = await HubConnectionBuilder ( ) . withUrl ( urlWithParams , options: httpOp ) . build ( ) ;
/ / await chatHubConnection ! . start ( ) ;
/ / print ( chatHubConnection ! . state ) ;
/ / chatHubConnection ! . onclose ( ( { error } ) {
/ / print ( " Connection Closed " ) ;
/ / } ) ;
/ / / / if ( chatHubConnection ! . state = = HubConnectionState . Connected ) {
/ / / / print ( " Success Chat Con ID::: " + chatHubConnection ! . connectionId . toString ( ) ) ;
/ / / / if ( kDebugMode ) {
/ / / / print ( " Hub Conn: Startedddddddd " ) ;
/ / / / }
/ / / / / / registerCallListners ( ) ;
/ / / / }
/ / } catch ( e ) {
/ / print ( e . toString ( ) ) ;
/ / Utils . showErrorToast ( e . toString ( ) ) ;
/ / }
/ / }
/ / { " id " : 341682 , " userName " : " Aamir.Muhammad " , " email " : " Aamir.Muhammad@cloudsolutions.com.sa " , " phone " : null , " title " : " Aamir Saleem Ahmad Dost Muhammad " , " token " : " eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJuYW1laWQiOiIzNDE2ODIiLCJlbWFpbCI6IkFhbWlyLk11aGFtbWFkQGNsb3Vkc29sdXRpb25zLmNvbS5zYSIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvdXNlcmRhdGEiOiJBYW1pci5NdWhhbW1hZCIsIm5iZiI6MTcwNzIwNTAyMywiZXhwIjoxNzA3MjkxNDIzLCJpYXQiOjE3MDcyMDUwMjN9.Zz2bcsMwiLAAWGZh2E_p5Qs6ItMt0JHhDFI_0DLdggCQtt6HHcT4n8nvqMZx-uYr3jLwqhA_IAjTuaCeSE15sw " , " isDomainUser " : true , " isActiveCode " : false , " encryptedUserId " : " nn7RkYzbnQc= " , " encryptedUserName " : " /QruOyp4QMcmNXKZlljUgg== " }
Future < HubConnection > getHubConnection ( ) async {
AppState ( ) . chatDetails = UserAutoLoginModel (
response: Response . fromJson (
{
AppState ( ) . chatDetails = getUserToken . UserAutoLoginModel (
response: getUserToken . Response . fromJson ( {
" id " : 341682 ,
" userName " : " Aamir.Muhammad " ,
" email " : " Aamir.Muhammad@cloudsolutions.com.sa " ,
" phone " : null ,
" title " : " Aamir Saleem Ahmad Dost Muhammad " ,
" token " :
" eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJuYW1laWQiOiIzNDE2ODIiLCJlbWFpbCI6IkFhbWlyLk11aGFtbWFkQGNsb3Vkc29sdXRpb25zLmNvbS5zYSIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvdXNlcmRhdGEiOiJBYW1pci5NdWhhbW1hZCIsIm5iZiI6MTcwNzYzMDY0MiwiZXhwIjoxNzA3NzE3MDQyLCJpYXQiOjE3MDc2MzA2NDJ9.OZZFkXduuhlWTYh67Cw5o6Bxx4J9EcAKoq6Hn-ZqzE39BfZtiF_sswAvecaIF2ULrL4qO2vk8EV0naGPcjZrZw " ,
" eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJuYW1laWQiOiIzNDE2ODIiLCJlbWFpbCI6IkFhbWlyLk11aGFtbWFkQGNsb3Vkc29sdXRpb25zLmNvbS5zYSIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvdXNlcmRhdGEiOiJBYW1pci5NdWhhbW1hZCIsIm5iZiI6MTcw ODI1NjI5NywiZXhwIjoxNzA4MzQyNjk3LCJpYXQiOjE3MDgyNTYyOTd9.Y-QDVbSbPzcMVkHyIT06WgM0EsKH183hASZrIsXGkFOR_XZcqCJS-CrgdN0nOtxCWV6wd01dgfhMTNTZhybwiQ " ,
" isDomainUser " : true ,
" isActiveCode " : false ,
" encryptedUserId " : " nn7RkYzbnQc= " ,
" encryptedUserName " : " /QruOyp4QMcmNXKZlljUgg== "
} ,
) ) ;
} ) ,
) ;
String to = " dfK3ZqFD-BTlJLnuf1F5Cw " ;
HubConnection hub ;
HttpConnectionOptions httpOp = HttpConnectionOptions ( skipNegotiation: false , logMessageContent: true ) ;
hub = HubConnectionBuilder ( )
. withUrl ( ApiConsts . chatHubConnectionUrl + " ?UserId= ${ AppState ( ) . chatDetails ! . response ! . id } &source=Desktop&access_token= ${ AppState ( ) . chatDetails ! . response ! . token } " , options: httpOp )
. withAutomaticReconnect ( retryDelays: < int > [ 2000 , 5000 , 10000 , 20000 ] ) . build ( ) ;
/ / hub = HubConnectionBuilder ( )
/ / . withUrl ( ApiConsts . chatHubConnectionUrl + " ?UserId= ${ AppState ( ) . chatDetails ! . response ! . id } &source=Desktop&access_token= ${ AppState ( ) . chatDetails ! . response ! . token } " , options: httpOp )
/ / . withAutomaticReconnect ( retryDelays: < int > [ 2000 , 5000 , 10000 , 20000 ] ) . build ( ) ;
var urlWithParams = " https://ms.hmg.com/livecare/liveCareHub?UserID=4767770&ProjectId=15&IsAdmin=false&To= $ to " ;
print ( " ChatHubURL:::: " + urlWithParams ) ;
hub = await HubConnectionBuilder ( ) . withUrl ( urlWithParams , options: httpOp ) . withAutomaticReconnect ( retryDelays: < int > [ 2000 , 5000 , 10000 , 20000 ] ) . build ( ) ;
return hub ;
}
@ -805,26 +835,104 @@ class ChatCallProvider with ChangeNotifier, DiagnosticableTreeMixin {
/ / mobileRecoder = null ;
/ / notifyListeners ( ) ;
/ / }
/ / WebRtc
/ / If you want only to log out the message for the higer level hub protocol:
final hubProtLogger = Logger ( " SignalR - hub " ) ;
final transportProtLogger = Logger ( " SignalR - transport " ) ;
late Logger _logger ;
late StreamSubscription < LogRecord > _logMessagesSub ;
void _handleLogMessage ( LogRecord msg ) {
print ( msg . message ) ;
}
void _httpClientCreateCallback ( Client httpClient ) {
HttpOverrides . global = HttpOverrideCertificateVerificationInDev ( ) ;
}
/ / Future < void > buildHubConnection ( ) async {
/ / try {
/ / chatHubConnection = await HubConnectionBuilder ( ) . withUrl ( urlWithParams , options: httpOp ) . build ( ) ;
/ / await chatHubConnection ! . start ( ) ;
/ / print ( chatHubConnection ! . state ) ;
/ / chatHubConnection ! . onclose ( ( { error } ) {
/ / print ( " Connection Closed " ) ;
/ / } ) ;
/ / / / if ( chatHubConnection ! . state = = HubConnectionState . Connected ) {
/ / / / print ( " Success Chat Con ID::: " + chatHubConnection ! . connectionId . toString ( ) ) ;
/ / / / if ( kDebugMode ) {
/ / / / print ( " Hub Conn: Startedddddddd " ) ;
/ / / / }
/ / / / / / registerCallListners ( ) ;
/ / / / }
/ / } catch ( e ) {
/ / print ( e . toString ( ) ) ;
/ / Utils . showErrorToast ( e . toString ( ) ) ;
/ / }
/ / }
handleRecivedOffer ( List < Object ? > ? val ) {
print ( " ----------Event Received ---------- " ) ;
print ( val ) ;
}
Future < void > openChatConnection ( ) async {
_logger = Logger ( " Demo " ) ;
final logger = _logger ;
if ( chatHubConnection = = null ) {
final httpConnectionOptions = HttpConnectionOptions ( httpClient: WebSupportingHttpClient ( logger , httpClientCreateCallback: _httpClientCreateCallback ) , logger: logger , logMessageContent: true ) ;
Logger . root . level = Level . ALL ;
_logMessagesSub = Logger . root . onRecord . listen ( _handleLogMessage ) ;
String to = " aXvFIMTEFRAKLJv5flIEWw " ;
var urlWithParams = " https://ms.hmg.com/livecare/liveCareHub?UserID=4767770&ProjectId=15&IsAdmin=false&To= $ to " ;
print ( " ChatHubURL:::: " + urlWithParams ) ;
chatHubConnection =
await HubConnectionBuilder ( ) . withUrl ( urlWithParams , options: httpConnectionOptions ) . withAutomaticReconnect ( retryDelays: [ 2000 , 5000 , 10000 , 20000 ] ) . configureLogging ( logger ) . build ( ) ;
}
print ( " ------------ Connection State -------------- " + chatHubConnection ! . state . toString ( ) ) ;
if ( chatHubConnection ! . state ! = HubConnectionState . Connected ) {
try {
await chatHubConnection ! . start ( ) ;
print ( " ------------ Connection State -------------- " + chatHubConnection ! . state . toString ( ) ) ;
print ( " Connection Stablished -------------- " ) ;
} catch ( e ) {
print ( e ) ;
}
}
print ( " ------------ Before Events Register -------------- " ) ;
chatHubConnection ! . on ( " pre-offer " , handleRecivedOffer ) ;
chatHubConnection ! . on ( " OnOffer " , handleRecivedOffer ) ;
chatHubConnection ! . on ( " IceCandidate " , handleRecivedOffer ) ;
print ( " ------------ After Events Register -------------- " ) ;
}
}
class HttpOverrideCertificateVerificationInDev extends HttpOverrides {
@ override
HttpClient createHttpClient ( SecurityContext ? context ) {
return super . createHttpClient ( context ) . . badCertificateCallback = ( X509Certificate cert , String host , int port ) = > true ;
}
}
class ChatService extends BaseService {
Future < UserAutoLoginModel > getUserCallToken ( { required String userid } ) async {
UserAutoLoginModel userLoginResponse = UserAutoLoginModel ( ) ;
baseAppClient . post (
" ${ ApiConsts . chatLoginTokenUrl } externaluserlogin " ,
body: {
" employeeNumber " : userid ,
" password " : " FxIu26rWIKoF8n6mpbOmAjDLphzFGmpG " ,
} ,
onSuccess: ( dynamic response , int statusCode ) {
userLoginResponse = userAutoLoginModelFromJson ( response . body ) ;
} ,
onFailure: ( String error , int statusCode ) {
hasError = true ;
print ( error ) ;
super . error = error ;
} ,
) ;
Future < getUserToken . UserAutoLoginModel > getUserCallToken ( { required String userid } ) async {
getUserToken . UserAutoLoginModel userLoginResponse = getUserToken . UserAutoLoginModel ( ) ;
http . post ( Uri . parse ( " ${ ApiConsts . chatLoginTokenUrl } externaluserlogin " ) , body: {
" employeeNumber " : userid ,
" password " : " FxIu26rWIKoF8n6mpbOmAjDLphzFGmpG " ,
} ) . then ( ( response ) {
userLoginResponse = getUserToken . userAutoLoginModelFromJson ( response . body ) ;
} ) . onError ( ( error , stackTrace ) {
hasError = true ;
print ( error ) ;
super . error = error . toString ( ) ;
} ) ;
return userLoginResponse ;
}
}