This commit is contained in:
J. A. Messias 2024-11-13 14:18:40 -03:00
parent 32c0b96074
commit 6c72e17515
2 changed files with 28 additions and 14 deletions

View File

@ -33,16 +33,24 @@ class SQLiteStorage implements BaseStorage {
_onCreate(Database database, int version) async {
log('Creating database...');
await database.execute(_keychain);
await database.execute(_license);
await initLicense(database);
try {
await database.execute(_keychain);
await database.execute(_license);
await setupLicense();
} catch (e) {
log('Tables keychain and license already exists');
}
}
_onUpgrade(Database database, int oldVersion, int newVersion) async {
log('Upgrading database from version $oldVersion to $newVersion...');
if (oldVersion < 2) {
await database.execute(_license);
await initLicense(database);
try {
await database.execute(_license);
await setupLicense();
} catch (e) {
log('Table license already exists');
}
}
}
@ -69,7 +77,7 @@ class SQLiteStorage implements BaseStorage {
);
''';
static Future<void> initLicense(Database database) async {
static Future<void> setupLicense() async {
log('initLicense()');
for (var element in LicenseStorageKey.values.map((e) => e.value)) {
await database.insert(

View File

@ -8,7 +8,6 @@ import 'package:hub/shared/helpers/sqlite_storage.dart';
import 'dart:convert';
import 'package:hub/shared/helpers/storage_helper.dart';
import 'package:sqflite/sqflite.dart';
class LicenseService {
static final LicenseService _instance = LicenseService._internal();
@ -37,16 +36,22 @@ class LicenseService {
static Future<void> fetchLicenses() async {
try {
log('Obtendo licenças...');
final ApiCallResponse response = await PhpGroup.testCall();
final List<dynamic> responseBody = response.jsonBody;
final ApiCallResponse response = await PhpGroup.getLicense();
final dynamic responseBody = response.jsonBody;
log('Licenças obtidas: $responseBody');
if (responseBody == []) {
// throw Exception('Licensa ausente ou vazia.');
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 (response.jsonBody is! List<dynamic> && response.jsonBody['error'] != null) {
final String error = response.jsonBody['error_msg'] ?? 'Erro desconhecido';
throw Exception('Erro ao consultar licenças: $error');
if (responseBody == []) {
await SQLiteStorage.setupLicense();
}
for (var element in responseBody) {
@ -97,6 +102,7 @@ class LicenseService {
}
} catch (e, s) {
log('Erro ao obter licença', error: e, stackTrace: s);
await SQLiteStorage.setupLicense();
}
}