|
|
|
|
@ -1,25 +1,19 @@
|
|
|
|
|
import 'dart:convert';
|
|
|
|
|
import 'package:firebase_messaging/firebase_messaging.dart';
|
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
|
import 'package:flutter_callkit_incoming/entities/entities.dart';
|
|
|
|
|
import 'package:flutter_callkit_incoming/flutter_callkit_incoming.dart';
|
|
|
|
|
import 'package:mohem_flutter_app/api/chat/chat_api_client.dart';
|
|
|
|
|
import 'package:mohem_flutter_app/app_state/app_state.dart';
|
|
|
|
|
import 'package:mohem_flutter_app/classes/consts.dart';
|
|
|
|
|
import 'package:mohem_flutter_app/classes/navigationService.dart';
|
|
|
|
|
import 'package:mohem_flutter_app/classes/notifications.dart';
|
|
|
|
|
import 'package:mohem_flutter_app/classes/utils.dart';
|
|
|
|
|
import 'package:mohem_flutter_app/config/routes.dart';
|
|
|
|
|
import 'package:mohem_flutter_app/main.dart';
|
|
|
|
|
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' as ALM;
|
|
|
|
|
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:mohem_flutter_app/ui/chat/call/chat_incoming_call_screen.dart';
|
|
|
|
|
import 'package:mohem_flutter_app/ui/landing/dashboard_screen.dart';
|
|
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
|
import 'package:signalr_netcore/hub_connection.dart';
|
|
|
|
|
import 'package:signalr_netcore/signalr_client.dart';
|
|
|
|
|
|
|
|
|
|
@ -29,15 +23,14 @@ class ChatVoipCall {
|
|
|
|
|
ChatVoipCall._internal();
|
|
|
|
|
|
|
|
|
|
factory ChatVoipCall() => _instance;
|
|
|
|
|
|
|
|
|
|
late ChatProviderModel prov;
|
|
|
|
|
late ChatCallProvider cProv;
|
|
|
|
|
dynamic inCallData;
|
|
|
|
|
bool isUserOnline = false;
|
|
|
|
|
dynamic callData;
|
|
|
|
|
late IncomingCallModel sessionData;
|
|
|
|
|
|
|
|
|
|
Future<void> showCallkitIncoming({required String uuid, RemoteMessage? data, CallDataModel? incomingCallData, bool background = false}) async {
|
|
|
|
|
await ChatVoipCall().listenerEvent();
|
|
|
|
|
await FlutterCallkitIncoming.endAllCalls();
|
|
|
|
|
ALM.Response autoLoginData;
|
|
|
|
|
SingleUserChatModel callerData;
|
|
|
|
|
@ -45,7 +38,7 @@ class ChatVoipCall {
|
|
|
|
|
// Online & App Logged In
|
|
|
|
|
ALM.Response sharedDetails = ALM.Response.fromJson(jsonDecode(await Utils.getStringFromPrefs("userLoginChatDetails")));
|
|
|
|
|
autoLoginData = ALM.Response.fromJson(AppState().getchatUserDetails == null ? sharedDetails.toJson() : AppState().getchatUserDetails!.response!.toJson());
|
|
|
|
|
dynamic items = jsonDecode(data!.data["user_chat_history_response"]);
|
|
|
|
|
dynamic items = jsonDecode(data.data["user_chat_history_response"]);
|
|
|
|
|
callerData = SingleUserChatModel(
|
|
|
|
|
targetUserId: items["CurrentUserId"],
|
|
|
|
|
targetUserEmail: items["CurrentUserEmail"],
|
|
|
|
|
@ -58,7 +51,7 @@ class ChatVoipCall {
|
|
|
|
|
} 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"]));
|
|
|
|
|
callerData = SingleUserChatModel.fromJson(json.decode(data.data["user_chat_history_response"]));
|
|
|
|
|
}
|
|
|
|
|
CallKitParams params = CallKitParams(
|
|
|
|
|
id: uuid,
|
|
|
|
|
@ -71,7 +64,13 @@ class ChatVoipCall {
|
|
|
|
|
textDecline: 'Decline',
|
|
|
|
|
textMissedCall: 'Missed call',
|
|
|
|
|
textCallback: 'Call back',
|
|
|
|
|
extra: {"loginDetails": autoLoginData.toJson(), "callerDetails": callerData.toJson(), 'isIncomingCall': true, 'isUserOnline': isUserOnline, 'callType': data!.data["callType"]},
|
|
|
|
|
extra: {
|
|
|
|
|
"loginDetails": autoLoginData.toJson(),
|
|
|
|
|
"callerDetails": callerData.toJson(),
|
|
|
|
|
'isIncomingCall': true,
|
|
|
|
|
'isUserOnline': isUserOnline,
|
|
|
|
|
'callType': data.data["callType"],
|
|
|
|
|
},
|
|
|
|
|
android: const AndroidParams(
|
|
|
|
|
isCustomNotification: true,
|
|
|
|
|
isShowLogo: false,
|
|
|
|
|
@ -99,121 +98,47 @@ class ChatVoipCall {
|
|
|
|
|
ringtonePath: 'system_ringtone_default',
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
sessionData = IncomingCallModel.fromJson(params.toJson());
|
|
|
|
|
if (callerData.chatEventId == 3) {
|
|
|
|
|
await Utils.saveStringFromPrefs("isIncomingCall", "true");
|
|
|
|
|
await FlutterCallkitIncoming.showCallkitIncoming(params);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future getCurrentCall() async {
|
|
|
|
|
var calls = await FlutterCallkitIncoming.activeCalls();
|
|
|
|
|
if (calls is List) {
|
|
|
|
|
if (calls.isNotEmpty) {
|
|
|
|
|
return calls[0];
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void checkAndNavigationCallingPage() async {
|
|
|
|
|
dynamic currentCall = await getCurrentCall();
|
|
|
|
|
if (currentCall != null) {
|
|
|
|
|
Future.delayed(const Duration(seconds: 1)).whenComplete(() {
|
|
|
|
|
Navigator.pushNamed(AppRoutes.navigatorKey.currentContext!, AppRoutes.chatStartCall);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> isCall() async {
|
|
|
|
|
dynamic calls = await FlutterCallkitIncoming.activeCalls();
|
|
|
|
|
if (calls is List) {
|
|
|
|
|
if (calls.isNotEmpty) {
|
|
|
|
|
NavigationService.navigateToPage(StartCallPage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Function(CallEvent) callback
|
|
|
|
|
Future<void> listenerEvent() async {
|
|
|
|
|
try {
|
|
|
|
|
FlutterCallkitIncoming.onEvent.listen((CallEvent? event) async {
|
|
|
|
|
switch (event!.event) {
|
|
|
|
|
case Event.ACTION_CALL_INCOMING:
|
|
|
|
|
break;
|
|
|
|
|
case Event.ACTION_CALL_START:
|
|
|
|
|
break;
|
|
|
|
|
case Event.ACTION_CALL_ACCEPT:
|
|
|
|
|
if (isUserOnline) {
|
|
|
|
|
checkAndNavigationCallingPage();
|
|
|
|
|
} else {
|
|
|
|
|
isCall();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case Event.ACTION_CALL_DECLINE:
|
|
|
|
|
Utils.saveStringFromPrefs("isIncomingCall", "false");
|
|
|
|
|
Utils.saveStringFromPrefs("inComingCallData", "null");
|
|
|
|
|
declineCall();
|
|
|
|
|
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");
|
|
|
|
|
Utils.saveStringFromPrefs("inComingCallData", "null");
|
|
|
|
|
FlutterCallkitIncoming.endAllCalls();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} on Exception {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> declineCall() async {
|
|
|
|
|
Future<void> declineCall({var payload}) async {
|
|
|
|
|
IncomingCallModel data = IncomingCallModel.fromJson(jsonDecode(payload));
|
|
|
|
|
if (isUserOnline) {
|
|
|
|
|
HubConnection hc = await makeHub();
|
|
|
|
|
await hc.start();
|
|
|
|
|
print("Connection State ===" + chatHubConnection.state.toString());
|
|
|
|
|
if (hc.state == HubConnectionState.Connected) {
|
|
|
|
|
if (sessionData?.extra != null) {
|
|
|
|
|
chatHubConnection.invoke("HangUpAsync", args: [
|
|
|
|
|
int.parse(sessionData.extra!.callerDetails!.currentUserId.toString()),
|
|
|
|
|
int.parse(sessionData.extra!.callerDetails!.targetUserId.toString()),
|
|
|
|
|
HubConnection _hc = await makeHub(sessionData: data);
|
|
|
|
|
await _hc.start();
|
|
|
|
|
if (_hc.state == HubConnectionState.Connected) {
|
|
|
|
|
if (data.extra != null) {
|
|
|
|
|
_hc.invoke("HangUpAsync", args: [
|
|
|
|
|
data.extra!.callerDetails!.currentUserId!,
|
|
|
|
|
data.extra!.callerDetails!.targetUserId!,
|
|
|
|
|
]);
|
|
|
|
|
chatHubConnection.invoke("UpdateUserStatusAsync", args: [
|
|
|
|
|
int.parse(sessionData.extra!.callerDetails!.currentUserId.toString()),
|
|
|
|
|
_hc.invoke("UpdateUserStatusAsync", args: [
|
|
|
|
|
int.parse(data.extra!.callerDetails!.currentUserId.toString()),
|
|
|
|
|
1,
|
|
|
|
|
]);
|
|
|
|
|
FlutterCallkitIncoming.endCall(sessionData.id!);
|
|
|
|
|
chatHubConnection = hc;
|
|
|
|
|
FlutterCallkitIncoming.endAllCalls();
|
|
|
|
|
chatHubConnection = _hc;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
HubConnection hc = await makeHub();
|
|
|
|
|
await hc.start();
|
|
|
|
|
await hc.invoke(
|
|
|
|
|
"HangUpAsync",
|
|
|
|
|
args: [
|
|
|
|
|
int.parse(sessionData.extra!.callerDetails!.currentUserId.toString()),
|
|
|
|
|
int.parse(sessionData.extra!.callerDetails!.targetUserId.toString()),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
FlutterCallkitIncoming.endCall(sessionData.id!);
|
|
|
|
|
if (!isUserOnline) {
|
|
|
|
|
hc.stop();
|
|
|
|
|
} else {
|
|
|
|
|
chatHubConnection = hc;
|
|
|
|
|
}
|
|
|
|
|
await ChatApiClient().callDecline(cUserID: data.extra!.callerDetails!.targetUserId!, tUserID: data.extra!.callerDetails!.currentUserId!, targetUsertoken: data.extra!.loginDetails!.token!);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<HubConnection> makeHub() async {
|
|
|
|
|
HttpConnectionOptions httpOp = HttpConnectionOptions(skipNegotiation: false, logMessageContent: true);
|
|
|
|
|
HubConnection hc = HubConnectionBuilder()
|
|
|
|
|
.withUrl(ApiConsts.chatHubConnectionUrl + "?UserId=${sessionData.extra!.loginDetails!.id}&source=Desktop&access_token=${sessionData.extra?.loginDetails!.token}", options: httpOp)
|
|
|
|
|
.withAutomaticReconnect(retryDelays: <int>[2000, 5000, 10000, 20000]).build();
|
|
|
|
|
return hc;
|
|
|
|
|
Future<HubConnection> makeHub({required IncomingCallModel sessionData}) async {
|
|
|
|
|
late HubConnection hc;
|
|
|
|
|
try {
|
|
|
|
|
HttpConnectionOptions httpOp = HttpConnectionOptions(skipNegotiation: false, logMessageContent: true);
|
|
|
|
|
hc = HubConnectionBuilder()
|
|
|
|
|
.withUrl(ApiConsts.chatHubConnectionUrl + "?UserId=${sessionData.extra!.loginDetails!.id}&source=Desktop&access_token=${sessionData.extra?.loginDetails!.token}", options: httpOp)
|
|
|
|
|
.withAutomaticReconnect(retryDelays: <int>[2000, 5000, 10000, 20000]).build();
|
|
|
|
|
return hc;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
print(e);
|
|
|
|
|
return hc;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|