diff --git a/lib/pages/routines/bloc/functions_bloc/functions_bloc_bloc.dart b/lib/pages/routines/bloc/functions_bloc/functions_bloc_bloc.dart index 659d3261..215b6d69 100644 --- a/lib/pages/routines/bloc/functions_bloc/functions_bloc_bloc.dart +++ b/lib/pages/routines/bloc/functions_bloc/functions_bloc_bloc.dart @@ -13,6 +13,35 @@ class FunctionBloc extends Bloc { on(_onAddFunction); on(_onSelectFunction); } + // void _onAddFunction(AddFunction event, Emitter emit) { + // final functions = List.from(state.addedFunctions); + // final existingIndex = functions.indexWhere( + // (f) => f.functionCode == event.functionData.functionCode, + // ); + + // if (existingIndex != -1) { + // final existingData = functions[existingIndex]; + // functions[existingIndex] = DeviceFunctionData( + // entityId: event.functionData.entityId, + // functionCode: event.functionData.functionCode, + // operationName: event.functionData.operationName, + // value: event.functionData.value ?? existingData.value, + // valueDescription: event.functionData.valueDescription ?? + // existingData.valueDescription, + // condition: event.functionData.condition ?? existingData.condition, + // step: event.functionData.step ?? existingData.step, + // ); + // } else { + // functions.clear(); + // functions.add(event.functionData); + // } + + // emit(state.copyWith( + // addedFunctions: functions, + // selectedFunction: event.functionData.functionCode, + // )); + // } + void _onAddFunction(AddFunction event, Emitter emit) { final functions = List.from(state.addedFunctions); final existingIndex = functions.indexWhere( @@ -20,19 +49,10 @@ class FunctionBloc extends Bloc { ); if (existingIndex != -1) { - final existingData = functions[existingIndex]; - functions[existingIndex] = DeviceFunctionData( - entityId: event.functionData.entityId, - functionCode: event.functionData.functionCode, - operationName: event.functionData.operationName, - value: event.functionData.value ?? existingData.value, - valueDescription: event.functionData.valueDescription ?? - existingData.valueDescription, - condition: event.functionData.condition ?? existingData.condition, - step: event.functionData.step ?? existingData.step, - ); + // Update the function value + functions[existingIndex] = event.functionData; } else { - functions.clear(); + // Add new function value functions.add(event.functionData); } diff --git a/lib/pages/routines/helper/save_routine_helper.dart b/lib/pages/routines/helper/save_routine_helper.dart index f8b52dab..2b506620 100644 --- a/lib/pages/routines/helper/save_routine_helper.dart +++ b/lib/pages/routines/helper/save_routine_helper.dart @@ -17,9 +17,10 @@ class SaveRoutineHelper { builder: (context) { return BlocBuilder( builder: (context, state) { - final selectedConditionLabel = state.selectedAutomationOperator == 'and' - ? 'All Conditions are met' - : 'Any Condition is met'; + final selectedConditionLabel = + state.selectedAutomationOperator == 'and' + ? 'All Conditions are met' + : 'Any Condition is met'; return AlertDialog( contentPadding: EdgeInsets.zero, @@ -37,10 +38,11 @@ class SaveRoutineHelper { Text( 'Create a scene: ${state.routineName ?? ""}', textAlign: TextAlign.center, - style: Theme.of(context).textTheme.headlineMedium!.copyWith( - color: ColorsManager.primaryColorWithOpacity, - fontWeight: FontWeight.bold, - ), + style: + Theme.of(context).textTheme.headlineMedium!.copyWith( + color: ColorsManager.primaryColorWithOpacity, + fontWeight: FontWeight.bold, + ), ), const SizedBox(height: 18), _buildDivider(), @@ -58,7 +60,8 @@ class SaveRoutineHelper { _buildIfConditions(state, context), Container( width: 1, - color: ColorsManager.greyColor.withValues(alpha: 0.8), + color: ColorsManager.greyColor + .withValues(alpha: 0.8), ), _buildThenActions(state, context), ], @@ -97,7 +100,8 @@ class SaveRoutineHelper { child: Row( spacing: 16, children: [ - Expanded(child: Text('IF: $selectedConditionLabel', style: textStyle)), + Expanded( + child: Text('IF: $selectedConditionLabel', style: textStyle)), const Expanded(child: Text('THEN:', style: textStyle)), ], ), @@ -109,7 +113,7 @@ class SaveRoutineHelper { spacing: 16, mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ - DialogFooterButton( + DialogFooterButton( text: 'Back', onTap: () => Navigator.pop(context), ), @@ -143,7 +147,8 @@ class SaveRoutineHelper { child: ListView( // shrinkWrap: true, children: state.thenItems.map((item) { - final functions = state.selectedFunctions[item['uniqueCustomId']] ?? []; + final functions = + state.selectedFunctions[item['uniqueCustomId']] ?? []; return functionRow(item, context, functions); }).toList(), ), @@ -203,19 +208,20 @@ class SaveRoutineHelper { ), ), child: Center( - child: item['type'] == 'tap_to_run' || item['type'] == 'scene' - ? Image.memory( - base64Decode(item['icon']), - width: 12, - height: 22, - fit: BoxFit.scaleDown, - ) - : SvgPicture.asset( - item['imagePath'], - width: 12, - height: 12, - fit: BoxFit.scaleDown, - ), + child: + item['type'] == 'tap_to_run' || item['type'] == 'scene' + ? Image.memory( + base64Decode(item['icon']), + width: 12, + height: 22, + fit: BoxFit.scaleDown, + ) + : SvgPicture.asset( + item['imagePath'], + width: 12, + height: 12, + fit: BoxFit.scaleDown, + ), ), ), Flexible( diff --git a/lib/pages/routines/models/pc/energy_clamp_functions.dart b/lib/pages/routines/models/pc/energy_clamp_functions.dart index 4bf3ddd8..f3a12c1b 100644 --- a/lib/pages/routines/models/pc/energy_clamp_functions.dart +++ b/lib/pages/routines/models/pc/energy_clamp_functions.dart @@ -405,8 +405,8 @@ class PowerFactorCStatusFunction extends EnergyClampFunctions { code: 'PowerFactorC', operationName: 'Power Factor C', icon: Assets.speedoMeter, - min: 0.00, - max: 1.00, + min: 0.0, + max: 1.0, step: 0.1, unit: "", ); diff --git a/lib/pages/routines/widgets/routine_dialogs/ac_dialog.dart b/lib/pages/routines/widgets/routine_dialogs/ac_dialog.dart index 6d0ddbfb..0ca6c125 100644 --- a/lib/pages/routines/widgets/routine_dialogs/ac_dialog.dart +++ b/lib/pages/routines/widgets/routine_dialogs/ac_dialog.dart @@ -117,10 +117,22 @@ class ACHelper { }, onConfirm: state.addedFunctions.isNotEmpty ? () { + final selectedFunctionData = + state.addedFunctions.firstWhere( + (f) => + f.functionCode == state.selectedFunction, + orElse: () => DeviceFunctionData( + entityId: '', + functionCode: state.selectedFunction ?? '', + operationName: '', + value: null, + ), + ); + /// add the functions to the routine bloc context.read().add( AddFunctionToRoutine( - state.addedFunctions, + [selectedFunctionData], uniqueCustomId, ), ); diff --git a/lib/pages/routines/widgets/routine_dialogs/ceiling_sensor/ceiling_sensor_dialog.dart b/lib/pages/routines/widgets/routine_dialogs/ceiling_sensor/ceiling_sensor_dialog.dart index 8fab09e8..8df1bbf2 100644 --- a/lib/pages/routines/widgets/routine_dialogs/ceiling_sensor/ceiling_sensor_dialog.dart +++ b/lib/pages/routines/widgets/routine_dialogs/ceiling_sensor/ceiling_sensor_dialog.dart @@ -78,12 +78,22 @@ class _CeilingSensorDialogState extends State { onCancel: () => Navigator.pop(context), onConfirm: state.addedFunctions.isNotEmpty ? () { + final selectedFunctionData = + state.addedFunctions.firstWhere( + (f) => f.functionCode == state.selectedFunction, + orElse: () => DeviceFunctionData( + entityId: '', + functionCode: state.selectedFunction ?? '', + operationName: '', + value: null, + ), + ); final functions = _updateValuesForAddedFunctions( state.addedFunctions, ); context.read().add( AddFunctionToRoutine( - functions, + [selectedFunctionData], '${widget.uniqueCustomId}', ), ); diff --git a/lib/pages/routines/widgets/routine_dialogs/flush_presence_sensor/flush_presence_sensor.dart b/lib/pages/routines/widgets/routine_dialogs/flush_presence_sensor/flush_presence_sensor.dart index dad64866..d233ebf8 100644 --- a/lib/pages/routines/widgets/routine_dialogs/flush_presence_sensor/flush_presence_sensor.dart +++ b/lib/pages/routines/widgets/routine_dialogs/flush_presence_sensor/flush_presence_sensor.dart @@ -192,9 +192,18 @@ class _WallPresenceSensorState extends State { onCancel: () => Navigator.pop(context), onConfirm: state.addedFunctions.isNotEmpty ? () { + final selectedFunctionData = state.addedFunctions.firstWhere( + (f) => f.functionCode == state.selectedFunction, + orElse: () => DeviceFunctionData( + entityId: '', + functionCode: state.selectedFunction ?? '', + operationName: '', + value: null, + ), + ); context.read().add( AddFunctionToRoutine( - state.addedFunctions, + [selectedFunctionData], widget.uniqueCustomId!, ), ); diff --git a/lib/pages/routines/widgets/routine_dialogs/gateway/gateway_dialog.dart b/lib/pages/routines/widgets/routine_dialogs/gateway/gateway_dialog.dart index 33cf8fc0..358d6e7e 100644 --- a/lib/pages/routines/widgets/routine_dialogs/gateway/gateway_dialog.dart +++ b/lib/pages/routines/widgets/routine_dialogs/gateway/gateway_dialog.dart @@ -115,9 +115,18 @@ class _GatewayDialogState extends State { onCancel: () => Navigator.pop(context), onConfirm: state.addedFunctions.isNotEmpty ? () { + final selectedFunctionData = state.addedFunctions.firstWhere( + (f) => f.functionCode == state.selectedFunction, + orElse: () => DeviceFunctionData( + entityId: '', + functionCode: state.selectedFunction ?? '', + operationName: '', + value: null, + ), + ); context.read().add( AddFunctionToRoutine( - state.addedFunctions, + [selectedFunctionData], widget.uniqueCustomId ?? '-1', ), ); diff --git a/lib/pages/routines/widgets/routine_dialogs/one_gang_switch_dialog.dart b/lib/pages/routines/widgets/routine_dialogs/one_gang_switch_dialog.dart index c892610c..6e882c71 100644 --- a/lib/pages/routines/widgets/routine_dialogs/one_gang_switch_dialog.dart +++ b/lib/pages/routines/widgets/routine_dialogs/one_gang_switch_dialog.dart @@ -147,7 +147,7 @@ class OneGangSwitchHelper { // } context.read().add( AddFunctionToRoutine( - state.addedFunctions, + [selectedFunctionData], uniqueCustomId, ), ); diff --git a/lib/pages/routines/widgets/routine_dialogs/power_clamp_enargy/energy_clamp_dialog.dart b/lib/pages/routines/widgets/routine_dialogs/power_clamp_enargy/energy_clamp_dialog.dart index b27c5f8a..be674046 100644 --- a/lib/pages/routines/widgets/routine_dialogs/power_clamp_enargy/energy_clamp_dialog.dart +++ b/lib/pages/routines/widgets/routine_dialogs/power_clamp_enargy/energy_clamp_dialog.dart @@ -250,9 +250,18 @@ class _EnergyClampDialogState extends State { onCancel: () => Navigator.pop(context), onConfirm: state.addedFunctions.isNotEmpty ? () { + final selectedFunctionData = state.addedFunctions.firstWhere( + (f) => f.functionCode == state.selectedFunction, + orElse: () => DeviceFunctionData( + entityId: '', + functionCode: state.selectedFunction ?? '', + operationName: '', + value: null, + ), + ); context.read().add( AddFunctionToRoutine( - state.addedFunctions, + [selectedFunctionData], widget.uniqueCustomId!, ), ); diff --git a/lib/pages/routines/widgets/routine_dialogs/power_clamp_enargy/energy_value_selector_widget.dart b/lib/pages/routines/widgets/routine_dialogs/power_clamp_enargy/energy_value_selector_widget.dart index 696251a1..93a29062 100644 --- a/lib/pages/routines/widgets/routine_dialogs/power_clamp_enargy/energy_value_selector_widget.dart +++ b/lib/pages/routines/widgets/routine_dialogs/power_clamp_enargy/energy_value_selector_widget.dart @@ -27,17 +27,16 @@ class EnergyValueSelectorWidget extends StatelessWidget { @override Widget build(BuildContext context) { - final selectedFn = - functions.firstWhere((f) => f.code == selectedFunction); + final selectedFn = functions.firstWhere((f) => f.code == selectedFunction); final values = selectedFn.getOperationalValues(); - final step = selectedFn.step ?? 1.0; + final step = selectedFn.step; final _unit = selectedFn.unit ?? ''; final (double, double) sliderRange = (selectedFn.min ?? 0.0, selectedFn.max ?? 100.0); if (_isSliderFunction(selectedFunction)) { return CustomRoutinesTextbox( - withSpecialChar: false, + withSpecialChar: true, currentCondition: functionData.condition, dialogType: dialogType, sliderRange: sliderRange, @@ -60,14 +59,14 @@ class EnergyValueSelectorWidget extends StatelessWidget { entityId: device?.uuid ?? '', functionCode: selectedFunction, operationName: functionData.operationName, - value: value.toInt(), + value: value, condition: functionData.condition, ), ), ), unit: _unit, dividendOfRange: 1, - stepIncreaseAmount: step, + stepIncreaseAmount: step!, ); } diff --git a/lib/pages/routines/widgets/routine_dialogs/two_gang_switch_dialog.dart b/lib/pages/routines/widgets/routine_dialogs/two_gang_switch_dialog.dart index 93ab83a7..8a516eed 100644 --- a/lib/pages/routines/widgets/routine_dialogs/two_gang_switch_dialog.dart +++ b/lib/pages/routines/widgets/routine_dialogs/two_gang_switch_dialog.dart @@ -145,9 +145,22 @@ class TwoGangSwitchHelper { // ), // ); // } + final selectedFunctionData = + state.addedFunctions.firstWhere( + (f) => + f.functionCode == + state.selectedFunction, + orElse: () => DeviceFunctionData( + entityId: '', + functionCode: + state.selectedFunction ?? '', + operationName: '', + value: null, + ), + ); context.read().add( AddFunctionToRoutine( - state.addedFunctions, + [selectedFunctionData], uniqueCustomId, ), ); diff --git a/lib/pages/routines/widgets/routine_dialogs/wall_sensor/wall_presence_sensor.dart b/lib/pages/routines/widgets/routine_dialogs/wall_sensor/wall_presence_sensor.dart index 996e46a8..a4e6da4e 100644 --- a/lib/pages/routines/widgets/routine_dialogs/wall_sensor/wall_presence_sensor.dart +++ b/lib/pages/routines/widgets/routine_dialogs/wall_sensor/wall_presence_sensor.dart @@ -210,9 +210,18 @@ class _WallPresenceSensorState extends State { onCancel: () => Navigator.pop(context), onConfirm: state.addedFunctions.isNotEmpty ? () { + final selectedFunctionData = state.addedFunctions.firstWhere( + (f) => f.functionCode == state.selectedFunction, + orElse: () => DeviceFunctionData( + entityId: '', + functionCode: state.selectedFunction ?? '', + operationName: '', + value: null, + ), + ); context.read().add( AddFunctionToRoutine( - state.addedFunctions, + [selectedFunctionData], widget.uniqueCustomId!, ), ); diff --git a/lib/pages/routines/widgets/routine_dialogs/water_heater/water_heater_presence_sensor.dart b/lib/pages/routines/widgets/routine_dialogs/water_heater/water_heater_presence_sensor.dart index a4f14aa9..fc614bdd 100644 --- a/lib/pages/routines/widgets/routine_dialogs/water_heater/water_heater_presence_sensor.dart +++ b/lib/pages/routines/widgets/routine_dialogs/water_heater/water_heater_presence_sensor.dart @@ -188,9 +188,18 @@ class _WaterHeaterDialogRoutinesState extends State { onCancel: () => Navigator.pop(context), onConfirm: state.addedFunctions.isNotEmpty ? () { + final selectedFunctionData = state.addedFunctions.firstWhere( + (f) => f.functionCode == state.selectedFunction, + orElse: () => DeviceFunctionData( + entityId: '', + functionCode: state.selectedFunction ?? '', + operationName: '', + value: null, + ), + ); context.read().add( AddFunctionToRoutine( - state.addedFunctions, + [selectedFunctionData], widget.uniqueCustomId!, ), );