From c6b20fd608f6e0c3a3aecde26727054ef910ea3a Mon Sep 17 00:00:00 2001 From: "J. A. Messias" Date: Fri, 27 Sep 2024 14:15:36 -0300 Subject: [PATCH] fix sqlite and pets --- .../card_item_template_component_widget.dart | 2 +- lib/main.dart | 17 +-- lib/pages/pets_page/pets_page_model.dart | 31 +++--- lib/pages/pets_page/pets_page_widget.dart | 102 ++++-------------- lib/shared/helpers/secure_storage_helper.dart | 21 ++-- lib/shared/helpers/sqlite_storage_helper.dart | 25 +---- lib/shared/utils/storage_util.dart | 33 ++++-- 7 files changed, 84 insertions(+), 147 deletions(-) diff --git a/lib/components/templates_components/card_item_template_component/card_item_template_component_widget.dart b/lib/components/templates_components/card_item_template_component/card_item_template_component_widget.dart index 4cb84ecc..0c36f135 100644 --- a/lib/components/templates_components/card_item_template_component/card_item_template_component_widget.dart +++ b/lib/components/templates_components/card_item_template_component/card_item_template_component_widget.dart @@ -98,7 +98,7 @@ class _CardItemTemplateComponentWidgetState } Widget _generateImage() { - CachedNetworkImage.evictFromCache(widget.imagePath ?? ''); + // CachedNetworkImage.evictFromCache(widget.imagePath ?? ''); return ClipRRect( borderRadius: BorderRadius.circular(20), child: CachedNetworkImage( diff --git a/lib/main.dart b/lib/main.dart index 20bdcc59..9fa5007c 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -20,16 +20,14 @@ import 'package:responsive_framework/responsive_framework.dart'; final GlobalKey navigatorKey = GlobalKey(); - - void main() async { await _initializeApp(); runApp(const App()); } - Future _initializeApp() async { WidgetsFlutterBinding.ensureInitialized(); + await _initializeStorageHelpers(); await _initializeTracking(); await _initializeFirebase(); @@ -39,15 +37,9 @@ Future _initializeApp() async { await _initializeFlutterFlow(); } - - - - Future _initializeStorageHelpers() async { await StorageUtil().ensureInitialization(); - - - + // StorageUtil().isLogged = false; } Future _initializeTracking() async { @@ -156,7 +148,8 @@ class _AppState extends State { ), ); - final Iterable>? localizationsDelegates = const [ + final Iterable>? localizationsDelegates = + const [ FFLocalizationsDelegate(), GlobalMaterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate, @@ -181,7 +174,6 @@ class _AppState extends State { ); }; - @override void initState() { super.initState(); @@ -201,7 +193,6 @@ class _AppState extends State { _backgroundHandleMessage(message); } }); - } void setLocale(String language) { diff --git a/lib/pages/pets_page/pets_page_model.dart b/lib/pages/pets_page/pets_page_model.dart index 70885f03..709caf4c 100644 --- a/lib/pages/pets_page/pets_page_model.dart +++ b/lib/pages/pets_page/pets_page_model.dart @@ -344,6 +344,7 @@ class PetsPageModel extends FlutterFlowModel { enText: 'Pet registration limit reached', ptText: 'Limite de cadastro de pets atingido', ); + DialogUtil.error(buildContext!, errorMessage); } else { DialogUtil.errorDefault(buildContext!); } @@ -367,22 +368,28 @@ class PetsPageModel extends FlutterFlowModel { clearFields(); } - void clearFields() { + Future clearFields() async { uploadedLocalFile = null; - textControllerName?.clear(); - textControllerName?.clear(); - textControllerSpecies?.clear(); - textControllerRace?.clear(); - textControllerColor?.clear(); - textControllerData?.clear(); - textControllerObservation?.clear(); + textControllerName = TextEditingController(text: ''); + textControllerSpecies = TextEditingController(text: ''); + textControllerRace = TextEditingController(text: ''); + textControllerColor = TextEditingController(text: ''); + textControllerObservation = TextEditingController(text: ''); - dropDownValue1 = null; - dropDownValue2 = null; + // textControllerData = TextEditingController( + // text: dateTimeFormat( + // 'dd/MM/yyyy', + // DateTime.now(), + // )); - dropDownValueController1 = FormFieldController(null); - dropDownValueController2 = FormFieldController(null); + // dropDownValue1 = ''; + // dropDownValue2 = ''; + + // dropDownValueController1 = + // FormFieldController('Selecione uma opção'); + // dropDownValueController2 = + // FormFieldController('Selecione uma opção'); } Widget buildPetDetails({ diff --git a/lib/pages/pets_page/pets_page_widget.dart b/lib/pages/pets_page/pets_page_widget.dart index 48834d3a..8351f33f 100644 --- a/lib/pages/pets_page/pets_page_widget.dart +++ b/lib/pages/pets_page/pets_page_widget.dart @@ -2,7 +2,6 @@ import 'dart:convert'; import 'package:flutter/material.dart'; - import 'package:google_fonts/google_fonts.dart'; import 'package:http/http.dart'; @@ -47,80 +46,22 @@ class _PetsPageWidgetState extends State _model.updateOnChange = true; _model.onUpdatePet = () { - safeSetState(() { - _model.clearFields(); + safeSetState(() async { + await _model.clearFields(); }); }; _model.onRegisterPet = () { - safeSetState(() { - _model.clearFields(); + safeSetState(() async { + await _model.clearFields().then((_) => safeSetState(() {})); }); }; _model.safeSetState = () { safeSetState(() {}); }; + _model.updateImage = () => setState(() { _model.handleUploadComplete(_model.uploadedTempFile!); }); - - if (widget.pet != null) { - int petId = widget.pet['id']; - _model.petId = petId; - - (() async { - Response response = await get(Uri.parse( - 'https://freaccess.com.br/freaccess/getImage.php?devUUID=${_model.devUUID}&userUUID=${_model.userUUID}&cliID=${_model.cliUUID}&atividade=consultaFotoPet&petId=$petId')); - String base64 = base64Encode(response.bodyBytes); - FFUploadedFile uploadedFile = - await ImageUtils.convertToUploadFile(base64); - setState(() { - _model.handleUploadComplete(uploadedFile); - }); - })(); - } - - _model.textControllerName ??= TextEditingController( - text: widget.pet != null ? widget.pet['name'] ?? '' : ''); - _model.textFieldFocusName ??= FocusNode(); - - _model.textControllerSpecies ??= TextEditingController( - text: widget.pet != null ? widget.pet['species'] ?? '' : ''); - _model.textFieldFocusSpecies ??= FocusNode(); - - _model.textControllerRace ??= TextEditingController( - text: widget.pet != null ? widget.pet['breed'] ?? '' : ''); - _model.textFieldFocusRace ??= FocusNode(); - - _model.textControllerColor ??= TextEditingController( - text: widget.pet != null ? widget.pet['color'] ?? '' : ''); - _model.textFieldFocusColor ??= FocusNode(); - - _model.textControllerData ??= TextEditingController( - text: widget.pet != null - ? widget.pet['birthdayDate'] != null - ? ValidatorUtil.formatDateTimePicker(widget.pet['birthdayDate']) - : '' - : ''); - - _model.textFieldFocusData ??= FocusNode(); - - _model.textControllerObservation ??= TextEditingController( - text: widget.pet != null ? widget.pet['notes'] ?? '' : ''); - _model.textFieldFocusObservation ??= FocusNode(); - - widget.pet != null - ? _model.dropDownValue1 ??= widget.pet['gender'] ?? '' - : _model.dropDownValue1 ??= ''; - - widget.pet != null - ? _model.dropDownValue2 ??= widget.pet['size'] ?? '' - : _model.dropDownValue2 ??= ''; - - _model.dropDownValueController1 ??= - FormFieldController(_model.dropDownValue1 ??= ''); - - _model.dropDownValueController2 ??= - FormFieldController(_model.dropDownValue2 ??= ''); } @override @@ -228,23 +169,20 @@ class _PetsPageWidgetState extends State onChanged: (value) => setState(() {}), maxLength: 80, ), - Padding( - padding: const EdgeInsets.fromLTRB(0, 0, 0, 15), - child: CustomInputUtil( - controller: _model.textControllerSpecies, - validator: _model.textControllerSpeciesValidator - .asValidator(context), - focusNode: _model.textFieldFocusSpecies, - labelText: FFLocalizations.of(context).getVariableText( - ptText: 'Espécie', enText: 'Species'), - hintText: FFLocalizations.of(context).getVariableText( - ptText: 'Ex: Cachorro, Gato, Papagaio', - enText: 'e.g. Dog, Cat, Parrot'), - suffixIcon: Symbols.sound_detection_dog_barking, - haveMaxLength: true, - onChanged: (value) => setState(() {}), - maxLength: 80, - ), + CustomInputUtil( + controller: _model.textControllerSpecies, + validator: _model.textControllerSpeciesValidator + .asValidator(context), + focusNode: _model.textFieldFocusSpecies, + labelText: FFLocalizations.of(context).getVariableText( + ptText: 'Espécie', enText: 'Species'), + hintText: FFLocalizations.of(context).getVariableText( + ptText: 'Ex: Cachorro, Gato, Papagaio', + enText: 'e.g. Dog, Cat, Parrot'), + suffixIcon: Symbols.sound_detection_dog_barking, + haveMaxLength: true, + onChanged: (value) => setState(() {}), + maxLength: 80, ), Padding( padding: const EdgeInsets.fromLTRB(0, 0, 0, 15), @@ -562,6 +500,7 @@ class _PetsPageWidgetState extends State changed: (val) => safeSetState(() { _model.dropDownValue2 = val; }), + dropDownValue: _model.dropDownValue2, optionsLabel: [ FFLocalizations.of(context).getVariableText( ptText: 'Mini', enText: 'Mini'), @@ -1029,6 +968,7 @@ class _PetsPageWidgetState extends State changed: (val) => safeSetState(() { _model.dropDownValue2 = val; }), + dropDownValue: _model.dropDownValue1, optionsLabel: [ FFLocalizations.of(context).getVariableText( ptText: 'Mini', enText: 'Mini'), diff --git a/lib/shared/helpers/secure_storage_helper.dart b/lib/shared/helpers/secure_storage_helper.dart index 5a996810..0845b698 100644 --- a/lib/shared/helpers/secure_storage_helper.dart +++ b/lib/shared/helpers/secure_storage_helper.dart @@ -14,24 +14,24 @@ class SecureStorageHelper extends ChangeNotifier implements Storage { static SecureStorageHelper get instance => _instance; - - - - Future setAndCacheString(String key, String value, Function(String) cacheSetter) async { + Future setAndCacheString( + String key, String value, Function(String) cacheSetter) async { log('setAndCacheString value for key: $key to $value'); await _secureStorage.write(key: key, value: value); CacheUtil.instance.set(key, value); cacheSetter(value); } - Future setAndCacheBool(String key, bool value, Function(bool) cacheSetter) async { + Future setAndCacheBool( + String key, bool value, Function(bool) cacheSetter) async { log('setAndCacheBool value for key: $key to $value'); await _secureStorage.write(key: key, value: value.toString()); CacheUtil.instance.set(key, value); cacheSetter(value); } - Future setAndCacheObject(String key, String value, Function(String) cacheSetter) async { + Future setAndCacheObject( + String key, String value, Function(String) cacheSetter) async { log('setAndCacheObject value for key: $key to $value'); await _secureStorage.write(key: key, value: value); CacheUtil.instance.set(key, value); @@ -69,7 +69,8 @@ class SecureStorageHelper extends ChangeNotifier implements Storage { } @override - Future set(String key, dynamic value, Function(dynamic) cacheSetter) async { + Future set( + String key, dynamic value, Function(dynamic) cacheSetter) async { if (value is String) { await setAndCacheString(key, value, cacheSetter); } else if (value is bool) { @@ -102,10 +103,8 @@ class SecureStorageHelper extends ChangeNotifier implements Storage { } Future purge() async { - log('purge values'); + log('Purging secure storage'); await _secureStorage.deleteAll(); CacheUtil.instance.clear(); } - - -} \ No newline at end of file +} diff --git a/lib/shared/helpers/sqlite_storage_helper.dart b/lib/shared/helpers/sqlite_storage_helper.dart index d008d7ed..402413ba 100644 --- a/lib/shared/helpers/sqlite_storage_helper.dart +++ b/lib/shared/helpers/sqlite_storage_helper.dart @@ -50,8 +50,6 @@ class SQLiteStorageHelper implements Storage { static SQLiteStorageHelper get instance => _instance; - - Future get database async { log('Getting database instance'); if (_database != null) return _database!; @@ -141,7 +139,6 @@ class SQLiteStorageHelper implements Storage { } } - Future setupLocalVariables() async { log('Setting up local variables'); try { @@ -190,8 +187,6 @@ class SQLiteStorageHelper implements Storage { return value == 'true'; } - - @override Future get(String key) async { log('Getting value for key: $key'); @@ -225,7 +220,8 @@ class SQLiteStorageHelper implements Storage { } @override - Future set(String key, dynamic value, Function(dynamic) cacheSetter) async { + Future set( + String key, dynamic value, Function(dynamic) cacheSetter) async { log('Setting value for key: $key to $value'); CacheUtil().set(key, value); final db = await database; @@ -241,7 +237,7 @@ class SQLiteStorageHelper implements Storage { data, conflictAlgorithm: ConflictAlgorithm.replace, ); - log('Value $value set for key: $key'); + log('Value $value set for key: $key with result: $result'); return result; } @@ -262,7 +258,7 @@ class SQLiteStorageHelper implements Storage { @override Future purge() async { - log('Purging database'); + log('Purging SQLite storage'); await deleteDatabaseDB(); await database; log('Database purged'); @@ -275,19 +271,6 @@ class SQLiteStorageHelper implements Storage { log('Database deleted'); _database = null; } - } - class SqliteStorageDelegate {} - - - - - - - - - - - diff --git a/lib/shared/utils/storage_util.dart b/lib/shared/utils/storage_util.dart index e1e6067c..6ab67cd6 100644 --- a/lib/shared/utils/storage_util.dart +++ b/lib/shared/utils/storage_util.dart @@ -118,20 +118,17 @@ class StorageUtil { } Future ensureInitialization() async { + await initSQLiteStorage(); await initSharedPreferences(); await initSecureStorage(); - await initSQLiteStorage(); } Future initSharedPreferences() async { - if (_sharedPreferences.prefs == null) { - _sharedPreferences.prefs = await SharedPreferences.getInstance(); - _sharedPreferences.isFirstRun = - _sharedPreferences.prefs?.getBool('first_run') ?? true; - } - if (_sharedPreferences.isFirstRun) { + _sharedPreferences.prefs ??= await SharedPreferences.getInstance(); + _isFirstRun = _sharedPreferences.prefs?.getBool('first_run') ?? true; + if (isFirstRun) { _sharedPreferences.isFirstRun = false; - _sharedPreferences.purge(); + _secureStorage.purge(); } } @@ -158,6 +155,26 @@ class StorageUtil { log('SQLiteStorageHelper: Starting initialization'); try { await _sqliteStorage.database; + _devUUID = await _sqliteStorage.get('devUUID') ?? ''; + _userUUID = await _sqliteStorage.get('userUUID') ?? ''; + _userDevUUID = await _sqliteStorage.get('userDevUUID') ?? ''; + _status = await _sqliteStorage.get('status') ?? ''; + _userName = await _sqliteStorage.get('userName') ?? ''; + _cliUUID = await _sqliteStorage.get('cliUUID') ?? ''; + _ownerUUID = await _sqliteStorage.get('ownerUUID') ?? ''; + _cliName = await _sqliteStorage.get('cliName') ?? ''; + _petAmountRegister = await _sqliteStorage.get('petAmountRegister') ?? ''; + _whatsapp = await _sqliteStorage.getBoolean('whatsapp'); + _provisional = await _sqliteStorage.getBoolean('provisional'); + _pets = await _sqliteStorage.getBoolean('pets'); + _local = await _sqliteStorage.getBoolean('local'); + _notify = await _sqliteStorage.getBoolean('notify'); + _fingerprint = await _sqliteStorage.getBoolean('fingerprint'); + _access = await _sqliteStorage.getBoolean('access'); + _panic = await _sqliteStorage.getBoolean('panic'); + _person = await _sqliteStorage.getBoolean('person'); + _requestOSnotification = + await _sqliteStorage.getBoolean('requestOSnotification'); } catch (e) { log('SQLiteStorageHelper: Error during initialization: $e'); }