52 lines
1.9 KiB
Dart
52 lines
1.9 KiB
Dart
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<bool> 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<SectionDTO>? sections = await ApiService.getAllSections(appContext.clientAPI, configuration.id!);
|
|
|
|
if(sections!.isNotEmpty) {
|
|
List<SectionDTO> 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;
|
|
}
|
|
}
|