|
|
|
|
@ -4,6 +4,7 @@ import 'dart:io';
|
|
|
|
|
|
|
|
|
|
import 'package:diplomaticquarterapp/locator.dart';
|
|
|
|
|
import 'package:diplomaticquarterapp/models/LiveCare/IncomingCallData.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';
|
|
|
|
|
@ -858,56 +859,79 @@ class ChatCallProvider with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
|
_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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> buildHubConnection() async {
|
|
|
|
|
Future<HubConnection> createHub() async {
|
|
|
|
|
doctorID = incomingCallData!.sessionId!.split("**").last;
|
|
|
|
|
// doctorID = "6wgFpttYUsdnB-klo-3xeg";
|
|
|
|
|
String path = incomingCallData!.sessionId!.split("**").first;
|
|
|
|
|
patientID = incomingCallData!.receiverID;
|
|
|
|
|
_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) {
|
|
|
|
|
chatHubConnection!.stop();
|
|
|
|
|
chatHubConnection = null;
|
|
|
|
|
}
|
|
|
|
|
chatHubConnection = await createHub();
|
|
|
|
|
if (doctorID != null) {
|
|
|
|
|
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}) {
|
|
|
|
|
// print("Connection Closed");
|
|
|
|
|
Utils.showErrorToast(error.toString());
|
|
|
|
|
});
|
|
|
|
|
register();
|
|
|
|
|
if (chatHubConnection!.state == HubConnectionState.Disconnected) {
|
|
|
|
|
await chatHubConnection!.start();
|
|
|
|
|
print(chatHubConnection!.state);
|
|
|
|
|
}
|
|
|
|
|
if (chatHubConnection!.state == HubConnectionState.Connected) {
|
|
|
|
|
if (chatHubConnection != null) {
|
|
|
|
|
patientID = chatHubConnection!.connectionId;
|
|
|
|
|
print("DOC: " + doctorID.toString());
|
|
|
|
|
print("PAT " + patientID.toString());
|
|
|
|
|
print("Success Chat Con ID::: " + chatHubConnection!.connectionId.toString());
|
|
|
|
|
try {
|
|
|
|
|
chatHubConnection!.invoke("Call_Accepts", args: [
|
|
|
|
|
// jsonEncode({"UserID": int.parse(incomingCallData!.receiverID!)})
|
|
|
|
|
{"UserID": 4767780}
|
|
|
|
|
]).onError(
|
|
|
|
|
(error, stackTrace) => print(
|
|
|
|
|
error.toString(),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
isConnectCall ? ping("Call_Accept") : ping("Call_Decline");
|
|
|
|
|
} catch (e) {
|
|
|
|
|
print(e);
|
|
|
|
|
}
|
|
|
|
|
@ -916,48 +940,58 @@ class ChatCallProvider with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
|
} catch (e) {
|
|
|
|
|
print(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 {
|
|
|
|
|
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 {
|
|
|
|
|
chatHubConnection!.on("pre-offer", handlePreOffer);
|
|
|
|
|
chatHubConnection!.on("OnOffer", handleOnOffer);
|
|
|
|
|
chatHubConnection!.on("IceCandidate", handleIceCandidate);
|
|
|
|
|
chatHubConnection!.on("pre_offer", handlePreOffer);
|
|
|
|
|
chatHubConnection!.on("on_offer", handleOnOffer);
|
|
|
|
|
chatHubConnection!.on("on_ice_candidate", handleIceCandidate);
|
|
|
|
|
// New Events
|
|
|
|
|
chatHubConnection!.on("Call_Decline", handleCallDecline);
|
|
|
|
|
chatHubConnection!.on("Call_NoAnswer", handleNoAnswerCall);
|
|
|
|
|
chatHubConnection!.on("CallCompleted", handleCallComplete);
|
|
|
|
|
chatHubConnection!.on("connected_users", genericCheckEvent);
|
|
|
|
|
print("Listners Initiated");
|
|
|
|
|
chatHubConnection!.on("call_completed", handleCallComplete);
|
|
|
|
|
if (kDebugMode) debugPrint("Listners Initiated");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
genericCheckEvent(List<Object?>? params) async {
|
|
|
|
|
print("------------- Generic Event Triggered ---------------");
|
|
|
|
|
print("------------- Generic Event Triggered ---------------");
|
|
|
|
|
print(params);
|
|
|
|
|
print("------------- Generic Event Triggered ---------------");
|
|
|
|
|
print("------------- Generic Event Triggered ---------------");
|
|
|
|
|
}
|
|
|
|
|
// genericCheckEvent(List<Object?>? params) async {
|
|
|
|
|
// print("------------- Generic Event Triggered ---------------");
|
|
|
|
|
// print("------------- Generic Event Triggered ---------------");
|
|
|
|
|
// print(params);
|
|
|
|
|
// print("------------- Generic Event Triggered ---------------");
|
|
|
|
|
// print("------------- Generic Event Triggered ---------------");
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
handlePreOffer(List<Object?>? params) async {
|
|
|
|
|
print("----------Event Pre Offer Received ----------");
|
|
|
|
|
dynamic items = params!.toList();
|
|
|
|
|
if (kDebugMode) {
|
|
|
|
|
print("Doctor ID : " + items[0]['from'].toString());
|
|
|
|
|
print("Patient ID: " + items[0]['to'].toString());
|
|
|
|
|
}
|
|
|
|
|
doctorID = items[0]['from'];
|
|
|
|
|
patientID = items[0]['to'];
|
|
|
|
|
initStreams();
|
|
|
|
|
@ -988,8 +1022,8 @@ class ChatCallProvider with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
startCallByRTC();
|
|
|
|
|
Future.delayed(Duration(seconds: 5), () {
|
|
|
|
|
Navigator.push(
|
|
|
|
|
Future.delayed(Duration(seconds: 2), () {
|
|
|
|
|
Navigator.pushReplacement(
|
|
|
|
|
locator<NavigationService>().navigatorKey.currentContext!,
|
|
|
|
|
MaterialPageRoute(
|
|
|
|
|
builder: (BuildContext context) => StartCallPage(),
|
|
|
|
|
@ -1014,17 +1048,17 @@ class ChatCallProvider with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
|
debuglogger.d(items);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleNoAnswerCall(List<Object?>? params) {
|
|
|
|
|
print("----------handle No Answer Received ----------");
|
|
|
|
|
dynamic items = params!.toList();
|
|
|
|
|
debuglogger.d(items);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleCallComplete(List<Object?>? params) {
|
|
|
|
|
print("----------handle Call Complete Received ----------");
|
|
|
|
|
dynamic items = params!.toList();
|
|
|
|
|
debuglogger.d(items);
|
|
|
|
|
|
|
|
|
|
if (isCallConnected) ping("Call_Complete");
|
|
|
|
|
if (!isCallConnected && !isAccepted) {
|
|
|
|
|
ping("Call_NoAnswer");
|
|
|
|
|
} else {
|
|
|
|
|
if (!isCallConnected) ping("Call_Decline");
|
|
|
|
|
}
|
|
|
|
|
isCallConnected = false;
|
|
|
|
|
isCallStarted = false;
|
|
|
|
|
isVideoCall = false;
|
|
|
|
|
isCamOff = false;
|
|
|
|
|
@ -1033,7 +1067,9 @@ class ChatCallProvider with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
|
isIncomingCall = false;
|
|
|
|
|
isOutGoingCall = 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!.dispose();
|
|
|
|
|
}
|
|
|
|
|
@ -1051,9 +1087,11 @@ class ChatCallProvider with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
|
_localStream!.dispose();
|
|
|
|
|
_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 {
|
|
|
|
|
|