flutter-freaccess-hub/lib/shared/services/license/license_service.dart

104 lines
3.6 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/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<List<dynamic>>();
Stream<List<dynamic>> get licenseStream => _licenseSubject.stream;
Future<void> 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<void> cleanLicense() async {
_licenseSubject.add([]);
}
Future<void> setupLicense(Database database, bool isNewVersion) async {
log('(B) => license');
final List<String> inactiveModuleKey = InactiveModuleKey.values.map((e) => e.value).toList();
final List<String> activeModuleKey = ActiveModuleKey.values.map((e) => e.value).toList();
final List<String> 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<bool> 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<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, 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<void> _saveModule(final dynamic body) async {
if (body is! Map<String, dynamic>) return;
log('Salvando módulo: ${body.toString()}');
await LicenseHelper().s(body['key'], body);
}
}