mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2025-07-15 17:47:28 +00:00
198 lines
7.5 KiB
Dart
198 lines
7.5 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/bloc/smart_scene/smart_scene_select_dart_bloc.dart';
|
|
import 'package:syncrow_app/features/scene/enum/operation_dialog_type.dart';
|
|
import 'package:syncrow_app/features/scene/model/create_automation_model.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, {
|
|
required bool updateScene,
|
|
required String sceneId,
|
|
required bool isAutomation,
|
|
required TextEditingController sceneName,
|
|
required List<SceneStaticFunction> actions,
|
|
required List<SceneStaticFunction> conditions,
|
|
}) {
|
|
final sceneBloc = context.read<CreateSceneBloc>();
|
|
final smartSceneBloc = context.read<SmartSceneSelectBloc>();
|
|
|
|
if (isAutomation) {
|
|
// Handle Automation Creation
|
|
if (isOnlyDelayOrDelayLast(actions)) {
|
|
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 createAutomationModel = CreateAutomationModel(
|
|
unitUuid: HomeCubit.getInstance().selectedSpace!.id ?? '',
|
|
automationName: sceneName.text,
|
|
decisionExpr: sceneBloc.conditionRule,
|
|
effectiveTime: sceneBloc.effectiveTime ??
|
|
EffectiveTime(start: '00:00', end: '23:59', loops: '1111111'),
|
|
conditions: List.generate(
|
|
conditions.length,
|
|
(index) {
|
|
final task = conditions[index];
|
|
return CreateCondition(
|
|
code: index + 1,
|
|
entityId: task.deviceId,
|
|
entityType: 'device_report',
|
|
expr: ConditionExpr(
|
|
statusCode: task.code,
|
|
comparator: task.comparator ?? '==',
|
|
statusValue: task.functionValue,
|
|
),
|
|
);
|
|
},
|
|
),
|
|
actions: List.generate(
|
|
actions.length,
|
|
(index) {
|
|
final task = actions[index];
|
|
if (task.deviceId == 'delay') {
|
|
return CreateSceneAction(
|
|
entityId: actions[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,
|
|
),
|
|
);
|
|
},
|
|
)..add(CreateSceneAction(
|
|
entityId: smartSceneBloc.smartSceneEnable?.entityId ?? '',
|
|
actionExecutor:
|
|
smartSceneBloc.smartSceneEnable?.actionExecutor ?? '',
|
|
executorProperty: null)),
|
|
);
|
|
sceneBloc.add(CreateSceneWithTasksEvent(
|
|
createSceneModel: null,
|
|
updateScene: updateScene,
|
|
sceneId: sceneId,
|
|
createAutomationModel: createAutomationModel,
|
|
));
|
|
Navigator.pop(context);
|
|
}
|
|
} else {
|
|
if (isOnlyDelayOrDelayLast(actions)) {
|
|
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 {
|
|
// Handle Scene Creation
|
|
final createSceneModel = CreateSceneModel(
|
|
unitUuid: HomeCubit.getInstance().selectedSpace!.id ?? '',
|
|
sceneName: sceneName.text,
|
|
decisionExpr: 'and',
|
|
actions: List.generate(
|
|
actions.length,
|
|
(index) {
|
|
final task = actions[index];
|
|
if (task.deviceId == 'delay') {
|
|
return CreateSceneAction(
|
|
entityId: actions[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,
|
|
),
|
|
);
|
|
},
|
|
)..add(CreateSceneAction(
|
|
entityId: smartSceneBloc.smartSceneEnable?.entityId ?? '',
|
|
actionExecutor:
|
|
smartSceneBloc.smartSceneEnable?.actionExecutor ?? '',
|
|
executorProperty: null)),
|
|
);
|
|
sceneBloc.add(CreateSceneWithTasksEvent(
|
|
createSceneModel: createSceneModel,
|
|
createAutomationModel: null,
|
|
updateScene: updateScene,
|
|
sceneId: sceneId,
|
|
));
|
|
Navigator.pop(context);
|
|
}
|
|
}
|
|
}
|
|
|
|
Widget getTheCorrectDialogBody(
|
|
SceneStaticFunction taskItem, dynamic functionValue,
|
|
{required bool isAutomation}) {
|
|
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,
|
|
isAutomation: isAutomation,
|
|
);
|
|
}
|
|
|
|
return AlertDialogFunctionsOperationsBody(
|
|
taskItem: taskItem,
|
|
functionValue: functionValue ?? taskItem.functionValue,
|
|
isAutomation: isAutomation,
|
|
);
|
|
}
|
|
}
|