This commit is contained in:
ashrafzarkanisala
2024-07-29 22:36:05 +03:00
parent 63cdca5717
commit 2f6d073955
12 changed files with 441 additions and 359 deletions

View File

@ -2,8 +2,14 @@ import 'dart:async';
import 'package:bloc/bloc.dart'; import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart'; import 'package:equatable/equatable.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/enum/create_scene_enum.dart';
import 'package:syncrow_app/features/scene/enum/operation_dialog_type.dart';
import 'package:syncrow_app/features/scene/model/smart_scene_enable.dart'; import 'package:syncrow_app/features/scene/model/smart_scene_enable.dart';
import 'package:syncrow_app/generated/assets.dart';
import 'package:syncrow_app/navigation/navigation_service.dart';
part 'smart_scene_select_dart_event.dart'; part 'smart_scene_select_dart_event.dart';
part 'smart_scene_select_dart_state.dart'; part 'smart_scene_select_dart_state.dart';
@ -19,7 +25,29 @@ class SmartSceneSelectBloc
FutureOr<void> _onSmartSceneEnable( FutureOr<void> _onSmartSceneEnable(
SmartSceneEnableEvent event, Emitter<SmartSceneSelectState> emit) { SmartSceneEnableEvent event, Emitter<SmartSceneSelectState> emit) {
smartSceneEnable = event.smartSceneEnable; smartSceneEnable = event.smartSceneEnable;
NavigationService.navigatorKey.currentState!.context
.read<CreateSceneBloc>()
.add(TempHoldSceneTasksEvent(
deviceControlModel: DeviceControlModel(
deviceId: smartSceneEnable?.entityId ?? '',
code: CreateSceneEnum.smartSceneSelect.name,
value: '',
),
deviceId: smartSceneEnable?.sceneORAutomationName ?? '',
operation: smartSceneEnable?.actionExecutor ?? '',
icon: smartSceneEnable?.isAutomation == true
? Assets.player
: Assets.handClickIcon,
deviceName: smartSceneEnable?.sceneORAutomationName ?? '',
uniqueId: '',
operationType: OperationDialogType.none,
isAutomation: false,
));
emit(SmartSceneSelected(smartSceneEnable: smartSceneEnable!)); emit(SmartSceneSelected(smartSceneEnable: smartSceneEnable!));
NavigationService.navigatorKey.currentState!.context
.read<CreateSceneBloc>()
.add(const AddTaskEvent(isAutomation: false));
} }
FutureOr<void> _smartSceneClear( FutureOr<void> _smartSceneClear(

View File

@ -1,5 +1,6 @@
enum CreateSceneEnum { enum CreateSceneEnum {
tabToRun, tabToRun,
deviceStatusChanges, deviceStatusChanges,
smartSceneSelect,
none, none,
} }

View File

@ -3,6 +3,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/app_layout/bloc/home_cubit.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/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/bloc/smart_scene/smart_scene_select_dart_bloc.dart';
import 'package:syncrow_app/features/scene/enum/create_scene_enum.dart';
import 'package:syncrow_app/features/scene/enum/operation_dialog_type.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_automation_model.dart';
import 'package:syncrow_app/features/scene/model/create_scene_model.dart'; import 'package:syncrow_app/features/scene/model/create_scene_model.dart';
@ -32,10 +33,7 @@ mixin SceneLogicHelper {
final sceneBloc = context.read<CreateSceneBloc>(); final sceneBloc = context.read<CreateSceneBloc>();
final smartSceneBloc = context.read<SmartSceneSelectBloc>(); final smartSceneBloc = context.read<SmartSceneSelectBloc>();
if (isAutomation) {
// Handle Automation Creation
if (isOnlyDelayOrDelayLast(actions)) { if (isOnlyDelayOrDelayLast(actions)) {
Navigator.pop(context);
context.showCustomSnackbar( context.showCustomSnackbar(
message: 'A single delay or delay-last operations are NOT allowed.', message: 'A single delay or delay-last operations are NOT allowed.',
icon: const Icon( icon: const Icon(
@ -43,7 +41,10 @@ mixin SceneLogicHelper {
color: Colors.red, color: Colors.red,
), ),
); );
} else { return;
}
if (isAutomation) {
final createAutomationModel = CreateAutomationModel( final createAutomationModel = CreateAutomationModel(
unitUuid: HomeCubit.getInstance().selectedSpace!.id ?? '', unitUuid: HomeCubit.getInstance().selectedSpace!.id ?? '',
automationName: sceneName.text, automationName: sceneName.text,
@ -82,6 +83,13 @@ mixin SceneLogicHelper {
), ),
); );
} }
if (task.code == CreateSceneEnum.smartSceneSelect.name) {
return CreateSceneAction(
entityId: smartSceneBloc.smartSceneEnable?.entityId ?? '',
actionExecutor:
smartSceneBloc.smartSceneEnable?.actionExecutor ?? '',
executorProperty: null);
}
return CreateSceneAction( return CreateSceneAction(
entityId: task.deviceId, entityId: task.deviceId,
actionExecutor: 'device_issue', actionExecutor: 'device_issue',
@ -93,12 +101,6 @@ mixin SceneLogicHelper {
); );
}, },
), ),
if (smartSceneBloc.smartSceneEnable != null)
CreateSceneAction(
entityId: smartSceneBloc.smartSceneEnable?.entityId ?? '',
actionExecutor:
smartSceneBloc.smartSceneEnable?.actionExecutor ?? '',
executorProperty: null)
], ],
); );
sceneBloc.add(CreateSceneWithTasksEvent( sceneBloc.add(CreateSceneWithTasksEvent(
@ -107,20 +109,7 @@ mixin SceneLogicHelper {
sceneId: sceneId, sceneId: sceneId,
createAutomationModel: createAutomationModel, createAutomationModel: createAutomationModel,
)); ));
Navigator.pop(context);
}
} else { } 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( final createSceneModel = CreateSceneModel(
unitUuid: HomeCubit.getInstance().selectedSpace!.id ?? '', unitUuid: HomeCubit.getInstance().selectedSpace!.id ?? '',
sceneName: sceneName.text, sceneName: sceneName.text,
@ -141,6 +130,13 @@ mixin SceneLogicHelper {
), ),
); );
} }
if (task.code == CreateSceneEnum.smartSceneSelect.name) {
return CreateSceneAction(
entityId: smartSceneBloc.smartSceneEnable?.entityId ?? '',
actionExecutor:
smartSceneBloc.smartSceneEnable?.actionExecutor ?? '',
executorProperty: null);
}
return CreateSceneAction( return CreateSceneAction(
entityId: task.deviceId, entityId: task.deviceId,
actionExecutor: 'device_issue', actionExecutor: 'device_issue',
@ -152,12 +148,6 @@ mixin SceneLogicHelper {
); );
}, },
), ),
if (smartSceneBloc.smartSceneEnable != null)
CreateSceneAction(
entityId: smartSceneBloc.smartSceneEnable?.entityId ?? '',
actionExecutor:
smartSceneBloc.smartSceneEnable?.actionExecutor ?? '',
executorProperty: null)
], ],
); );
sceneBloc.add(CreateSceneWithTasksEvent( sceneBloc.add(CreateSceneWithTasksEvent(
@ -166,8 +156,6 @@ mixin SceneLogicHelper {
updateScene: updateScene, updateScene: updateScene,
sceneId: sceneId, sceneId: sceneId,
)); ));
Navigator.pop(context);
}
} }
} }

View File

@ -102,12 +102,18 @@ class CreateSceneAction {
} }
Map<String, dynamic> toMap() { Map<String, dynamic> toMap() {
if (executorProperty != null) {
return { return {
'entityId': entityId, 'entityId': entityId,
'actionExecutor': actionExecutor, 'actionExecutor': actionExecutor,
if (executorProperty != null)
'executorProperty': executorProperty?.toMap(actionExecutor), 'executorProperty': executorProperty?.toMap(actionExecutor),
}; };
} else {
return {
'entityId': entityId,
'actionExecutor': actionExecutor,
};
}
} }
factory CreateSceneAction.fromMap(Map<String, dynamic> map) { factory CreateSceneAction.fromMap(Map<String, dynamic> map) {

View File

@ -1,16 +1,22 @@
class SmartSceneEnable { class SmartSceneEnable {
final String entityId; final String entityId;
final String actionExecutor; final String actionExecutor;
final String sceneORAutomationName;
final bool isAutomation;
SmartSceneEnable({ SmartSceneEnable({
required this.entityId, required this.entityId,
required this.actionExecutor, required this.actionExecutor,
required this.sceneORAutomationName,
required this.isAutomation,
}); });
factory SmartSceneEnable.fromJson(Map<String, dynamic> json) { factory SmartSceneEnable.fromJson(Map<String, dynamic> json) {
return SmartSceneEnable( return SmartSceneEnable(
entityId: json['entityId'], entityId: json['entityId'],
actionExecutor: json['actionExecutor'], actionExecutor: json['actionExecutor'],
sceneORAutomationName: json['sceneORAutomationName'],
isAutomation: json['isAutomation'],
); );
} }
@ -18,6 +24,8 @@ class SmartSceneEnable {
return { return {
'entityId': entityId, 'entityId': entityId,
'actionExecutor': actionExecutor, 'actionExecutor': actionExecutor,
'sceneORAutomationName': sceneORAutomationName,
'isAutomation': isAutomation,
}; };
} }
} }

View File

@ -36,6 +36,13 @@ class SceneTasksView extends StatelessWidget {
? sceneSettings.sceneName ? sceneSettings.sceneName
: StringsManager.createScene, : StringsManager.createScene,
padding: EdgeInsets.zero, padding: EdgeInsets.zero,
leading: IconButton(
onPressed: () {
navigateToRoute(context, Routes.homeRoute);
},
icon: const Icon(
Icons.arrow_back_ios,
)),
actions: [ actions: [
Visibility( Visibility(
visible: sceneSettings.sceneType.isNotEmpty, visible: sceneSettings.sceneType.isNotEmpty,

View File

@ -1,4 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/scene/bloc/create_scene/create_scene_bloc.dart';
import 'package:syncrow_app/features/scene/enum/create_scene_enum.dart';
import 'package:syncrow_app/features/scene/widgets/scene_list_tile.dart'; import 'package:syncrow_app/features/scene/widgets/scene_list_tile.dart';
import 'package:syncrow_app/features/scene/widgets/select_smart_scene/smart_enable_autoamtion.dart'; import 'package:syncrow_app/features/scene/widgets/select_smart_scene/smart_enable_autoamtion.dart';
import 'package:syncrow_app/features/scene/widgets/select_smart_scene/smart_enable_tab_run.dart'; import 'package:syncrow_app/features/scene/widgets/select_smart_scene/smart_enable_tab_run.dart';
@ -15,6 +18,7 @@ class SmartAutomationSelectView extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final sceneType = context.read<CreateSceneBloc>().sceneType;
return DefaultScaffold( return DefaultScaffold(
title: "Select Smart Scene", title: "Select Smart Scene",
padding: const EdgeInsets.only(top: 24), padding: const EdgeInsets.only(top: 24),
@ -25,13 +29,18 @@ class SmartAutomationSelectView extends StatelessWidget {
icon: const Icon( icon: const Icon(
Icons.arrow_back_ios, Icons.arrow_back_ios,
)), )),
height: 260, height: sceneType.name == CreateSceneEnum.deviceStatusChanges.name
? 260
: 200,
child: DefaultContainer( child: DefaultContainer(
width: double.infinity, width: double.infinity,
child: Column( child: Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
SceneListTile( Visibility(
visible:
sceneType.name == CreateSceneEnum.deviceStatusChanges.name,
child: SceneListTile(
assetPath: Assets.handClickIcon, assetPath: Assets.handClickIcon,
titleString: "Tap To Run", titleString: "Tap To Run",
subtitleString: '', subtitleString: '',
@ -45,9 +54,14 @@ class SmartAutomationSelectView extends StatelessWidget {
); );
}, },
), ),
const Divider( ),
Visibility(
visible:
sceneType.name == CreateSceneEnum.deviceStatusChanges.name,
child: const Divider(
color: ColorsManager.dividerColor, color: ColorsManager.dividerColor,
), ),
),
SceneListTile( SceneListTile(
assetPath: Assets.refreshIcon, assetPath: Assets.refreshIcon,
titleString: "Automation", titleString: "Automation",

View File

@ -8,6 +8,7 @@ import 'package:syncrow_app/features/shared_widgets/text_widgets/body_large.dart
import 'package:syncrow_app/navigation/navigate_to_route.dart'; import 'package:syncrow_app/navigation/navigate_to_route.dart';
import 'package:syncrow_app/navigation/routing_constants.dart'; import 'package:syncrow_app/navigation/routing_constants.dart';
import 'package:syncrow_app/utils/context_extension.dart'; import 'package:syncrow_app/utils/context_extension.dart';
import 'package:syncrow_app/utils/helpers/snack_bar.dart';
import 'package:syncrow_app/utils/resource_manager/color_manager.dart'; import 'package:syncrow_app/utils/resource_manager/color_manager.dart';
class CreateSceneSaveButton extends StatefulWidget { class CreateSceneSaveButton extends StatefulWidget {
@ -57,13 +58,7 @@ class _CreateSceneSaveButtonState extends State<CreateSceneSaveButton>
sceneNameController.text = ''; sceneNameController.text = '';
} }
} else if (state is CreateSceneError) { } else if (state is CreateSceneError) {
context.showCustomSnackbar( CustomSnackBar.displaySnackBar(state.message);
message: state.message,
icon: const Icon(
Icons.error,
color: Colors.red,
),
);
} }
}, },
builder: (context, state) { builder: (context, state) {
@ -106,6 +101,7 @@ class _CreateSceneSaveButtonState extends State<CreateSceneSaveButton>
), ),
title: 'Scene Name', title: 'Scene Name',
onConfirm: () { onConfirm: () {
Navigator.pop(context);
if (sceneNameController.text.isNotEmpty) { if (sceneNameController.text.isNotEmpty) {
handleSaveButtonPress( handleSaveButtonPress(
context, context,

View File

@ -27,6 +27,8 @@ class IFDefaultContainer extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocBuilder<CreateSceneBloc, CreateSceneState>(
builder: (context, state) {
final sceneType = context.read<CreateSceneBloc>().sceneType; final sceneType = context.read<CreateSceneBloc>().sceneType;
return DefaultContainer( return DefaultContainer(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 2), padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 2),
@ -110,7 +112,8 @@ class IFDefaultContainer extends StatelessWidget {
builder: (context, state) { builder: (context, state) {
bool isClickable = false; bool isClickable = false;
if (state is AddSceneTask) { if (state is AddSceneTask) {
isClickable = state.automationTasksList?.isNotEmpty ?? false; isClickable =
state.automationTasksList?.isNotEmpty ?? false;
} }
return GestureDetector( return GestureDetector(
onTap: isClickable onTap: isClickable
@ -172,18 +175,21 @@ class IFDefaultContainer extends StatelessWidget {
titleString: '+ Add Condition', titleString: '+ Add Condition',
textAlign: TextAlign.center, textAlign: TextAlign.center,
onPressed: () { onPressed: () {
final sceneType = context.read<CreateSceneBloc>().sceneType; final sceneType =
context.read<CreateSceneBloc>().sceneType;
if (sceneType.name == CreateSceneEnum.none.name) { if (sceneType.name == CreateSceneEnum.none.name) {
Navigator.push( Navigator.push(
context, context,
CustomPageRoute( CustomPageRoute(
builder: (context) => const CreateSceneView())); builder: (context) =>
const CreateSceneView()));
} else { } else {
Navigator.pushNamed( Navigator.pushNamed(
context, context,
Routes.sceneControlDevicesRoute, Routes.sceneControlDevicesRoute,
arguments: SceneSettingsRouteArguments( arguments: SceneSettingsRouteArguments(
sceneType: CreateSceneEnum.deviceStatusChanges.name, sceneType:
CreateSceneEnum.deviceStatusChanges.name,
sceneId: '', sceneId: '',
sceneName: '', sceneName: '',
), ),
@ -195,5 +201,7 @@ class IFDefaultContainer extends StatelessWidget {
], ],
), ),
); );
},
);
} }
} }

View File

@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/flutter_svg.dart'; import 'package:flutter_svg/flutter_svg.dart';
import 'package:syncrow_app/features/scene/bloc/create_scene/create_scene_bloc.dart'; import 'package:syncrow_app/features/scene/bloc/create_scene/create_scene_bloc.dart';
import 'package:syncrow_app/features/scene/enum/create_scene_enum.dart';
import 'package:syncrow_app/features/scene/helper/scene_logic_helper.dart'; import 'package:syncrow_app/features/scene/helper/scene_logic_helper.dart';
import 'package:syncrow_app/features/scene/helper/scene_operations_data_helper.dart'; import 'package:syncrow_app/features/scene/helper/scene_operations_data_helper.dart';
import 'package:syncrow_app/features/scene/model/scene_static_function.dart'; import 'package:syncrow_app/features/scene/model/scene_static_function.dart';
@ -62,7 +63,9 @@ class ThenAddedTasksContainer extends StatelessWidget
operationValue = functionValue.toString(); operationValue = functionValue.toString();
} }
return DefaultContainer( return DefaultContainer(
onTap: () { onTap: taskItem.code == CreateSceneEnum.smartSceneSelect.name
? null
: () {
List<SceneStaticFunction> functionOperation = []; List<SceneStaticFunction> functionOperation = [];
/// get the task functions /// get the task functions
@ -73,11 +76,13 @@ class ThenAddedTasksContainer extends StatelessWidget
/// show alert dialog based on type /// show alert dialog based on type
context.customAlertDialog( context.customAlertDialog(
alertBody: getTheCorrectDialogBody(functionOperation.first, null, alertBody: getTheCorrectDialogBody(
functionOperation.first, null,
isAutomation: isAutomation ?? false), isAutomation: isAutomation ?? false),
title: functionOperation.first.operationName, title: functionOperation.first.operationName,
onConfirm: () { onConfirm: () {
final savedCode = functionOperation.first.deviceId.contains('delay') final savedCode =
functionOperation.first.deviceId.contains('delay')
? 'delay' ? 'delay'
: functionOperation.first.code; : functionOperation.first.code;
if (isAutomation == true) { if (isAutomation == true) {
@ -96,7 +101,8 @@ class ThenAddedTasksContainer extends StatelessWidget
debugPrint('Error adding UpdateTaskEvent: $e'); debugPrint('Error adding UpdateTaskEvent: $e');
} }
} else { } else {
final selectedValue = createSceneBloc.selectedValues[savedCode]; final selectedValue =
createSceneBloc.selectedValues[savedCode];
try { try {
createSceneBloc.add( createSceneBloc.add(
@ -171,12 +177,20 @@ class ThenAddedTasksContainer extends StatelessWidget
subtitleWidget: Row( subtitleWidget: Row(
children: [ children: [
BodyMedium( BodyMedium(
text: "${taskItem.operationName}: ", text: taskItem.code == CreateSceneEnum.smartSceneSelect.name
? taskItem.icon.contains('player')
? 'Automation: '
: "Tab-To-Run: "
: "${taskItem.operationName}: ",
fontColor: ColorsManager.secondaryTextColor, fontColor: ColorsManager.secondaryTextColor,
fontWeight: FontWeight.normal, fontWeight: FontWeight.normal,
), ),
BodyMedium( BodyMedium(
text: operationValue, text: taskItem.code == CreateSceneEnum.smartSceneSelect.name
? taskItem.operationName == 'rule_enable'
? 'Enable'
: "Disable"
: operationValue,
fontColor: ColorsManager.secondaryTextColor, fontColor: ColorsManager.secondaryTextColor,
fontWeight: FontWeight.normal, fontWeight: FontWeight.normal,
), ),

View File

@ -125,6 +125,7 @@ class _SmartSceneSelectAutomationListState
alertBody: EnableDisableAutomationDialog( alertBody: EnableDisableAutomationDialog(
automationId: automation.id, automationId: automation.id,
descriptionSelected: descriptionSelected, descriptionSelected: descriptionSelected,
sceneORAutomationName: automation.name,
), ),
title: automation.name, title: automation.name,
onConfirm: () { onConfirm: () {
@ -157,6 +158,7 @@ class _SmartSceneSelectAutomationListState
alertBody: EnableDisableAutomationDialog( alertBody: EnableDisableAutomationDialog(
automationId: automation.id, automationId: automation.id,
descriptionSelected: descriptionSelected, descriptionSelected: descriptionSelected,
sceneORAutomationName: automation.name,
), ),
title: automation.name, title: automation.name,
onConfirm: () { onConfirm: () {
@ -191,10 +193,12 @@ class EnableDisableAutomationDialog extends StatefulWidget {
super.key, super.key,
required this.automationId, required this.automationId,
required this.descriptionSelected, required this.descriptionSelected,
required this.sceneORAutomationName,
}); });
final String automationId; final String automationId;
final String descriptionSelected; final String descriptionSelected;
final String sceneORAutomationName;
@override @override
State<EnableDisableAutomationDialog> createState() => State<EnableDisableAutomationDialog> createState() =>
@ -253,7 +257,9 @@ class _EnableDisableAutomationDialogState
SmartSceneEnable( SmartSceneEnable(
entityId: widget.automationId, entityId: widget.automationId,
actionExecutor: value!, actionExecutor: value!,
), sceneORAutomationName:
widget.sceneORAutomationName,
isAutomation: true),
)); ));
}, },
), ),
@ -267,6 +273,8 @@ class _EnableDisableAutomationDialogState
SmartSceneEnable( SmartSceneEnable(
entityId: widget.automationId, entityId: widget.automationId,
actionExecutor: operation.value, actionExecutor: operation.value,
sceneORAutomationName: widget.sceneORAutomationName,
isAutomation: true,
), ),
)); ));
}, },

View File

@ -89,6 +89,8 @@ class _SmartSceneSelectTabToRunListState
.add(SmartSceneEnableEvent(SmartSceneEnable( .add(SmartSceneEnableEvent(SmartSceneEnable(
entityId: scene.id, entityId: scene.id,
actionExecutor: 'rule_enable', actionExecutor: 'rule_enable',
sceneORAutomationName: scene.name,
isAutomation: false,
))); )));
} }
}), }),
@ -101,6 +103,8 @@ class _SmartSceneSelectTabToRunListState
.add(SmartSceneEnableEvent(SmartSceneEnable( .add(SmartSceneEnableEvent(SmartSceneEnable(
entityId: scene.id, entityId: scene.id,
actionExecutor: 'rule_enable', actionExecutor: 'rule_enable',
sceneORAutomationName: scene.name,
isAutomation: false,
))); )));
}, },
); );