|
| 1 | +import 'package:flutter/cupertino.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:flutter_bloc/flutter_bloc.dart'; |
| 4 | +import 'package:go_router/go_router.dart'; |
| 5 | +import 'package:hugeicons/hugeicons.dart'; |
| 6 | +import 'package:open_player/base/assets/fonts/styles.dart'; |
| 7 | +import 'package:open_player/base/router/router.dart'; |
| 8 | +import 'package:open_player/data/models/video_model.dart'; |
| 9 | +import 'package:open_player/data/services/favorites_video_hive_service/favorites_video_hive_service.dart'; |
| 10 | +import 'package:open_player/data/services/file_service/file_service.dart'; |
| 11 | +import 'package:open_player/logic/audio_player_bloc/audio_player_bloc.dart'; |
| 12 | +import 'package:open_player/logic/video_player_bloc/video_player_bloc.dart'; |
| 13 | +import 'package:open_player/logic/videos_bloc/videos_bloc.dart'; |
| 14 | +import 'package:open_player/presentation/pages/audio/sub/albums/view/album_preview_page.dart'; |
| 15 | +import 'package:open_player/utils/extensions.dart'; |
| 16 | +import 'package:share_plus/share_plus.dart'; |
| 17 | +import 'package:velocity_x/velocity_x.dart'; |
| 18 | + |
| 19 | +class ElegantVideoTileWidget extends StatelessWidget { |
| 20 | + final List<VideoModel> filteredVideos; |
| 21 | + final String videoTitle; |
| 22 | + final VideoModel video; |
| 23 | + final int index; |
| 24 | + |
| 25 | + const ElegantVideoTileWidget({ |
| 26 | + super.key, |
| 27 | + required this.filteredVideos, |
| 28 | + required this.videoTitle, |
| 29 | + required this.video, |
| 30 | + required this.index, |
| 31 | + }); |
| 32 | + |
| 33 | + @override |
| 34 | + Widget build(BuildContext context) { |
| 35 | + final isDarkMode = context.themeCubit.state.isDarkMode; |
| 36 | + return GestureDetector( |
| 37 | + onTap: () => _playVideo(context), |
| 38 | + child: Container( |
| 39 | + margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8), |
| 40 | + padding: const EdgeInsets.symmetric(vertical: 12), |
| 41 | + decoration: BoxDecoration( |
| 42 | + color: isDarkMode? Colors.black54: Colors.white, |
| 43 | + borderRadius: BorderRadius.circular(12), |
| 44 | + boxShadow: [ |
| 45 | + BoxShadow( |
| 46 | + color: isDarkMode ? Colors.white12 : Colors.black12, |
| 47 | + blurRadius: 8, |
| 48 | + offset: Offset(0, 4), |
| 49 | + ), |
| 50 | + ], |
| 51 | + ), |
| 52 | + child: Row( |
| 53 | + children: [ |
| 54 | + // Video Icon |
| 55 | + Container( |
| 56 | + margin: const EdgeInsets.symmetric(horizontal: 16), |
| 57 | + padding: const EdgeInsets.all(10), |
| 58 | + decoration: BoxDecoration( |
| 59 | + color: Theme.of(context).primaryColor.withOpacity(0.1), |
| 60 | + shape: BoxShape.circle, |
| 61 | + ), |
| 62 | + child: Stack( |
| 63 | + children: [ |
| 64 | + Icon( |
| 65 | + HugeIcons.strokeRoundedVideoReplay, |
| 66 | + color: Theme.of(context).primaryColor, |
| 67 | + size: 24, |
| 68 | + ), |
| 69 | + |
| 70 | + CircleAvatar( |
| 71 | + backgroundColor: isDarkMode? Colors.white:Colors.black, |
| 72 | + radius: 6, |
| 73 | + child: Text((index+1).toString(),style: TextStyle(fontSize: 8, color: isDarkMode?Colors.black: Colors.white),), |
| 74 | + ).positioned(bottom: 0, right: 0) |
| 75 | + ], |
| 76 | + ), |
| 77 | + ), |
| 78 | + |
| 79 | + // Title |
| 80 | + Expanded( |
| 81 | + child: Text( |
| 82 | + videoTitle, |
| 83 | + style: TextStyle( |
| 84 | + fontFamily: AppFonts.poppins, |
| 85 | + fontSize: 14, |
| 86 | + fontWeight: FontWeight.w500, |
| 87 | + color: isDarkMode?null :Colors.black87, |
| 88 | + ), |
| 89 | + maxLines: 3, |
| 90 | + overflow: TextOverflow.ellipsis, |
| 91 | + ), |
| 92 | + ), |
| 93 | + |
| 94 | + // More Options |
| 95 | + IconButton( |
| 96 | + icon: Icon( |
| 97 | + CupertinoIcons.ellipsis, |
| 98 | + color:isDarkMode? null: Colors.grey.shade600, |
| 99 | + ), |
| 100 | + onPressed: () => _showVideoOptions(context), |
| 101 | + ), |
| 102 | + ], |
| 103 | + ), |
| 104 | + ), |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + void _playVideo(BuildContext context) { |
| 109 | + context.read<AudioPlayerBloc>().add(AudioPlayerStopEvent()); |
| 110 | + context.read<VideoPlayerBloc>().add( |
| 111 | + VideoInitializeEvent(videoIndex: index, playlist: filteredVideos), |
| 112 | + ); |
| 113 | + GoRouter.of(context).push(AppRoutes.videoPlayerRoute); |
| 114 | + } |
| 115 | + |
| 116 | + void _showVideoOptions(BuildContext context) { |
| 117 | + showCupertinoModalPopup( |
| 118 | + context: context, |
| 119 | + builder: (BuildContext context) => CupertinoActionSheet( |
| 120 | + title: Text( |
| 121 | + 'Video Options', |
| 122 | + style: TextStyle( |
| 123 | + fontFamily: AppFonts.poppins, |
| 124 | + fontSize: 16, |
| 125 | + fontWeight: FontWeight.w600, |
| 126 | + ), |
| 127 | + ), |
| 128 | + actions: <CupertinoActionSheetAction>[ |
| 129 | + // Favorite Option |
| 130 | + _buildActionSheetAction( |
| 131 | + context, |
| 132 | + text: _getFavoriteText(), |
| 133 | + icon: _getFavoriteIcon(), |
| 134 | + onPressed: () => _toggleFavorite(), |
| 135 | + ), |
| 136 | + |
| 137 | + // Rename Option |
| 138 | + _buildActionSheetAction( |
| 139 | + context, |
| 140 | + text: 'Rename', |
| 141 | + icon: CupertinoIcons.pencil, |
| 142 | + onPressed: () => _renameVideo(context), |
| 143 | + ), |
| 144 | + |
| 145 | + // Delete Option |
| 146 | + _buildActionSheetAction( |
| 147 | + context, |
| 148 | + text: 'Delete', |
| 149 | + icon: CupertinoIcons.delete, |
| 150 | + onPressed: () => _deleteVideo(context), |
| 151 | + isDestructiveAction: true, |
| 152 | + ), |
| 153 | + |
| 154 | + // Share Option |
| 155 | + _buildActionSheetAction( |
| 156 | + context, |
| 157 | + text: 'Share', |
| 158 | + icon: CupertinoIcons.share, |
| 159 | + onPressed: () => _shareVideo(), |
| 160 | + ), |
| 161 | + ], |
| 162 | + cancelButton: CupertinoActionSheetAction( |
| 163 | + onPressed: () => Navigator.pop(context), |
| 164 | + child: Text( |
| 165 | + 'Cancel', |
| 166 | + style: TextStyle( |
| 167 | + fontFamily: AppFonts.poppins, |
| 168 | + color: Theme.of(context).primaryColor, |
| 169 | + ), |
| 170 | + ), |
| 171 | + ), |
| 172 | + ), |
| 173 | + ); |
| 174 | + } |
| 175 | + |
| 176 | + CupertinoActionSheetAction _buildActionSheetAction( |
| 177 | + BuildContext context, { |
| 178 | + required String text, |
| 179 | + required IconData icon, |
| 180 | + required VoidCallback onPressed, |
| 181 | + bool isDestructiveAction = false, |
| 182 | + }) { |
| 183 | + return CupertinoActionSheetAction( |
| 184 | + onPressed: () { |
| 185 | + Navigator.pop(context); |
| 186 | + onPressed(); |
| 187 | + }, |
| 188 | + child: Row( |
| 189 | + mainAxisAlignment: MainAxisAlignment.center, |
| 190 | + children: [ |
| 191 | + Icon( |
| 192 | + icon, |
| 193 | + color: isDestructiveAction ? Colors.red : null, |
| 194 | + ), |
| 195 | + const SizedBox(width: 10), |
| 196 | + Text( |
| 197 | + text, |
| 198 | + style: TextStyle( |
| 199 | + fontFamily: AppFonts.poppins, |
| 200 | + color: isDestructiveAction ? Colors.red : null, |
| 201 | + ), |
| 202 | + ), |
| 203 | + ], |
| 204 | + ), |
| 205 | + ); |
| 206 | + } |
| 207 | + |
| 208 | + String _getFavoriteText() { |
| 209 | + final isFavorite = |
| 210 | + FavoritesVideoHiveService().getFavoriteStatus(video.path); |
| 211 | + return isFavorite ? 'Remove from Favorites' : 'Add to Favorites'; |
| 212 | + } |
| 213 | + |
| 214 | + IconData _getFavoriteIcon() { |
| 215 | + final isFavorite = |
| 216 | + FavoritesVideoHiveService().getFavoriteStatus(video.path); |
| 217 | + return isFavorite ? CupertinoIcons.heart_fill : CupertinoIcons.heart; |
| 218 | + } |
| 219 | + |
| 220 | + void _toggleFavorite() { |
| 221 | + FavoritesVideoHiveService().toggleFavorite(video.path); |
| 222 | + } |
| 223 | + |
| 224 | + void _renameVideo(BuildContext context) { |
| 225 | + showCupertinoDialog( |
| 226 | + context: context, |
| 227 | + builder: (context) { |
| 228 | + final controller = TextEditingController(text: videoTitle); |
| 229 | + return CupertinoAlertDialog( |
| 230 | + title: Text( |
| 231 | + 'Rename Video', |
| 232 | + style: TextStyle(fontFamily: AppFonts.poppins), |
| 233 | + ), |
| 234 | + content: CupertinoTextField( |
| 235 | + controller: controller, |
| 236 | + placeholder: 'Enter new name', |
| 237 | + style: TextStyle(fontFamily: AppFonts.poppins), |
| 238 | + ), |
| 239 | + actions: <CupertinoDialogAction>[ |
| 240 | + CupertinoDialogAction( |
| 241 | + isDefaultAction: true, |
| 242 | + onPressed: () => Navigator.pop(context), |
| 243 | + child: Text( |
| 244 | + 'Cancel', |
| 245 | + style: TextStyle(fontFamily: AppFonts.poppins), |
| 246 | + ), |
| 247 | + ), |
| 248 | + CupertinoDialogAction( |
| 249 | + isDestructiveAction: true, |
| 250 | + onPressed: () { |
| 251 | + FileService() |
| 252 | + .renameFile(video.path, controller.text.trim()) |
| 253 | + .whenComplete(() { |
| 254 | + context.read<VideosBloc>().add(VideosLoadEvent()); |
| 255 | + Navigator.pop(context); |
| 256 | + }); |
| 257 | + }, |
| 258 | + child: Text( |
| 259 | + 'Rename', |
| 260 | + style: TextStyle(fontFamily: AppFonts.poppins), |
| 261 | + ), |
| 262 | + ), |
| 263 | + ], |
| 264 | + ); |
| 265 | + }, |
| 266 | + ); |
| 267 | + } |
| 268 | + |
| 269 | + void _deleteVideo(BuildContext context) { |
| 270 | + FileService().deleteFile(video.path).whenComplete(() { |
| 271 | + context.read<VideosBloc>().add(VideosLoadEvent()); |
| 272 | + // You might want to add a toast or snackbar here |
| 273 | + }); |
| 274 | + } |
| 275 | + |
| 276 | + void _shareVideo() async { |
| 277 | + await Share.shareXFiles([XFile(video.path)]); |
| 278 | + } |
| 279 | +} |
0 commit comments