diff --git a/lib/classes/chat_call_kit.dart b/lib/classes/chat_call_kit.dart index c2dc3e1..66eef6a 100644 --- a/lib/classes/chat_call_kit.dart +++ b/lib/classes/chat_call_kit.dart @@ -30,11 +30,11 @@ class ChatVoipCall { Future showCallkitIncoming({required String uuid, RemoteMessage? data, CallDataModel? incomingCallData, bool background = false}) async { await ChatVoipCall().listenerEvent(); + await FlutterCallkitIncoming.endAllCalls(); ALM.Response autoLoginData; SingleUserChatModel callerData; - // if(!background){} - await initProviders(); if (data!.data["user_token_response"] == null || data.data["user_token_response"].isEmpty) { + // Online & App Logged In autoLoginData = ALM.Response.fromJson(AppState().getchatUserDetails!.response!.toJson()); dynamic items = jsonDecode(data!.data["user_chat_history_response"]); callerData = SingleUserChatModel( @@ -47,24 +47,10 @@ class ChatVoipCall { chatEventId: 3); isUserOnline = true; } else { + // Offline or App in Background or App is At Verify Screen autoLoginData = ALM.Response.fromJson(jsonDecode(data.data["user_token_response"])); callerData = SingleUserChatModel.fromJson(json.decode(data!.data["user_chat_history_response"])); } - callData = jsonEncode([ - { - "loginDetails": autoLoginData.toJson(), - "callerDetails": callerData.toJson(), - } - ]); - if (!background) { - await initProviders(); - if (data!.data["callType"] == "video") { - cProv.isVideoCall = true; - } else { - cProv.isAudioCall = true; - cProv.isVideoCall = false; - } - } CallKitParams params = CallKitParams( id: uuid, nameCaller: callerData.targetUserName, @@ -79,6 +65,7 @@ class ChatVoipCall { extra: { "loginDetails": autoLoginData.toJson(), "callerDetails": callerData.toJson(), + 'isIncomingCall' : true, }, android: const AndroidParams( isCustomNotification: true, @@ -139,40 +126,56 @@ class ChatVoipCall { } } + Future getCurrentCall() async { + var calls = await FlutterCallkitIncoming.activeCalls(); + if (calls is List) { + if (calls.isNotEmpty) { + print('DATA------------: $calls'); + return calls[0]; + } else { + return null; + } + } + } + + void checkAndNavigationCallingPage() async { + var currentCall = await getCurrentCall(); + if (currentCall != null) { + Future.delayed(const Duration(seconds: 2)).whenComplete(() { + Navigator.pushNamed(AppRoutes.navigatorKey.currentContext!, AppRoutes.chatStartCall); + // NavigationService.instance + // .pushNamedIfNotCurrent(AppRoute.callingPage, args: currentCall); + }); + } + } + //Function(CallEvent) callback Future listenerEvent() async { try { - FlutterCallkitIncoming.onEvent.listen((event) async { + FlutterCallkitIncoming.onEvent.listen((CallEvent? event) async { switch (event!.event) { case Event.ACTION_CALL_INCOMING: - // TODO: received an incoming call - connection(data: callData, isUserOnline: isUserOnline); break; case Event.ACTION_CALL_START: - // TODO: started an outgoing call - // TODO: show screen calling in Flutter break; case Event.ACTION_CALL_ACCEPT: - if (isUserOnline) { - cProv.init(); - } - if (!isUserOnline) { - initProviders(); - } - - Navigator.pushNamed(AppRoutes.navigatorKey.currentContext!, AppRoutes.chatStartCall); - + print("------------------------------------------Call Accespted By User ----------------------------------------------"); + checkAndNavigationCallingPage(); // Navigator.pushNamed(AppRoutes.navigatorKey.currentContext!, AppRoutes.chatStartCall); + // checkAndNavigationCallingPage(); + break; case Event.ACTION_CALL_DECLINE: cProv.isIncomingCall = true; Utils.saveStringFromPrefs("isIncomingCall", "false"); Utils.saveStringFromPrefs("inComingCallData", "null"); cProv.endCall(); + FlutterCallkitIncoming.endAllCalls(); break; case Event.ACTION_CALL_ENDED: Utils.saveStringFromPrefs("isIncomingCall", "false"); Utils.saveStringFromPrefs("inComingCallData", "null"); + FlutterCallkitIncoming.endAllCalls(); break; case Event.ACTION_CALL_TIMEOUT: Utils.saveStringFromPrefs("isIncomingCall", "false"); @@ -204,6 +207,60 @@ class ChatVoipCall { } on Exception {} } +// +// void didChangeAppLifecycleState(AppLifecycleState state) { +// super.didChangeAppLifecycleState(state); +// switch (state) { +// case AppLifecycleState.paused: +// logger.i('paused'); +// break; +// case AppLifecycleState.inactive: +// logger.i('inactive'); +// break; +// case AppLifecycleState.resumed: +// logger.i('resumed'); +// break; +// case AppLifecycleState.detached: +// logger.i('detached'); +// break; +// } +// } + +// @override +// void dispose() { +// WidgetsBinding.instance.removeObserver(this); +// } + + void nothing() { + // if (!background) { + // await initProviders(); + // if (data!.data["callType"] == "video") { + // cProv.isVideoCall = true; + // } else { + // cProv.isAudioCall = true; + // cProv.isVideoCall = false; + // } + // } + // + // // if(!background){} + // await initProviders(); + + // callData = jsonEncode([ + // { + // "loginDetails": autoLoginData.toJson(), + // "callerDetails": callerData.toJson(), + // } + // ]); + + // connection(data: callData, isUserOnline: isUserOnline).whenComplete(() => {}); + // if (isUserOnline) { + // cProv.init(); + // } + // if (!isUserOnline) { + // initProviders(); + // } + // Navigator.pushNamed(AppRoutes.navigatorKey.currentContext!, AppRoutes.chatStartCall); + } // void didChangeAppLifecycleState(AppLifecycleState state) { // super.didChangeAppLifecycleState(state); // switch (state) { diff --git a/lib/classes/notifications.dart b/lib/classes/notifications.dart index 336af50..bc9c201 100644 --- a/lib/classes/notifications.dart +++ b/lib/classes/notifications.dart @@ -139,8 +139,6 @@ class AppNotifications extends WidgetsBindingObserver { } } - - AndroidNotificationChannel channel = const AndroidNotificationChannel( "high_importance_channel", "High Importance Notifications", diff --git a/lib/ui/chat/call/start_call_screen.dart b/lib/ui/chat/call/start_call_screen.dart index 793c2c7..2c9156e 100644 --- a/lib/ui/chat/call/start_call_screen.dart +++ b/lib/ui/chat/call/start_call_screen.dart @@ -1,25 +1,12 @@ + import 'dart:convert'; import 'dart:core'; -import 'dart:io'; -import 'dart:ui'; import 'package:draggable_widget/draggable_widget.dart'; import 'package:flutter/material.dart'; -import 'package:flutter/services.dart'; -import 'package:flutter_svg/flutter_svg.dart'; -import 'package:flutter_webrtc/flutter_webrtc.dart'; -import 'package:mohem_flutter_app/app_state/app_state.dart'; -import 'package:mohem_flutter_app/classes/colors.dart'; -import 'package:mohem_flutter_app/classes/utils.dart'; -import 'package:mohem_flutter_app/extensions/int_extensions.dart'; -import 'package:mohem_flutter_app/extensions/string_extensions.dart'; +import 'package:flutter_callkit_incoming/flutter_callkit_incoming.dart'; import 'package:mohem_flutter_app/main.dart'; -import 'package:mohem_flutter_app/models/chat/get_single_user_chat_list_model.dart'; import 'package:mohem_flutter_app/models/chat/get_user_login_token_model.dart'; -import 'package:mohem_flutter_app/models/chat/incomingCall.dart'; -import 'package:mohem_flutter_app/provider/chat_call_provider.dart'; -import 'package:mohem_flutter_app/provider/chat_provider_model.dart'; -import 'package:mohem_flutter_app/ui/landing/dashboard_screen.dart'; -import 'package:provider/provider.dart'; +import 'package:mohem_flutter_app/models/chat/get_user_login_token_model.dart' as ALM; class StartCallPage extends StatefulWidget { @override @@ -32,6 +19,7 @@ class _StartCallPageState extends State { @override void initState() { super.initState(); + startCall(); } @override @@ -39,255 +27,265 @@ class _StartCallPageState extends State { super.dispose(); } - // void connection() async { - // inCallData = await Utils.getStringFromPrefs("inComingCallData"); - // dynamic callData = await jsonDecode(inCallData); - // AppState().setchatUserDetails = UserAutoLoginModel( - // response: Response.fromJson(callData[0]["loginDetails"]), - // errorResponses: null, - // ); - // callProvider.startIncomingCallViaKit(inCallData: inCallData); - // try { - // await chatProvider.buildHubConnection(context: context, ccProvider: callProvider).whenComplete(() { - // callProvider.init(); - // }); - // } catch (e) { - // logger.w(e); - // } - // - // // callProvider.afterHub(); - // } + void startCall() async { + ALM.Response? sessionData; + var calls = await FlutterCallkitIncoming.activeCalls(); + if (calls is List) { + if (calls.isNotEmpty) { + //print('DATA------------: $calls'); + var items = jsonEncode(calls); + logger.w(items); + // sessionData = ALM.Response.fromJson(items); + // logger.w(items[0]["extra"]["loginDetails"]); + // logger.d(calls[0]["extra"]["callerDetails"]); + // logger.w(calls[0]["extra"]["isIncomingCall"]); + } + } + logger.i(sessionData); +print("========================"); + + + } @override Widget build(BuildContext context) { return Scaffold( - extendBody: true, - body: Consumer2(builder: (BuildContext context, ChatCallProvider provider, ChatProviderModel cpm, Widget? child) { - // if (provider.isEndedByCaller && Platform.isAndroid) SystemNavigator.pop(); - return SizedBox( - width: double.infinity, - height: double.infinity, - child:Stack( - alignment: FractionalOffset.center, - children: [ - if(!provider.isAudioCall && provider.isVideoCall) - Positioned.fill( - child: RTCVideoView( - provider.remoteRenderer!, - objectFit: RTCVideoViewObjectFit.RTCVideoViewObjectFitCover, - key: const Key('remote'), - ), - ), - if (provider.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( - provider.localVideoRenderer, - mirror: true, - objectFit: RTCVideoViewObjectFit.RTCVideoViewObjectFitCover, - ), - ), - ), - if (!provider.isVideoCall) - Positioned.fill( - child: ClipRect( - child: BackdropFilter( - 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: [ - 40.height, - Row( - crossAxisAlignment: CrossAxisAlignment.center, - mainAxisAlignment: MainAxisAlignment.center, - children: [ - 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: [ - SvgPicture.asset( - "assets/images/user.svg", - height: 70, - width: 70, - fit: BoxFit.cover, - ), - 10.height, - Text( - provider.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( - alignment: Alignment.bottomCenter, - child: Container( - padding: const EdgeInsets.only( - bottom: 20, - left: 40, - right: 40, - ), - child: Row( - mainAxisSize: MainAxisSize.max, - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - // if (provider.isVideoCall) - RawMaterialButton( - constraints: const BoxConstraints(), - onPressed: () { - provider.loudOn(); - }, - elevation: 2.0, - fillColor: provider.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, - ), - ), - RawMaterialButton( - constraints: const BoxConstraints(), - onPressed: () { - provider.camOff(); - }, - elevation: 2.0, - fillColor: provider.isCamOff ? MyColors.textMixColor : Colors.grey, - padding: const EdgeInsets.all( - 10.0, - ), - shape: const CircleBorder(), - child: Icon( - provider.isCamOff ? Icons.videocam_off : Icons.videocam, - color: MyColors.white, - size: 30.0, - ), - ), - RawMaterialButton( - constraints: const BoxConstraints(), - onPressed: () { - provider.switchCamera(); - }, - elevation: 2.0, - fillColor: provider.isFrontCamera ? Colors.grey : MyColors.textMixColor, - padding: const EdgeInsets.all( - 10.0, - ), - shape: const CircleBorder(), - child: Icon( - provider.isFrontCamera ? Icons.switch_camera_outlined : Icons.switch_camera, - color: MyColors.white, - size: 30.0, - ), - ), - RawMaterialButton( - constraints: const BoxConstraints(), - onPressed: () { - provider.micOff(); - }, - elevation: 2.0, - fillColor: provider.isMicOff ? MyColors.textMixColor : Colors.grey, - padding: const EdgeInsets.all( - 10.0, - ), - shape: const CircleBorder(), - child: Icon( - provider.isMicOff ? Icons.mic_off : Icons.mic, - color: MyColors.white, - size: 30.0, - ), - ), - RawMaterialButton( - constraints: const BoxConstraints(), - onPressed: () { - provider.endCall().then((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, - ), - ), - ], - ), - ), - ), - ], - ) - ); - }), + body: Center( + child: Container( + color: Colors.red, + width: 200, + height: 200, + ), + ), ); + // return Scaffold( + // extendBody: true, + // body: Consumer2(builder: (BuildContext context, ChatCallProvider provider, ChatProviderModel cpm, Widget? child) { + // // if (provider.isEndedByCaller && Platform.isAndroid) SystemNavigator.pop(); + // return SizedBox( + // width: double.infinity, + // height: double.infinity, + // child:Stack( + // alignment: FractionalOffset.center, + // children: [ + // if(!provider.isAudioCall && provider.isVideoCall) + // Positioned.fill( + // child: RTCVideoView( + // provider.remoteRenderer!, + // objectFit: RTCVideoViewObjectFit.RTCVideoViewObjectFitCover, + // key: const Key('remote'), + // ), + // ), + // if (provider.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( + // provider.localVideoRenderer, + // mirror: true, + // objectFit: RTCVideoViewObjectFit.RTCVideoViewObjectFitCover, + // ), + // ), + // ), + // if (!provider.isVideoCall) + // Positioned.fill( + // child: ClipRect( + // child: BackdropFilter( + // 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: [ + // 40.height, + // Row( + // crossAxisAlignment: CrossAxisAlignment.center, + // mainAxisAlignment: MainAxisAlignment.center, + // children: [ + // 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: [ + // SvgPicture.asset( + // "assets/images/user.svg", + // height: 70, + // width: 70, + // fit: BoxFit.cover, + // ), + // 10.height, + // Text( + // provider.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( + // alignment: Alignment.bottomCenter, + // child: Container( + // padding: const EdgeInsets.only( + // bottom: 20, + // left: 40, + // right: 40, + // ), + // child: Row( + // mainAxisSize: MainAxisSize.max, + // mainAxisAlignment: MainAxisAlignment.spaceBetween, + // children: [ + // // if (provider.isVideoCall) + // RawMaterialButton( + // constraints: const BoxConstraints(), + // onPressed: () { + // provider.loudOn(); + // }, + // elevation: 2.0, + // fillColor: provider.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, + // ), + // ), + // RawMaterialButton( + // constraints: const BoxConstraints(), + // onPressed: () { + // provider.camOff(); + // }, + // elevation: 2.0, + // fillColor: provider.isCamOff ? MyColors.textMixColor : Colors.grey, + // padding: const EdgeInsets.all( + // 10.0, + // ), + // shape: const CircleBorder(), + // child: Icon( + // provider.isCamOff ? Icons.videocam_off : Icons.videocam, + // color: MyColors.white, + // size: 30.0, + // ), + // ), + // RawMaterialButton( + // constraints: const BoxConstraints(), + // onPressed: () { + // provider.switchCamera(); + // }, + // elevation: 2.0, + // fillColor: provider.isFrontCamera ? Colors.grey : MyColors.textMixColor, + // padding: const EdgeInsets.all( + // 10.0, + // ), + // shape: const CircleBorder(), + // child: Icon( + // provider.isFrontCamera ? Icons.switch_camera_outlined : Icons.switch_camera, + // color: MyColors.white, + // size: 30.0, + // ), + // ), + // RawMaterialButton( + // constraints: const BoxConstraints(), + // onPressed: () { + // provider.micOff(); + // }, + // elevation: 2.0, + // fillColor: provider.isMicOff ? MyColors.textMixColor : Colors.grey, + // padding: const EdgeInsets.all( + // 10.0, + // ), + // shape: const CircleBorder(), + // child: Icon( + // provider.isMicOff ? Icons.mic_off : Icons.mic, + // color: MyColors.white, + // size: 30.0, + // ), + // ), + // RawMaterialButton( + // constraints: const BoxConstraints(), + // onPressed: () { + // provider.endCall().then((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, + // ), + // ), + // ], + // ), + // ), + // ), + // ], + // ) + // ); + // }), + // ); } }