import 'package:SEFER/constant/api_key.dart'; import 'package:SEFER/controller/functions/crud.dart'; import 'package:SEFER/controller/home/map_passenger_controller.dart'; import 'package:agora_rtc_engine/agora_rtc_engine.dart'; import 'package:get/get.dart'; import 'package:permission_handler/permission_handler.dart'; import '../../constant/box_name.dart'; import '../firebase/firbase_messge.dart'; import '../../main.dart'; class CallController extends GetxController { String channelName = ''; // Get.find().rideId; String token = ''; // int uid = int.parse(box.read(BoxName.phoneDriver)); // uid of the local user int uid = 0; int? remoteUid; // uid of the remote user bool _isJoined = false; // Indicates if the local user has joined the channel String status = ''; late RtcEngine agoraEngine; // Agora engine instance @override void onInit() { super.onInit(); // if (box.read(BoxName.passengerID) != null) { channelName = Get.find().rideId; // 'sefer300'; // remoteUid = int.parse(Get.find().driverPhone); uid = int.parse(box.read(BoxName.phone)); // } else { // channelName = Get.find().rideId; // 'sefer300'; // // remoteUid = int.parse(Get.find().passengerPhone); // uid = int.parse(box.read(BoxName.phoneDriver)); // } initAgoraFull(); } initAgoraFull() async { print('channelName is $channelName'); print('remoteid is $remoteUid'); print('uid is $uid'); await fetchToken(); // Set up an instance of Agora engine setupVoiceSDKEngine(); // join(); FirebaseMessagesController().sendNotificationToPassengerToken( 'Call Income from Passenger', '${'You have call from Passenger'.tr} ${box.read(BoxName.name)}', Get.find().driverToken, [ token, channelName, uid.toString(), remoteUid.toString(), ], ); join(); } @override void onClose() { agoraEngine.leaveChannel(); super.onClose(); } Future setupVoiceSDKEngine() async { // retrieve or request microphone permission await [Permission.microphone].request(); //create an instance of the Agora engine agoraEngine = createAgoraRtcEngine(); await agoraEngine.initialize(RtcEngineContext(appId: AK.agoraAppId)); print('eeeeeeeeeeeeeeeeeeee'); print(agoraEngine); // Register the event handler agoraEngine.registerEventHandler( RtcEngineEventHandler( onJoinChannelSuccess: (RtcConnection connection, int elapsed) { // Get.snackbar( // "Local user uid:${connection.localUid} joined the channel", ''); status = 'joined'.tr; _isJoined = true; update(); }, onUserJoined: (RtcConnection connection, int remoteUid, int elapsed) { // Get.snackbar("Remote user uid:$remoteUid joined the channel", ''); status = Get.find().driverName.toString(); ' joined'.tr; remoteUid = remoteUid; update(); }, onUserOffline: (RtcConnection connection, int? remoteUid, UserOfflineReasonType reason) { // Get.snackbar("Remote user uid:$remoteUid left the channel", ''); status = 'Call left'.tr; remoteUid = null; update(); }, ), ); } void join() async { // Set channel options including the client role and channel profile ChannelMediaOptions options = const ChannelMediaOptions( clientRoleType: ClientRoleType.clientRoleBroadcaster, channelProfile: ChannelProfileType.channelProfileCommunication, ); await agoraEngine.joinChannel( token: token, channelId: channelName, options: options, uid: uid, ); } void leave() { _isJoined = false; remoteUid = null; update(); agoraEngine.leaveChannel(); } // Clean up the resources when you leave @override void dispose() async { await agoraEngine.leaveChannel(); super.dispose(); } fetchToken() async { var res = await CRUD() .getAgoraToken(channelName: channelName, uid: uid.toString()); token = res; print('token is $token'); update(); } }