mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-15 17:47:53 +00:00
push ac function state selection
This commit is contained in:
100
lib/pages/routiens/bloc/routine_bloc/routine_bloc.dart
Normal file
100
lib/pages/routiens/bloc/routine_bloc/routine_bloc.dart
Normal file
@ -0,0 +1,100 @@
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:syncrow_web/pages/routiens/models/device_functions.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<RoutineEvent, RoutineState> {
|
||||
RoutineBloc() : super(const RoutineState()) {
|
||||
on<AddToIfContainer>(_onAddToIfContainer);
|
||||
on<AddToThenContainer>(_onAddToThenContainer);
|
||||
on<LoadScenes>(_onLoadScenes);
|
||||
on<LoadAutomation>(_onLoadAutomation);
|
||||
on<AddFunction>(_onAddFunction);
|
||||
on<RemoveFunction>(_onRemoveFunction);
|
||||
on<ClearFunctions>(_onClearFunctions);
|
||||
}
|
||||
|
||||
void _onAddToIfContainer(AddToIfContainer event, Emitter<RoutineState> emit) {
|
||||
if (!_isDuplicate(state.ifItems, event.item)) {
|
||||
final updatedIfItems = List<Map<String, dynamic>>.from(state.ifItems)
|
||||
..add(event.item);
|
||||
emit(state.copyWith(ifItems: updatedIfItems));
|
||||
}
|
||||
}
|
||||
|
||||
void _onAddToThenContainer(
|
||||
AddToThenContainer event, Emitter<RoutineState> emit) {
|
||||
if (!_isDuplicate(state.thenItems, event.item)) {
|
||||
final updatedThenItems = List<Map<String, dynamic>>.from(state.thenItems)
|
||||
..add(event.item);
|
||||
emit(state.copyWith(thenItems: updatedThenItems));
|
||||
}
|
||||
}
|
||||
|
||||
void _onAddFunction(AddFunction event, Emitter<RoutineState> emit) {
|
||||
final functions = List<DeviceFunctionData>.from(state.selectedFunctions);
|
||||
functions.add(event.function);
|
||||
emit(state.copyWith(selectedFunctions: functions));
|
||||
}
|
||||
|
||||
void _onRemoveFunction(RemoveFunction event, Emitter<RoutineState> emit) {
|
||||
final functions = List<DeviceFunctionData>.from(state.selectedFunctions)
|
||||
..removeWhere((f) =>
|
||||
f.function == event.function.function &&
|
||||
f.value == event.function.value);
|
||||
emit(state.copyWith(selectedFunctions: functions));
|
||||
}
|
||||
|
||||
void _onClearFunctions(ClearFunctions event, Emitter<RoutineState> emit) {
|
||||
emit(state.copyWith(selectedFunctions: []));
|
||||
}
|
||||
|
||||
bool _isDuplicate(
|
||||
List<Map<String, dynamic>> items, Map<String, dynamic> newItem) {
|
||||
return items.any((item) =>
|
||||
item['imagePath'] == newItem['imagePath'] &&
|
||||
item['title'] == newItem['title']);
|
||||
}
|
||||
|
||||
Future<void> _onLoadScenes(
|
||||
LoadScenes event, Emitter<RoutineState> 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<void> _onLoadAutomation(
|
||||
LoadAutomation event, Emitter<RoutineState> 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',
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user