208 lines
7.5 KiB
Dart
208 lines
7.5 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:manager_api_new/api.dart';
|
|
import 'package:mymuseum_visitapp/Helpers/DatabaseHelper.dart';
|
|
import 'package:mymuseum_visitapp/Helpers/networkCheck.dart';
|
|
import 'package:mymuseum_visitapp/Models/resourceModel.dart';
|
|
import 'package:mymuseum_visitapp/Models/visitContext.dart';
|
|
import 'package:mymuseum_visitapp/app_context.dart';
|
|
import 'package:mymuseum_visitapp/client.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
class ApiService {
|
|
static Future<List<ConfigurationDTO>?> getConfigurations(Client client, VisitAppContext? visitAppContext) async {
|
|
try {
|
|
List<ConfigurationDTO>? configurations;
|
|
bool isOnline = await hasNetwork();
|
|
if(isOnline) {
|
|
configurations = await client.configurationApi!.configurationGet(instanceId: visitAppContext?.instanceId);
|
|
|
|
if(configurations != null) {
|
|
if(configurations.isNotEmpty) {
|
|
for(var configuration in configurations) {
|
|
if(configuration.imageId != null) {
|
|
await downloadAndPushLocalImage(client, ContentDTO(resourceId: configuration.imageId, resource: ResourceDTO(url: configuration.imageSource, id: configuration.imageId)));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
return [];
|
|
}
|
|
|
|
return configurations;
|
|
|
|
} catch (e) {
|
|
print(e);
|
|
print("getConfigurations IN CATCH");
|
|
return [];
|
|
}
|
|
}
|
|
|
|
static Future<List<SectionDTO>?> getAllSections(Client client, String configurationId) async {
|
|
try {
|
|
bool isOnline = await hasNetwork();
|
|
if(isOnline) {
|
|
List<SectionDTO>? sections = await client.sectionApi!.sectionGetFromConfiguration(configurationId);
|
|
return sections;
|
|
} else {
|
|
return []; // TODO return local list..
|
|
}
|
|
} catch (e) {
|
|
print(e);
|
|
print("getAllSections IN CATCH");
|
|
return [];
|
|
}
|
|
}
|
|
|
|
static Future<List<SectionDTO>?> getAllBeacons(Client client, String instanceId) async {
|
|
try {
|
|
bool isOnline = await hasNetwork();
|
|
if(isOnline) {
|
|
List<SectionDTO>? sections = await client.sectionApi!.sectionGetAllBeaconsForInstance(instanceId);
|
|
return sections;
|
|
} else {
|
|
return []; // TODO return local list..
|
|
}
|
|
} catch (e) {
|
|
print(e);
|
|
print("getAllSections IN CATCH");
|
|
return [];
|
|
}
|
|
}
|
|
|
|
static Future<bool> downloadAndPushLocalImage(Client client, ContentDTO contentDTO) async {
|
|
try {
|
|
|
|
// Test if already here or not..
|
|
List<Map<String, dynamic>> resourceTest = await DatabaseHelper.instance.queryWithColumnId(DatabaseTableType.resources, contentDTO.resourceId!);
|
|
if(resourceTest.isNotEmpty) {
|
|
return true;
|
|
} else {
|
|
bool isOnline = await hasNetwork();
|
|
if(isOnline) {
|
|
ResourceModel? resourceModel = await downloadImage(client, contentDTO);
|
|
if(resourceModel != null) {
|
|
//await DatabaseHelper.instance.insert(DatabaseTableType.resources, resourceModel.toMap());
|
|
}
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
} catch (e) {
|
|
print(e);
|
|
print("getAndDownloadImage IN CATCH");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
static Future<ResourceModel?> downloadImage(Client client, ContentDTO contentDTO) async { // TODO CHECK wtf mymuseum
|
|
var source = contentDTO.resource!.url;
|
|
//print("SOURCE getAndDownloadImage");
|
|
if(contentDTO.resource!.url != null) {
|
|
if(contentDTO.resource!.url!.contains("localhost:5000")){
|
|
print("Contains localhost:5000");
|
|
source = contentDTO.resource!.url!.replaceAll("http://localhost:5000", client.apiApi!.basePath);
|
|
}
|
|
} else {
|
|
source = "https://api.mymuseum.be/api/Resource/"+contentDTO.resourceId!; // TODO UPDATE ROUTE
|
|
}
|
|
|
|
HttpClient client2 = HttpClient();
|
|
var _downloadData = <int>[];
|
|
final HttpClientRequest request = await client2.getUrl(Uri.parse(source!));
|
|
//print("RESPONSE");
|
|
HttpClientResponse response = await request.close();
|
|
await for(dynamic d in response) { _downloadData.addAll(d); }
|
|
//print("AFTER");
|
|
final base64Str = base64.encode(_downloadData);
|
|
ResourceModel resourceModel = ResourceModel(id: contentDTO.resourceId, source: contentDTO.resource!.url, path: base64Str, type: ResourceType.Image);
|
|
return resourceModel;
|
|
}
|
|
|
|
|
|
static Future<bool> downloadAndPushLocalAudio(Client client, String audioId) async {
|
|
try {
|
|
|
|
// Test if already here or not..
|
|
List<Map<String, dynamic>> resourceTest = await DatabaseHelper.instance.queryWithColumnId(DatabaseTableType.resources, audioId);
|
|
if(resourceTest.isNotEmpty) {
|
|
return true;
|
|
} else {
|
|
bool isOnline = await hasNetwork();
|
|
if(isOnline) {
|
|
ResourceModel? resourceModel = await downloadAudio(client, audioId, resourceTest.first[0]);
|
|
if(resourceModel != null) {
|
|
await DatabaseHelper.instance.insert(DatabaseTableType.resources, resourceModel.toMap());
|
|
}
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
} catch (e) {
|
|
print(e);
|
|
print("getAndDownloadAudio IN CATCH");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
static Future<ResourceModel?> downloadAudio(Client client, String resourceUrl, String audioId) async {
|
|
//var url = "https://api.myinfomate.be/api/Resource/"+audioId; // TO TEST TODO UPDATE ROUTE
|
|
HttpClient client2 = HttpClient();
|
|
var _downloadData = <int>[];
|
|
final HttpClientRequest request = await client2.getUrl(Uri.parse(resourceUrl));
|
|
HttpClientResponse response = await request.close();
|
|
await for(dynamic d in response) { _downloadData.addAll(d); }
|
|
final base64Str = base64.encode(_downloadData);
|
|
ResourceModel resourceModel = ResourceModel(id: audioId, path: base64Str, type: ResourceType.Audio, source: "");
|
|
return resourceModel;
|
|
}
|
|
|
|
static Future<File?> getResource(AppContext appContext, ConfigurationDTO configurationDTO, String imageId) async {
|
|
if((appContext.getContext() as VisitAppContext).configuration == null || (appContext.getContext() as VisitAppContext).configuration!.isOffline!)
|
|
{
|
|
Directory? appDocumentsDirectory = Platform.isIOS ? await getApplicationDocumentsDirectory() : await getDownloadsDirectory();
|
|
String localPath = appDocumentsDirectory!.path;
|
|
Directory configurationDirectory = Directory('$localPath/${configurationDTO.id}');
|
|
List<FileSystemEntity> fileList = configurationDirectory.listSync();
|
|
|
|
if(fileList.any((fileL) => fileL.uri.pathSegments.last.contains(imageId))) {
|
|
File file = File(fileList.firstWhere((fileL) => fileL.uri.pathSegments.last.contains(imageId)).path);
|
|
return file;
|
|
}
|
|
// OFFLINE
|
|
/*List<Map<String, dynamic>> resourceTest = await DatabaseHelper.instance.queryWithColumnId(DatabaseTableType.resources, imageId);
|
|
if(resourceTest.isNotEmpty) {
|
|
return DatabaseHelper.instance.getResourceFromDB(resourceTest.first);
|
|
} else {
|
|
return null;
|
|
}*/
|
|
|
|
|
|
}
|
|
|
|
// ONLINE
|
|
return null;
|
|
}
|
|
|
|
static Future<ExportConfigurationDTO?> exportConfiguration(Client client, String configurationId, String? language) async {
|
|
try {
|
|
bool isOnline = await hasNetwork();
|
|
if(isOnline) {
|
|
ExportConfigurationDTO? exportConfiguration = await client.configurationApi!.configurationExport(configurationId, language: language);
|
|
return exportConfiguration;
|
|
} else {
|
|
return null; // TODO return local list..
|
|
}
|
|
} catch (e) {
|
|
print(e);
|
|
print("exportConfiguration IN CATCH");
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|