96 lines
3.0 KiB
Dart
96 lines
3.0 KiB
Dart
// ignore_for_file: curly_braces_in_flow_control_structures, prefer_is_empty
|
|
|
|
import 'dart:developer';
|
|
|
|
import 'package:hub/backend/api_requests/api_calls.dart';
|
|
import 'package:hub/shared/helpers/database/database_helper.dart';
|
|
|
|
import 'package:hub/shared/helpers/license/license_helper.dart';
|
|
import 'package:hub/shared/helpers/storage/base_storage.dart';
|
|
import 'package:hub/shared/helpers/storage/storage_helper.dart';
|
|
|
|
import 'package:sqflite/sqflite.dart';
|
|
|
|
class LicenseService {
|
|
static final LicenseService _instance = LicenseService._internal();
|
|
factory LicenseService() => _instance;
|
|
LicenseService._internal();
|
|
|
|
static Future<void> setupLicense(Database database) async {
|
|
await LicenseHelper.insertLicenseFoo(
|
|
InactiveModuleKey.values.map((e) => e.value).toList(), 'INVISIVEL');
|
|
await LicenseHelper.insertLicenseFoo(
|
|
ActiveModuleKey.values.map((e) => e.value).toList(), 'VISIVEL');
|
|
}
|
|
|
|
static Future<void> initLicenseService() async {
|
|
// for (var element in LicenseStorageKey.values.map((e) => e.value)) {
|
|
// await SQLiteStorage.database.insert(
|
|
// SQLiteStorage.tableLicense,
|
|
// {
|
|
// 'key': element,
|
|
// 'display': 'VISIVEL',
|
|
// 'expirationDate': '',
|
|
// 'startDate': '',
|
|
// 'quantity': '',
|
|
// },
|
|
// conflictAlgorithm: ConflictAlgorithm.ignore,
|
|
// );
|
|
// }
|
|
}
|
|
|
|
static Future<void> processLicense() async {}
|
|
|
|
static Future<bool> fetchLicenses() async {
|
|
try {
|
|
log('Obtendo licenças...');
|
|
final response = await PhpGroup.getLicense();
|
|
final dynamic responseBody = response.jsonBody;
|
|
log('Licenças obtidas: $responseBody');
|
|
|
|
if (response.jsonBody is! List<dynamic>) {
|
|
late final String error;
|
|
if (response.jsonBody is Map<String, dynamic>)
|
|
error = response.jsonBody['error_msg'];
|
|
else
|
|
error = 'Erro desconhecido';
|
|
|
|
throw Exception('Erro ao consultar licenças: $error');
|
|
}
|
|
|
|
if (responseBody == []) {
|
|
await setupLicense(DatabaseStorage.database);
|
|
return true;
|
|
}
|
|
|
|
for (var element in responseBody) {
|
|
if (licenseContainsKey(element['key'])) {
|
|
_saveModule(element);
|
|
}
|
|
}
|
|
return true;
|
|
} catch (e) {
|
|
log('Erro ao obter licenças: $e');
|
|
await setupLicense(DatabaseStorage.database);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
static bool licenseContainsKey(final String key) {
|
|
return Module.values.map((e) => e.value).toList().contains(key);
|
|
}
|
|
|
|
static Future<void> _saveModule(final dynamic body) async {
|
|
if (body is Map<String, dynamic>)
|
|
log('Salvando módulo: ${body.toString()}');
|
|
// if (body is Map<String, dynamic>) await StorageHelper().s(body['key'], '');
|
|
if (body is Map<String, dynamic>)
|
|
await StorageHelper().s(body['key'], body);
|
|
// StorageHelper.getInstance(Storage.SQLiteStorage).set(key, value);
|
|
}
|
|
|
|
static Future<void> updateLicense(final dynamic body) async {}
|
|
|
|
static Future<dynamic> catchLicense() async {}
|
|
}
|