push adding the selected functiond and device to then container

This commit is contained in:
ashrafzarkanisala
2024-11-23 01:39:05 +03:00
parent 53eb18c075
commit 5e91d7c03a
7 changed files with 32 additions and 20 deletions

View File

@ -15,7 +15,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
on<AddToThenContainer>(_onAddToThenContainer); on<AddToThenContainer>(_onAddToThenContainer);
on<LoadScenes>(_onLoadScenes); on<LoadScenes>(_onLoadScenes);
on<LoadAutomation>(_onLoadAutomation); on<LoadAutomation>(_onLoadAutomation);
on<AddFunction>(_onAddFunction); on<AddFunctionToRoutine>(_onAddFunction);
on<RemoveFunction>(_onRemoveFunction); on<RemoveFunction>(_onRemoveFunction);
on<ClearFunctions>(_onClearFunctions); on<ClearFunctions>(_onClearFunctions);
} }
@ -37,7 +37,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
} }
} }
void _onAddFunction(AddFunction event, Emitter<RoutineState> emit) { void _onAddFunction(AddFunctionToRoutine event, Emitter<RoutineState> emit) {
final functions = List<DeviceFunctionData>.from(state.selectedFunctions); final functions = List<DeviceFunctionData>.from(state.selectedFunctions);
functions.add(event.function); functions.add(event.function);
emit(state.copyWith(selectedFunctions: functions)); emit(state.copyWith(selectedFunctions: functions));

View File

@ -43,9 +43,9 @@ class LoadAutomation extends RoutineEvent {
List<Object> get props => [unitId]; List<Object> get props => [unitId];
} }
class AddFunction extends RoutineEvent { class AddFunctionToRoutine extends RoutineEvent {
final DeviceFunctionData function; final DeviceFunctionData function;
const AddFunction(this.function); const AddFunctionToRoutine(this.function);
@override @override
List<Object> get props => [function]; List<Object> get props => [function];
} }

View File

@ -1,5 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart'; import 'package:flutter_svg/flutter_svg.dart';
import 'package:syncrow_web/pages/routiens/bloc/routine_bloc/routine_bloc.dart';
import 'package:syncrow_web/pages/routiens/models/ac/ac_function.dart'; import 'package:syncrow_web/pages/routiens/models/ac/ac_function.dart';
import 'package:syncrow_web/pages/routiens/models/device_functions.dart'; import 'package:syncrow_web/pages/routiens/models/device_functions.dart';
import 'package:syncrow_web/pages/routiens/widgets/dialog_footer.dart'; import 'package:syncrow_web/pages/routiens/widgets/dialog_footer.dart';
@ -36,9 +37,6 @@ class ACHelper {
contentPadding: EdgeInsets.zero, contentPadding: EdgeInsets.zero,
content: BlocBuilder<FunctionBloc, FunctionBlocState>( content: BlocBuilder<FunctionBloc, FunctionBlocState>(
builder: (context, state) { builder: (context, state) {
debugPrint(
'Current state - Selected: ${state.selectedFunction}, Functions: ${state.functions}');
final selectedFunction = state.selectedFunction; final selectedFunction = state.selectedFunction;
final selectedFunctionData = selectedFunction != null final selectedFunctionData = selectedFunction != null
? state.functions.firstWhere( ? state.functions.firstWhere(
@ -118,7 +116,18 @@ class ACHelper {
}, },
onConfirm: selectedFunction != null && onConfirm: selectedFunction != null &&
selectedFunctionData?.value != null selectedFunctionData?.value != null
? () {} ? () {
/// add the functions to the routine bloc
for (var function in state.functions) {
context.read<RoutineBloc>().add(
AddFunctionToRoutine(
function,
),
);
}
// Return the device data to be added to the container
Navigator.pop(context);
}
: null, : null,
isConfirmEnabled: selectedFunction != null, isConfirmEnabled: selectedFunction != null,
), ),

View File

@ -13,11 +13,15 @@ class DeviceDialogHelper {
final functions = data['functions'] as List<DeviceFunction>; final functions = data['functions'] as List<DeviceFunction>;
try { try {
await _getDialogForDeviceType( final result = await _getDialogForDeviceType(
context, context,
data['productType'], data['productType'],
functions, functions,
); );
if (result != null) {
return result;
}
} catch (e) { } catch (e) {
debugPrint('Error: $e'); debugPrint('Error: $e');
} }
@ -25,23 +29,22 @@ class DeviceDialogHelper {
return null; return null;
} }
static Future<void> _getDialogForDeviceType( static Future<Map<String, dynamic>?> _getDialogForDeviceType(
BuildContext context, BuildContext context,
String productType, String productType,
List<DeviceFunction> functions, List<DeviceFunction> functions,
) async { ) async {
switch (productType) { switch (productType) {
case 'AC': case 'AC':
await ACHelper.showACFunctionsDialog(context, functions); return ACHelper.showACFunctionsDialog(context, functions);
break;
case '1G': case '1G':
await OneGangSwitchHelper.showSwitchFunctionsDialog(context, functions); return OneGangSwitchHelper.showSwitchFunctionsDialog(
break; context, functions);
case '2G': case '2G':
await TwoGangSwitchHelper.showSwitchFunctionsDialog(context, functions); return TwoGangSwitchHelper.showSwitchFunctionsDialog(
break; context, functions);
case '3G': case '3G':
await ThreeGangSwitchHelper.showSwitchFunctionsDialog( return ThreeGangSwitchHelper.showSwitchFunctionsDialog(
context, functions); context, functions);
break; break;
} }

View File

@ -9,7 +9,7 @@ import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/utils/extension/build_context_x.dart'; import 'package:syncrow_web/utils/extension/build_context_x.dart';
class ThreeGangSwitchHelper { class ThreeGangSwitchHelper {
static Future<void> showSwitchFunctionsDialog( static Future<Map<String, dynamic>?> showSwitchFunctionsDialog(
BuildContext context, List<DeviceFunction<dynamic>> functions) async { BuildContext context, List<DeviceFunction<dynamic>> functions) async {
List<DeviceFunction<dynamic>> switchFunctions = functions List<DeviceFunction<dynamic>> switchFunctions = functions
.where((f) => .where((f) =>

View File

@ -9,7 +9,7 @@ import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/utils/extension/build_context_x.dart'; import 'package:syncrow_web/utils/extension/build_context_x.dart';
class TwoGangSwitchHelper { class TwoGangSwitchHelper {
static Future<void> showSwitchFunctionsDialog( static Future<Map<String, dynamic>?> showSwitchFunctionsDialog(
BuildContext context, List<DeviceFunction<dynamic>> functions) async { BuildContext context, List<DeviceFunction<dynamic>> functions) async {
List<DeviceFunction<dynamic>> switchFunctions = functions List<DeviceFunction<dynamic>> switchFunctions = functions
.where((f) => .where((f) =>

View File

@ -46,7 +46,7 @@ class ThenContainer extends StatelessWidget {
final result = final result =
await DeviceDialogHelper.showDeviceDialog(context, data); await DeviceDialogHelper.showDeviceDialog(context, data);
if (result != null) { if (result != null) {
context.read<RoutineBloc>().add(AddToThenContainer(result)); context.read<RoutineBloc>().add(AddToThenContainer(data));
} else if (!['AC', '1G', '2G', '3G'] } else if (!['AC', '1G', '2G', '3G']
.contains(data['productType'])) { .contains(data['productType'])) {
context.read<RoutineBloc>().add(AddToThenContainer(data)); context.read<RoutineBloc>().add(AddToThenContainer(data));