diff --git a/lib/shared/helpers/sqlite_storage.dart b/lib/shared/helpers/sqlite_storage.dart index 9c4fd052..7f3261fa 100644 --- a/lib/shared/helpers/sqlite_storage.dart +++ b/lib/shared/helpers/sqlite_storage.dart @@ -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 initLicense(Database database) async { + static Future setupLicense() async { log('initLicense()'); for (var element in LicenseStorageKey.values.map((e) => e.value)) { await database.insert( diff --git a/lib/shared/services/license/license_service.dart b/lib/shared/services/license/license_service.dart index 4b562a09..01bb6f9f 100644 --- a/lib/shared/services/license/license_service.dart +++ b/lib/shared/services/license/license_service.dart @@ -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 fetchLicenses() async { try { log('Obtendo licenças...'); - final ApiCallResponse response = await PhpGroup.testCall(); - final List 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) { + 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 (response.jsonBody is! List && 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(); } }