mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2025-07-15 01:35:23 +00:00
70 lines
2.4 KiB
Dart
70 lines
2.4 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/model/create_scene_model.dart';
|
|
import 'package:syncrow_app/features/scene/model/scene_static_function.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,
|
|
) {
|
|
if (isOnlyDelayOrDelayLast(tasks)) {
|
|
// Show snackbar indicating restriction
|
|
context.showCustomSnackbar(
|
|
message: 'Only a single delay or delay-last operations are 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<CreateSceneBloc>()
|
|
.add(CreateSceneWithTasksEvent(createSceneModel: createSceneModel));
|
|
|
|
Navigator.pop(context);
|
|
}
|
|
}
|
|
}
|