import 'dart:io'; import 'package:cached_network_image/cached_network_image.dart'; import 'package:flutter/material.dart'; import 'package:manager_api/api.dart'; import 'package:path_provider/path_provider.dart'; import 'package:provider/provider.dart'; import 'package:tablet_app/Components/cached_custom_resource.dart'; import 'package:tablet_app/Models/tabletContext.dart'; import 'package:tablet_app/app_context.dart'; import 'package:tablet_app/constants.dart'; class LoadingCommon extends StatefulWidget { const LoadingCommon({Key? key}) : super(key: key); @override State createState() => _LoadingCommonState(); } class _LoadingCommonState extends State with TickerProviderStateMixin { AnimationController? _controller; @override void initState() { _controller = AnimationController( duration: const Duration(milliseconds: 5000), vsync: this, )..repeat(); super.initState(); } @override void dispose() { _controller!.dispose(); super.dispose(); } @override Widget build(BuildContext context) { final appContext = Provider.of(context); TabletAppContext tabletAppContext = appContext.getContext(); Size size = MediaQuery.of(context).size; _controller!.forward(from: 0.0); _controller!.addListener(() { if (_controller!.isCompleted) { _controller!.reverse(); } if(_controller!.isDismissed){ _controller!.forward(); } }); var primaryColor = tabletAppContext.configuration != null ? tabletAppContext.configuration!.primaryColor != null ? new Color(int.parse(tabletAppContext.configuration!.primaryColor!.split('(0x')[1].split(')')[0], radix: 16)) : kTestSecondColor : kTestSecondColor; return Center( child: SizedBox( height: size.height * 0.1, child: RotationTransition( turns: Tween(begin: 0.0, end: 3.0).animate(_controller!), child: tabletAppContext.configuration != null && tabletAppContext.configuration!.loaderImageUrl != null ? //Image.network(tabletAppContext.configuration!.loaderImageUrl!) // TODO Replace by CustomCacheResource.. FutureBuilder( future: _checkIfLocalResourceExists(tabletAppContext, tabletAppContext.configuration!.loaderImageId!), builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.waiting) { // Loader ou indicateur de chargement pendant la vérification return CircularProgressIndicator(); } else if (snapshot.hasError || snapshot.data == null) { // Si la ressource locale n'existe pas ou s'il y a une erreur print("not a local loader succck"); return CachedNetworkImage( imageUrl: tabletAppContext.configuration!.loaderImageUrl!, progressIndicatorBuilder: (context, url, downloadProgress) => CircularProgressIndicator(value: downloadProgress.progress, color: primaryColor), errorWidget: (context, url, error) => Icon(Icons.error), ); } else { print("Its a local loaaader yeah"); return Image.file( snapshot.data!, ); } }, ) : Icon(Icons.museum_outlined, color: kTestSecondColor, size: size.height*0.1), ), ), ); } } Future _checkIfLocalResourceExists(TabletAppContext tabletAppContext, String resourceId) async { try { Directory? appDocumentsDirectory = await getDownloadsDirectory(); String localPath = appDocumentsDirectory!.path; Directory configurationDirectory = Directory('$localPath/${tabletAppContext.configuration!.id}'); List fileList = configurationDirectory.listSync(); if(fileList.any((fileL) => fileL.uri.pathSegments.last.contains(resourceId))) { File file = File(fileList.firstWhere((fileL) => fileL.uri.pathSegments.last.contains(resourceId)).path); return file; } } catch(e) { print("ERROR _checkIfLocalResourceExists CachedCustomResource"); print(e); } return null; }