voip call

dev_v3.13.6_voipcall
Aamir Muhammad 2 years ago
parent 231e3dc36d
commit c414fd7f20

@ -1,3 +1,5 @@
import 'dart:async';
import 'package:diplomaticquarterapp/models/LiveCare/IncomingCallData.dart'; import 'package:diplomaticquarterapp/models/LiveCare/IncomingCallData.dart';
import 'package:diplomaticquarterapp/pages/conference/widgets/platform_exception_alert_dialog.dart'; import 'package:diplomaticquarterapp/pages/conference/widgets/platform_exception_alert_dialog.dart';
import 'package:diplomaticquarterapp/pages/landing/landing_page.dart'; import 'package:diplomaticquarterapp/pages/landing/landing_page.dart';
@ -9,7 +11,6 @@ import 'package:diplomaticquarterapp/voipcall/provider/chat_call_provider.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart'; import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:just_audio/just_audio.dart'; import 'package:just_audio/just_audio.dart';
import 'package:logger/logger.dart';
import 'package:permission_handler/permission_handler.dart'; import 'package:permission_handler/permission_handler.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
@ -29,17 +30,16 @@ class _IncomingCallState extends State<IncomingCall> with SingleTickerProviderSt
bool openCallInWeb = true; bool openCallInWeb = true;
final player = AudioPlayer(); final player = AudioPlayer();
bool isCameraReady = false; bool isCameraReady = false;
Logger mylogs = Logger();
ChatCallProvider? callProv; ChatCallProvider? callProv;
bool isAccepted = false;
@override @override
void initState() { void initState() {
_animationController = AnimationController(vsync: this, duration: Duration(milliseconds: 500)); _animationController = AnimationController(vsync: this, duration: Duration(milliseconds: 500));
isCameraReady = false; isCameraReady = false;
WidgetsBinding.instance.addPostFrameCallback((_) => _runAnimation()); WidgetsBinding.instance.addPostFrameCallback((_) => _runAnimation());
//mylogs.d(widget.incomingCallData!.toJson());
super.initState(); super.initState();
initCount();
} }
@override @override
@ -50,11 +50,12 @@ class _IncomingCallState extends State<IncomingCall> with SingleTickerProviderSt
super.dispose(); super.dispose();
} }
acceptCall() async { acceptCall({required bool isConnectCall}) async {
Future<PermissionStatus> micPer = Permission.microphone.request(); Future<PermissionStatus> micPer = Permission.microphone.request();
Future<PermissionStatus> camPer = Permission.camera.request(); Future<PermissionStatus> camPer = Permission.camera.request();
if (await micPer.isGranted && await camPer.isGranted) { if (await micPer.isGranted && await camPer.isGranted) {
await callProv!.buildHubConnection(); isAccepted = true;
await callProv!.initCall(isConnectCall: isConnectCall);
} else if (await micPer.isDenied) { } else if (await micPer.isDenied) {
micPer = Permission.microphone.request(); micPer = Permission.microphone.request();
} else if (await camPer.isDenied) { } else if (await camPer.isDenied) {
@ -62,9 +63,18 @@ class _IncomingCallState extends State<IncomingCall> with SingleTickerProviderSt
} }
} }
endCall() async {
await callProv!.initCall(isConnectCall: false);
LandingPage.isOpenCallPage = false;
isAccepted = false;
player.stop();
Navigator.of(context).pop();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
callProv = Provider.of<ChatCallProvider>(context); callProv = Provider.of<ChatCallProvider>(context);
callProv!.incomingCallData = widget.incomingCallData;
return AppScaffold( return AppScaffold(
isShowAppBar: false, isShowAppBar: false,
isShowDecPage: false, isShowDecPage: false,
@ -154,9 +164,7 @@ class _IncomingCallState extends State<IncomingCall> with SingleTickerProviderSt
child: Container( child: Container(
child: RawMaterialButton( child: RawMaterialButton(
onPressed: () { onPressed: () {
// _submit(); acceptCall(isConnectCall: true);
callProv!.incomingCallData = widget.incomingCallData;
acceptCall();
}, },
elevation: 2.0, elevation: 2.0,
fillColor: Colors.green, fillColor: Colors.green,
@ -172,7 +180,8 @@ class _IncomingCallState extends State<IncomingCall> with SingleTickerProviderSt
Container( Container(
child: RawMaterialButton( child: RawMaterialButton(
onPressed: () { onPressed: () {
backToHome(); //backToHome();
endCall();
}, },
elevation: 2.0, elevation: 2.0,
fillColor: Colors.red, fillColor: Colors.red,
@ -276,4 +285,15 @@ class _IncomingCallState extends State<IncomingCall> with SingleTickerProviderSt
print("Error: $e"); print("Error: $e");
} }
} }
void initCount() {
Future.delayed(
Duration(seconds: 30),
() async {
if (!isAccepted) {
callProv!.NoAnswer();
}
},
);
}
} }

@ -156,12 +156,8 @@ _incomingCall(Map<String, dynamic> data) async {
LandingPage.incomingCallData = IncomingCallData.fromJson(data); LandingPage.incomingCallData = IncomingCallData.fromJson(data);
if (LandingPage.isOpenCallPage == false) { if (LandingPage.isOpenCallPage == false) {
LandingPage.isOpenCallPage = true; LandingPage.isOpenCallPage = true;
final bool permited = await AppPermission.askVideoCallPermission(currentContext!); final bool permitted = await AppPermission.askVideoCallPermission(currentContext!);
if (permited) if (permitted) await NavigationService.navigateToPage(IncomingCall(incomingCallData: LandingPage.incomingCallData));
await NavigationService.navigateToPage(
IncomingCall(incomingCallData: LandingPage.incomingCallData),
);
LandingPage.isOpenCallPage = false; LandingPage.isOpenCallPage = false;
} }
await Future.delayed(Duration(milliseconds: 500)); await Future.delayed(Duration(milliseconds: 500));

@ -23,7 +23,6 @@ class StartCallPage extends StatefulWidget {
class _StartCallPageState extends State<StartCallPage> { class _StartCallPageState extends State<StartCallPage> {
DragController dragController = DragController(); DragController dragController = DragController();
ChatCallProvider? cProv;
@override @override
void initState() { void initState() {
@ -122,17 +121,6 @@ class _StartCallPageState extends State<StartCallPage> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
cProv = context.read<ChatCallProvider>();
// if (!cProv!.isOutGoingCall) {
// if (Platform.isAndroid) {
// // startCall();
// } else if (Platform.isIOS) {
// // cProv!.buildHubConnection();
// // startIosCall();
// // startIosCall();
// }
// }
return Scaffold( return Scaffold(
extendBody: true, extendBody: true,
body: Consumer<ChatCallProvider>( body: Consumer<ChatCallProvider>(
@ -143,467 +131,230 @@ class _StartCallPageState extends State<StartCallPage> {
height: double.infinity, height: double.infinity,
child: Center(child: CircularProgressIndicator()), child: Center(child: CircularProgressIndicator()),
) )
: prov.isIncomingCall : Container(
? Container( width: double.infinity,
width: double.infinity, height: double.infinity,
height: double.infinity, color: Colors.black,
color: Colors.black, child: Stack(
child: Stack( alignment: FractionalOffset.center,
alignment: FractionalOffset.center, children: <Widget>[
children: <Widget>[ if (!prov.isAudioCall && prov.isVideoCall)
if (!prov.isAudioCall && prov.isVideoCall) RTCVideoView(
RTCVideoView( prov.remoteRenderer!,
prov.remoteRenderer!, objectFit: RTCVideoViewObjectFit.RTCVideoViewObjectFitContain,
objectFit: RTCVideoViewObjectFit.RTCVideoViewObjectFitContain, key: const Key('remote'),
key: const Key('remote'), ),
if (prov.isVideoCall)
DraggableWidget(
bottomMargin: 20,
topMargin: 40,
intialVisibility: true,
horizontalSpace: 20,
shadowBorderRadius: 50,
initialPosition: AnchoringPosition.topLeft,
dragController: dragController,
normalShadow: const BoxShadow(spreadRadius: 0.0, blurRadius: 0.0),
draggingShadow: const BoxShadow(spreadRadius: 0.0, blurRadius: 0.0),
child: SizedBox(
height: 200,
width: 140,
child: RTCVideoView(
prov.localVideoRenderer!,
mirror: true,
// filterQuality: FilterQuality.high,
objectFit: RTCVideoViewObjectFit.RTCVideoViewObjectFitCover,
), ),
if (prov.isVideoCall) ),
DraggableWidget( ),
bottomMargin: 20, if (!prov.isVideoCall)
topMargin: 40, Positioned.fill(
intialVisibility: true, child: ClipRect(
horizontalSpace: 20, child: BackdropFilter(
shadowBorderRadius: 50, filter: ImageFilter.blur(sigmaX: 5.0, sigmaY: 5.0),
initialPosition: AnchoringPosition.topLeft, child: Container(
dragController: dragController, decoration: BoxDecoration(
normalShadow: const BoxShadow(spreadRadius: 0.0, blurRadius: 0.0), color: MyColors.grey57Color.withOpacity(
draggingShadow: const BoxShadow(spreadRadius: 0.0, blurRadius: 0.0), 0.3,
child: SizedBox( ),
height: 200,
width: 140,
child: RTCVideoView(
prov.localVideoRenderer!,
mirror: true,
// filterQuality: FilterQuality.high,
objectFit: RTCVideoViewObjectFit.RTCVideoViewObjectFitCover,
), ),
), child: Column(
), crossAxisAlignment: CrossAxisAlignment.start,
if (!prov.isVideoCall) mainAxisSize: MainAxisSize.max,
Positioned.fill( children: <Widget>[
child: ClipRect( SizedBox(
child: BackdropFilter( height: 40,
filter: ImageFilter.blur(sigmaX: 5.0, sigmaY: 5.0),
child: Container(
decoration: BoxDecoration(
color: MyColors.grey57Color.withOpacity(
0.3,
),
), ),
child: Column( Row(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max, mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[ children: <Widget>[
SizedBox( Container(
height: 40, margin: const EdgeInsets.all(21.0),
), child: Container(
Row( margin: const EdgeInsets.only(
crossAxisAlignment: CrossAxisAlignment.center, left: 10.0,
mainAxisAlignment: MainAxisAlignment.center, right: 10.0,
children: <Widget>[ ),
Container( child: Column(
margin: const EdgeInsets.all(21.0), crossAxisAlignment: CrossAxisAlignment.center,
child: Container( mainAxisSize: MainAxisSize.min,
margin: const EdgeInsets.only( mainAxisAlignment: MainAxisAlignment.spaceAround,
left: 10.0, children: <Widget>[
right: 10.0, SvgPicture.asset(
"assets/images/user.svg",
height: 70,
width: 70,
fit: BoxFit.cover,
), ),
child: Column( SizedBox(
crossAxisAlignment: CrossAxisAlignment.center, height: 10,
mainAxisSize: MainAxisSize.min, ),
mainAxisAlignment: MainAxisAlignment.spaceAround, Text(
children: <Widget>[ prov.incomingCallData!.doctorname ?? "",
SvgPicture.asset( style: const TextStyle(
"assets/images/user.svg", fontSize: 21,
height: 70, decoration: TextDecoration.none,
width: 70, fontWeight: FontWeight.bold,
fit: BoxFit.cover, color: MyColors.white,
), letterSpacing: -1.26,
SizedBox( height: 23 / 12,
height: 10, ),
), ),
Text( const Text(
prov.incomingCallData!.doctorname ?? "", "On Call",
style: const TextStyle( style: TextStyle(
fontSize: 21, fontSize: 16,
decoration: TextDecoration.none, decoration: TextDecoration.none,
fontWeight: FontWeight.bold, fontWeight: FontWeight.w600,
color: MyColors.white, color: Color(
letterSpacing: -1.26, 0xffC6C6C6,
height: 23 / 12,
),
),
const Text(
"On Call",
style: TextStyle(
fontSize: 16,
decoration: TextDecoration.none,
fontWeight: FontWeight.w600,
color: Color(
0xffC6C6C6,
),
letterSpacing: -0.48,
height: 23 / 24,
),
),
const SizedBox(
height: 2,
), ),
], letterSpacing: -0.48,
height: 23 / 24,
),
), ),
), const SizedBox(
height: 2,
),
],
), ),
], ),
), ),
], ],
), ),
), ],
), ),
), ),
), ),
Align(
alignment: Alignment.bottomCenter,
child: Container(
padding: const EdgeInsets.only(
bottom: 20,
left: 40,
right: 40,
),
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
// if (provider.isVideoCall)
RawMaterialButton(
constraints: const BoxConstraints(),
onPressed: () {
prov.loudOn();
},
elevation: 2.0,
fillColor: prov.isLoudSpeaker ? MyColors.textMixColor : Colors.grey,
padding: const EdgeInsets.all(
10.0,
),
shape: const CircleBorder(),
child: const Icon(
Icons.volume_up,
color: MyColors.white,
size: 30.0,
),
),
if (prov.isVideoCall)
RawMaterialButton(
constraints: const BoxConstraints(),
onPressed: () {
prov.camOff();
},
elevation: 2.0,
fillColor: prov.isCamOff ? MyColors.textMixColor : Colors.grey,
padding: const EdgeInsets.all(
10.0,
),
shape: const CircleBorder(),
child: Icon(
prov.isCamOff ? Icons.videocam_off : Icons.videocam,
color: MyColors.white,
size: 30.0,
),
),
if (prov.isVideoCall)
RawMaterialButton(
constraints: const BoxConstraints(),
onPressed: () {
prov.switchCamera();
},
elevation: 2.0,
fillColor: prov.isFrontCamera ? Colors.grey : MyColors.textMixColor,
padding: const EdgeInsets.all(
10.0,
),
shape: const CircleBorder(),
child: Icon(
prov.isFrontCamera ? Icons.switch_camera_outlined : Icons.switch_camera,
color: MyColors.white,
size: 30.0,
),
),
RawMaterialButton(
constraints: const BoxConstraints(),
onPressed: () {
prov.micOff();
},
elevation: 2.0,
fillColor: prov.isMicOff ? MyColors.textMixColor : Colors.grey,
padding: const EdgeInsets.all(
10.0,
),
shape: const CircleBorder(),
child: Icon(
prov.isMicOff ? Icons.mic_off : Icons.mic,
color: MyColors.white,
size: 30.0,
),
),
RawMaterialButton(
constraints: const BoxConstraints(),
onPressed: () {
// prov.endCall(isUserOnline: prov.isUserOnline).then((bool value) {
// if (value) {
// Navigator.of(context).pop();
// }
// });
},
elevation: 2.0,
fillColor: MyColors.redA3Color,
padding: const EdgeInsets.all(
10.0,
),
shape: const CircleBorder(),
child: const Icon(
Icons.call_end,
color: MyColors.white,
size: 30.0,
),
),
],
),
),
), ),
], ),
), Align(
) alignment: Alignment.bottomCenter,
: prov.isOutGoingCall child: Container(
? Container( padding: const EdgeInsets.only(
width: double.infinity, bottom: 20,
height: double.infinity, left: 40,
color: Colors.black, right: 40,
child: Stack( ),
alignment: FractionalOffset.center, child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[ children: <Widget>[
if (!prov.isAudioCall && prov.isVideoCall) // if (provider.isVideoCall)
RTCVideoView( RawMaterialButton(
prov.remoteRenderer!, constraints: const BoxConstraints(),
objectFit: RTCVideoViewObjectFit.RTCVideoViewObjectFitContain, onPressed: () {
key: const Key('remote'), prov.loudOn();
},
elevation: 2.0,
fillColor: prov.isLoudSpeaker ? MyColors.textMixColor : Colors.grey,
padding: const EdgeInsets.all(
10.0,
),
shape: const CircleBorder(),
child: const Icon(
Icons.volume_up,
color: MyColors.white,
size: 30.0,
), ),
),
if (prov.isVideoCall) if (prov.isVideoCall)
DraggableWidget( RawMaterialButton(
bottomMargin: 20, constraints: const BoxConstraints(),
topMargin: 40, onPressed: () {
intialVisibility: true, prov.camOff();
horizontalSpace: 20, },
shadowBorderRadius: 50, elevation: 2.0,
initialPosition: AnchoringPosition.topLeft, fillColor: prov.isCamOff ? MyColors.textMixColor : Colors.grey,
dragController: dragController, padding: const EdgeInsets.all(
normalShadow: const BoxShadow(spreadRadius: 0.0, blurRadius: 0.0), 10.0,
draggingShadow: const BoxShadow(spreadRadius: 0.0, blurRadius: 0.0),
child: SizedBox(
height: 200,
width: 140,
child: RTCVideoView(
prov.localVideoRenderer!,
mirror: true,
objectFit: RTCVideoViewObjectFit.RTCVideoViewObjectFitCover,
),
), ),
), shape: const CircleBorder(),
if (!prov.isVideoCall) child: Icon(
Positioned.fill( prov.isCamOff ? Icons.videocam_off : Icons.videocam,
child: ClipRect( color: MyColors.white,
child: BackdropFilter( size: 30.0,
filter: ImageFilter.blur(sigmaX: 5.0, sigmaY: 5.0),
child: Container(
decoration: BoxDecoration(
color: MyColors.grey57Color.withOpacity(
0.3,
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
SizedBox(
height: 40,
),
Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
margin: const EdgeInsets.all(21.0),
child: Container(
margin: const EdgeInsets.only(
left: 10.0,
right: 10.0,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
SvgPicture.asset(
"assets/images/user.svg",
height: 70,
width: 70,
fit: BoxFit.cover,
),
SizedBox(
height: 10,
),
Text(
prov.outGoingCallData!.receiverName!,
style: const TextStyle(
fontSize: 21,
decoration: TextDecoration.none,
fontWeight: FontWeight.bold,
color: MyColors.white,
letterSpacing: -1.26,
height: 23 / 12,
),
),
const Text(
"On Call",
style: TextStyle(
fontSize: 16,
decoration: TextDecoration.none,
fontWeight: FontWeight.w600,
color: Color(
0xffC6C6C6,
),
letterSpacing: -0.48,
height: 23 / 24,
),
),
const SizedBox(
height: 2,
),
],
),
),
),
],
),
],
),
),
),
), ),
), ),
Align( if (prov.isVideoCall)
alignment: Alignment.bottomCenter, RawMaterialButton(
child: Container( constraints: const BoxConstraints(),
padding: const EdgeInsets.only( onPressed: () {
bottom: 20, prov.switchCamera();
left: 40, },
right: 40, elevation: 2.0,
fillColor: prov.isFrontCamera ? Colors.grey : MyColors.textMixColor,
padding: const EdgeInsets.all(
10.0,
), ),
child: Row( shape: const CircleBorder(),
mainAxisSize: MainAxisSize.max, child: Icon(
mainAxisAlignment: MainAxisAlignment.spaceBetween, prov.isFrontCamera ? Icons.switch_camera_outlined : Icons.switch_camera,
children: <Widget>[ color: MyColors.white,
// if (provider.isVideoCall) size: 30.0,
RawMaterialButton(
constraints: const BoxConstraints(),
onPressed: () {
prov.loudOn();
},
elevation: 2.0,
fillColor: prov.isLoudSpeaker ? MyColors.textMixColor : Colors.grey,
padding: const EdgeInsets.all(
10.0,
),
shape: const CircleBorder(),
child: const Icon(
Icons.volume_up,
color: MyColors.white,
size: 30.0,
),
),
if (prov.isVideoCall)
RawMaterialButton(
constraints: const BoxConstraints(),
onPressed: () {
prov.camOff();
},
elevation: 2.0,
fillColor: prov.isCamOff ? MyColors.textMixColor : Colors.grey,
padding: const EdgeInsets.all(
10.0,
),
shape: const CircleBorder(),
child: Icon(
prov.isCamOff ? Icons.videocam_off : Icons.videocam,
color: MyColors.white,
size: 30.0,
),
),
if (prov.isVideoCall)
RawMaterialButton(
constraints: const BoxConstraints(),
onPressed: () {
prov.switchCamera();
},
elevation: 2.0,
fillColor: prov.isFrontCamera ? Colors.grey : MyColors.textMixColor,
padding: const EdgeInsets.all(
10.0,
),
shape: const CircleBorder(),
child: Icon(
prov.isFrontCamera ? Icons.switch_camera_outlined : Icons.switch_camera,
color: MyColors.white,
size: 30.0,
),
),
RawMaterialButton(
constraints: const BoxConstraints(),
onPressed: () {
prov.micOff();
},
elevation: 2.0,
fillColor: prov.isMicOff ? MyColors.textMixColor : Colors.grey,
padding: const EdgeInsets.all(
10.0,
),
shape: const CircleBorder(),
child: Icon(
prov.isMicOff ? Icons.mic_off : Icons.mic,
color: MyColors.white,
size: 30.0,
),
),
RawMaterialButton(
constraints: const BoxConstraints(),
onPressed: () {
// prov.endCall(isUserOnline: prov.isUserOnline).then(
// (bool value) {
// if (value) {
// Navigator.of(context).pop();
// }
// },
// );
},
elevation: 2.0,
fillColor: MyColors.redA3Color,
padding: const EdgeInsets.all(
10.0,
),
shape: const CircleBorder(),
child: const Icon(
Icons.call_end,
color: MyColors.white,
size: 30.0,
),
),
],
), ),
), ),
RawMaterialButton(
constraints: const BoxConstraints(),
onPressed: () {
prov.micOff();
},
elevation: 2.0,
fillColor: prov.isMicOff ? MyColors.textMixColor : Colors.grey,
padding: const EdgeInsets.all(
10.0,
),
shape: const CircleBorder(),
child: Icon(
prov.isMicOff ? Icons.mic_off : Icons.mic,
color: MyColors.white,
size: 30.0,
),
),
RawMaterialButton(
constraints: const BoxConstraints(),
onPressed: () {
prov.handleCallComplete([{}]);
},
elevation: 2.0,
fillColor: MyColors.redA3Color,
padding: const EdgeInsets.all(
10.0,
),
shape: const CircleBorder(),
child: const Icon(
Icons.call_end,
color: MyColors.white,
size: 30.0,
),
), ),
], ],
), ),
) ),
: const SizedBox(); ),
],
),
);
}, },
), ),
); );

@ -4,6 +4,7 @@ import 'dart:io';
import 'package:diplomaticquarterapp/locator.dart'; import 'package:diplomaticquarterapp/locator.dart';
import 'package:diplomaticquarterapp/models/LiveCare/IncomingCallData.dart'; import 'package:diplomaticquarterapp/models/LiveCare/IncomingCallData.dart';
import 'package:diplomaticquarterapp/routes.dart';
import 'package:diplomaticquarterapp/uitl/navigation_service.dart'; import 'package:diplomaticquarterapp/uitl/navigation_service.dart';
import 'package:diplomaticquarterapp/uitl/utils.dart'; import 'package:diplomaticquarterapp/uitl/utils.dart';
import 'package:diplomaticquarterapp/voipcall/call/chat_incoming_call_screen.dart'; import 'package:diplomaticquarterapp/voipcall/call/chat_incoming_call_screen.dart';
@ -858,56 +859,79 @@ class ChatCallProvider with ChangeNotifier, DiagnosticableTreeMixin {
_patientID = value; _patientID = value;
} }
void _httpClientCreateCallback(Client httpClient) { bool _isAccepted = false;
bool get isAccepted => _isAccepted;
set isAccepted(bool value) {
_isAccepted = value;
}
// int _secondsRemaining = 30;
// late Timer _timer;
// void startCountTimer() {
// _timer = Timer.periodic(Duration(seconds: 1), (timer) async {
// if (_secondsRemaining > 0) {
// _secondsRemaining--;
// if (isAccepted) {
// _timer.cancel(); // Stop the timer if isAccepted becomes true
// }
// } else {
// _timer.cancel();
// if (chatHubConnection != null) {
// chatHubConnection!.stop();
// chatHubConnection = null;
// }
// chatHubConnection = await createHub();
// NoAnswer();
// }
// });
// // notifyListeners();
// }
NoAnswer() {
handleCallComplete([{}]);
}
void httpCallback(Client httpClient) {
HttpOverrides.global = HttpOverrideCertificateVerificationInDev(); HttpOverrides.global = HttpOverrideCertificateVerificationInDev();
} }
Future<void> buildHubConnection() async { Future<HubConnection> createHub() async {
doctorID = incomingCallData!.sessionId!.split("**").last; doctorID = incomingCallData!.sessionId!.split("**").last;
// doctorID = "6wgFpttYUsdnB-klo-3xeg";
String path = incomingCallData!.sessionId!.split("**").first; String path = incomingCallData!.sessionId!.split("**").first;
patientID = incomingCallData!.receiverID; patientID = incomingCallData!.receiverID;
_logger = Logger("ChatPageViewModel"); _logger = Logger("ChatPageViewModel");
final logger = _logger; final httpConnectionOptions = HttpConnectionOptions(logMessageContent: true, httpClient: WebSupportingHttpClient(_logger, httpClientCreateCallback: httpCallback));
var urlWithParams = path + "UserID=$patientID&ProjectId=15&IsAdmin=false&To=$doctorID";
HubConnection hubCon =
await HubConnectionBuilder().withUrl(urlWithParams, options: httpConnectionOptions).withAutomaticReconnect(retryDelays: [2000, 5000, 10000, 20000]).configureLogging(_logger).build();
return hubCon;
}
Future<void> initCall({required bool isConnectCall}) async {
if (chatHubConnection != null) { if (chatHubConnection != null) {
chatHubConnection!.stop(); chatHubConnection!.stop();
chatHubConnection = null; chatHubConnection = null;
} }
chatHubConnection = await createHub();
if (doctorID != null) { if (doctorID != null) {
try { try {
final httpConnectionOptions =
new HttpConnectionOptions(httpClient: WebSupportingHttpClient(logger, httpClientCreateCallback: _httpClientCreateCallback), logger: logger, logMessageContent: true);
var urlWithParams = path + "UserID=$patientID&ProjectId=15&IsAdmin=false&To=$doctorID";
print(urlWithParams);
chatHubConnection =
await HubConnectionBuilder().withUrl(urlWithParams, options: httpConnectionOptions).withAutomaticReconnect(retryDelays: [2000, 5000, 10000, 20000]).configureLogging(logger).build();
chatHubConnection!.onclose(({error}) { chatHubConnection!.onclose(({error}) {
// print("Connection Closed");
Utils.showErrorToast(error.toString()); Utils.showErrorToast(error.toString());
}); });
register(); register();
if (chatHubConnection!.state == HubConnectionState.Disconnected) { if (chatHubConnection!.state == HubConnectionState.Disconnected) {
await chatHubConnection!.start(); await chatHubConnection!.start();
print(chatHubConnection!.state);
} }
if (chatHubConnection!.state == HubConnectionState.Connected) { if (chatHubConnection!.state == HubConnectionState.Connected) {
if (chatHubConnection != null) { if (chatHubConnection != null) {
patientID = chatHubConnection!.connectionId; patientID = chatHubConnection!.connectionId;
print("DOC: " + doctorID.toString()); print("DOC: " + doctorID.toString());
print("PAT " + patientID.toString()); print("PAT " + patientID.toString());
print("Success Chat Con ID::: " + chatHubConnection!.connectionId.toString());
try { try {
chatHubConnection!.invoke("Call_Accepts", args: [ isConnectCall ? ping("Call_Accept") : ping("Call_Decline");
// jsonEncode({"UserID": int.parse(incomingCallData!.receiverID!)})
{"UserID": 4767780}
]).onError(
(error, stackTrace) => print(
error.toString(),
),
);
} catch (e) { } catch (e) {
print(e); print(e);
} }
@ -916,48 +940,58 @@ class ChatCallProvider with ChangeNotifier, DiagnosticableTreeMixin {
} catch (e) { } catch (e) {
print(e.toString()); print(e.toString());
Utils.showErrorToast(e.toString()); Utils.showErrorToast(e.toString());
chatHubConnection!.invoke("Call_Accepts", args: [
// jsonEncode({"UserID": int.parse(incomingCallData!.receiverID!)})
{4767780}
]).onError(
(error, stackTrace) => print(
error.toString(),
),
);
// await buildHubConnection();
} }
} else { } else {
Utils.showErrorToast("Doctor ID is not correct"); Utils.showErrorToast("Doctor ID is not correct");
} }
} }
invoke(String method) async {} ping(String eventName) async {
print("============ $eventName =====================");
if (chatHubConnection != null) if (chatHubConnection!.state == HubConnectionState.Connected) {
print("============ CONNECTED ==============");
dynamic payload = {"UserID": int.parse(incomingCallData!.receiverID!)};
if (eventName == "Call_Complete") payload = {"from": patientID, "to": doctorID};
await chatHubConnection!.invoke(eventName, args: [payload]).onError((error, stackTrace) {}).whenComplete(() {
if (eventName == "Call_Decline" || eventName == "Call_Complete" || eventName == "Call_NoAnswer") {
if (chatHubConnection != null) {
chatHubConnection!.stop();
}
}
});
} else {
print("============ ELSE RECONNECT ==============");
chatHubConnection = await createHub();
chatHubConnection!.start();
ping(eventName);
}
}
void register() async { void register() async {
chatHubConnection!.on("pre-offer", handlePreOffer); chatHubConnection!.on("pre_offer", handlePreOffer);
chatHubConnection!.on("OnOffer", handleOnOffer); chatHubConnection!.on("on_offer", handleOnOffer);
chatHubConnection!.on("IceCandidate", handleIceCandidate); chatHubConnection!.on("on_ice_candidate", handleIceCandidate);
// New Events // New Events
chatHubConnection!.on("Call_Decline", handleCallDecline); chatHubConnection!.on("Call_Decline", handleCallDecline);
chatHubConnection!.on("Call_NoAnswer", handleNoAnswerCall); chatHubConnection!.on("call_completed", handleCallComplete);
chatHubConnection!.on("CallCompleted", handleCallComplete); if (kDebugMode) debugPrint("Listners Initiated");
chatHubConnection!.on("connected_users", genericCheckEvent);
print("Listners Initiated");
} }
genericCheckEvent(List<Object?>? params) async { // genericCheckEvent(List<Object?>? params) async {
print("------------- Generic Event Triggered ---------------"); // print("------------- Generic Event Triggered ---------------");
print("------------- Generic Event Triggered ---------------"); // print("------------- Generic Event Triggered ---------------");
print(params); // print(params);
print("------------- Generic Event Triggered ---------------"); // print("------------- Generic Event Triggered ---------------");
print("------------- Generic Event Triggered ---------------"); // print("------------- Generic Event Triggered ---------------");
} // }
handlePreOffer(List<Object?>? params) async { handlePreOffer(List<Object?>? params) async {
print("----------Event Pre Offer Received ----------"); print("----------Event Pre Offer Received ----------");
dynamic items = params!.toList(); dynamic items = params!.toList();
print("Doctor ID : " + items[0]['from'].toString()); if (kDebugMode) {
print("Patient ID: " + items[0]['to'].toString()); print("Doctor ID : " + items[0]['from'].toString());
print("Patient ID: " + items[0]['to'].toString());
}
doctorID = items[0]['from']; doctorID = items[0]['from'];
patientID = items[0]['to']; patientID = items[0]['to'];
initStreams(); initStreams();
@ -988,8 +1022,8 @@ class ChatCallProvider with ChangeNotifier, DiagnosticableTreeMixin {
}); });
} }
startCallByRTC(); startCallByRTC();
Future.delayed(Duration(seconds: 5), () { Future.delayed(Duration(seconds: 2), () {
Navigator.push( Navigator.pushReplacement(
locator<NavigationService>().navigatorKey.currentContext!, locator<NavigationService>().navigatorKey.currentContext!,
MaterialPageRoute( MaterialPageRoute(
builder: (BuildContext context) => StartCallPage(), builder: (BuildContext context) => StartCallPage(),
@ -1014,17 +1048,17 @@ class ChatCallProvider with ChangeNotifier, DiagnosticableTreeMixin {
debuglogger.d(items); debuglogger.d(items);
} }
handleNoAnswerCall(List<Object?>? params) {
print("----------handle No Answer Received ----------");
dynamic items = params!.toList();
debuglogger.d(items);
}
handleCallComplete(List<Object?>? params) { handleCallComplete(List<Object?>? params) {
print("----------handle Call Complete Received ----------"); print("----------handle Call Complete Received ----------");
dynamic items = params!.toList(); dynamic items = params!.toList();
debuglogger.d(items); debuglogger.d(items);
if (isCallConnected) ping("Call_Complete");
if (!isCallConnected && !isAccepted) {
ping("Call_NoAnswer");
} else {
if (!isCallConnected) ping("Call_Decline");
}
isCallConnected = false;
isCallStarted = false; isCallStarted = false;
isVideoCall = false; isVideoCall = false;
isCamOff = false; isCamOff = false;
@ -1033,7 +1067,9 @@ class ChatCallProvider with ChangeNotifier, DiagnosticableTreeMixin {
isIncomingCall = false; isIncomingCall = false;
isOutGoingCall = false; isOutGoingCall = false;
isAudioCall = false; isAudioCall = false;
if (pCon!.connectionState == RTCPeerConnectionState.RTCPeerConnectionStateConnected) { isAccepted = false;
if (pCon != null) if (pCon!.connectionState == RTCPeerConnectionState.RTCPeerConnectionStateConnected) {
if (_localStream != null) pCon!.removeStream(_localStream!);
pCon!.close(); pCon!.close();
pCon!.dispose(); pCon!.dispose();
} }
@ -1051,9 +1087,11 @@ class ChatCallProvider with ChangeNotifier, DiagnosticableTreeMixin {
_localStream!.dispose(); _localStream!.dispose();
_localStream = null; _localStream = null;
} }
if (chatHubConnection != null && !isUserOnline) {
chatHubConnection!.stop(); // if (chatHubConnection != null && !isUserOnline) {
} // chatHubConnection!.stop();
// }
Navigator.of(locator<NavigationService>().navigatorKey.currentContext!).popAndPushNamed(HOME);
} }
void createPConnection() async { void createPConnection() async {

Loading…
Cancel
Save