Files
tripz/lib/views/home/Captin/home_captain/call_controller.dart
Hamza Aleghwairyeen 88b671c112 4/10/2
2024-04-10 02:42:17 +03:00

134 lines
4.0 KiB
Dart

import 'package:SEFER/constant/api_key.dart';
import 'package:SEFER/controller/functions/crud.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 '../../../../controller/firebase/firbase_messge.dart';
import '../../../../controller/home/captin/map_driver_controller.dart';
import '../../../../main.dart';
class CallController extends GetxController {
String channelName = ''; // Get.find<MapDriverController>().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();
channelName = Get.find<MapDriverController>().rideId; // 'sefer300'; //
remoteUid = int.parse(Get.find<MapDriverController>().passengerPhone);
uid = int.parse(box.read(BoxName.phoneDriver));
initAgoraFull();
}
initAgoraFull() async {
print('remoteid is $remoteUid');
print('uid is $uid');
await fetchToken();
// Set up an instance of Agora engine
setupVoiceSDKEngine();
// join();
FirebaseMessagesController().sendNotificationToPassengerToken(
'Call Income',
'${'You have call from driver'.tr} ${box.read(BoxName.nameDriver)}',
Get.find<MapDriverController>().tokenPassenger,
[
token,
channelName,
uid.toString(),
remoteUid.toString(),
],
);
join();
}
@override
void onClose() {
agoraEngine.leaveChannel();
super.onClose();
}
Future<void> 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<MapDriverController>().passengerName}'
' joined'
.tr
.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();
}
}