Created
June 8, 2024 10:08
-
-
Save DhruvamUnikon/fc5f219590b187b177e5001a31315e63 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_screenutil/flutter_screenutil.dart'; | |
import 'package:permission_handler/permission_handler.dart'; | |
import 'package:unikon/features/post/domain/entity/reaction/short_user_vm.dart'; | |
import 'package:unikon/utils/dimensions/sizes.dart'; | |
import 'package:unikon/utils/environment.dart'; | |
import 'package:unikon/utils/extensions/text/hardcoded.dart'; | |
import 'package:unikon/utils/images/constants.dart'; | |
import 'package:unikon/utils/permissions/permission_helper.dart'; | |
import 'package:unikon/utils/snackbars/overlay_snackbar.dart'; | |
import 'package:unikon/widget_library/images/base_image.dart'; | |
import 'package:wakelock_plus/wakelock_plus.dart'; | |
import 'package:zego_uikit_prebuilt_call/zego_uikit_prebuilt_call.dart'; | |
class BaseVideoCallHolder extends StatefulWidget { | |
final String roomId; | |
final ShortUserVM user; | |
final bool isHost; | |
final DateTime? endTime; | |
/// Does group video call | |
const BaseVideoCallHolder({ | |
super.key, | |
required this.roomId, | |
required this.user, | |
this.isHost = false, | |
this.endTime, | |
}); | |
@override | |
State<BaseVideoCallHolder> createState() => _BaseVideoCallHolderState(); | |
} | |
class _BaseVideoCallHolderState extends State<BaseVideoCallHolder> { | |
late String _callID; | |
late String _userID; | |
late String _userName; | |
Timer? _timer; | |
bool snackBarShown = false; | |
@override | |
void initState() { | |
WakelockPlus.enable(); | |
_callID = widget.roomId; // * room id | |
_userID = 'user_${widget.user.id}'; | |
_userName = '${widget.user.firstName} ${widget.user.lastName}'; | |
initTimer(); | |
// check permissions for host | |
checkForHostPermissions(); | |
super.initState(); | |
} | |
void initTimer() async { | |
if (widget.endTime != null) { | |
_timer = Timer.periodic(const Duration(seconds: 1), (Timer timer) { | |
final difference = widget.endTime!.difference(DateTime.now()).inSeconds; | |
if (difference < (60 * 5) && !snackBarShown) { | |
snackBarShown = true; | |
showTopSnackBar( | |
context, "Your UniShow is going to end in 5 minutes".hardcoded); | |
} else if (difference <= 0) { | |
Navigator.pop(context); | |
} | |
}); | |
} | |
} | |
@override | |
void dispose() { | |
WakelockPlus.disable(); | |
_timer?.cancel(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return SafeArea( | |
child: ZegoUIKitPrebuiltCall( | |
appID: AppEnvironment.instance.zegoAppId, | |
/*input your AppID*/ | |
appSign: AppEnvironment.instance.zegoAppSign /*input your AppSign*/, | |
userID: _userID, | |
userName: _userName, | |
callID: _callID, | |
config: widget.isHost | |
? ZegoUIKitPrebuiltCallConfig.groupVideoCall() | |
: ZegoUIKitPrebuiltCallConfig.groupVideoCall() | |
..topMenuBar = ZegoCallTopMenuBarConfig( | |
hideByClick: false, | |
height: 50.sp, | |
hideAutomatically: false, | |
isVisible: true, | |
padding: const EdgeInsets.only( | |
top: 10, | |
bottom: 10, | |
), | |
buttons: widget.isHost | |
? [ | |
ZegoCallMenuBarButtonName.chatButton, | |
ZegoCallMenuBarButtonName.showMemberListButton, | |
] | |
: List.empty(), | |
extendButtons: [ | |
ClipRRect( | |
borderRadius: BorderRadius.circular(smallRadius), | |
child: SizedBox( | |
height: 34.sp, | |
width: 34.sp, | |
child: const BaseImageWidget( | |
imageUri: ImageConstants.mainUnikonNewLogo, | |
), | |
), | |
), | |
], | |
) | |
..bottomMenuBarConfig = ZegoBottomMenuBarConfig( | |
buttons: widget.isHost | |
? [ | |
ZegoCallMenuBarButtonName.toggleCameraButton, | |
ZegoCallMenuBarButtonName.switchCameraButton, | |
ZegoCallMenuBarButtonName.hangUpButton, | |
ZegoCallMenuBarButtonName.toggleMicrophoneButton, | |
ZegoCallMenuBarButtonName.switchAudioOutputButton, | |
] | |
: [ | |
ZegoCallMenuBarButtonName.chatButton, | |
ZegoCallMenuBarButtonName.hangUpButton, | |
ZegoCallMenuBarButtonName.showMemberListButton, | |
], | |
) | |
..turnOnCameraWhenJoining = widget.isHost | |
..turnOnMicrophoneWhenJoining = widget.isHost | |
..layout = ZegoLayout.gallery(), | |
), | |
); | |
} | |
void checkForHostPermissions() async { | |
// check for permissions | |
if (widget.isHost) { | |
final permissions = [ | |
Permission.camera, | |
Permission.microphone, | |
]; | |
// ask for permissions if not granted | |
// if granted, then proceed | |
// if not granted, then show snackbar and pop | |
final isGranted = await PermissionHelper.requestMultiplePermissions( | |
permissions: permissions, context: context); | |
if (isGranted) { | |
return; | |
} else { | |
showTopSnackBar( | |
context, "Please grant permissions to continue".hardcoded); | |
Navigator.pop(context); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment