import 'dart:convert'; import 'dart:io'; import 'package:flutter/material.dart'; import 'package:manager_api_new/api.dart'; import 'package:mymuseum_visitapp/Helpers/DatabaseHelper.dart'; import 'package:mymuseum_visitapp/Helpers/modelsHelper.dart'; import 'package:mymuseum_visitapp/Helpers/translationHelper.dart'; import 'package:mymuseum_visitapp/Models/resourceModel.dart'; import 'package:mymuseum_visitapp/Models/visitContext.dart'; import 'package:mymuseum_visitapp/Services/apiService.dart'; import 'package:mymuseum_visitapp/app_context.dart'; import 'package:mymuseum_visitapp/constants.dart'; import 'package:permission_handler/permission_handler.dart'; import 'package:provider/provider.dart'; import 'package:http/http.dart' as http; import 'package:fluttertoast/fluttertoast.dart'; class DownloadConfigurationWidget extends StatefulWidget { DownloadConfigurationWidget({Key? key, required this.configuration}) : super(key: key); final ConfigurationDTO configuration; @override State createState() => _DownloadConfigurationWidgetState(); } class _DownloadConfigurationWidgetState extends State { ValueNotifier currentResourceIndex = ValueNotifier(0); ValueNotifier currentResourceNbr = ValueNotifier(-1); bool isAlreadyDownloading = false; //OtaEvent? currentEvent; Future download(BuildContext buildContext, VisitAppContext visitAppContext) async { bool isAllLanguages = true; if(visitAppContext.isAllLanguages != null) { isAllLanguages = visitAppContext.isAllLanguages!; if(isAllLanguages) { print("Admin here and all download all audios !"); /*ScaffoldMessenger.of(buildContext).showSnackBar( const SnackBar( content: Text("Tous les audios vont être téléchargés"), backgroundColor: kMainColor), );*/ } } if(!isAlreadyDownloading) { isAlreadyDownloading = true; // HERE CHECK VERSION APK if(true) { Map statuses = await [ Permission.requestInstallPackages, ].request(); //tryOtaUpdate(); } ExportConfigurationDTO? exportConfigurationDTO; try{ // Retrieve all url from resource to download (get all resource from configuration en somme) exportConfigurationDTO = await visitAppContext.clientAPI.configurationApi!.configurationExport(widget.configuration.id!, language: isAllLanguages ? null : visitAppContext.language); // tabletAppContext.configuration!.id! // 65c5f0ee4c030e63ce16bff5 TODO Remove } catch(e) { print("Erreur lors du téléchargement de la configuration et de ses ressources !"); print(e); return false; } exportConfigurationDTO.resources!.forEach((element) { print(element.id); print(element.label); }); if(exportConfigurationDTO.resources != null && exportConfigurationDTO.resources!.isNotEmpty) { Map statuses = await [ Permission.storage, ].request(); //if(statuses[Permission.storage] == PermissionStatus.granted) { try{ try { Directory directory = Directory('${visitAppContext.localPath}'); List allConfigurations = directory.listSync(); Directory configurationDirectory = Directory('${visitAppContext.localPath}/${widget.configuration.id}'); if(!allConfigurations.any((configurationDirectory) => configurationDirectory.uri.pathSegments.any((element) => element == widget.configuration.id))) { // create directory print("Trying to create directory"); configurationDirectory.createSync(recursive: true); print('Répertoire créé avec succès.'); } } catch(e) { print("Listing failed, so try to create directory"); Directory configurationDirectory = Directory('${visitAppContext.localPath}/${widget.configuration.id}'); configurationDirectory.createSync(recursive: true); print('Répertoire créé avec succès.'); } Directory configurationDirectory = Directory('${visitAppContext.localPath}/${widget.configuration.id}'); List fileList = configurationDirectory.listSync(); for (var file in fileList) { print(file.uri.pathSegments.last); } var resourcesToDownload = exportConfigurationDTO.resources!.where((resource) => resource.type != ResourceType.ImageUrl && resource.type != ResourceType.VideoUrl && resource.type != ResourceType.JsonUrl && resource.url != null && !fileList.any((fileL) => fileL.uri.pathSegments.last.contains(resource.id!))); currentResourceNbr.value = resourcesToDownload.length; // 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 for (var resource in resourcesToDownload) { String? filePath = await downloadResource(visitAppContext, widget.configuration, resource, visitAppContext.localPath!); if (filePath != null) { // Insert in database ResourceModel resourceModel = ResourceModel(id: resource.id, source: resource.url, path: filePath, type: resource.type); try { await DatabaseHelper.instance.insert(DatabaseTableType.resources, resourceModel.toMap()); } catch (e) { print("We got an issue inserting image metadata ${resource.id}"); } currentResourceIndex.value++; } else { print("NOT SUCCESSS"); } } // Delete others that are no more used var resourceToDelete = fileList.where((fileL) => !exportConfigurationDTO!.resources!.any((resource) => resource.id != null && fileL.uri.pathSegments.last.contains(resource.id!))); for (var resource in resourceToDelete) { print("resource to DELETE"); print(resource.path); // resource.deleteSync(); // Preserve call to firebase // TODO uncomment if needed } await DatabaseHelper.instance.insert(DatabaseTableType.configurations, ModelsHelper.configurationToMap(widget.configuration)); List? sections = exportConfigurationDTO.sections; List usedImageOrAudioIds = []; if(sections!.isNotEmpty) { List sectionsInDB = await DatabaseHelper.instance.queryWithConfigurationId(DatabaseTableType.sections, widget.configuration.id!); List sectionsToKeep = sections.where((s) => s.type == SectionType.Article || s.type == SectionType.Quiz).toList(); // TODO handle other type of section (for now, Article and Quizz) sectionsToKeep.sort((a,b) => a.order!.compareTo(b.order!)); int newOrder = 0; // Update local DB - Sections for(var section in sectionsToKeep) { section.order = newOrder; try { await DatabaseHelper.instance.insert(DatabaseTableType.sections, ModelsHelper.sectionToMap(section)); } catch (e) { print("We got an issue inserting section data ${section.id}"); } // Download section image if(section.imageId != null) { usedImageOrAudioIds.add(section.imageId!); var imageData = exportConfigurationDTO.resources!.where((element) => element.id == section.imageId); if(imageData.isNotEmpty) { // TODO get all resources from API + store it in download directory of the app ResourceModel resourceModel = ResourceModel(id: imageData.first.id, source: section.imageSource, /*data: imageData.first.data,*/ type: imageData.first.type); try { await DatabaseHelper.instance.insert(DatabaseTableType.resources, resourceModel.toMap()); } catch (e) { print("We got an issue inserting image data ${imageData.first.id}"); } } } // Download all images.. ArticleDTO? articleDTO = ArticleDTO.fromJson(jsonDecode(section.data!)); if(articleDTO != null) { for(var image in articleDTO.contents!) { usedImageOrAudioIds.add(image.resourceId!); /*var imageData = exportConfigurationDTO.resources!.where((element) => element.id == image.resourceId); if(imageData.isNotEmpty) { // TODO get all resources from API + store it in download directory of the app ResourceModel resourceModel = ResourceModel(id: imageData.first.id, source: image.resourceUrl, /*data: imageData.first.data,*/ type: imageData.first.type); try { await DatabaseHelper.instance.insert(DatabaseTableType.resources, resourceModel.toMap()); } catch (e) { print("We got an issue inserting image data ${imageData.first.id}"); } }*/ } var audioIdsArticle = isAllLanguages ? articleDTO.audioIds! : articleDTO.audioIds!.where((audioId) => audioId.language == visitAppContext.language); for(var audioId in audioIdsArticle) { if(audioId.value != null) { usedImageOrAudioIds.add(audioId.value!); //audiosNotWorking = await importAudio(visitAppContext, exportConfigurationDTO, audioId.value!, audiosNotWorking); } } } newOrder = newOrder + 1; } List sectionIdsToRemove = sectionsInDB.map((s) => s.id).where((sectionId) => !sectionsToKeep.map((sk) => sk.id).contains(sectionId)).toList(); for(var sectionIdToRemove in sectionIdsToRemove) { print("section with id removed"); print(sectionIdToRemove); try { await DatabaseHelper.instance.delete(sectionIdToRemove!, DatabaseTableType.sections); } catch (e) { print("We got an issue deleting section id: ${sectionIdToRemove}"); } } // TODO CLEAN AND REMOVE FILES ! //cleanLocalResources(usedImageOrAudioIds, widget.configuration); } } catch(e) { print("ERRORRRR"); print(e); if(statuses[Permission.storage] != PermissionStatus.granted) { Fluttertoast.showToast( msg: "PermissionStatus not granted, issue may be linked to that. Please check os version (if less than 13, real issue).", toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.BOTTOM, timeInSecForIosWeb: 1, backgroundColor: Colors.redAccent, textColor: Colors.white, fontSize: 16.0 ); } return false; } /*} else { print("PermissionStatus.granted NOT GRANTED"); Fluttertoast.showToast( msg: "PermissionStatus not granted", toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.BOTTOM, timeInSecForIosWeb: 1, backgroundColor: Colors.redAccent, textColor: Colors.white, fontSize: 16.0 ); return false; }*/ } } return true; } @override Widget build(BuildContext context) { final appContext = Provider.of(context); VisitAppContext visitAppContext = appContext.getContext(); Size size = MediaQuery.of(context).size; return Center( child: FutureBuilder(future: download(context, visitAppContext), builder: (context, snapshot) { return ValueListenableBuilder( valueListenable: currentResourceNbr, builder: (context, valueNbr, _) { return ValueListenableBuilder( valueListenable: currentResourceIndex, builder: (context, valueIndex, _) { var valueInPercentage = valueNbr > 0 ? (valueIndex / valueNbr) * 100 : 100; String formattedPercentage = valueInPercentage.toStringAsFixed(0); return Column( mainAxisAlignment: MainAxisAlignment.spaceAround, crossAxisAlignment: CrossAxisAlignment.center, children: [ Center( child: Padding( padding: const EdgeInsets.all(8.0), child: Text( textAlign: TextAlign.center, valueIndex == valueNbr && valueNbr != -1 ? valueNbr == 0 ? TranslationHelper.getFromLocale( "upToDate", appContext.getContext()) : TranslationHelper.getFromLocale( "downloadFinish", appContext.getContext()) : TranslationHelper.getFromLocale( "downloadInProgress", appContext.getContext()), style: const TextStyle(fontSize: 20), ), ), ), if(valueNbr != -1 && valueNbr != 0) Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, mainAxisSize: MainAxisSize.min, children: [ Padding( padding: const EdgeInsets.all(8.0), child: LinearProgressIndicator( value: valueInPercentage / 100, semanticsLabel: 'Linear progress indicator', color: kMainColor0, ), ), Padding( padding: const EdgeInsets.all(8.0), child: Text( '$formattedPercentage%', style: const TextStyle(fontSize: 28, fontWeight: FontWeight.w500), ), ), ], ), ) ], ); } ); } ); }), ); } } /*class DownloadConfiguration { static Future> download(BuildContext buildContext, AppContext appContext, ConfigurationDTO configuration) async { VisitAppContext visitAppContext = (appContext.getContext() as VisitAppContext); bool isAllLanguages = false; List audiosNotWorking = []; if(visitAppContext.isAllLanguages != null) { isAllLanguages = visitAppContext.isAllLanguages!; if(isAllLanguages) { print("Admin here and all download all audios !"); ScaffoldMessenger.of(buildContext).showSnackBar( const SnackBar( content: Text("Tous les audios vont être téléchargés"), backgroundColor: kMainColor), ); } } print("Start export configuration to store it locally"); ExportConfigurationDTO? exportConfigurationDTO; try{ exportConfigurationDTO = await ApiService.exportConfiguration(visitAppContext.clientAPI, configuration.id!, isAllLanguages ? null : visitAppContext.language!); } catch(e) { print("Erreur lors du téléchargement de la visite"); print(e); } if(exportConfigurationDTO != null) { // Delete all from local DB ref to this configurationId then add it all await DatabaseHelper.instance.insert(DatabaseTableType.configurations, ModelsHelper.configurationToMap(configuration)); List usedImageOrAudioIds = []; var currentLanguage = (appContext.getContext() as VisitAppContext).language; if(configuration.imageId != null) { usedImageOrAudioIds.add(configuration.imageId!); var imageData = exportConfigurationDTO.resources!.where((element) => element.id == configuration.imageId); if(imageData.isNotEmpty) { ResourceModel resourceModel = ResourceModel(id: imageData.first.id, source: configuration.imageSource, /*data: imageData.first.data,*/ type: imageData.first.type); try { await DatabaseHelper.instance.insert(DatabaseTableType.resources, resourceModel.toMap()); } catch (e) { print("We got an issue inserting image data ${imageData.first.id}"); } } } List? sections = exportConfigurationDTO.sections; if(sections!.isNotEmpty) { List sectionsInDB = await DatabaseHelper.instance.queryWithConfigurationId(DatabaseTableType.sections, configuration.id!); List sectionsToKeep = sections.where((s) => s.type == SectionType.Article || s.type == SectionType.Quizz).toList(); // TODO handle other type of section (for now, Article and Quizz) sectionsToKeep.sort((a,b) => a.order!.compareTo(b.order!)); int newOrder = 0; // Update local DB - Sections for(var section in sectionsToKeep) { section.order = newOrder; try { await DatabaseHelper.instance.insert(DatabaseTableType.sections, ModelsHelper.sectionToMap(section)); } catch (e) { print("We got an issue inserting section data ${section.id}"); } // Download section image if(section.imageId != null) { usedImageOrAudioIds.add(section.imageId!); var imageData = exportConfigurationDTO.resources!.where((element) => element.id == section.imageId); if(imageData.isNotEmpty) { // TODO get all resources from API + store it in download directory of the app ResourceModel resourceModel = ResourceModel(id: imageData.first.id, source: section.imageSource, /*data: imageData.first.data,*/ type: imageData.first.type); try { await DatabaseHelper.instance.insert(DatabaseTableType.resources, resourceModel.toMap()); } catch (e) { print("We got an issue inserting image data ${imageData.first.id}"); } } } // Download all images.. ArticleDTO? articleDTO = ArticleDTO.fromJson(jsonDecode(section.data!)); if(articleDTO != null) { for(var image in articleDTO.contents!) { usedImageOrAudioIds.add(image.resourceId!); var imageData = exportConfigurationDTO.resources!.where((element) => element.id == image.resourceId); if(imageData.isNotEmpty) { // TODO get all resources from API + store it in download directory of the app ResourceModel resourceModel = ResourceModel(id: imageData.first.id, source: image.resourceUrl, /*data: imageData.first.data,*/ type: imageData.first.type); try { await DatabaseHelper.instance.insert(DatabaseTableType.resources, resourceModel.toMap()); } catch (e) { print("We got an issue inserting image data ${imageData.first.id}"); } } } var audioIdsArticle = isAllLanguages ? articleDTO.audioIds! : articleDTO.audioIds!.where((audioId) => audioId.language == currentLanguage); for(var audioId in audioIdsArticle) { if(audioId.value != null) { usedImageOrAudioIds.add(audioId.value!); audiosNotWorking = await importAudio(visitAppContext, exportConfigurationDTO, audioId.value!, audiosNotWorking); } } } newOrder = newOrder + 1; } List sectionIdsToRemove = sectionsInDB.map((s) => s.id).where((sectionId) => !sectionsToKeep.map((sk) => sk.id).contains(sectionId)).toList(); for(var sectionIdToRemove in sectionIdsToRemove) { print("section with id removed"); print(sectionIdToRemove); try { await DatabaseHelper.instance.delete(sectionIdToRemove!, DatabaseTableType.sections); } catch (e) { print("We got an issue deleting section id: ${sectionIdToRemove}"); } } cleanLocalResources(usedImageOrAudioIds, configuration); } } return audiosNotWorking; } } */ Future downloadResource(VisitAppContext visitAppContext, ConfigurationDTO configurationDTO, 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/${configurationDTO.id}/${resourceDTO.id}.$extension'); // Écriture du contenu téléchargé dans le fichier local await file.writeAsBytes(response.bodyBytes); return file.path; } else { print("Échec du téléchargement de la ressource - ${response.statusCode}"); return null; } } catch (e) { print("Erreur lors du téléchargement de la ressource !"); print(e); return null; } } String _getExtensionFromContentType(String contentType) { Map 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"; } Future> importAudio(VisitAppContext visitAppContext, ExportConfigurationDTO exportConfigurationDTO, String audioId, List audiosNotWorking) async { var audioData = exportConfigurationDTO.resources!.where((element) => element.id == audioId); if(audioData.isNotEmpty) { // TODO get all resources from API + store it in download directory of the app ResourceModel resourceModel = ResourceModel(id: audioData.first.id, source: "", /*data: audioData.first.data,*/ type: audioData.first.type); try { await DatabaseHelper.instance.insert(DatabaseTableType.resources, resourceModel.toMap()); return audiosNotWorking; } catch (e) { print("We got an issue inserting audio data ${audioData.first.id}"); resourceModel.label = audioData.first.label; print(resourceModel.label); // Update context with not working audios //VisitAppContext visitAppContext = appContext.getContext() as VisitAppContext; audiosNotWorking.add(resourceModel); audiosNotWorking = audiosNotWorking.toSet().toList(); // Remove duplicates // appContext.setContext(visitAppContext); return audiosNotWorking; } } return audiosNotWorking; } void cleanLocalResources(List usedImageIds, ConfigurationDTO configuration) async { List> resourcesInDB = await DatabaseHelper.instance.queryAllRows(DatabaseTableType.resources); List resourcesModel = []; for(var resource in resourcesInDB) { resourcesModel.add(DatabaseHelper.instance.getResourceFromDB(resource)); } List resourceIdsToRemove = resourcesModel.map((r) => r.id).where((resourceId) => !usedImageIds.contains(resourceId)).toList(); for(var resourceIdToRemove in resourceIdsToRemove) { print("resource with id removed ________________ !!!!!"); print(resourceIdToRemove); try { await DatabaseHelper.instance.delete(resourceIdToRemove!, DatabaseTableType.resources); } catch (e) { print("We got an issue deleting resource id: ${resourceIdToRemove}"); } } }