import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart'; import 'package:syncrow_app/features/scene/bloc/create_scene/create_scene_bloc.dart'; import 'package:syncrow_app/features/scene/model/create_scene_model.dart'; import 'package:syncrow_app/features/scene/model/scene_static_function.dart'; import 'package:syncrow_app/features/scene/widgets/alert_dialogs/alert_dialog_countdown.dart'; import 'package:syncrow_app/features/scene/widgets/alert_dialogs/alert_dialog_functions_body.dart'; import 'package:syncrow_app/features/scene/widgets/alert_dialogs/alert_dialog_temperature_body.dart'; import 'package:syncrow_app/utils/context_extension.dart'; mixin SceneLogicHelper { bool isOnlyDelayOrDelayLast(List tasks) { final lastTask = tasks.last; return tasks.every((task) => task.code == 'delay') || lastTask.code == 'delay'; } void handleSaveButtonPress( BuildContext context, TextEditingController sceneNameController, List tasks, { required bool updateScene, required String sceneId, }) { if (isOnlyDelayOrDelayLast(tasks)) { // Show snackbar indicating restriction Navigator.pop(context); context.showCustomSnackbar( message: 'A single delay or delay-last operations are NOT allowed.', icon: const Icon( Icons.error, color: Colors.red, ), ); } else { final createSceneModel = CreateSceneModel( unitUuid: HomeCubit.getInstance().selectedSpace!.id ?? '', sceneName: sceneNameController.text, decisionExpr: 'and', actions: List.generate( tasks.length, (index) { final task = tasks[index]; if (task.code == 'delay') { return CreateSceneAction( entityId: tasks[index].deviceId, actionExecutor: 'delay', executorProperty: CreateSceneExecutorProperty( functionCode: task.code, functionValue: task.operationalValues.first.value, delaySeconds: 0, ), ); } return CreateSceneAction( entityId: task.deviceId, actionExecutor: 'device_issue', executorProperty: CreateSceneExecutorProperty( functionCode: task.code, functionValue: task.operationalValues.first.value, delaySeconds: 0, ), ); }, ), ); context.read().add(CreateSceneWithTasksEvent( createSceneModel: createSceneModel, updateScene: updateScene, sceneId: sceneId, )); Navigator.pop(context); } } Widget getTheCorrectDialogBody( SceneStaticFunction taskItem, List? listOfSceneStaticFunction, int? index, // List functionOperation ) { bool checkTaskItemAndOperation() { return taskItem.code == 'temp_set' || taskItem.code == 'countdown'; } bool checkListAndIndex() { if (listOfSceneStaticFunction == null || index == null) { return false; } return listOfSceneStaticFunction[index].code == 'temp_set' || listOfSceneStaticFunction[index].code.contains('countdown') || listOfSceneStaticFunction[index].code.contains('presence_time'); } if (checkTaskItemAndOperation()) { if (taskItem.code == 'temp_set') { return AlertDialogTemperatureBody( index: index!, functions: listOfSceneStaticFunction ?? [], functionValue: taskItem.functionValue, ); } else if (taskItem.code == 'countdown') { return AlertDialogCountdown( durationValue: taskItem.functionValue, functionValue: taskItem.functionValue, function: taskItem, ); } } if (checkListAndIndex()) { if (listOfSceneStaticFunction![index!].code == 'temp_set') { return AlertDialogTemperatureBody( index: index, functions: listOfSceneStaticFunction, functionValue: taskItem.functionValue, ); } else { return AlertDialogCountdown( durationValue: listOfSceneStaticFunction[index].functionValue ?? taskItem.functionValue, functionValue: taskItem.functionValue, function: listOfSceneStaticFunction[index], ); } } return AlertDialogFunctionsOperationsBody( index: index ?? 0, functions: listOfSceneStaticFunction ?? [], functionValue: taskItem.functionValue, ); } }