latest services ok

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
2026-04-30 12:39:25 +02:00
parent 11c1e28aaa
commit a562606613
9 changed files with 171 additions and 55 deletions

View File

@@ -1,3 +1,5 @@
import 'dart:developer';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:flux/features/services/data/services_repository.dart';
@@ -17,19 +19,38 @@ class LatestStoreServicesBloc
status: LatestStoreServicesStatus.initial,
),
) {
on<InitLastServicesEvent>((event, emit) async {
on<InitLastStoreServicesEvent>((event, emit) async {
emit(state.copyWith(status: LatestStoreServicesStatus.loading));
try {
// 1. Creiamo uno stream "intermedio" che idrata i dati
final hydratedStream = _repository
.getLastStoreServicesStream(storeId: event.storeId, limit: 5)
.asyncMap((List<ServiceModel> rawServices) async {
// Questo gira ad ogni "scatto" dello stream di Supabase
List<ServiceModel> fullyHydratedServices = [];
for (ServiceModel service in rawServices) {
// Peschiamo i dati completi (incluso il cliente)
ServiceModel fullService = await _repository.fetchServiceById(
service.id!,
);
fullyHydratedServices.add(fullService);
}
// Passiamo la lista completa allo step successivo
return fullyHydratedServices;
});
// 2. Ora passiamo lo stream idratato all'emit.forEach
await emit.forEach(
_repository.getLastStoreServicesStream(
storeId: event.storeId,
limit: 5,
),
onData: (List<ServiceModel> data) => state.copyWith(
status: LatestStoreServicesStatus.success,
services: data,
),
hydratedStream, // Usiamo lo stream modificato!
onData: (List<ServiceModel> fullyHydratedServices) {
// Qui ora è tutto sincrono e bellissimo
return state.copyWith(
services: fullyHydratedServices,
status: LatestStoreServicesStatus.success,
);
},
onError: (error, stackTrace) => state.copyWith(
status: LatestStoreServicesStatus.failure,
error: error.toString(),

View File

@@ -7,10 +7,10 @@ sealed class LatestStoreServicesEvent extends Equatable {
List<Object> get props => [];
}
class InitLastServicesEvent extends LatestStoreServicesEvent {
class InitLastStoreServicesEvent extends LatestStoreServicesEvent {
final String storeId;
const InitLastServicesEvent(this.storeId);
const InitLastStoreServicesEvent(this.storeId);
@override
List<Object> get props => [storeId];

View File

@@ -2,7 +2,9 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flux/core/blocs/session/session_cubit.dart';
import 'package:flux/core/theme/theme.dart';
import 'package:flux/core/utils/extensions.dart';
import 'package:flux/features/home/latest_store_services/bloc/latest_store_services_bloc.dart';
import 'package:go_router/go_router.dart';
class LatestStoreServicesCard extends StatelessWidget {
const LatestStoreServicesCard({super.key});
@@ -15,7 +17,7 @@ class LatestStoreServicesCard extends StatelessWidget {
// 1. Creiamo il Bloc e facciamo partire subito la query
create: (context) =>
LatestStoreServicesBloc()
..add(InitLastServicesEvent(currentStoreId ?? '')),
..add(InitLastStoreServicesEvent(currentStoreId ?? '')),
child: BlocListener<SessionCubit, SessionState>(
// 2. MAGIA: Se l'utente cambia negozio dalla barra in alto, riavviamo lo stream!
listenWhen: (previous, current) =>
@@ -23,7 +25,7 @@ class LatestStoreServicesCard extends StatelessWidget {
listener: (context, state) {
if (state.currentStore?.id != null) {
context.read<LatestStoreServicesBloc>().add(
InitLastServicesEvent(state.currentStore!.id!),
InitLastStoreServicesEvent(state.currentStore!.id!),
);
}
},
@@ -67,15 +69,18 @@ class _LatestServicesCardContent extends StatelessWidget {
),
const SizedBox(width: 12),
Expanded(
child: Text(
"Ultimi Servizi",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16,
color: context.primaryText,
child: TextButton(
onPressed: () => context.push('/services'),
child: Text(
context.l10n.homeLatestServices,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16,
color: context.primaryText,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
],
@@ -120,31 +125,46 @@ class _LatestServicesCardContent extends StatelessWidget {
),
itemBuilder: (context, index) {
final service = state.services[index];
return Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Text(
service.number,
style: TextStyle(
fontWeight: FontWeight.w600,
color: context.primaryText,
return InkWell(
onTap: () =>
context.push('/service-form', extra: service),
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
flex: 5,
child: Text(
service.customerDisplayName ??
'Cliente sconosciuto',
style: TextStyle(
fontWeight: FontWeight.w700,
color: context.primaryText,
),
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
// Se hai formattato la data, stampala qui (es. 12/04/2026)
Text(
"${service.createdAt?.day}/${service.createdAt?.month}",
style: TextStyle(
color: context.secondaryText,
fontSize: 12,
Expanded(
flex: 5,
child: Text(
service.number,
style: TextStyle(
fontWeight: FontWeight.w600,
color: context.primaryText,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
),
],
Text(
"${service.createdAt?.day}/${service.createdAt?.month}",
style: TextStyle(
color: context.secondaryText,
fontSize: 12,
),
),
],
),
),
);
},