You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mohemm-flutter-app/lib/ui/chat/call/chat_incoming_call_screen.dart

617 lines
32 KiB
Dart

import 'dart:convert';
import 'dart:core';
import 'dart:io';
3 years ago
import 'dart:ui';
import 'package:draggable_widget/draggable_widget.dart';
import 'package:flutter/foundation.dart';
3 years ago
import 'package:flutter/material.dart';
import 'package:flutter_callkit_incoming/flutter_callkit_incoming.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:flutter_webrtc/flutter_webrtc.dart';
2 years ago
import 'package:mohem_flutter_app/api/chat/chat_api_client.dart';
import 'package:mohem_flutter_app/app_state/app_state.dart';
3 years ago
import 'package:mohem_flutter_app/classes/colors.dart';
2 years ago
import 'package:mohem_flutter_app/classes/utils.dart';
import 'package:mohem_flutter_app/extensions/int_extensions.dart';
import 'package:mohem_flutter_app/main.dart';
2 years ago
import 'package:mohem_flutter_app/models/chat/call.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/incoming_call_model.dart';
import 'package:mohem_flutter_app/provider/chat_call_provider.dart';
import 'package:mohem_flutter_app/provider/chat_provider_model.dart';
import 'package:provider/provider.dart';
3 years ago
bool isCallConnected = false;
3 years ago
class StartCallPage extends StatefulWidget {
IosCallPayload? payload;
StartCallPage({this.payload});
3 years ago
@override
_StartCallPageState createState() => _StartCallPageState();
3 years ago
}
class _StartCallPageState extends State<StartCallPage> {
DragController dragController = DragController();
late ChatCallProvider cProv;
late ChatProviderModel provider;
3 years ago
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
void startCall() async {
IncomingCallModel? sessionData;
dynamic calls = await FlutterCallkitIncoming.activeCalls();
if (calls.isNotEmpty) {
2 years ago
sessionData = IncomingCallModel.fromRawJson(jsonEncode(calls[0]));
if (kDebugMode) {
print(sessionData.toRawJson());
}
if (provider.isUserOnline) {
AppState().isBackgroundCall = true;
cProv.isUserOnline = provider.isUserOnline;
if (kDebugMode) {
print("====== Processing Incoming Call in Online State =========");
}
await cProv.startIncomingCallViaKit(inCallData: sessionData.extra!.callerDetails!.toJson(), isVCall: sessionData.extra!.callType == "video" ? true : false);
cProv.init();
isCallConnected = true;
} else {
AppState().isBackgroundCall = true;
if (kDebugMode) {
print("====== Processing Incoming Call =========");
}
cProv.isUserOnline = provider.isUserOnline;
await cProv.startIncomingCallViaKit(inCallData: sessionData.extra!.callerDetails!.toJson(), isVCall: sessionData.extra!.callType == "video" ? true : false);
try {
AppState().setchatUserDetails = UserAutoLoginModel(response: Response.fromJson(sessionData.extra!.loginDetails!.toJson()), errorResponses: null);
await provider.buildHubConnection(context: context, ccProvider: cProv).whenComplete(() {
cProv.init();
isCallConnected = true;
});
} catch (e) {
logger.w(e);
}
}
}
// cProv.startRecording();
}
2 years ago
void startIosCall() async {
// IosCallPayload? iosCallPayload = IosCallPayload.fromRawJson(await Utils.getStringFromPrefs("iosCallPayload"));
IosCallPayload? iosCallPayload = widget.payload;
var userID = iosCallPayload!.incomingCallReciverId;
var callType = iosCallPayload.incomingCallType;
2 years ago
SingleUserChatModel inCallData = SingleUserChatModel(
targetUserName: iosCallPayload.incomingCallerName,
2 years ago
chatEventId: 3,
targetUserId: int.parse(iosCallPayload.incomingCallerId!),
currentUserId: int.parse(userID.toString()),
2 years ago
);
if (provider.isUserOnline) {
AppState().isBackgroundCall = true;
2 years ago
cProv.isUserOnline = provider.isUserOnline;
if (kDebugMode) {
print("====== Processing Incoming Call in Online State =========");
}
await cProv.startIncomingCallViaKit(inCallData: inCallData.toJson(), isVCall: callType == "video" ? true : false);
cProv.init();
isCallConnected = true;
} else {
if (kDebugMode) {
print("====== Processing Incoming Call =========");
}
AppState().isBackgroundCall = true;
2 years ago
cProv.isUserOnline = provider.isUserOnline;
UserAutoLoginModel userLoginResponse = await ChatApiClient().getUserCallToken(userid: userID.toString());
2 years ago
if (userLoginResponse.response != null) {
AppState().setchatUserDetails = userLoginResponse;
Utils.saveStringFromPrefs("userLoginChatDetails", jsonEncode(userLoginResponse.response));
2 years ago
await cProv.startIncomingCallViaKit(inCallData: inCallData.toJson(), isVCall: callType == "video" ? true : false);
try {
AppState().setchatUserDetails = UserAutoLoginModel(response: userLoginResponse.response, errorResponses: null);
await provider.buildHubConnection(context: context, ccProvider: cProv).whenComplete(() {
cProv.init();
isCallConnected = true;
});
} catch (e) {
logger.w(e);
}
}
}
}
3 years ago
@override
Widget build(BuildContext context) {
cProv = context.read<ChatCallProvider>();
provider = context.read<ChatProviderModel>();
if (!cProv.isOutGoingCall) {
if (Platform.isAndroid) {
startCall();
} else if (Platform.isIOS) {
startIosCall();
}
}
3 years ago
return Scaffold(
extendBody: true,
body: Consumer2<ChatCallProvider, ChatProviderModel>(
builder: (BuildContext context, ChatCallProvider prov, ChatProviderModel cpm, Widget? child) {
return prov.isIncomingCallLoader
? const SizedBox(
2 years ago
width: double.infinity,
height: double.infinity,
child: Center(child: CircularProgressIndicator()),
)
: prov.isIncomingCall
? Container(
2 years ago
width: double.infinity,
height: double.infinity,
color: Colors.black,
2 years ago
child: Stack(
alignment: FractionalOffset.center,
children: <Widget>[
if (!prov.isAudioCall && prov.isVideoCall)
RTCVideoView(
prov.remoteRenderer!,
objectFit: RTCVideoViewObjectFit.RTCVideoViewObjectFitContain,
2 years ago
// filterQuality: FilterQuality.high,
key: const Key('remote'),
),
if (prov.isVideoCall)
2 years ago
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!,
2 years ago
mirror: true,
2 years ago
// filterQuality: FilterQuality.high,
2 years ago
objectFit: RTCVideoViewObjectFit.RTCVideoViewObjectFitCover,
),
),
),
if (!prov.isVideoCall)
2 years ago
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,
),
2 years ago
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
40.height,
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,
),
10.height,
Text(
prov.incomingCallData.targetUserName!,
2 years ago
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,
),
],
),
),
3 years ago
),
2 years ago
],
),
],
),
),
),
),
),
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();
2 years ago
},
elevation: 2.0,
fillColor: prov.isLoudSpeaker ? MyColors.textMixColor : Colors.grey,
2 years ago
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: () {
prov.camOff();
2 years ago
},
elevation: 2.0,
fillColor: prov.isCamOff ? MyColors.textMixColor : Colors.grey,
2 years ago
padding: const EdgeInsets.all(
10.0,
),
shape: const CircleBorder(),
child: Icon(
prov.isCamOff ? Icons.videocam_off : Icons.videocam,
2 years ago
color: MyColors.white,
size: 30.0,
),
),
RawMaterialButton(
constraints: const BoxConstraints(),
onPressed: () {
prov.switchCamera();
2 years ago
},
elevation: 2.0,
fillColor: prov.isFrontCamera ? Colors.grey : MyColors.textMixColor,
2 years ago
padding: const EdgeInsets.all(
10.0,
),
shape: const CircleBorder(),
child: Icon(
prov.isFrontCamera ? Icons.switch_camera_outlined : Icons.switch_camera,
2 years ago
color: MyColors.white,
size: 30.0,
),
),
RawMaterialButton(
constraints: const BoxConstraints(),
onPressed: () {
prov.micOff();
2 years ago
},
elevation: 2.0,
fillColor: prov.isMicOff ? MyColors.textMixColor : Colors.grey,
2 years ago
padding: const EdgeInsets.all(
10.0,
),
shape: const CircleBorder(),
child: Icon(
prov.isMicOff ? Icons.mic_off : Icons.mic,
2 years ago
color: MyColors.white,
size: 30.0,
),
),
RawMaterialButton(
constraints: const BoxConstraints(),
onPressed: () {
prov.endCall(isUserOnline: cpm.isUserOnline).then((bool value) {
2 years ago
if (value) {
Navigator.of(context).pop();
// print("Reintiiiiiiitttiiiiiiii");
2 years ago
// provider.initStreams();
}
});
},
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,
3 years ago
),
),
],
),
),
),
2 years ago
],
),
)
: prov.isOutGoingCall
? Container(
2 years ago
width: double.infinity,
height: double.infinity,
color: Colors.black,
2 years ago
child: Stack(
alignment: FractionalOffset.center,
children: <Widget>[
if (!prov.isAudioCall && prov.isVideoCall)
RTCVideoView(
prov.remoteRenderer!,
objectFit: RTCVideoViewObjectFit.RTCVideoViewObjectFitContain,
key: const Key('remote'),
2 years ago
),
if (prov.isVideoCall)
2 years ago
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!,
2 years ago
mirror: true,
objectFit: RTCVideoViewObjectFit.RTCVideoViewObjectFitCover,
),
),
),
if (!prov.isVideoCall)
2 years ago
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,
),
2 years ago
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
40.height,
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,
),
10.height,
Text(
prov.outGoingCallData.receiverName!,
2 years ago
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,
),
],
),
),
),
],
),
2 years ago
],
),
3 years ago
),
),
),
2 years ago
),
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();
2 years ago
},
elevation: 2.0,
fillColor: prov.isLoudSpeaker ? MyColors.textMixColor : Colors.grey,
2 years ago
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: () {
prov.camOff();
2 years ago
},
elevation: 2.0,
fillColor: prov.isCamOff ? MyColors.textMixColor : Colors.grey,
2 years ago
padding: const EdgeInsets.all(
10.0,
),
shape: const CircleBorder(),
child: Icon(
prov.isCamOff ? Icons.videocam_off : Icons.videocam,
2 years ago
color: MyColors.white,
size: 30.0,
),
),
RawMaterialButton(
constraints: const BoxConstraints(),
onPressed: () {
prov.switchCamera();
2 years ago
},
elevation: 2.0,
fillColor: prov.isFrontCamera ? Colors.grey : MyColors.textMixColor,
2 years ago
padding: const EdgeInsets.all(
10.0,
),
shape: const CircleBorder(),
child: Icon(
prov.isFrontCamera ? Icons.switch_camera_outlined : Icons.switch_camera,
2 years ago
color: MyColors.white,
size: 30.0,
),
),
RawMaterialButton(
constraints: const BoxConstraints(),
onPressed: () {
prov.micOff();
2 years ago
},
elevation: 2.0,
fillColor: prov.isMicOff ? MyColors.textMixColor : Colors.grey,
2 years ago
padding: const EdgeInsets.all(
10.0,
),
shape: const CircleBorder(),
child: Icon(
prov.isMicOff ? Icons.mic_off : Icons.mic,
2 years ago
color: MyColors.white,
size: 30.0,
),
),
RawMaterialButton(
constraints: const BoxConstraints(),
onPressed: () {
prov.endCall(isUserOnline: cpm.isUserOnline).then((bool value) {
2 years ago
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,
),
),
],
),
),
3 years ago
),
],
),
2 years ago
)
: const SizedBox();
3 years ago
},
),
);
}
}
/// if (Platform.isAndroid) {
// SystemNavigator.pop();
// } else if (Platform.isIOS) {
// exit(0);
// }