refactor: extract IF and THEN sections into separate methods for better readability

This commit is contained in:
Faris Armoush
2025-04-17 14:38:04 +03:00
parent a0dd128557
commit c46cfb48a8

View File

@ -36,90 +36,13 @@ class SaveRoutineHelper {
child: Row( child: Row(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
// Left side - IF _buildIfConditions(state, context),
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);
}),
],
),
),
const SizedBox(width: 16), const SizedBox(width: 16),
// Right side - THEN items _buildThenActions(state, context),
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);
}),
],
),
),
], ],
), ),
), ),
DialogFooter( _buildDialogFooter(context, state),
onCancel: () => Navigator.pop(context),
onConfirm: () async {
if (state.isAutomation) {
if (state.isUpdate ?? false) {
context
.read<RoutineBloc>()
.add(const UpdateAutomation());
} else {
context
.read<RoutineBloc>()
.add(const CreateAutomationEvent());
}
} else {
if (state.isUpdate ?? false) {
context.read<RoutineBloc>().add(const UpdateScene());
} else {
context
.read<RoutineBloc>()
.add(const CreateSceneEvent());
}
}
Navigator.pop(context);
},
isConfirmEnabled: true,
),
], ],
), ),
), ),
@ -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<RoutineBloc>().add(const UpdateAutomation());
} else {
context.read<RoutineBloc>().add(const CreateAutomationEvent());
}
} else {
if (state.isUpdate ?? false) {
context.read<RoutineBloc>().add(const UpdateScene());
} else {
context.read<RoutineBloc>().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( static Widget functionRow(
dynamic item, dynamic item,
BuildContext context, BuildContext context,