mirror of
https://bitbucket.org/FransoletThomas/tablet-app.git
synced 2025-12-06 08:31:19 +00:00
219 lines
7.2 KiB
Dart
219 lines
7.2 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:manager_api/api.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:tablet_app/Models/tabletContext.dart';
|
|
import 'package:tablet_app/app_context.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
class DownloadConfigurationWidget extends StatefulWidget {
|
|
DownloadConfigurationWidget();
|
|
|
|
@override
|
|
State<DownloadConfigurationWidget> createState() => _DownloadConfigurationWidgetState();
|
|
}
|
|
|
|
class _DownloadConfigurationWidgetState extends State<DownloadConfigurationWidget> {
|
|
int? nbrResources;
|
|
ValueNotifier<int> currentResourceIndex = ValueNotifier<int>(0);
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
super.dispose();
|
|
}
|
|
|
|
Future<bool> download(BuildContext buildContext, TabletAppContext tabletAppContext) async {
|
|
bool isAllLanguages = true;
|
|
|
|
ExportConfigurationDTO? exportConfigurationDTO;
|
|
try{
|
|
print(tabletAppContext.configuration!.id!);
|
|
print("essai");
|
|
|
|
// Retrieve all url from resource to download (get all resource from configuration en somme)
|
|
exportConfigurationDTO = await tabletAppContext.clientAPI!.configurationApi!.configurationExport(tabletAppContext.configuration!.id!);
|
|
} catch(e) {
|
|
print("Erreur lors du téléchargement de la configuration et de ses ressources !");
|
|
print(e);
|
|
return false;
|
|
}
|
|
|
|
print("COUCUO heeere");
|
|
print(exportConfigurationDTO);
|
|
print(exportConfigurationDTO.resources!.length);
|
|
|
|
exportConfigurationDTO.resources!.forEach((element) {
|
|
print(element.id);
|
|
print(element.label);
|
|
});
|
|
|
|
|
|
if(exportConfigurationDTO != null && exportConfigurationDTO.resources != null && exportConfigurationDTO.resources!.isNotEmpty) {
|
|
nbrResources = exportConfigurationDTO.resources!.where((resource) => resource.type != ResourceType.ImageUrl && resource.type != ResourceType.VideoUrl && resource.url != null).length;
|
|
|
|
print("i'm here");
|
|
print(nbrResources);
|
|
Directory? appDocumentsDirectory = await getDownloadsDirectory();
|
|
String localPath = appDocumentsDirectory!.path;
|
|
|
|
print(localPath);
|
|
|
|
Map<Permission, PermissionStatus> statuses = await [
|
|
Permission.storage,
|
|
].request();
|
|
|
|
print(statuses[Permission.storage]);
|
|
print(statuses);
|
|
|
|
if(statuses[Permission.storage] == PermissionStatus.granted) {
|
|
|
|
try{
|
|
Directory directory = Directory('$localPath');
|
|
List<FileSystemEntity> allConfigurations = directory.listSync();
|
|
|
|
for (var file in allConfigurations) {
|
|
print('file.uri.pathSegments');
|
|
print(file.uri.pathSegments);
|
|
print('file.uri.pathSegments.last');
|
|
print(file.uri.pathSegments.last);
|
|
}
|
|
|
|
Directory configurationDirectory = Directory('$localPath/${tabletAppContext.configuration!.id}');
|
|
|
|
if(!allConfigurations.any((configurationDirectory) => configurationDirectory.uri.pathSegments.any((element) => element == tabletAppContext.configuration!.id))) {
|
|
// create directory
|
|
configurationDirectory.createSync(recursive: true);
|
|
print('Répertoire créé avec succès.');
|
|
} else {
|
|
print('EXISTE D2J0 NIGAUD.');
|
|
}
|
|
|
|
List<FileSystemEntity> fileList = configurationDirectory.listSync();
|
|
|
|
print("HERE LIST in directory");
|
|
|
|
for (var file in fileList) {
|
|
print(file.uri.pathSegments.last);
|
|
}
|
|
|
|
// foreach ou on va tout télécharger - avec un joli etape 0 / length - on peut rendre tout lent on s'en fou ça ne ce fait qu'une fois
|
|
exportConfigurationDTO.resources!.forEach((resource) async {
|
|
|
|
if(fileList.any((fileL) => fileL.uri.pathSegments.last.contains(resource.id!))) {
|
|
print("Already exist TRIPLE NIGAUD!");
|
|
currentResourceIndex.value++;
|
|
} else {
|
|
if(resource.type != ResourceType.ImageUrl && resource.type != ResourceType.VideoUrl && resource.url != null) {
|
|
bool success = await downloadResource(tabletAppContext, resource, localPath);
|
|
|
|
if (success) {
|
|
currentResourceIndex.value++;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
} catch(e) {
|
|
print("ERRORRRR");
|
|
print(e);
|
|
return false;
|
|
}
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Puis après faudra changer tous les appels à une resourceUrl avec un get local de la resource
|
|
return true;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final appContext = Provider.of<AppContext>(context);
|
|
TabletAppContext tabletAppContext = appContext.getContext();
|
|
Size size = MediaQuery.of(context).size;
|
|
|
|
return Column(
|
|
children: [
|
|
FutureBuilder(future: download(context, tabletAppContext), builder: (context, snapshot) {
|
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
|
// Loader ou indicateur de chargement pendant la vérification
|
|
return Center(child: CircularProgressIndicator());
|
|
} else {
|
|
return Text("Mise à jour finie");
|
|
}
|
|
}),
|
|
ValueListenableBuilder<int>(
|
|
valueListenable: currentResourceIndex,
|
|
builder: (context, value, _) {
|
|
return Center(
|
|
child: Text(
|
|
value.toString()+'/'+nbrResources.toString(),
|
|
style: TextStyle(fontSize: 15, fontWeight: FontWeight.w500),
|
|
),
|
|
);
|
|
}
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<bool> downloadResource(TabletAppContext tabletAppContext, ResourceDTO resourceDTO, String localPath) async {
|
|
try {
|
|
// Téléchargement de la ressource depuis l'URL
|
|
http.Response response = await http.get(Uri.parse(resourceDTO.url!));
|
|
|
|
if (response.statusCode == 200) {
|
|
// Vérification de l'en-tête Content-Type
|
|
String contentType = response.headers["content-type"] ?? "";
|
|
|
|
// Déduction de l'extension en fonction du Content-Type
|
|
String extension = _getExtensionFromContentType(contentType);
|
|
|
|
print("LOCAL PATTH");
|
|
print(localPath);
|
|
|
|
File file = File('$localPath/${tabletAppContext.configuration!.id}/${resourceDTO.id}.$extension');
|
|
|
|
// Écriture du contenu téléchargé dans le fichier local
|
|
await file.writeAsBytes(response.bodyBytes);
|
|
|
|
return true;
|
|
} else {
|
|
print("Échec du téléchargement de la ressource - ${response.statusCode}");
|
|
return false;
|
|
}
|
|
} catch (e) {
|
|
print("Erreur lors du téléchargement de la ressource !");
|
|
print(e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
String _getExtensionFromContentType(String contentType) {
|
|
Map<String, String> contentTypeToExtension = {
|
|
"image/jpeg": "jpg",
|
|
"image/jpg": "jpg",
|
|
"image/png": "png",
|
|
"image/gif": "gif",
|
|
"audio/mp3": "mp3",
|
|
"video/mp4": "mp4",
|
|
"video/webm": "webm",
|
|
"video/avi": "avi",
|
|
"video/quicktime": "mov",
|
|
"application/pdf": "pdf",
|
|
"application/json": "json"
|
|
};
|
|
|
|
return contentTypeToExtension[contentType] ?? "unknown";
|
|
} |