Files
tripz/lib/views/home/Captin/home_captain/call_controller.dart
Hamza-Ayed 9d839f02ae 2/18/1
2024-02-18 13:44:35 +03:00

128 lines
3.8 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 '../../../../main.dart';
class CallController extends GetxController {
String channelName =
'sefer300'; //Get.find<MapDriverController>().passengerId;
String token =
"00612994c6e707543e68d5638894d04f989IAA9fx7yHezOOXPq1l4MwrNgPVOxWj7VnirB9Ks6X37jS6MLiA8AAAAAEABXi+nQ7GjSZQEAAQAAAAAA";
// 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();
// initAgoraFull();
}
initAgoraFull() async {
_remoteUid = box.read(BoxName.phone) != null
? int.parse(box.read(BoxName.phone))
: int.parse(box.read(BoxName.phoneDriver));
uid = box.read(BoxName.phoneDriver) != null
? int.parse(box.read(BoxName.phoneDriver))
: int.parse(box.read(BoxName.phone));
print('remoteid is $_remoteUid');
print('uid is $uid');
// await fetchToken();
// Set up an instance of Agora engine
setupVoiceSDKEngine();
}
@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(const 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 = '${box.read(BoxName.nameDriver) ?? box.read(BoxName.name)}'
' 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().getAgora(
channelName: 'sefer22'); //Get.find<MapDriverController>().rideId);
print('hhhhhhhhhhhhhhhhhhhhhhh');
print(res);
channelName = 'sefer22';
token = res['token'];
print('token is $token');
update();
}
}