@@ -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(),
|
||||
|
||||
@@ -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];
|
||||
|
||||
Reference in New Issue
Block a user