push tab to run and handling automation

This commit is contained in:
ashrafzarkanisala
2024-11-24 23:07:03 +03:00
parent 87c47a74ce
commit 0c555cda83
13 changed files with 295 additions and 154 deletions

View File

@ -26,6 +26,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
on<SearchRoutines>(_onSearchRoutines);
on<AddSelectedIcon>(_onAddSelectedIcon);
on<CreateSceneEvent>(_onCreateScene);
on<RemoveDragCard>(_onRemoveDragCard);
// on<RemoveFunction>(_onRemoveFunction);
// on<ClearFunctions>(_onClearFunctions);
}
@ -33,7 +34,13 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
void _onAddToIfContainer(AddToIfContainer event, Emitter<RoutineState> emit) {
final updatedIfItems = List<Map<String, dynamic>>.from(state.ifItems)
..add(event.item);
isTabToRun = event.isTabToRun;
if (event.isTabToRun) {
isTabToRun = true;
isAutomation = false;
} else {
isTabToRun = false;
isAutomation = true;
}
emit(state.copyWith(ifItems: updatedIfItems));
}
@ -46,30 +53,27 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
void _onAddFunctionsToRoutine(
AddFunctionToRoutine event, Emitter<RoutineState> emit) {
final currentSelectedFunctions =
Map<String, List<DeviceFunctionData>>.from(state.selectedFunctions);
try {
if (event.functions.isEmpty) return;
if (currentSelectedFunctions.containsKey(event.uniqueCustomId)) {
currentSelectedFunctions[event.uniqueCustomId]!.addAll(event.functions);
} else {
currentSelectedFunctions[event.uniqueCustomId] = event.functions;
final currentSelectedFunctions =
Map<String, List<DeviceFunctionData>>.from(state.selectedFunctions);
if (currentSelectedFunctions.containsKey(event.uniqueCustomId)) {
currentSelectedFunctions[event.uniqueCustomId] =
List.from(currentSelectedFunctions[event.uniqueCustomId]!)
..addAll(event.functions);
} else {
currentSelectedFunctions[event.uniqueCustomId] =
List.from(event.functions);
}
emit(state.copyWith(selectedFunctions: currentSelectedFunctions));
} catch (e) {
debugPrint('Error adding functions: $e');
}
emit(state.copyWith(selectedFunctions: currentSelectedFunctions));
}
// void _onRemoveFunction(RemoveFunction event, Emitter<RoutineState> emit) {
// final functions = List<DeviceFunctionData>.from(state.selectedFunctions)
// ..removeWhere((f) =>
// f.functionCode == event.function.functionCode &&
// f.value == event.function.value);
// emit(state.copyWith(selectedFunctions: functions));
// }
// void _onClearFunctions(ClearFunctions event, Emitter<RoutineState> emit) {
// emit(state.copyWith(selectedFunctions: []));
// }
Future<void> _onLoadScenes(
LoadScenes event, Emitter<RoutineState> emit) async {
emit(state.copyWith(isLoading: true, errorMessage: null));
@ -83,7 +87,9 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
} catch (e) {
emit(state.copyWith(
isLoading: false,
errorMessage: 'Failed to load scenes',
loadScenesErrorMessage: 'Failed to load scenes',
errorMessage: '',
loadAutomationErrorMessage: '',
));
}
}
@ -101,7 +107,9 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
} catch (e) {
emit(state.copyWith(
isLoading: false,
errorMessage: 'Failed to load automations',
loadAutomationErrorMessage: 'Failed to load automations',
errorMessage: '',
loadScenesErrorMessage: '',
));
}
}
@ -178,10 +186,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
final result = await SceneApi.createScene(createSceneModel);
if (result['success']) {
emit(state.copyWith(
isLoading: false,
errorMessage: null,
));
emit(const RoutineState());
} else {
emit(state.copyWith(
isLoading: false,
@ -195,4 +200,18 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
));
}
}
FutureOr<void> _onRemoveDragCard(
RemoveDragCard event, Emitter<RoutineState> emit) {
if (event.isFromThen) {
/// remove element from thenItems at specific index
final thenItems = List<Map<String, dynamic>>.from(state.thenItems);
thenItems.removeAt(event.index);
emit(state.copyWith(thenItems: thenItems));
} else {
final ifItems = List<Map<String, dynamic>>.from(state.ifItems);
ifItems.removeAt(event.index);
emit(state.copyWith(ifItems: ifItems));
}
}
}