75 lines
2.2 KiB
Dart
75 lines
2.2 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 'dart:convert';
|
|
|
|
class LicenseService {
|
|
static final LicenseService _instance = LicenseService._internal();
|
|
|
|
factory LicenseService() => _instance;
|
|
LicenseService._internal();
|
|
|
|
List<Module> _modules = [];
|
|
|
|
List<Module> get modules => List.unmodifiable(_modules);
|
|
|
|
Future<void> initLicenseService() async {
|
|
await _fetchLicenses();
|
|
}
|
|
|
|
Future<void> _fetchLicenses() async {
|
|
try {
|
|
log('Obtendo licenças...');
|
|
final ApiCallResponse response = await PhpGroup.testCall();
|
|
final String responseBody = response.jsonBody.toString();
|
|
|
|
if (responseBody.isEmpty) {
|
|
throw Exception('Licensa ausente ou vazia.');
|
|
}
|
|
|
|
// if (responseBody == "[]") {
|
|
// log('Nenhuma licença encontrada.');
|
|
// _modules = [];
|
|
// log('() MOD => $_modules');
|
|
// return;
|
|
// }
|
|
|
|
final dynamic jsonBody = jsonDecode(responseBody);
|
|
|
|
if (jsonBody is Map && jsonBody['error'] == true) {
|
|
final String error = jsonBody['error_msg'] ?? 'Erro desconhecido';
|
|
throw Exception('Erro ao consultar licenças: $error');
|
|
}
|
|
|
|
final bool isList = jsonBody is List;
|
|
final bool isMap = jsonBody is Map<String, dynamic>;
|
|
final dynamic license = isList
|
|
? (jsonBody).map<Module>((data) => Module.fromJson(data as Map<String, dynamic>)).toList()
|
|
: isMap
|
|
? [Module.fromJson(jsonBody)]
|
|
: [];
|
|
log('Modulos obtidos: $license');
|
|
if (license is List<Module> && license.isNotEmpty && license != _modules) {
|
|
_modules = license;
|
|
log('Licença obtida com sucesso');
|
|
log('$_modules');
|
|
} else if (license is List<Module> && license.isEmpty) {
|
|
throw Exception('Nenhum módulo encontrado.');
|
|
}
|
|
} catch (e, s) {
|
|
log('Erro ao obter licença', error: e, stackTrace: s);
|
|
}
|
|
}
|
|
|
|
Future<void> checkLicense() async {
|
|
log('Verificando licença...');
|
|
await _fetchLicenses();
|
|
}
|
|
|
|
Future<void> catchLicense() async {
|
|
await PhpGroup.getLicense();
|
|
}
|
|
}
|