import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:iktifaa/App/Common/Utilities/Constants/AppStyles.dart';
import 'package:iktifaa/App/Qanati/Modules/Live/widgets/invite_guest_button.dart';
import 'package:zego_uikit_beauty_plugin/zego_uikit_beauty_plugin.dart';
import 'package:zego_uikit_prebuilt_live_streaming/zego_uikit_prebuilt_live_streaming.dart';
import 'package:zego_uikit_signaling_plugin/zego_uikit_signaling_plugin.dart';
import 'package:get/get.dart';
import 'package:iktifaa/App/Common/Utilities/Constants/Strings/General.dart';
import 'package:iktifaa/App/Qanati/Modules/Live/controller/live_controller.dart';
import '../../../../Common/Utilities/Constants/AppColors.dart';
import '../../../../Common/Widgets/CustomButton.dart';
import '../../../widgets/live_avatar.dart';
import '../widgets/live_badge.dart';
class LivePage extends GetView<LiveController> {
final String liveID;
final bool isHost;
const LivePage({Key? key, required this.liveID, this.isHost = false})
: super(key: key);
@override
Widget build(BuildContext context) {
controller.isHost = isHost;
return SafeArea(
child: ZegoUIKitPrebuiltLiveStreaming(
appID: ZEGO_APP_ID,
appSign: ZEGO_APP_SIGN,
userID: controller.authManager.appUser.value.userName!,
userName: controller.authManager.appUser.value.userName!,
liveID: liveID,
controller: controller.zegoController,
config: isHost
? (ZegoUIKitPrebuiltLiveStreamingConfig.host(
plugins: [
ZegoUIKitSignalingPlugin(),
ZegoUIKitBeautyPlugin(),
],
)
..audioVideoViewConfig.foregroundBuilder = (BuildContext context,
Size size, ZegoUIKitUser? user, Map extraInfo) {
return user != null
? Obx(() => controller.streaming.value
? Stack(
children: [LiveBadge(), InviteGuestButton()],
)
: const SizedBox())
: const SizedBox();
}
..bottomMenuBarConfig = ZegoBottomMenuBarConfig(hostButtons: [
ZegoMenuBarButtonName.beautyEffectButton,
ZegoMenuBarButtonName.switchCameraButton,
ZegoMenuBarButtonName.toggleCameraButton,
ZegoMenuBarButtonName.toggleMicrophoneButton,
])
..durationConfig.isVisible = false
..durationConfig.onDurationUpdate = (Duration duration) {
if (controller.authManager.appUser.value.liveStreamEnabled !=
true) {
if (duration.inMinutes == 25 && !controller.alerted) {
controller.alerted = true;
controller.authManager.commonTools
.ShowWarningDialogSingleMessageButton(
context,
'warningLiveTimeExceed'.tr,
'ok'.tr,
CupertinoColors.activeBlue, () {
Get.back();
});
} else if (duration.inMinutes == 30 &&
!controller.durationLeaved) {
controller.durationLeaved = true;
controller.zegoController
.leave(context)
.then((value) => Get.back());
}
}
}
..effectConfig = ZegoEffectConfig(
selectedIconBorderColor: AppColors.primary,
sliderActiveTrackColor: AppColors.primary,
selectedTextStyle: Get.textTheme.headlineSmall!
.copyWith(fontSize: 12.0, color: AppColors.primary),
sliderThumbColor: AppColors.primary)
..innerText = controller.qanatiTools.getZegoTranslation()
..inRoomMessageConfig = ZegoInRoomMessageConfig(
nameTextStyle: Get.textTheme.headlineSmall!.copyWith(
fontSize: 14.0,
color: AppColors.primary,
))
..confirmDialogInfo =
controller.qanatiTools.getZegoDialogInfo(isHost)
..beautyConfig = ZegoBeautyPluginConfig(
effectsTypes: ZegoBeautyPluginConfig.beautifyEffectsTypes(
enableBasic: true,
enableAdvanced: true,
enableMakeup: true,
enableStyle: true,
),
innerText: controller.qanatiTools.getZegoBeautyTranslation(),
uiConfig: ZegoBeautyPluginUIConfig(
sliderActiveTrackColor: AppColors.primary,
// selectedTextStyle: Get.textTheme.headlineSmall!
// .copyWith(fontSize: 12.0, color: AppColors.primary),
selectedIconBorderColor: AppColors.primary,
selectedIconDotColor: AppColors.primary,
),
)
..avatarBuilder = (BuildContext context, Size size,
ZegoUIKitUser? user, Map extraInfo) {
return user != null
? LiveAvatar(
userId: user.id,
)
: const SizedBox();
}
..startLiveButtonBuilder =
(BuildContext context, VoidCallback startLive) {
return CustomButton(
onTap: () {
startLive();
controller.streaming.value = true;
},
width: Get.width * 0.4,
height: Get.width * 0.14,
background: AppColors.primary,
text: Text('startLive'.tr,
textAlign: TextAlign.center,
style: Get.textTheme.bodyLarge!.copyWith(
fontWeight: FontWeight.w600, color: Colors.white)),
borderRadius: AppStyles.borderRadius,
padding: const EdgeInsets.symmetric(vertical: 16),
icon: SvgPicture.asset(
'assets/svg/live.svg',
color: AppColors.white,
),
shadow: true);
}
..onLiveStreamingStateUpdate = (status) {
if (status != ZegoLiveStreamingState.ended &&
status != ZegoLiveStreamingState.idle) {
controller.streaming.value = true;
} else {
controller.streaming.value = false;
}
}
..layout = ZegoLayout.gallery(
showScreenSharingFullscreenModeToggleButtonRules:
ZegoShowFullscreenModeToggleButtonRules.alwaysShow,
showNewScreenSharingViewInFullscreenMode: false))
: (ZegoUIKitPrebuiltLiveStreamingConfig.audience(
plugins: [ZegoUIKitSignalingPlugin(), ZegoUIKitBeautyPlugin()],
)
..audioVideoViewConfig.useVideoViewAspectFill = false
..onCameraTurnOnByOthersConfirmation = (BuildContext context) {
return onTurnOnAudienceDeviceConfirmation(
context,
isCameraOrMicrophone: true,
);
}
..audioVideoViewConfig.foregroundBuilder = (BuildContext context,
Size size, ZegoUIKitUser? user, Map extraInfo) {
return user != null
? Obx(() => controller.streaming.value
? LiveBadge()
: const SizedBox())
: const SizedBox();
}
..confirmDialogInfo =
controller.qanatiTools.getZegoDialogInfo(isHost)
..innerText = controller.qanatiTools.getZegoTranslation()
..avatarBuilder = (BuildContext context, Size size,
ZegoUIKitUser? user, Map extraInfo) {
return user != null
? LiveAvatar(
userId: user.id,
)
: const SizedBox();
}
..audioVideoViewConfig.foregroundBuilder = (BuildContext context,
Size size, ZegoUIKitUser? user, Map extraInfo) {
return user != null
? Obx(
() => controller.streaming.value
? LiveBadge()
: const SizedBox(),
)
: const SizedBox();
}
..layout = ZegoLayout.gallery(
showScreenSharingFullscreenModeToggleButtonRules:
ZegoShowFullscreenModeToggleButtonRules.alwaysShow,
showNewScreenSharingViewInFullscreenMode: false)
..onLiveStreamingStateUpdate = (status) {
if (status != ZegoLiveStreamingState.ended &&
status != ZegoLiveStreamingState.idle) {
controller.streaming.value = true;
} else {
controller.streaming.value = false;
}
}
..onMicrophoneTurnOnByOthersConfirmation = (BuildContext context) {
return onTurnOnAudienceDeviceConfirmation(
context,
isCameraOrMicrophone: false,
);
}),
));
}
Future<bool> onTurnOnAudienceDeviceConfirmation(
BuildContext context, {
required bool isCameraOrMicrophone,
}) async {
const textStyle = TextStyle(
fontSize: 10,
color: Colors.white70,
);
return await showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
backgroundColor: Colors.blue[900]!.withOpacity(0.9),
title: Text(
"You have a request to turn on your ${isCameraOrMicrophone ? "camera" : "microphone"}",
style: textStyle),
content: Text(
"Do you agree to turn on the ${isCameraOrMicrophone ? "camera" : "microphone"}?",
style: textStyle),
actions: [
ElevatedButton(
child: Text('cancel'.tr, style: textStyle),
onPressed: () => Navigator.of(context).pop(false),
),
ElevatedButton(
child: Text('ok'.tr, style: textStyle),
onPressed: () {
Navigator.of(context).pop(true);
},
),
],
);
},
);
}
}