From 3da294a09e5dabbb81542049342a6398a1774ab6 Mon Sep 17 00:00:00 2001 From: "J. A. Messias" Date: Fri, 13 Sep 2024 10:52:46 -0300 Subject: [PATCH] fix pets and ios foreground notification --- lib/app_state.dart | 17 ++++ lib/main.dart | 11 ++- lib/pages/home_page/home_page_widget.dart | 2 + lib/pages/pets_page/pets_history_screen.dart | 17 +++- lib/pages/pets_page/pets_page_model.dart | 32 ++++++++ lib/pages/pets_page/pets_page_widget.dart | 81 ++++++++++---------- 6 files changed, 112 insertions(+), 48 deletions(-) diff --git a/lib/app_state.dart b/lib/app_state.dart index 8d469e35..49aef2fb 100644 --- a/lib/app_state.dart +++ b/lib/app_state.dart @@ -74,6 +74,7 @@ class AppState extends ChangeNotifier { await _safeInitAsync(() async { _cliUUID = await secureStorage.getString('ff_cliUUID') ?? _cliUUID; }); + await _safeInitAsync(() async { _ownerUUID = await secureStorage.getString('ff_ownerUUID') ?? _ownerUUID; }); @@ -166,6 +167,10 @@ class AppState extends ChangeNotifier { await _safeInitAsync(() async { _haveLocal = await secureStorage.getBool('ff_have_local') ?? _haveLocal; }); + await _safeInitAsync(() async { + _petAmountRegister = + await secureStorage.getInt('petAmountRegister') ?? _petAmountRegister; + }); await _safeInitAsync(() async { _isRequestOSNotification = @@ -180,6 +185,18 @@ class AppState extends ChangeNotifier { } late FlutterSecureStorage secureStorage; + + int _petAmountRegister = 0; + int get petAmountRegister => _petAmountRegister; + set petAmountRegister(int value) { + _petAmountRegister = value; + secureStorage.setInt('petAmountRegister', value); + } + + void deletePetAmountRegister() { + secureStorage.delete(key: 'petAmountRegister'); + } + bool _isRequestOSNotification = false; bool get isRequestOSNotification => _isRequestOSNotification; set isRequestOSNotification(bool value) { diff --git a/lib/main.dart b/lib/main.dart index 5e2cc196..73e07a1c 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,4 +1,5 @@ import 'dart:developer'; +import 'dart:io'; import 'package:app_tracking_transparency/app_tracking_transparency.dart'; import 'package:firebase_core/firebase_core.dart'; @@ -48,10 +49,12 @@ Future initializeApp() async { } Future foregroundHandleMessage(RemoteMessage message) async { - NotificationService.show( - title: message.notification!.title!, - body: message.notification!.body!, - payload: Map.from(message.data)); + if (!Platform.isIOS) { + NotificationService.show( + title: message.notification!.title!, + body: message.notification!.body!, + payload: Map.from(message.data)); + } } Future _backgroundHandleMessage(RemoteMessage message) async {} diff --git a/lib/pages/home_page/home_page_widget.dart b/lib/pages/home_page/home_page_widget.dart index 47c8a945..1a14c55b 100644 --- a/lib/pages/home_page/home_page_widget.dart +++ b/lib/pages/home_page/home_page_widget.dart @@ -51,6 +51,8 @@ class _HomePageWidgetState extends State { AppState().whatsapp = response.jsonBody['whatsapp'] ?? false; AppState().provisional = response.jsonBody['provisional'] ?? false; AppState().pets = response.jsonBody['pet'] ?? false; + AppState().petAmountRegister = + response.jsonBody['petAmountRegister'] ?? '0'; AppState().name = response.jsonBody['visitado']['VDO_NOME']; safeSetState(() {}); return; diff --git a/lib/pages/pets_page/pets_history_screen.dart b/lib/pages/pets_page/pets_history_screen.dart index a1055f4d..704e6983 100644 --- a/lib/pages/pets_page/pets_history_screen.dart +++ b/lib/pages/pets_page/pets_history_screen.dart @@ -141,10 +141,21 @@ class _PetsHistoryScreenState extends State shrinkWrap: true, physics: const BouncingScrollPhysics(), controller: _scrollController, - itemCount: _petsWrap.length, + itemCount: _petsWrap.length + 1, itemBuilder: (context, index) { - final item = _petsWrap[index]; - return _item(context, item); + if (index == 0) { + // Add your item here + return Padding( + padding: EdgeInsets.only(right: 30, top: 10), + child: Text( + "${_petsWrap.length}/${AppState().petAmountRegister}", + textAlign: TextAlign.right, + ), + ); + } else { + final item = _petsWrap[index - 1]; + return _item(context, item); + } }); }, )), diff --git a/lib/pages/pets_page/pets_page_model.dart b/lib/pages/pets_page/pets_page_model.dart index 9e7a18a5..480cc6f5 100644 --- a/lib/pages/pets_page/pets_page_model.dart +++ b/lib/pages/pets_page/pets_page_model.dart @@ -11,10 +11,12 @@ import '/custom_code/actions/index.dart' as actions; class PetsPageModel extends FlutterFlowModel { late final TabController tabBarController; + VoidCallback? onUpdatePet; ApiCallResponse? petsResponse; int? petId; BuildContext? buildContext; + bool isEditing = false; // Controller para o Upload de Arquivos bool isDataUploading = false; @@ -204,6 +206,8 @@ class PetsPageModel extends FlutterFlowModel { enText: 'Pet successfully updated', ptText: 'Pet atualizado com sucesso', )); + clearFields(); + switchTab(1); }).catchError((error) { log(error.toString()); DialogUtil.errorDefault(buildContext!); @@ -239,8 +243,36 @@ class PetsPageModel extends FlutterFlowModel { enText: 'Pet successfully registered', ptText: 'Pet cadastrado com sucesso', )); + clearFields(); }).catchError((error) { DialogUtil.errorDefault(buildContext!); }); } + + void switchTab(int index) { + tabBarController.animateTo(index); + if (index == 1) handleEditingChanged(false); + onUpdatePet?.call(); + } + + void handleUploadComplete(FFUploadedFile uploadedFile) { + uploadedLocalFile = uploadedFile; + } + + void handleEditingChanged(bool editing) { + isEditing = editing; + clearFields(); + } + + void clearFields() { + uploadedLocalFile = null; + textControllerName.text = ''; + textControllerSpecies.text = ''; + textControllerRace.text = ''; + textControllerColor.text = ''; + textControllerData.text = ''; + textControllerObservation.text = ''; + dropDownValueController1 = FormFieldController(''); + dropDownValueController2 = FormFieldController(''); + } } diff --git a/lib/pages/pets_page/pets_page_widget.dart b/lib/pages/pets_page/pets_page_widget.dart index c9542b06..9363100c 100644 --- a/lib/pages/pets_page/pets_page_widget.dart +++ b/lib/pages/pets_page/pets_page_widget.dart @@ -52,15 +52,17 @@ class _PetsPageWidgetState extends State with SingleTickerProviderStateMixin { late PetsPageModel _model; final _formKey = GlobalKey(); - bool isEditing = false; @override void initState() { super.initState(); _model = PetsPageModel(); _model.tabBarController = TabController(length: 2, vsync: this); + _model.onUpdatePet = () { + setState(() {}); + }; - widget.pet != null ? isEditing = true : isEditing = false; + widget.pet != null ? _model.isEditing = true : _model.isEditing = false; // _handleUploadComplete(actions.convertToUploadFile()) @@ -73,40 +75,47 @@ class _PetsPageWidgetState extends State 'https://freaccess.com.br/freaccess/getImage.php?devUUID=${AppState().devUUID}&userUUID=${AppState().userUUID}&cliID=${AppState().cliUUID}&atividade=consultaFotoPet&petId=$petId')); String base64 = base64Encode(response.bodyBytes); FFUploadedFile uploadedFile = await convertToUploadFile(base64); - _handleUploadComplete(uploadedFile); + setState(() { + _model.handleUploadComplete(uploadedFile); + }); })(); } _model.textControllerName ??= TextEditingController( - text: widget.pet != null ? widget.pet['name'] : ''); + text: widget.pet != null ? widget.pet['name'] ?? '' : ''); _model.textFieldFocusName ??= FocusNode(); _model.textControllerSpecies ??= TextEditingController( - text: widget.pet != null ? widget.pet['species'] : ''); + text: widget.pet != null ? widget.pet['species'] ?? '' : ''); _model.textFieldFocusSpecies ??= FocusNode(); _model.textControllerRace ??= TextEditingController( - text: widget.pet != null ? widget.pet['breed'] : ''); + text: widget.pet != null ? widget.pet['breed'] ?? '' : ''); _model.textFieldFocusRace ??= FocusNode(); _model.textControllerColor ??= TextEditingController( - text: widget.pet != null ? widget.pet['color'] : ''); + text: widget.pet != null ? widget.pet['color'] ?? '' : ''); _model.textFieldFocusColor ??= FocusNode(); _model.textControllerData ??= TextEditingController( text: widget.pet != null - ? ValidatorUtil.formatDateTimePicker(widget.pet['birthdayDate']) + ? widget.pet['birthdayDate'] != null + ? ValidatorUtil.formatDateTimePicker(widget.pet['birthdayDate']) + : '' : ''); _model.textFieldFocusData ??= FocusNode(); _model.textControllerObservation ??= TextEditingController( - text: widget.pet != null ? widget.pet['notes'] : ''); + text: widget.pet != null ? widget.pet['notes'] ?? '' : ''); _model.textFieldFocusObservation ??= FocusNode(); - if (widget.pet != null) { - _model.dropDownValue1 ??= widget.pet['gender'] ?? ''; - _model.dropDownValue2 ??= widget.pet['size'] ?? ''; - } + 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 ??= ''); @@ -121,30 +130,10 @@ class _PetsPageWidgetState extends State _model.dispose(); } - void _handleUploadComplete(FFUploadedFile uploadedFile) { - setState(() { - _model.uploadedLocalFile = uploadedFile; - }); - } - - void _handleEditingChanged(bool editing) { - setState(() { - isEditing = editing; - _model.uploadedLocalFile = null; - _model.textControllerName.text = ''; - _model.textControllerSpecies.text = ''; - _model.textControllerRace.text = ''; - _model.textControllerColor.text = ''; - _model.textControllerData.text = ''; - _model.textControllerObservation.text = ''; - _model.dropDownValueController1 = FormFieldController(''); - _model.dropDownValueController2 = FormFieldController(''); - }); - } - @override Widget build(BuildContext context) { _model.buildContext = context; + return Scaffold( appBar: _buildAppBar(context), backgroundColor: FlutterFlowTheme.of(context).primaryBackground, @@ -152,14 +141,23 @@ class _PetsPageWidgetState extends State } PreferredSizeWidget _buildAppBar(BuildContext context) { - return AppBarUtil(title: 'Pets', onBackButtonPressed: () => context.pop()); + return AppBarUtil( + title: 'Pets', + onBackButtonPressed: () => context.pop(), + ); + } + + void onEditingChanged(bool value) { + setState(() { + _model.handleEditingChanged(value); + }); } Widget _buildTabView(BuildContext context) { return TabViewUtil( context: context, model: _model, - labelTab1: isEditing + labelTab1: _model.isEditing ? FFLocalizations.of(context) .getVariableText(ptText: 'Editar', enText: 'Edit') : FFLocalizations.of(context) @@ -167,10 +165,11 @@ class _PetsPageWidgetState extends State labelTab2: FFLocalizations.of(context) .getVariableText(ptText: 'Consultar', enText: 'History'), controller: _model.tabBarController, - widget1: - isEditing ? _buildEditForm(context) : _buildRegisterForm(context), + widget1: _model.isEditing + ? _buildEditForm(context) + : _buildRegisterForm(context), widget2: PetsHistoryScreen(), - onEditingChanged: _handleEditingChanged, + onEditingChanged: onEditingChanged, ); } @@ -208,7 +207,7 @@ class _PetsPageWidgetState extends State Padding( padding: const EdgeInsets.fromLTRB(0, 10, 0, 20), child: MediaUploadButtonUtil( - onUploadComplete: _handleUploadComplete, + onUploadComplete: _model.handleUploadComplete, isUploading: _model.isDataUploading, uploadedFiles: _model.uploadedLocalFile, labelText: FFLocalizations.of(context).getVariableText( @@ -666,7 +665,7 @@ class _PetsPageWidgetState extends State Padding( padding: const EdgeInsets.fromLTRB(0, 10, 0, 20), child: MediaUploadButtonUtil( - onUploadComplete: _handleUploadComplete, + onUploadComplete: _model.handleUploadComplete, isUploading: _model.isDataUploading, uploadedFiles: _model.uploadedLocalFile, labelText: FFLocalizations.of(context).getVariableText(