// 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/pages/about_property_page/about_property_model.dart'; import 'package:hub/pages/about_property_page/about_property_screen.dart'; import 'package:hub/shared/helpers/database/database_helper.dart'; import 'package:hub/shared/helpers/license/license_helper.dart'; import 'package:rxdart/rxdart.dart'; import 'package:sqflite/sqflite.dart'; class LicenseService { static final LicenseService _instance = LicenseService._internal(); factory LicenseService() => _instance; LicenseService._internal(); final _licenseSubject = BehaviorSubject>(); Stream> get licenseStream => _licenseSubject.stream; Future processLicense() async { if (body['key'] == Module.pets.value && body['display'] == 'VISIVEL') { await LicenseHelper().s(Module.petsHistory.value, body); } final bool isAboutProperty = AboutPropertyModules.values.contains(body['key']); final bool isVisible = body['display'] == 'VISIVEL'; log('contains () => ${body['key']} - $isAboutProperty'); if (isAboutProperty && isVisible) { await LicenseHelper().s(Module.aboutProperty.value, body); } } Future cleanLicense() async { _licenseSubject.add([]); } Future setupLicense(Database database, bool isNewVersion) async { log('(B) => license'); final List inactiveModuleKey = InactiveModuleKey.values.map((e) => e.value).toList(); final List activeModuleKey = ActiveModuleKey.values.map((e) => e.value).toList(); final List disabledModuleKey = DisabledModuleKey.values.map((e) => e.value).toList(); await LicenseHelper().setByKey(inactiveModuleKey, 'INVISIVEL'); await LicenseHelper().setByKey(activeModuleKey, 'VISIVEL'); if (isNewVersion == true) { await LicenseHelper().setByKey(disabledModuleKey, 'VISIVEL'); await LicenseHelper().setByKey([Module.aboutProperty.value], 'VISIVEL'); } else { await LicenseHelper().setByKey(disabledModuleKey, 'DESABILITADO'); } _licenseSubject.add([...activeModuleKey]); } Future fetchLicenses(bool isNewVersion) async { log('(A) => license'); 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) { late final String error; if (response.jsonBody is Map) error = response.jsonBody['error_msg']; else error = 'Erro desconhecido'; throw Exception('Erro ao consultar licenças: $error'); } if (responseBody == []) { await setupLicense(DatabaseStorage.database, isNewVersion); _licenseSubject.add([]); return false; } for (var element in responseBody) { if (licenseContainsKey(element['key'])) { _saveModule(element); } } _licenseSubject.add(responseBody); return true; } catch (e) { log('Erro ao obter licenças: $e'); await setupLicense(DatabaseStorage.database, isNewVersion); return false; } } static bool licenseContainsKey(final String key) { return Module.values.map((e) => e.value).toList().contains(key); } static Future _saveModule(final dynamic body) async { if (body is! Map) return; log('Salvando módulo: ${body.toString()}'); await LicenseHelper().s(body['key'], body); } }