161 lines
5.6 KiB
Dart
161 lines
5.6 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/app_context.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 downloadAndPushLocalImage(client, ImageDTO(source_: configuration.imageSource, resourceId: 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<bool> downloadAndPushLocalImage(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) {
|
|
ResourceModel? resourceModel = await downloadImage(client, imageDTO);
|
|
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, ImageDTO imageDTO) async {
|
|
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, source: imageDTO.source_, data: 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);
|
|
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 audioId) async {
|
|
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, source: "");
|
|
return resourceModel;
|
|
}
|
|
|
|
static Future<ResourceModel?> getResource(AppContext appContext, String imageId) async {
|
|
if((appContext.getContext() as VisitAppContext).configuration == null || (appContext.getContext() as VisitAppContext).configuration!.isOffline!)
|
|
{
|
|
// 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;
|
|
}
|
|
} else
|
|
{
|
|
// ONLINE
|
|
return ResourceModel(); // To mock
|
|
}
|
|
}
|
|
}
|
|
|