thumbnail for youtube video

This commit is contained in:
Thomas Fransolet 2024-04-19 16:22:26 +02:00
parent ef89b623d2
commit 1db1c78538
3 changed files with 29 additions and 11 deletions

View File

@ -6,14 +6,14 @@ import 'package:tablet_app/Models/tabletContext.dart';
import 'package:tablet_app/app_context.dart'; import 'package:tablet_app/app_context.dart';
class ImageCustomProvider { class ImageCustomProvider {
static ImageProvider<Object> getImageProvider(AppContext appContext, String imageId, String imageSource) { static ImageProvider<Object> getImageProvider(AppContext appContext, String? imageId, String imageSource) {
TabletAppContext tabletAppContext = appContext.getContext(); TabletAppContext tabletAppContext = appContext.getContext();
try { try {
if(appContext.getContext().localPath != null && tabletAppContext.configuration != null) { if(appContext.getContext().localPath != null && tabletAppContext.configuration != null) {
Directory configurationDirectory = Directory('${tabletAppContext.localPath!}/${tabletAppContext.configuration!.id!}'); Directory configurationDirectory = Directory('${tabletAppContext.localPath!}/${tabletAppContext.configuration!.id!}');
List<FileSystemEntity> fileList = configurationDirectory.listSync(); List<FileSystemEntity> fileList = configurationDirectory.listSync();
if(fileList.any((fileL) => fileL.uri.pathSegments.last.contains(imageId))) { if(imageId != null && fileList.any((fileL) => fileL.uri.pathSegments.last.contains(imageId))) {
File file = File(fileList.firstWhere((fileL) => fileL.uri.pathSegments.last.contains(imageId)).path); File file = File(fileList.firstWhere((fileL) => fileL.uri.pathSegments.last.contains(imageId)).path);
print("FILE EXISTT"); print("FILE EXISTT");
return FileImage(file); return FileImage(file);

View File

@ -68,6 +68,9 @@ class _MainViewWidget extends State<MainViewWidget> {
@override @override
void initState() { void initState() {
final appContext = Provider.of<AppContext>(context, listen: false); final appContext = Provider.of<AppContext>(context, listen: false);
configurationDTO = appContext.getContext().configuration; configurationDTO = appContext.getContext().configuration;
if(configurationDTO.isHour != null && configurationDTO.isHour!) { if(configurationDTO.isHour != null && configurationDTO.isHour!) {
Timer.periodic(Duration(seconds: 1), (Timer t) => _getTime(currentHourDate)); Timer.periodic(Duration(seconds: 1), (Timer t) => _getTime(currentHourDate));
@ -344,9 +347,9 @@ class _MainViewWidget extends State<MainViewWidget> {
decoration: BoxDecoration( decoration: BoxDecoration(
color: sectionsLocal![index].imageSource == null ? kBackgroundLight : null, // default color if no image color: sectionsLocal![index].imageSource == null ? kBackgroundLight : null, // default color if no image
shape: BoxShape.rectangle, shape: BoxShape.rectangle,
image: sectionsLocal![index].imageSource != null ? new DecorationImage( image: sectionsLocal![index].imageSource != null || sectionsLocal![index].type == SectionType.Video ? new DecorationImage(
fit: BoxFit.cover, fit: BoxFit.cover,
image: ImageCustomProvider.getImageProvider(appContext, sectionsLocal![index].imageId!, sectionsLocal![index].imageSource!), image: ImageCustomProvider.getImageProvider(appContext, sectionsLocal![index].imageId, sectionsLocal![index].type == SectionType.Video ? getYoutubeThumbnailUrl(sectionsLocal![index]) : sectionsLocal![index].imageSource!),
): null, ): null,
), ),
) )
@ -386,10 +389,10 @@ boxDecoration(AppContext appContext, SectionDTO section, bool isSelected) {
color: !isSelected ? kBackgroundLight : section.imageSource == null ? kBackgroundLight : null, color: !isSelected ? kBackgroundLight : section.imageSource == null ? kBackgroundLight : null,
shape: BoxShape.rectangle, shape: BoxShape.rectangle,
borderRadius: BorderRadius.circular(tabletAppContext.configuration!.roundedValue?.toDouble() ?? 30.0), borderRadius: BorderRadius.circular(tabletAppContext.configuration!.roundedValue?.toDouble() ?? 30.0),
image: section.imageSource != null ? new DecorationImage( image: section.imageSource != null || section.type == SectionType.Video ? new DecorationImage(
fit: BoxFit.cover, fit: BoxFit.cover,
colorFilter: !isSelected? new ColorFilter.mode(kBackgroundLight.withOpacity(0.2), BlendMode.dstATop) : null, colorFilter: !isSelected? new ColorFilter.mode(kBackgroundLight.withOpacity(0.2), BlendMode.dstATop) : null,
image: ImageCustomProvider.getImageProvider(appContext, section.imageId!, section.imageSource!), image: ImageCustomProvider.getImageProvider(appContext, section.imageId, section.type == SectionType.Video ? getYoutubeThumbnailUrl(section) : section.imageSource!),
): null, ): null,
boxShadow: [ boxShadow: [
BoxShadow( BoxShadow(

View File

@ -126,9 +126,9 @@ class _MenuView extends State<MenuView> {
decoration: BoxDecoration( decoration: BoxDecoration(
color: menuDTO.sections![index].imageSource == null ? kBackgroundColor : null, // default color if no image color: menuDTO.sections![index].imageSource == null ? kBackgroundColor : null, // default color if no image
shape: BoxShape.rectangle, shape: BoxShape.rectangle,
image: menuDTO.sections![index].imageSource != null ? new DecorationImage( image: menuDTO.sections![index].imageSource != null || menuDTO.sections![index].type == SectionType.Video ? new DecorationImage(
fit: BoxFit.cover, fit: BoxFit.cover,
image: ImageCustomProvider.getImageProvider(appContext, menuDTO.sections![index].imageId!, menuDTO.sections![index].imageSource!), image: ImageCustomProvider.getImageProvider(appContext, menuDTO.sections![index].imageId, menuDTO.sections![index].type == SectionType.Video ? getYoutubeThumbnailUrl(menuDTO.sections![index]) : menuDTO.sections![index].imageSource!),
): null, ): null,
), ),
) )
@ -162,10 +162,10 @@ boxDecoration(AppContext appContext, SectionDTO section, bool isSelected) {
color: kBackgroundLight, color: kBackgroundLight,
shape: BoxShape.rectangle, shape: BoxShape.rectangle,
borderRadius: BorderRadius.circular(tabletAppContext.configuration!.roundedValue?.toDouble() ?? 20.0), borderRadius: BorderRadius.circular(tabletAppContext.configuration!.roundedValue?.toDouble() ?? 20.0),
image: section.imageSource != null ? new DecorationImage( image: section.imageSource != null || section.type == SectionType.Video ? new DecorationImage(
fit: BoxFit.cover, fit: BoxFit.cover,
colorFilter: !isSelected? new ColorFilter.mode(kBackgroundLight.withOpacity(0.2), BlendMode.dstATop) : null, colorFilter: !isSelected? new ColorFilter.mode(kBackgroundLight.withOpacity(0.35), BlendMode.dstATop) : null,
image: ImageCustomProvider.getImageProvider(appContext, section.imageId!, section.imageSource!), image: ImageCustomProvider.getImageProvider(appContext, section.imageId, section.type == SectionType.Video ? getYoutubeThumbnailUrl(section) : section.imageSource!),
): null, ): null,
boxShadow: [ boxShadow: [
BoxShadow( BoxShadow(
@ -176,4 +176,19 @@ boxDecoration(AppContext appContext, SectionDTO section, bool isSelected) {
), ),
], ],
); );
}
String getYoutubeThumbnailUrl(SectionDTO sectionDTO) {
try{
VideoDTO? videoDTO = VideoDTO.fromJson(jsonDecode(sectionDTO.data!));
Uri uri = Uri.parse(videoDTO!.source_!);
String videoId = uri.queryParameters['v']!;
// Construire l'URL du thumbnail en utilisant l'identifiant de la vidéo YouTube
String thumbnailUrl = 'https://img.youtube.com/vi/$videoId/0.jpg';
return thumbnailUrl;
} catch(e) {
return "";
}
} }