From f285afcbe6b72094faf9d6ef91eb6cdada429845 Mon Sep 17 00:00:00 2001 From: ashrafzarkanisala Date: Mon, 1 Jul 2024 05:23:27 +0300 Subject: [PATCH] adding the delay --- .../scene/helper/scene_logic_helper.dart | 5 +- .../scene/model/scene_details_model.dart | 17 +++-- lib/features/scene/view/scene_tasks_view.dart | 1 - .../scene/widgets/bottom_sheet_widget.dart | 71 ++++++++++++++++++- .../widgets/create_scene_save_button.dart | 3 + 5 files changed, 85 insertions(+), 12 deletions(-) diff --git a/lib/features/scene/helper/scene_logic_helper.dart b/lib/features/scene/helper/scene_logic_helper.dart index 48ca8ed..14fb32e 100644 --- a/lib/features/scene/helper/scene_logic_helper.dart +++ b/lib/features/scene/helper/scene_logic_helper.dart @@ -2,9 +2,11 @@ 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'; @@ -26,8 +28,9 @@ mixin SceneLogicHelper { }) { if (isOnlyDelayOrDelayLast(tasks)) { // Show snackbar indicating restriction + Navigator.pop(context); context.showCustomSnackbar( - message: 'Only a single delay or delay-last operations are allowed.', + message: 'A single delay or delay-last operations are NOT allowed.', icon: const Icon( Icons.error, color: Colors.red, diff --git a/lib/features/scene/model/scene_details_model.dart b/lib/features/scene/model/scene_details_model.dart index 13839b8..069b18e 100644 --- a/lib/features/scene/model/scene_details_model.dart +++ b/lib/features/scene/model/scene_details_model.dart @@ -68,27 +68,26 @@ class Action { } class ExecutorProperty { - final String functionCode; + final String? functionCode; final dynamic functionValue; + final dynamic delaySeconds; ExecutorProperty({ - required this.functionCode, - required this.functionValue, + this.functionCode, + this.functionValue, + this.delaySeconds, }); - factory ExecutorProperty.fromRawJson(String str) => - ExecutorProperty.fromJson(json.decode(str)); - - String toRawJson() => json.encode(toJson()); - factory ExecutorProperty.fromJson(Map json) => ExecutorProperty( - functionCode: json["functionCode"], + functionCode: json["functionCode"] ?? '', functionValue: json["functionValue"], + delaySeconds: json["delaySeconds"], ); Map toJson() => { "functionCode": functionCode, "functionValue": functionValue, + "delaySeconds": delaySeconds, }; } diff --git a/lib/features/scene/view/scene_tasks_view.dart b/lib/features/scene/view/scene_tasks_view.dart index a0e9780..5183faf 100644 --- a/lib/features/scene/view/scene_tasks_view.dart +++ b/lib/features/scene/view/scene_tasks_view.dart @@ -13,7 +13,6 @@ import 'package:syncrow_app/features/scene/widgets/scene_list_tile.dart'; import 'package:syncrow_app/features/shared_widgets/default_container.dart'; import 'package:syncrow_app/features/shared_widgets/default_scaffold.dart'; import 'package:syncrow_app/generated/assets.dart'; -import 'package:syncrow_app/navigation/routing_constants.dart'; import 'package:syncrow_app/utils/context_extension.dart'; import 'package:syncrow_app/utils/resource_manager/color_manager.dart'; import 'package:syncrow_app/utils/resource_manager/strings_manager.dart'; diff --git a/lib/features/scene/widgets/bottom_sheet_widget.dart b/lib/features/scene/widgets/bottom_sheet_widget.dart index c435722..7700fbb 100644 --- a/lib/features/scene/widgets/bottom_sheet_widget.dart +++ b/lib/features/scene/widgets/bottom_sheet_widget.dart @@ -1,4 +1,9 @@ import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:syncrow_app/features/devices/model/device_control_model.dart'; +import 'package:syncrow_app/features/scene/bloc/create_scene/create_scene_bloc.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/scene_list_tile.dart'; import 'package:syncrow_app/features/shared_widgets/light_divider.dart'; @@ -74,10 +79,74 @@ class CustomBottomSheetWidget extends StatelessWidget { size: 16, color: ColorsManager.greyColor, ), - onPressed: () {}, + onPressed: () => _onDelayActionPressed(context), ), ], ), ); } + + void _onDelayActionPressed(BuildContext context) { + final functionValues = + context.read().selectedValues['delay']; + final functions = [ + SceneStaticFunction( + deviceId: 'delay', + deviceName: 'Delay The Action', + icon: '', + operationName: 'Delay The Action', + code: 'delay', + functionValue: 0, + operationalValues: [ + SceneOperationalValue(icon: '', value: 0), + ], + ), + ]; + context.customAlertDialog( + alertBody: AlertDialogCountdown( + durationValue: functions[0].operationalValues.first.value, + function: functions[0], + functionValue: functionValues, + ), + title: functions[0].operationName, + onConfirm: () { + final selectedValue = + context.read().selectedValues[functions[0].code]; + context.read().add(TempHoldSceneTasksEvent( + deviceControlModel: DeviceControlModel( + deviceId: 'delay', + code: functions[0].code, + value: selectedValue, + ), + deviceId: 'delay', + operation: functions[0].operationName, + icon: Assets.delay, + deviceName: 'Delay The Action', + uniqueId: functions[0].uniqueCustomId, + )); + context + .read() + .add(const AddTaskEvent(updateTaskListFromTemp: true)); + Navigator.pop(context); + }, + onDismiss: () { + final tempTaskList = context.read().tempTasksList; + if (tempTaskList.isEmpty) { + context.read().add(const ClearTempTaskListEvent()); + } else { + for (var element in tempTaskList) { + if (element.code == functions[0].code) { + context + .read() + .add(RemoveTempTaskByIdEvent(code: functions[0].code)); + context + .read() + .add(RemoveFromSelectedValueById(code: functions[0].code)); + } + } + } + Navigator.pop(context); + }, + ); + } } diff --git a/lib/features/scene/widgets/create_scene_save_button.dart b/lib/features/scene/widgets/create_scene_save_button.dart index defcfc3..ee55909 100644 --- a/lib/features/scene/widgets/create_scene_save_button.dart +++ b/lib/features/scene/widgets/create_scene_save_button.dart @@ -43,6 +43,9 @@ class _CreateSceneSaveButtonState extends State listener: (context, state) { if (state is CreateSceneWithTasks) { if (state.success == true) { + Navigator.pop(context); + Navigator.pop(context); + context.showCustomSnackbar( message: 'Scene created successfully', icon: const Icon(