From 1f49b9bc496d9ab68f99c4533506330bd3d890cb Mon Sep 17 00:00:00 2001 From: Abdullah Alassaf Date: Sun, 24 Nov 2024 01:12:28 +0300 Subject: [PATCH] Added tab to run to if container --- .../bloc/routine_bloc/routine_bloc.dart | 24 ++++---- .../bloc/routine_bloc/routine_event.dart | 5 +- lib/pages/routiens/widgets/dragable_card.dart | 8 +-- lib/pages/routiens/widgets/if_container.dart | 59 ++++++++++++------- 4 files changed, 54 insertions(+), 42 deletions(-) diff --git a/lib/pages/routiens/bloc/routine_bloc/routine_bloc.dart b/lib/pages/routiens/bloc/routine_bloc/routine_bloc.dart index 2b711f39..babd820a 100644 --- a/lib/pages/routiens/bloc/routine_bloc/routine_bloc.dart +++ b/lib/pages/routiens/bloc/routine_bloc/routine_bloc.dart @@ -11,6 +11,9 @@ part 'routine_state.dart'; const spaceId = '25c96044-fadf-44bb-93c7-3c079e527ce6'; class RoutineBloc extends Bloc { + bool isAutomation = false; + bool isTabToRun = false; + RoutineBloc() : super(const RoutineState()) { on(_onAddToIfContainer); on(_onAddToThenContainer); @@ -23,17 +26,15 @@ class RoutineBloc extends Bloc { void _onAddToIfContainer(AddToIfContainer event, Emitter emit) { // if (!_isDuplicate(state.ifItems, event.item)) { - final updatedIfItems = List>.from(state.ifItems) - ..add(event.item); + final updatedIfItems = List>.from(state.ifItems)..add(event.item); + isTabToRun = event.isTabToRun; emit(state.copyWith(ifItems: updatedIfItems)); // } } - void _onAddToThenContainer( - AddToThenContainer event, Emitter emit) { + void _onAddToThenContainer(AddToThenContainer event, Emitter emit) { // if (!_isDuplicate(state.thenItems, event.item)) { - final updatedThenItems = List>.from(state.thenItems) - ..add(event.item); + final updatedThenItems = List>.from(state.thenItems)..add(event.item); emit(state.copyWith(thenItems: updatedThenItems)); // } } @@ -47,9 +48,8 @@ class RoutineBloc extends Bloc { void _onRemoveFunction(RemoveFunction event, Emitter emit) { final functions = List.from(state.selectedFunctions) - ..removeWhere((f) => - f.functionCode == event.function.functionCode && - f.value == event.function.value); + ..removeWhere( + (f) => f.functionCode == event.function.functionCode && f.value == event.function.value); emit(state.copyWith(selectedFunctions: functions)); } @@ -64,8 +64,7 @@ class RoutineBloc extends Bloc { // item['title'] == newItem['title']); // } - Future _onLoadScenes( - LoadScenes event, Emitter emit) async { + Future _onLoadScenes(LoadScenes event, Emitter emit) async { emit(state.copyWith(isLoading: true, errorMessage: null)); try { @@ -82,8 +81,7 @@ class RoutineBloc extends Bloc { } } - Future _onLoadAutomation( - LoadAutomation event, Emitter emit) async { + Future _onLoadAutomation(LoadAutomation event, Emitter emit) async { emit(state.copyWith(isLoading: true, errorMessage: null)); try { diff --git a/lib/pages/routiens/bloc/routine_bloc/routine_event.dart b/lib/pages/routiens/bloc/routine_bloc/routine_event.dart index 3321af5e..8ac1efd7 100644 --- a/lib/pages/routiens/bloc/routine_bloc/routine_event.dart +++ b/lib/pages/routiens/bloc/routine_bloc/routine_event.dart @@ -9,11 +9,12 @@ abstract class RoutineEvent extends Equatable { class AddToIfContainer extends RoutineEvent { final Map item; + final bool isTabToRun; - const AddToIfContainer(this.item); + const AddToIfContainer(this.item, this.isTabToRun); @override - List get props => [item]; + List get props => [item, isTabToRun]; } class AddToThenContainer extends RoutineEvent { diff --git a/lib/pages/routiens/widgets/dragable_card.dart b/lib/pages/routiens/widgets/dragable_card.dart index 197ea0d2..fa4f7f7c 100644 --- a/lib/pages/routiens/widgets/dragable_card.dart +++ b/lib/pages/routiens/widgets/dragable_card.dart @@ -22,9 +22,8 @@ class DraggableCard extends StatelessWidget { Widget build(BuildContext context) { return BlocBuilder( builder: (context, state) { - final deviceFunctions = state.selectedFunctions - .where((f) => f.entityId == deviceData['deviceId']) - .toList(); + final deviceFunctions = + state.selectedFunctions.where((f) => f.entityId == deviceData['deviceId']).toList(); return Draggable>( data: deviceData, @@ -39,8 +38,7 @@ class DraggableCard extends StatelessWidget { ); } - Widget _buildCardContent( - BuildContext context, List deviceFunctions) { + Widget _buildCardContent(BuildContext context, List deviceFunctions) { return Card( color: ColorsManager.whiteColors, child: Container( diff --git a/lib/pages/routiens/widgets/if_container.dart b/lib/pages/routiens/widgets/if_container.dart index 779453f1..a7afa245 100644 --- a/lib/pages/routiens/widgets/if_container.dart +++ b/lib/pages/routiens/widgets/if_container.dart @@ -3,6 +3,7 @@ import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:syncrow_web/pages/routiens/bloc/routine_bloc/routine_bloc.dart'; import 'package:syncrow_web/pages/routiens/helper/dialog_helper/device_dialog_helper.dart'; import 'package:syncrow_web/pages/routiens/widgets/dragable_card.dart'; +import 'package:syncrow_web/utils/constants/assets.dart'; class IfContainer extends StatelessWidget { const IfContainer({super.key}); @@ -19,35 +20,49 @@ class IfContainer extends StatelessWidget { child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ - const Text('IF', - style: - TextStyle(fontSize: 18, fontWeight: FontWeight.bold)), + const Text('IF', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)), const SizedBox(height: 16), - Wrap( - spacing: 8, - runSpacing: 8, - children: state.ifItems - .map((item) => DraggableCard( - key: Key(item['key']!), - imagePath: item['imagePath']!, - title: item['title']!, - deviceData: item, - )) - .toList(), - ), + if (context.read().isTabToRun) + const Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + DraggableCard( + imagePath: Assets.tabToRun, + title: 'Tab to run', + deviceData: {}, + ), + ], + ), + if (!context.read().isTabToRun) + Wrap( + spacing: 8, + runSpacing: 8, + children: state.ifItems + .map((item) => DraggableCard( + // key: Key(item['key']!), + imagePath: item['imagePath'] ?? '', + title: item['title'] ?? 'Tab to run', + deviceData: item, + )) + .toList(), + ), ], ), ); }, onWillAccept: (data) => data != null, onAccept: (data) async { - final result = - await DeviceDialogHelper.showDeviceDialog(context, data); - if (result != null) { - context.read().add(AddToIfContainer(result)); - } else if (!['AC', '1G', '2G', '3G'] - .contains(data['productType'])) { - context.read().add(AddToIfContainer(data)); + if (!context.read().isTabToRun) { + if (data['deviceId'] == 'tab_to_run') { + context.read().add(AddToIfContainer(data, true)); + } else { + final result = await DeviceDialogHelper.showDeviceDialog(context, data); + if (result != null) { + context.read().add(AddToIfContainer(result, false)); + } else if (!['AC', '1G', '2G', '3G'].contains(data['productType'])) { + context.read().add(AddToIfContainer(data, false)); + } + } } }, );