Added tab to run to if container

This commit is contained in:
Abdullah Alassaf
2024-11-24 01:12:28 +03:00
parent 590a41ff83
commit 1f49b9bc49
4 changed files with 54 additions and 42 deletions

View File

@ -11,6 +11,9 @@ part 'routine_state.dart';
const spaceId = '25c96044-fadf-44bb-93c7-3c079e527ce6';
class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
bool isAutomation = false;
bool isTabToRun = false;
RoutineBloc() : super(const RoutineState()) {
on<AddToIfContainer>(_onAddToIfContainer);
on<AddToThenContainer>(_onAddToThenContainer);
@ -23,17 +26,15 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
void _onAddToIfContainer(AddToIfContainer event, Emitter<RoutineState> emit) {
// if (!_isDuplicate(state.ifItems, event.item)) {
final updatedIfItems = List<Map<String, dynamic>>.from(state.ifItems)
..add(event.item);
final updatedIfItems = List<Map<String, dynamic>>.from(state.ifItems)..add(event.item);
isTabToRun = event.isTabToRun;
emit(state.copyWith(ifItems: updatedIfItems));
// }
}
void _onAddToThenContainer(
AddToThenContainer event, Emitter<RoutineState> emit) {
void _onAddToThenContainer(AddToThenContainer event, Emitter<RoutineState> emit) {
// if (!_isDuplicate(state.thenItems, event.item)) {
final updatedThenItems = List<Map<String, dynamic>>.from(state.thenItems)
..add(event.item);
final updatedThenItems = List<Map<String, dynamic>>.from(state.thenItems)..add(event.item);
emit(state.copyWith(thenItems: updatedThenItems));
// }
}
@ -47,9 +48,8 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
void _onRemoveFunction(RemoveFunction event, Emitter<RoutineState> emit) {
final functions = List<DeviceFunctionData>.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<RoutineEvent, RoutineState> {
// item['title'] == newItem['title']);
// }
Future<void> _onLoadScenes(
LoadScenes event, Emitter<RoutineState> emit) async {
Future<void> _onLoadScenes(LoadScenes event, Emitter<RoutineState> emit) async {
emit(state.copyWith(isLoading: true, errorMessage: null));
try {
@ -82,8 +81,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
}
}
Future<void> _onLoadAutomation(
LoadAutomation event, Emitter<RoutineState> emit) async {
Future<void> _onLoadAutomation(LoadAutomation event, Emitter<RoutineState> emit) async {
emit(state.copyWith(isLoading: true, errorMessage: null));
try {

View File

@ -9,11 +9,12 @@ abstract class RoutineEvent extends Equatable {
class AddToIfContainer extends RoutineEvent {
final Map<String, dynamic> item;
final bool isTabToRun;
const AddToIfContainer(this.item);
const AddToIfContainer(this.item, this.isTabToRun);
@override
List<Object> get props => [item];
List<Object> get props => [item, isTabToRun];
}
class AddToThenContainer extends RoutineEvent {

View File

@ -22,9 +22,8 @@ class DraggableCard extends StatelessWidget {
Widget build(BuildContext context) {
return BlocBuilder<RoutineBloc, RoutineState>(
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<Map<String, dynamic>>(
data: deviceData,
@ -39,8 +38,7 @@ class DraggableCard extends StatelessWidget {
);
}
Widget _buildCardContent(
BuildContext context, List<DeviceFunctionData> deviceFunctions) {
Widget _buildCardContent(BuildContext context, List<DeviceFunctionData> deviceFunctions) {
return Card(
color: ColorsManager.whiteColors,
child: Container(

View File

@ -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<RoutineBloc>().isTabToRun)
const Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
DraggableCard(
imagePath: Assets.tabToRun,
title: 'Tab to run',
deviceData: {},
),
],
),
if (!context.read<RoutineBloc>().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<RoutineBloc>().add(AddToIfContainer(result));
} else if (!['AC', '1G', '2G', '3G']
.contains(data['productType'])) {
context.read<RoutineBloc>().add(AddToIfContainer(data));
if (!context.read<RoutineBloc>().isTabToRun) {
if (data['deviceId'] == 'tab_to_run') {
context.read<RoutineBloc>().add(AddToIfContainer(data, true));
} else {
final result = await DeviceDialogHelper.showDeviceDialog(context, data);
if (result != null) {
context.read<RoutineBloc>().add(AddToIfContainer(result, false));
} else if (!['AC', '1G', '2G', '3G'].contains(data['productType'])) {
context.read<RoutineBloc>().add(AddToIfContainer(data, false));
}
}
}
},
);