import 'package:bloc/bloc.dart'; import 'package:equatable/equatable.dart'; import 'package:syncrow_web/pages/routiens/models/routine_model.dart'; import 'package:syncrow_web/services/routines_api.dart'; part 'routine_event.dart'; part 'routine_state.dart'; const spaceId = '25c96044-fadf-44bb-93c7-3c079e527ce6'; class RoutineBloc extends Bloc { RoutineBloc() : super(const RoutineState()) { on(_onAddToIfContainer); on(_onAddToThenContainer); on(_onLoadScenes); on(_onLoadAutomation); } void _onAddToIfContainer(AddToIfContainer event, Emitter emit) { if (!_isDuplicate(state.ifItems, event.item)) { final updatedIfItems = List>.from(state.ifItems)..add(event.item); emit(state.copyWith(ifItems: updatedIfItems)); } } void _onAddToThenContainer(AddToThenContainer event, Emitter emit) { if (!_isDuplicate(state.thenItems, event.item)) { final updatedThenItems = List>.from(state.thenItems)..add(event.item); emit(state.copyWith(thenItems: updatedThenItems)); } } bool _isDuplicate(List> items, Map newItem) { return items.any((item) => item['imagePath'] == newItem['imagePath'] && item['title'] == newItem['title']); } Future _onLoadScenes(LoadScenes event, Emitter emit) async { emit(state.copyWith(isLoading: true, errorMessage: null)); try { final scenes = await SceneApi.getScenesByUnitId(event.unitId); emit(state.copyWith( scenes: scenes, isLoading: false, )); } catch (e) { emit(state.copyWith( isLoading: false, errorMessage: 'Something went wrong', )); } } Future _onLoadAutomation(LoadAutomation event, Emitter emit) async { emit(state.copyWith(isLoading: true, errorMessage: null)); try { final automations = await SceneApi.getAutomationByUnitId(event.unitId); emit(state.copyWith( automations: automations, isLoading: false, )); } catch (e) { emit(state.copyWith( isLoading: false, errorMessage: 'Something went wrong', )); } } }