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.
This commit is contained in:
mandreshope 2025-08-04 14:50:25 +03:00
parent 8c06ec7dad
commit d131928235
5 changed files with 69 additions and 34 deletions

View File

@ -4,7 +4,7 @@ import 'package:e_scan/components/components.dart';
import 'package:e_scan/themes/app_theme.dart'; import 'package:e_scan/themes/app_theme.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
class DeleteCategorieDialogComponentWidget extends ConsumerWidget { class DeleteCategorieDialogComponentWidget extends StatelessWidget {
const DeleteCategorieDialogComponentWidget({ const DeleteCategorieDialogComponentWidget({
super.key, super.key,
required this.stockPickingId, required this.stockPickingId,
@ -41,8 +41,7 @@ class DeleteCategorieDialogComponentWidget extends ConsumerWidget {
} }
@override @override
Widget build(BuildContext context, WidgetRef ref) { Widget build(BuildContext context) {
final state = ref.watch(receptionDetailsPageModelProvider);
return Container( return Container(
width: double.infinity, width: double.infinity,
decoration: BoxDecoration( decoration: BoxDecoration(
@ -74,12 +73,19 @@ class DeleteCategorieDialogComponentWidget extends ConsumerWidget {
mainAxisSize: MainAxisSize.max, mainAxisSize: MainAxisSize.max,
children: [ children: [
Expanded( Expanded(
child: PrimaryButtonComponent( child: Consumer(
builder: (context, ref, child) {
final state = ref.watch(
receptionDetailsPageModelProvider,
);
return PrimaryButtonComponent(
loading: state.withBackorderLoading, loading: state.withBackorderLoading,
text: 'Créer un reliquat', text: 'Créer un reliquat',
onPressed: () async { onPressed: () {
await ref ref
.read(receptionDetailsPageModelProvider.notifier) .read(
receptionDetailsPageModelProvider.notifier,
)
.withBackorder( .withBackorder(
receptionId: stockPickingId, receptionId: stockPickingId,
onSuccess: () { onSuccess: () {
@ -87,16 +93,25 @@ class DeleteCategorieDialogComponentWidget extends ConsumerWidget {
}, },
); );
}, },
);
},
), ),
), ),
Expanded( Expanded(
child: PrimaryButtonComponent( child: Consumer(
builder: (context, ref, child) {
final state = ref.watch(
receptionDetailsPageModelProvider,
);
return PrimaryButtonComponent(
loading: state.withoutBackorderLoading, loading: state.withoutBackorderLoading,
backgroundColor: Colors.red, backgroundColor: Colors.red,
text: 'Aucun reliquat', text: 'Aucun reliquat',
onPressed: () async { onPressed: () {
await ref ref
.read(receptionDetailsPageModelProvider.notifier) .read(
receptionDetailsPageModelProvider.notifier,
)
.withoutBackorder( .withoutBackorder(
receptionId: stockPickingId, receptionId: stockPickingId,
onSuccess: () { onSuccess: () {
@ -105,6 +120,8 @@ class DeleteCategorieDialogComponentWidget extends ConsumerWidget {
); );
// Navigator.pop(context); // Navigator.pop(context);
}, },
);
},
), ),
), ),
], ],

View File

@ -152,7 +152,7 @@ class ReceptionDetailsPageModel
final stockPickingRecords = objectboxManager.store final stockPickingRecords = objectboxManager.store
.box<StockPickingRecordEntity>(); .box<StockPickingRecordEntity>();
final stockPikingEntity = stockPickingRecords.get(receptionId); final stockPikingEntity = stockPickingRecords.get(receptionId);
state = state.copyWith(withBackorderLoading: true); state = state.copyWith(withoutBackorderLoading: true);
final createBackorderConfirmationId = final createBackorderConfirmationId =
await ApiCalls.createBackorderConfirmation( await ApiCalls.createBackorderConfirmation(
stockPickingId: receptionId, stockPickingId: receptionId,
@ -175,10 +175,10 @@ class ReceptionDetailsPageModel
} else { } else {
onError?.call(); onError?.call();
} }
state = state.copyWith(withBackorderLoading: false); state = state.copyWith(withoutBackorderLoading: false);
} catch (e) { } catch (e) {
onError?.call(); onError?.call();
state = state.copyWith(withBackorderLoading: false); state = state.copyWith(withoutBackorderLoading: false);
} }
} }
} }

View File

@ -86,7 +86,7 @@ class _ReceptionPageState extends ConsumerState<ReceptionPage> {
receptionPageModelProvider receptionPageModelProvider
.notifier, .notifier,
) )
.getAllReceptions(), .refreshReceptions(),
); );
}, },
child: StockPickingCard( child: StockPickingCard(

View File

@ -106,6 +106,24 @@ class ReceptionPageModel extends StateNotifier<ReceptionPageState> {
state = state.copyWith(loadingReceptions: false); 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 @freezed

View File

@ -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 # 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 # 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. # 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: environment:
sdk: ^3.8.1 sdk: ^3.8.1