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];