onufitness_mobile/lib/screens/u_vault/views/update_uvault_screen.dart
2026-01-13 11:36:24 +05:30

317 lines
12 KiB
Dart

import 'dart:io';
import 'package:dotted_border/dotted_border.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:onufitness/constants/asset_constants.dart';
import 'package:onufitness/constants/color_constant.dart';
import 'package:onufitness/constants/text_constant.dart';
import 'package:onufitness/screens/u_vault/controllers/uvault_video_controller.dart';
import 'package:onufitness/services/local_storage_services/shared_services.dart';
import 'package:onufitness/utils/custom_sneakbar.dart';
import 'package:onufitness/widgets/appbars/custom_appbar.dart';
import 'package:onufitness/widgets/Dropdowns/custom_normal_hintext_dropdown.dart';
import 'package:onufitness/widgets/TextFields/custom_textfield_with_inside_container_label_text.dart';
import 'package:onufitness/widgets/bottomsheet/common_upload_option_bottomsheet.dart';
class UpdateUvaultScreen extends StatelessWidget {
const UpdateUvaultScreen({super.key});
@override
Widget build(BuildContext context) {
final controller = Get.find<UvaultController>();
return Scaffold(
backgroundColor: Colors.white,
appBar: CustomAppBar(
title: "Update",
leading: IconButton(
onPressed: () {
Navigator.pop(context);
},
icon: Icon(Icons.arrow_back_ios),
),
backgroundColor: Colors.white,
textColor: appbarTextColor,
titleFontSize: appBarHeardingText,
actions: [
Obx(() {
final isUpdating = controller.isUpdating.value;
if (isUpdating) {
return const Padding(
padding: EdgeInsets.symmetric(horizontal: 16.0),
child: Center(
child: Text(
"Updating...",
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.w600,
),
),
),
);
}
return TextButton(
onPressed: () async {
final isFormValid =
controller.selectedFitnessGoalId.value != 0 &&
controller.videoTitleController.text.isNotEmpty;
if (!isFormValid) {
customSnackbar(
title: "Missing Title",
message: "Please enter a title before proceeding.",
duration: 2,
);
return;
} else {
await controller.updateUVault().then((value) async {
if (value) {
Get.back();
await controller.fetchVideos(
userId:
SharedServices.getUserDetails()!.data!.userId!
.toString(),
isMyVideoScreen: true,
isRefresh: true,
);
}
});
}
},
child: Text(
"Update",
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.w600,
),
),
);
}),
],
),
body: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(16.w),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Obx(() {
return controller.isFitnessGoalsLoading.value
? const Center(child: CircularProgressIndicator())
: CustomDropdownField(
title: "Fitness Goals",
items: controller.localFitnessGoalsList,
selectedItem:
controller.selectedFitnessGoal.isEmpty
? null
: controller.selectedFitnessGoal.value,
onChanged: (String? newValue) {
if (newValue != null) {
controller.selectedFitnessGoal.value = newValue;
var selectedObj = controller.apiFitnessGoalsList
.firstWhere(
(element) =>
element.fitnessGoalTitle == newValue,
);
// Correctly update the selected ID
controller.selectedFitnessGoalId.value =
selectedObj.fitnessGoalId!;
}
},
);
}),
SizedBox(height: 20.h),
CustomTextFieldContainerInsideLabel(
controller: controller.videoTitleController,
label: "Video Title",
height: 35.h,
lebelTextColor: greyTextColor1,
),
SizedBox(height: 20.h),
GestureDetector(
onTap: () {
CommonUploadBottomSheet.show(
context: context,
title: "Upload video",
allowedFileTypes: ['mp4', 'm4v'],
maxFileSizeMB: 50.0,
showCamera: false,
showGallery: true,
showDocument: true,
onFileSelected: (
String filePath,
String fileName,
String fileExtension,
) {
// Convert String path to PlatformFile
controller.selectedFile.value = PlatformFile(
path: filePath,
name: fileName,
size: File(filePath).lengthSync(),
bytes: null,
);
},
onError: (String errorMessage) {
// Show error message
customSnackbar(title: "Error", message: errorMessage);
},
);
},
child: DottedBorder(
color: greyBorderColor,
strokeWidth: 1,
dashPattern: [8, 4],
child: Container(
width: double.infinity,
alignment: Alignment.center,
child: Padding(
padding: EdgeInsets.symmetric(
vertical: 30.h,
horizontal: 5.w,
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
height: 20.h,
width: 20.h,
child: Image.asset(AssetConstants.cloudUpload),
),
SizedBox(height: 10.h),
Text(
'Choose a video file here',
style: TextStyle(
color: Colors.black,
fontSize: mediumSizeText,
fontWeight: FontWeight.w600,
),
),
SizedBox(height: 5.h),
Text(
'MP4 and M4V up to 50MB',
textAlign: TextAlign.center,
style: TextStyle(
color: greyTextColor1,
fontSize: smallSizeText,
fontWeight: FontWeight.w500,
),
),
SizedBox(height: 10.h),
Container(
decoration: BoxDecoration(
border: Border.all(
color: containerBorderColor,
width: 2,
),
borderRadius: BorderRadius.circular(10.r),
),
child: Padding(
padding: EdgeInsets.symmetric(
horizontal: 15.w,
vertical: 5.h,
),
child: Text(
"Browse File",
style: TextStyle(
color: Colors.black,
fontSize: smallSizeText,
fontWeight: FontWeight.w600,
),
),
),
),
],
),
),
),
),
),
SizedBox(height: 20.h),
Obx(() {
final file = controller.selectedFile.value;
if (file == null) return SizedBox();
final progress = controller.uploadProgress.value;
// Calculate uploaded KB for display (assuming file.size is in bytes).
final uploaded = (file.size * progress).round();
final total = file.size;
return Container(
padding: EdgeInsets.all(12),
decoration: BoxDecoration(
color: textFieldFillColor,
borderRadius: BorderRadius.circular(12),
border: Border.all(color: Colors.grey.shade300),
),
child: Row(
children: [
Icon(Icons.video_file_rounded, color: Colors.grey[700]),
SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
file.name,
style: TextStyle(fontWeight: FontWeight.w600),
),
SizedBox(height: 4),
Row(
children: [
Text(
"${(uploaded / 1024).toStringAsFixed(0)} KB of ${(total / 1024).toStringAsFixed(0)} KB",
style: TextStyle(
fontSize: 12,
color: Colors.grey,
),
),
SizedBox(width: 8),
Text("·", style: TextStyle(color: Colors.grey)),
SizedBox(width: 8),
Text(
controller.uploadStatus.value,
style: TextStyle(
fontSize: 12,
color: Colors.grey,
),
),
],
),
SizedBox(height: 6),
LinearProgressIndicator(
value: progress,
minHeight: 5,
backgroundColor: Colors.grey[300],
color: Colors.black,
),
],
),
),
IconButton(
onPressed: controller.cancelUpload,
icon: Icon(Icons.close, color: Colors.grey),
),
],
),
);
}),
],
),
),
),
);
}
}