138 lines
4.8 KiB
Dart
138 lines
4.8 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:manager_api/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/client.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 getAndDownloadImage(client, ImageDTO(source_: configuration.imageSource, resourceId: configuration.imageId));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
return [];
|
|
}
|
|
|
|
return configurations;
|
|
|
|
} catch (e) {
|
|
print(e);
|
|
print("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("IN CATCH");
|
|
return [];
|
|
}
|
|
}
|
|
|
|
static Future<bool> getAndDownloadImage(Client client, ImageDTO imageDTO) async {
|
|
try {
|
|
|
|
// Test if already here or not..
|
|
List<Map<String, dynamic>> resourceTest = await DatabaseHelper.instance.queryWithColumnId(DatabaseTableType.resources, imageDTO.resourceId!);
|
|
if(resourceTest.isNotEmpty) {
|
|
return true;
|
|
} else {
|
|
bool isOnline = await hasNetwork();
|
|
if(isOnline) {
|
|
var source = imageDTO.source_;
|
|
//print("SOURCE getAndDownloadImage");
|
|
//print(source);
|
|
if(imageDTO.source_!.contains("localhost:5000")) {
|
|
print("Contains localhost:5000");
|
|
source = imageDTO.source_!.replaceAll("http://localhost:5000", client.apiApi!.basePath);
|
|
}
|
|
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: imageDTO.resourceId, data: base64Str, type: ResourceType.Image);
|
|
await DatabaseHelper.instance.insert(DatabaseTableType.resources, resourceModel.toMap());
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
} catch (e) {
|
|
print(e);
|
|
print("IN CATCH");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
static Future<bool> getAndDownloadAudio(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) {
|
|
var url = "https://api.mymuseum.be/api/Resource/"+audioId; // TO TEST TODO UPDATE ROUTE
|
|
HttpClient client2 = HttpClient();
|
|
var _downloadData = <int>[];
|
|
final HttpClientRequest request = await client2.getUrl(Uri.parse(url));
|
|
HttpClientResponse response = await request.close();
|
|
await for(dynamic d in response) { _downloadData.addAll(d); }
|
|
final base64Str = base64.encode(_downloadData);
|
|
ResourceModel resourceModel = ResourceModel(id: audioId, data: base64Str, type: ResourceType.Audio);
|
|
await DatabaseHelper.instance.insert(DatabaseTableType.resources, resourceModel.toMap());
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
} catch (e) {
|
|
print(e);
|
|
print("IN CATCH");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
static Future<ResourceModel?> getResource(String imageId) async {
|
|
List<Map<String, dynamic>> resourceTest = await DatabaseHelper.instance.queryWithColumnId(DatabaseTableType.resources, imageId);
|
|
if(resourceTest.isNotEmpty) {
|
|
return DatabaseHelper.instance.getResourceFromDB(resourceTest.first);
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|