tablet-app/lib/Components/loading_common.dart
2024-02-08 16:08:43 +01:00

113 lines
4.3 KiB
Dart

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<LoadingCommon> createState() => _LoadingCommonState();
}
class _LoadingCommonState extends State<LoadingCommon> 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<AppContext>(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<File?>(
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<File?> _checkIfLocalResourceExists(TabletAppContext tabletAppContext, String resourceId) async {
try {
Directory? appDocumentsDirectory = Platform.isIOS ? await getApplicationSupportDirectory() : await getDownloadsDirectory();
String localPath = appDocumentsDirectory!.path;
Directory configurationDirectory = Directory('$localPath/${tabletAppContext.configuration!.id}');
List<FileSystemEntity> 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;
}