mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2025-07-14 17:25:47 +00:00
114 lines
4.0 KiB
Dart
114 lines
4.0 KiB
Dart
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/enum/operation_dialog_type.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_slider_steps.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<SceneStaticFunction> tasks) {
|
|
final lastTask = tasks.last;
|
|
return tasks.every((task) => task.code == 'delay') ||
|
|
lastTask.code == 'delay';
|
|
}
|
|
|
|
void handleSaveButtonPress(
|
|
BuildContext context,
|
|
TextEditingController sceneNameController,
|
|
List<SceneStaticFunction> 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.deviceId == 'delay') {
|
|
return CreateSceneAction(
|
|
entityId: tasks[index].deviceId,
|
|
actionExecutor: 'delay',
|
|
executorProperty: CreateSceneExecutorProperty(
|
|
functionCode: '',
|
|
functionValue: '',
|
|
delaySeconds: task.functionValue,
|
|
),
|
|
);
|
|
}
|
|
return CreateSceneAction(
|
|
entityId: task.deviceId,
|
|
actionExecutor: 'device_issue',
|
|
executorProperty: CreateSceneExecutorProperty(
|
|
functionCode: task.code,
|
|
functionValue: task.functionValue,
|
|
delaySeconds: 0,
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
context.read<CreateSceneBloc>().add(CreateSceneWithTasksEvent(
|
|
createSceneModel: createSceneModel,
|
|
updateScene: updateScene,
|
|
sceneId: sceneId,
|
|
));
|
|
|
|
Navigator.pop(context);
|
|
}
|
|
}
|
|
|
|
Widget getTheCorrectDialogBody(
|
|
SceneStaticFunction taskItem,
|
|
dynamic functionValue,
|
|
) {
|
|
if (taskItem.operationDialogType == OperationDialogType.temperature) {
|
|
return AlertDialogTemperatureBody(
|
|
taskItem: taskItem,
|
|
functionValue: functionValue ?? taskItem.functionValue,
|
|
);
|
|
} else if ((taskItem.operationDialogType ==
|
|
OperationDialogType.countdown) ||
|
|
(taskItem.operationDialogType == OperationDialogType.delay)) {
|
|
return AlertDialogCountdown(
|
|
durationValue: taskItem.functionValue ?? 0,
|
|
functionValue: functionValue ?? taskItem.functionValue,
|
|
function: taskItem,
|
|
);
|
|
} else if (taskItem.operationDialogType ==
|
|
OperationDialogType.integerSteps) {
|
|
return AlertDialogSliderSteps(
|
|
taskItem: taskItem,
|
|
functionValue: functionValue ?? taskItem.functionValue,
|
|
);
|
|
}
|
|
|
|
return AlertDialogFunctionsOperationsBody(
|
|
taskItem: taskItem,
|
|
functionValue: functionValue ?? taskItem.functionValue,
|
|
);
|
|
}
|
|
}
|