import 'dart:convert'; import 'package:manager_api/api.dart'; import 'package:mymuseum_visitapp/Helpers/DatabaseHelper.dart'; import 'package:mymuseum_visitapp/Helpers/modelsHelper.dart'; import 'package:mymuseum_visitapp/Services/apiService.dart'; import 'package:mymuseum_visitapp/app_context.dart'; class DownloadConfiguration { static Future downloadClicked(AppContext appContext, ConfigurationDTO configuration) async { print("coucou downloadClicked"); // Update local DB - Configuration await DatabaseHelper.instance.insert(DatabaseTableType.configurations, ModelsHelper.configurationToMap(configuration)); print("coucou downloadClicked 0"); if(configuration.imageId != null) { ImageDTO image = ImageDTO(resourceId: configuration.imageId, source_: configuration.imageSource); await ApiService.getAndDownloadImage(appContext.clientAPI, image); } print("coucou downloadClicked 1"); List? sections = await ApiService.getAllSections(appContext.clientAPI, configuration.id!); if(sections!.isNotEmpty) { List sectionsToKeep = sections.where((s) => s.type == SectionType.Article).toList(); // TODO handle other type of section sectionsToKeep.sort((a,b) => a.order!.compareTo(b.order!)); int newOrder = 0; // Update local DB - Sections for(var section in sectionsToKeep) { section.order = newOrder; await DatabaseHelper.instance.insert(DatabaseTableType.sections, ModelsHelper.sectionToMap(section)); // Download all images.. ArticleDTO? articleDTO = ArticleDTO.fromJson(jsonDecode(section.data!)); if(articleDTO != null) { for(var image in articleDTO.images!) { await ApiService.getAndDownloadImage(appContext.clientAPI, image); } } newOrder = newOrder + 1; } } return true; } }