From d13192823598b57e43dc3d908696c3a727955eac Mon Sep 17 00:00:00 2001 From: mandreshope Date: Mon, 4 Aug 2025 14:50:25 +0300 Subject: [PATCH] enhance: Optimizes backorder dialog and fixes loading state Refactors the backorder dialog to use local Consumer widgets, reducing unnecessary rebuilds and improving performance. Corrects the loading state variable updated when confirming backorders, ensuring accurate UI feedback. Introduces a dedicated refresh method for receptions that includes a pre-sync, ensuring up-to-date data. --- .../backorder_dialog_component.dart | 75 ++++++++++++------- .../reception_details_page_model.dart | 6 +- .../operation/reception/reception_page.dart | 2 +- .../reception/reception_page_model.dart | 18 +++++ pubspec.yaml.bak | 2 +- 5 files changed, 69 insertions(+), 34 deletions(-) diff --git a/lib/components/backorder_dialog_component.dart b/lib/components/backorder_dialog_component.dart index c996ea3..91dcfd3 100644 --- a/lib/components/backorder_dialog_component.dart +++ b/lib/components/backorder_dialog_component.dart @@ -4,7 +4,7 @@ import 'package:e_scan/components/components.dart'; import 'package:e_scan/themes/app_theme.dart'; import 'package:flutter/material.dart'; -class DeleteCategorieDialogComponentWidget extends ConsumerWidget { +class DeleteCategorieDialogComponentWidget extends StatelessWidget { const DeleteCategorieDialogComponentWidget({ super.key, required this.stockPickingId, @@ -41,8 +41,7 @@ class DeleteCategorieDialogComponentWidget extends ConsumerWidget { } @override - Widget build(BuildContext context, WidgetRef ref) { - final state = ref.watch(receptionDetailsPageModelProvider); + Widget build(BuildContext context) { return Container( width: double.infinity, decoration: BoxDecoration( @@ -74,36 +73,54 @@ class DeleteCategorieDialogComponentWidget extends ConsumerWidget { mainAxisSize: MainAxisSize.max, children: [ Expanded( - child: PrimaryButtonComponent( - loading: state.withBackorderLoading, - text: 'Créer un reliquat', - onPressed: () async { - await ref - .read(receptionDetailsPageModelProvider.notifier) - .withBackorder( - receptionId: stockPickingId, - onSuccess: () { - Navigator.of(context).pop(); - }, - ); + child: Consumer( + builder: (context, ref, child) { + final state = ref.watch( + receptionDetailsPageModelProvider, + ); + return PrimaryButtonComponent( + loading: state.withBackorderLoading, + text: 'Créer un reliquat', + onPressed: () { + ref + .read( + receptionDetailsPageModelProvider.notifier, + ) + .withBackorder( + receptionId: stockPickingId, + onSuccess: () { + Navigator.of(context).pop(); + }, + ); + }, + ); }, ), ), Expanded( - child: PrimaryButtonComponent( - loading: state.withoutBackorderLoading, - backgroundColor: Colors.red, - text: 'Aucun reliquat', - onPressed: () async { - await ref - .read(receptionDetailsPageModelProvider.notifier) - .withoutBackorder( - receptionId: stockPickingId, - onSuccess: () { - Navigator.of(context).pop(); - }, - ); - // Navigator.pop(context); + child: Consumer( + builder: (context, ref, child) { + final state = ref.watch( + receptionDetailsPageModelProvider, + ); + return PrimaryButtonComponent( + loading: state.withoutBackorderLoading, + backgroundColor: Colors.red, + text: 'Aucun reliquat', + onPressed: () { + ref + .read( + receptionDetailsPageModelProvider.notifier, + ) + .withoutBackorder( + receptionId: stockPickingId, + onSuccess: () { + Navigator.of(context).pop(); + }, + ); + // Navigator.pop(context); + }, + ); }, ), ), diff --git a/lib/pages/operation/reception/reception_details_page_model.dart b/lib/pages/operation/reception/reception_details_page_model.dart index 4d6bd7a..8ee2dae 100644 --- a/lib/pages/operation/reception/reception_details_page_model.dart +++ b/lib/pages/operation/reception/reception_details_page_model.dart @@ -152,7 +152,7 @@ class ReceptionDetailsPageModel final stockPickingRecords = objectboxManager.store .box(); final stockPikingEntity = stockPickingRecords.get(receptionId); - state = state.copyWith(withBackorderLoading: true); + state = state.copyWith(withoutBackorderLoading: true); final createBackorderConfirmationId = await ApiCalls.createBackorderConfirmation( stockPickingId: receptionId, @@ -175,10 +175,10 @@ class ReceptionDetailsPageModel } else { onError?.call(); } - state = state.copyWith(withBackorderLoading: false); + state = state.copyWith(withoutBackorderLoading: false); } catch (e) { onError?.call(); - state = state.copyWith(withBackorderLoading: false); + state = state.copyWith(withoutBackorderLoading: false); } } } diff --git a/lib/pages/operation/reception/reception_page.dart b/lib/pages/operation/reception/reception_page.dart index 0849eac..b49e1e4 100644 --- a/lib/pages/operation/reception/reception_page.dart +++ b/lib/pages/operation/reception/reception_page.dart @@ -86,7 +86,7 @@ class _ReceptionPageState extends ConsumerState { receptionPageModelProvider .notifier, ) - .getAllReceptions(), + .refreshReceptions(), ); }, child: StockPickingCard( diff --git a/lib/pages/operation/reception/reception_page_model.dart b/lib/pages/operation/reception/reception_page_model.dart index 4a5f1f9..27b2aa7 100644 --- a/lib/pages/operation/reception/reception_page_model.dart +++ b/lib/pages/operation/reception/reception_page_model.dart @@ -106,6 +106,24 @@ class ReceptionPageModel extends StateNotifier { state = state.copyWith(loadingReceptions: false); } } + + Future refreshReceptions() async { + // sync all data first + await synchroAllData(); + try { + final res = await ApiCalls.getAllStockPiking(); + res.when( + (data) { + state = state.copyWith(receptions: data, loadingReceptions: false); + }, + (error) { + state = state.copyWith(loadingReceptions: false); + }, + ); + } catch (e) { + state = state.copyWith(loadingReceptions: false); + } + } } @freezed diff --git a/pubspec.yaml.bak b/pubspec.yaml.bak index 48cfc1b..517b67a 100644 --- a/pubspec.yaml.bak +++ b/pubspec.yaml.bak @@ -16,7 +16,7 @@ publish_to: "none" # Remove this line if you wish to publish to pub.dev # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # In Windows, build-name is used as the major, minor, and patch parts # of the product and file versions while build-number is used as the build suffix. -version: 0.2.2+7 +version: 0.2.3+8 environment: sdk: ^3.8.1