From f33b63c0c634260dc51f4a93e739cbffd7018db3 Mon Sep 17 00:00:00 2001 From: mark-cachy Date: Mon, 13 Apr 2026 19:27:23 +0200 Subject: [PATCH] mmmmh --- lib/features/store/bloc/store_bloc.dart | 14 ++++++++++++- lib/features/store/bloc/store_state.dart | 12 +++++++++++- lib/features/store/ui/stores_screen.dart | 25 ++++++++++++------------ 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/lib/features/store/bloc/store_bloc.dart b/lib/features/store/bloc/store_bloc.dart index 8cdf95d..7502eb3 100644 --- a/lib/features/store/bloc/store_bloc.dart +++ b/lib/features/store/bloc/store_bloc.dart @@ -1,6 +1,7 @@ import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:equatable/equatable.dart'; import 'package:flux/core/blocs/session/session_bloc.dart'; +import 'package:flux/features/staff/models/staff_member_model.dart'; import 'package:flux/features/store/data/store_repository.dart'; import 'package:flux/features/store/models/store_model.dart'; import 'package:get_it/get_it.dart'; @@ -12,7 +13,8 @@ class StoreBloc extends Bloc { final StoreRepository _repository = GetIt.I(); final SessionBloc _sessionBloc; - StoreBloc(this._sessionBloc) : super(const StoreState(stores: [])) { + StoreBloc(this._sessionBloc) + : super(const StoreState(stores: [], staffByStore: {})) { on(_onCreateStore); on(_onLoadStores); } @@ -41,10 +43,20 @@ class StoreBloc extends Bloc { final stores = await _repository.getStoresByCompany( _sessionBloc.state.company!.id, ); + final staffByStore = >{}; + + for (final store in stores) { + final staff = await _repository.getStaffMembersInStore( + _sessionBloc.state.company!.id, + ); + staffByStore[store] = staff; + } + emit( state.copyWith( status: StoreStatus.success, stores: stores, // Assicurati di avere 'stores' nello StoreState + staffByStore: staffByStore, ), ); } catch (e) { diff --git a/lib/features/store/bloc/store_state.dart b/lib/features/store/bloc/store_state.dart index 38a1c01..3add3ce 100644 --- a/lib/features/store/bloc/store_state.dart +++ b/lib/features/store/bloc/store_state.dart @@ -7,12 +7,14 @@ class StoreState extends Equatable { final StoreModel? store; final String? errorMessage; final List stores; + final Map> staffByStore; const StoreState({ this.status = StoreStatus.initial, this.store, this.errorMessage, required this.stores, + required this.staffByStore, }); StoreState copyWith({ @@ -20,15 +22,23 @@ class StoreState extends Equatable { StoreModel? store, String? errorMessage, List? stores, + Map>? staffByStore, }) { return StoreState( status: status ?? this.status, store: store ?? this.store, errorMessage: errorMessage ?? this.errorMessage, stores: stores ?? this.stores, + staffByStore: staffByStore ?? this.staffByStore, ); } @override - List get props => [status, store, errorMessage, stores]; + List get props => [ + status, + store, + errorMessage, + stores, + staffByStore, + ]; } diff --git a/lib/features/store/ui/stores_screen.dart b/lib/features/store/ui/stores_screen.dart index 447991b..fd522f5 100644 --- a/lib/features/store/ui/stores_screen.dart +++ b/lib/features/store/ui/stores_screen.dart @@ -4,6 +4,7 @@ import 'package:flux/core/blocs/session/session_bloc.dart'; import 'package:flux/core/theme/theme.dart'; import 'package:flux/core/widgets/flux_text_field.dart'; import 'package:flux/features/staff/blocs/staff_cubit.dart'; +import 'package:flux/features/staff/models/staff_member_model.dart'; import 'package:flux/features/store/bloc/store_bloc.dart'; import 'package:flux/features/store/models/store_model.dart'; @@ -29,15 +30,16 @@ class _StoresScreenState extends State { appBar: AppBar(title: const Text("I Tuoi Negozi")), body: BlocBuilder( builder: (context, state) { - if (state.status == StoreStatus.loading) + if (state.status == StoreStatus.loading) { return const Center(child: CircularProgressIndicator()); + } return ListView.builder( padding: const EdgeInsets.all(16), itemCount: state.stores.length, itemBuilder: (context, index) { final store = state.stores[index]; - return _buildStoreCard(store); + return _buildStoreCard(store, state.staffByStore); }, ); }, @@ -50,7 +52,10 @@ class _StoresScreenState extends State { ); } - Widget _buildStoreCard(StoreModel store) { + Widget _buildStoreCard( + StoreModel store, + Map> map, + ) { return Card( margin: const EdgeInsets.only(bottom: 16), shape: RoundedRectangleBorder( @@ -89,16 +94,10 @@ class _StoresScreenState extends State { mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ // Mostra quanti dipendenti ci sono (usando lo StaffCubit) - BlocBuilder( - builder: (context, staffState) { - final staffCount = - staffState.staffByStore[store.id]?.length ?? 0; - return ActionChip( - avatar: const Icon(Icons.people, size: 16), - label: Text("$staffCount Dipendenti"), - onPressed: () => _manageStoreStaff(store), - ); - }, + ActionChip( + avatar: const Icon(Icons.people, size: 16), + label: Text("${map[store]?.length ?? 0} Dipendenti"), + onPressed: () => _manageStoreStaff(store), ), TextButton.icon( onPressed: () => _openStoreForm(context, store: store),