From c46cfb48a83f92e8347431a9cf876a37ab3b4fba Mon Sep 17 00:00:00 2001 From: Faris Armoush Date: Thu, 17 Apr 2025 14:38:04 +0300 Subject: [PATCH] refactor: extract IF and THEN sections into separate methods for better readability --- .../routines/helper/save_routine_helper.dart | 161 +++++++++--------- 1 file changed, 81 insertions(+), 80 deletions(-) diff --git a/lib/pages/routines/helper/save_routine_helper.dart b/lib/pages/routines/helper/save_routine_helper.dart index 57081230..0750ba07 100644 --- a/lib/pages/routines/helper/save_routine_helper.dart +++ b/lib/pages/routines/helper/save_routine_helper.dart @@ -36,90 +36,13 @@ class SaveRoutineHelper { child: Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ - // Left side - IF - Expanded( - child: ListView( - // crossAxisAlignment: CrossAxisAlignment.start, - shrinkWrap: true, - children: [ - const Text( - 'IF:', - style: TextStyle( - fontSize: 16, - ), - ), - const SizedBox(height: 8), - if (state.isTabToRun) - ListTile( - leading: SvgPicture.asset( - Assets.tabToRun, - width: 24, - height: 24, - ), - title: const Text('Tab to run'), - ), - if (state.isAutomation) - ...state.ifItems.map((item) { - final functions = state.selectedFunctions[ - item['uniqueCustomId']] ?? - []; - return functionRow(item, context, functions); - }), - ], - ), - ), + _buildIfConditions(state, context), const SizedBox(width: 16), - // Right side - THEN items - Expanded( - child: ListView( - shrinkWrap: true, - children: [ - const Text( - 'THEN:', - style: TextStyle( - fontSize: 16, - ), - ), - const SizedBox(height: 8), - ...state.thenItems.map((item) { - final functions = state.selectedFunctions[ - item['uniqueCustomId']] ?? - []; - return functionRow(item, context, functions); - }), - ], - ), - ), + _buildThenActions(state, context), ], ), ), - DialogFooter( - onCancel: () => Navigator.pop(context), - onConfirm: () async { - if (state.isAutomation) { - if (state.isUpdate ?? false) { - context - .read() - .add(const UpdateAutomation()); - } else { - context - .read() - .add(const CreateAutomationEvent()); - } - } else { - if (state.isUpdate ?? false) { - context.read().add(const UpdateScene()); - } else { - context - .read() - .add(const CreateSceneEvent()); - } - } - - Navigator.pop(context); - }, - isConfirmEnabled: true, - ), + _buildDialogFooter(context, state), ], ), ), @@ -130,6 +53,84 @@ class SaveRoutineHelper { ); } + static DialogFooter _buildDialogFooter(BuildContext context, RoutineState state) { + return DialogFooter( + onCancel: () => Navigator.pop(context), + onConfirm: () async { + if (state.isAutomation) { + if (state.isUpdate ?? false) { + context.read().add(const UpdateAutomation()); + } else { + context.read().add(const CreateAutomationEvent()); + } + } else { + if (state.isUpdate ?? false) { + context.read().add(const UpdateScene()); + } else { + context.read().add(const CreateSceneEvent()); + } + } + + Navigator.pop(context); + }, + isConfirmEnabled: true, + ); + } + + static Expanded _buildThenActions(RoutineState state, BuildContext context) { + return Expanded( + child: ListView( + shrinkWrap: true, + children: [ + const Text( + 'THEN:', + style: TextStyle( + fontSize: 16, + ), + ), + const SizedBox(height: 8), + ...state.thenItems.map((item) { + final functions = state.selectedFunctions[item['uniqueCustomId']] ?? []; + return functionRow(item, context, functions); + }), + ], + ), + ); + } + + static Expanded _buildIfConditions(RoutineState state, BuildContext context) { + return Expanded( + child: ListView( + // crossAxisAlignment: CrossAxisAlignment.start, + shrinkWrap: true, + children: [ + const Text( + 'IF:', + style: TextStyle( + fontSize: 16, + ), + ), + const SizedBox(height: 8), + if (state.isTabToRun) + ListTile( + leading: SvgPicture.asset( + Assets.tabToRun, + width: 24, + height: 24, + ), + title: const Text('Tab to run'), + ), + if (state.isAutomation) + ...state.ifItems.map((item) { + final functions = + state.selectedFunctions[item['uniqueCustomId']] ?? []; + return functionRow(item, context, functions); + }), + ], + ), + ); + } + static Widget functionRow( dynamic item, BuildContext context,