This commit is contained in:
J. A. Messias 2025-01-06 10:01:24 -03:00
parent 8478d0d372
commit 8ae8ce849f
7 changed files with 90 additions and 57 deletions

View File

@ -18,7 +18,11 @@ const _kPrivateApiFunctionName = 'ffPrivateApiCall';
/// Start PHP Group Code
class PhpGroup {
abstract class Api {
GetLicense getLicense = GetLicense();
}
class PhpGroup extends Api {
static String getBaseUrl() => 'https://freaccess.com.br/freaccess';
static Map<String, String> headers = {};
static LoginCall loginCall = LoginCall();
@ -62,7 +66,7 @@ class PhpGroup {
static GetResidentsByProperty getResidentsByProperty =
GetResidentsByProperty();
static GetOpenedVisits getOpenedVisits = GetOpenedVisits();
static GetLicense getLicense = GetLicense();
GetLicense getLicense = GetLicense();
static GetProvSchedules getProvSchedules = GetProvSchedules();
}

View File

@ -2,6 +2,7 @@ import 'dart:developer';
import 'package:hub/features/menu/index.dart';
import 'package:hub/features/module/index.dart';
import 'package:hub/features/storage/index.dart';
import 'package:hub/flutter_flow/custom_functions.dart';
class MenuRepositoryImpl implements MenuRepository {
@ -55,7 +56,7 @@ class MenuRepositoryImpl implements MenuRepository {
if (await _shouldUpdateDisplay(module, isNewVersion)) {
final displayValue =
module['display'] == EnumDisplay.active ? 'VISIVEL' : 'INVISIVEL';
await LicenseLocalDataSourceImpl()
await LicenseLocalDataSourceImpl(DatabaseService.database)
.setDisplayByKey(['FRE-HUB-ABOUT-PROPERTY'], displayValue);
return EnumDisplay.fromString(displayValue);
}

View File

@ -1,6 +1,7 @@
import 'dart:async';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hub/features/backend/index.dart';
import 'package:hub/features/menu/index.dart';
import 'package:hub/features/module/index.dart';
import 'package:hub/features/local/index.dart';
@ -36,7 +37,7 @@ class MenuBloc extends Bloc<MenuEvent, MenuState> {
}
Future<void> _onMenuEvent(MenuEvent event, Emitter<MenuState> emit) async {
await LicenseRemoteDataSourceImpl().waitForSaveCompletion();
await LicenseRemoteDataSourceImpl(PhpGroup()).waitForSaveCompletion();
final List<MenuItem?> newEntries =
await MenuRepositoryImpl().generateMenuEntries(entries, item);

View File

@ -1,6 +1,9 @@
import 'dart:developer';
import 'package:hub/features/module/index.dart';
import 'package:hub/features/storage/index.dart';
import 'package:hub/shared/extensions/string_extensions.dart';
import 'package:sqflite/sqflite.dart';
abstract class LicenseLocalDataSource {
Future<void> init();
@ -9,13 +12,22 @@ abstract class LicenseLocalDataSource {
Future<String?> get(String key);
Future<void> set<T>(String key, T value);
Future<void> del(String key);
Future<bool> setupLicense(bool isNewVersion);
Future<void> clear();
}
class LicenseLocalDataSourceImpl implements LicenseLocalDataSource {
late final Database database;
static final LicenseLocalDataSourceImpl _instance =
LicenseLocalDataSourceImpl._internal();
factory LicenseLocalDataSourceImpl() => _instance;
factory LicenseLocalDataSourceImpl(Database database) {
_instance.database = database;
return _instance;
}
LicenseLocalDataSourceImpl._internal();
@override
@ -23,6 +35,38 @@ class LicenseLocalDataSourceImpl implements LicenseLocalDataSource {
await DatabaseService.instance.init();
}
@override
Future<bool> setupLicense(bool isNewVersion) async {
log('Setting up license...');
try {
final License license = await License.getLicense(isNewVersion);
final List<String> inactiveModuleKey = license.modules
.where((module) => module.display == ModuleStatus.inactive.key)
.map((module) => module.key)
.toList();
final List<String> activeModuleKey = license.modules
.where((module) => module.display == ModuleStatus.active.key)
.map((module) => module.key)
.toList();
final List<String> disabledModuleKey = license.modules
.where((module) => module.display == ModuleStatus.disabled.key)
.map((module) => module.key)
.toList();
await setDisplayByKey(inactiveModuleKey, 'INVISIVEL');
await setDisplayByKey(activeModuleKey, 'VISIVEL');
await setDisplayByKey(disabledModuleKey, 'DESABILITADO');
return true;
} catch (e, s) {
log('Erro ao configurar licenças: $e', stackTrace: s);
return false;
}
}
@override
Future<void> setDisplayByKey(
final List<String> keys, final String display) async {
@ -48,8 +92,8 @@ class LicenseLocalDataSourceImpl implements LicenseLocalDataSource {
@override
Future<String?> get(String key) async {
var response = await DatabaseService.database
.query(tableLicense, where: 'key = ?', whereArgs: [key]);
var response =
await database.query(tableLicense, where: 'key = ?', whereArgs: [key]);
if (response.isEmpty) {
return null;
}
@ -63,7 +107,7 @@ class LicenseLocalDataSourceImpl implements LicenseLocalDataSource {
if (value.isEmpty) return;
if (key == 'null') return;
await DatabaseService.database.rawInsert(
await database.rawInsert(
'INSERT OR REPLACE INTO $tableLicense (key, display, expirationDate, startDate, quantity) VALUES (?, ?, ?, ?, ?)',
[
key,
@ -78,12 +122,11 @@ class LicenseLocalDataSourceImpl implements LicenseLocalDataSource {
@override
Future<void> del(String key) async {
await DatabaseService.database
.delete(tableLicense, where: 'key = ?', whereArgs: [key]);
await database.delete(tableLicense, where: 'key = ?', whereArgs: [key]);
}
@override
Future<void> clear() async {
await DatabaseService.database.delete(tableLicense);
await database.delete(tableLicense);
}
}

View File

@ -7,19 +7,23 @@ import 'package:hub/features/backend/index.dart';
import 'package:hub/features/local/data/repositories/locals_repository_impl.dart';
import 'package:hub/features/module/index.dart';
import 'package:sqflite/sqflite.dart';
abstract class LicenseRemoteDataSource {
Future<bool> fetchLicenses(bool isNewVersion);
Future<void> cleanLicense();
Future<void> processLicense();
Future<bool> setupLicense(Database database, bool isNewVersion);
}
class LicenseRemoteDataSourceImpl implements LicenseRemoteDataSource {
late final Api api;
static final LicenseRemoteDataSourceImpl _instance =
LicenseRemoteDataSourceImpl._internal();
factory LicenseRemoteDataSourceImpl() => _instance;
factory LicenseRemoteDataSourceImpl(Api api) {
_instance.api = api;
return _instance;
}
LicenseRemoteDataSourceImpl._internal();
@override
@ -30,46 +34,11 @@ class LicenseRemoteDataSourceImpl implements LicenseRemoteDataSource {
LocalsRepositoryImpl.license.add(false);
}
@override
Future<bool> setupLicense(Database database, bool isNewVersion) async {
log('Setting up license...');
try {
final License license = await License.getLicense(isNewVersion);
final List<String> inactiveModuleKey = license.modules
.where((module) => module.display == ModuleStatus.inactive.key)
.map((module) => module.key)
.toList();
final List<String> activeModuleKey = license.modules
.where((module) => module.display == ModuleStatus.active.key)
.map((module) => module.key)
.toList();
final List<String> disabledModuleKey = license.modules
.where((module) => module.display == ModuleStatus.disabled.key)
.map((module) => module.key)
.toList();
await LicenseLocalDataSourceImpl()
.setDisplayByKey(inactiveModuleKey, 'INVISIVEL');
await LicenseLocalDataSourceImpl()
.setDisplayByKey(activeModuleKey, 'VISIVEL');
await LicenseLocalDataSourceImpl()
.setDisplayByKey(disabledModuleKey, 'DESABILITADO');
return true;
} catch (e, s) {
log('Erro ao configurar licenças: $e', stackTrace: s);
return false;
}
}
@override
Future<bool> fetchLicenses(bool isNewVersion) async {
log('Fetching licenses...');
try {
final response = await PhpGroup.getLicense();
final response = await _instance.api.getLicense();
final dynamic responseBody = response.jsonBody;
if (responseBody == []) {

View File

@ -1,5 +1,6 @@
import 'dart:developer';
import 'package:hub/features/backend/index.dart';
import 'package:hub/features/module/data/data_sources/index.dart';
import 'package:hub/features/module/domain/index.dart';
import 'package:hub/features/storage/index.dart';
@ -8,19 +9,33 @@ import 'package:sqflite/sqflite.dart';
typedef LicenseStatus = bool;
class LicenseRepositoryImpl implements LicenseRepository {
final LicenseLocalDataSource localDataSource = LicenseLocalDataSourceImpl();
final LicenseRemoteDataSource remoteDataSource =
LicenseRemoteDataSourceImpl();
final Database database = DatabaseService.database;
late final Database database;
late final PhpGroup api;
late final LicenseLocalDataSource localDataSource;
late final LicenseRemoteDataSource remoteDataSource;
LicenseRepositoryImpl();
LicenseRepositoryImpl([Database? database, PhpGroup? api]) {
if (database != null) {
this.database = database;
} else {
this.database = DatabaseService.database;
}
if (api != null) {
this.api = api;
} else {
this.api = PhpGroup();
}
localDataSource = LicenseLocalDataSourceImpl(this.database);
remoteDataSource = LicenseRemoteDataSourceImpl(this.api);
}
@override
Future<bool> updateLicense() async {
log('updateLicense');
bool result = false;
final bool isNewVersion = await localDataSource.isNewVersion();
result = await remoteDataSource.setupLicense(database, isNewVersion);
result = await localDataSource.setupLicense(isNewVersion);
if (isNewVersion) {
result = await remoteDataSource.fetchLicenses(isNewVersion);
}