mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-16 01:56:24 +00:00
push ac function state selection
This commit is contained in:
@ -6,6 +6,8 @@ import 'package:syncrow_web/pages/routiens/widgets/dialog_footer.dart';
|
||||
import 'package:syncrow_web/pages/routiens/widgets/dialog_header.dart';
|
||||
import 'package:syncrow_web/utils/color_manager.dart';
|
||||
import 'package:syncrow_web/utils/extension/build_context_x.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:syncrow_web/pages/routiens/bloc/functions_bloc/functions_bloc_bloc.dart';
|
||||
|
||||
class ACHelper {
|
||||
static Future<Map<String, dynamic>?> showACFunctionsDialog(
|
||||
@ -13,97 +15,122 @@ class ACHelper {
|
||||
List<DeviceFunction<dynamic>> functions,
|
||||
) async {
|
||||
List<ACFunction> acFunctions = functions.whereType<ACFunction>().toList();
|
||||
final selectedFunctionNotifier = ValueNotifier<String?>(null);
|
||||
final selectedValueNotifier = ValueNotifier<dynamic>(null);
|
||||
final selectedConditionNotifier = ValueNotifier<String?>('==');
|
||||
final selectedConditionsNotifier =
|
||||
ValueNotifier<List<bool>>([false, true, false]);
|
||||
|
||||
// Initialize the FunctionBloc with existing functions
|
||||
final initialFunctions = acFunctions
|
||||
.map((f) => DeviceFunctionData(
|
||||
entityId: '',
|
||||
function: f.code,
|
||||
operationName: f.operationName,
|
||||
value: null,
|
||||
))
|
||||
.toList();
|
||||
|
||||
return showDialog<Map<String, dynamic>?>(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
return ValueListenableBuilder<String?>(
|
||||
valueListenable: selectedFunctionNotifier,
|
||||
builder: (context, selectedFunction, _) {
|
||||
return AlertDialog(
|
||||
contentPadding: EdgeInsets.zero,
|
||||
content: Container(
|
||||
width: selectedFunction != null ? 600 : 360,
|
||||
height: 450,
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
),
|
||||
padding: const EdgeInsets.only(top: 20),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const DialogHeader('AC Functions'),
|
||||
Expanded(
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
// Function list
|
||||
SizedBox(
|
||||
width: selectedFunction != null ? 320 : 360,
|
||||
child: _buildFunctionsList(
|
||||
context,
|
||||
acFunctions,
|
||||
selectedFunctionNotifier,
|
||||
),
|
||||
),
|
||||
// Value selector
|
||||
if (selectedFunction != null)
|
||||
Expanded(
|
||||
child: _buildValueSelector(
|
||||
return BlocProvider(
|
||||
create: (_) =>
|
||||
FunctionBloc()..add(InitializeFunctions(initialFunctions)),
|
||||
child: AlertDialog(
|
||||
contentPadding: EdgeInsets.zero,
|
||||
content: BlocBuilder<FunctionBloc, FunctionBlocState>(
|
||||
builder: (context, state) {
|
||||
debugPrint(
|
||||
'Current state - Selected: ${state.selectedFunction}, Functions: ${state.functions}');
|
||||
|
||||
final selectedFunction = state.selectedFunction;
|
||||
final selectedFunctionData = selectedFunction != null
|
||||
? state.functions.firstWhere(
|
||||
(f) => f.function == selectedFunction,
|
||||
orElse: () => DeviceFunctionData(
|
||||
entityId: '',
|
||||
function: selectedFunction,
|
||||
operationName: '',
|
||||
value: null,
|
||||
),
|
||||
)
|
||||
: null;
|
||||
|
||||
return Container(
|
||||
width: selectedFunction != null ? 600 : 360,
|
||||
height: 450,
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
),
|
||||
padding: const EdgeInsets.only(top: 20),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const DialogHeader('AC Functions'),
|
||||
Expanded(
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
// Function list
|
||||
SizedBox(
|
||||
width: selectedFunction != null ? 320 : 360,
|
||||
child: _buildFunctionsList(
|
||||
context,
|
||||
selectedFunction,
|
||||
selectedValueNotifier,
|
||||
selectedConditionNotifier,
|
||||
selectedConditionsNotifier,
|
||||
acFunctions,
|
||||
(functionCode) =>
|
||||
context.read<FunctionBloc>().add(
|
||||
AddFunction(
|
||||
functionData: DeviceFunctionData(
|
||||
entityId: '',
|
||||
function: functionCode,
|
||||
operationName: '',
|
||||
value: null,
|
||||
)),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
// Value selector
|
||||
if (selectedFunction != null)
|
||||
Expanded(
|
||||
child: _buildValueSelector(
|
||||
context,
|
||||
selectedFunction,
|
||||
selectedFunctionData,
|
||||
(value) => context.read<FunctionBloc>().add(
|
||||
UpdateFunctionValue(
|
||||
function: selectedFunction,
|
||||
value: value,
|
||||
),
|
||||
),
|
||||
(condition) =>
|
||||
context.read<FunctionBloc>().add(
|
||||
UpdateFunctionCondition(
|
||||
function: selectedFunction,
|
||||
condition: condition,
|
||||
),
|
||||
),
|
||||
acFunctions,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
DialogFooter(
|
||||
onCancel: () {
|
||||
selectedFunctionNotifier.dispose();
|
||||
selectedValueNotifier.dispose();
|
||||
selectedConditionNotifier.dispose();
|
||||
selectedConditionsNotifier.dispose();
|
||||
Navigator.pop(context);
|
||||
},
|
||||
onConfirm: selectedFunctionNotifier.value != null &&
|
||||
selectedValueNotifier.value != null
|
||||
? () {
|
||||
selectedFunctionNotifier.dispose();
|
||||
selectedValueNotifier.dispose();
|
||||
selectedConditionNotifier.dispose();
|
||||
selectedConditionsNotifier.dispose();
|
||||
Navigator.pop(context, {
|
||||
'function': selectedFunctionNotifier.value,
|
||||
'value': selectedValueNotifier.value,
|
||||
'condition':
|
||||
selectedConditionNotifier.value ?? "==",
|
||||
});
|
||||
}
|
||||
: null,
|
||||
isConfirmEnabled: selectedFunction != null,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
DialogFooter(
|
||||
onCancel: () {
|
||||
Navigator.pop(context);
|
||||
},
|
||||
onConfirm: selectedFunction != null &&
|
||||
selectedFunctionData?.value != null
|
||||
? () {}
|
||||
: null,
|
||||
isConfirmEnabled: selectedFunction != null,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
).then((value) {
|
||||
selectedFunctionNotifier.dispose();
|
||||
selectedValueNotifier.dispose();
|
||||
selectedConditionNotifier.dispose();
|
||||
selectedConditionsNotifier.dispose();
|
||||
return value;
|
||||
});
|
||||
}
|
||||
@ -112,7 +139,7 @@ class ACHelper {
|
||||
static Widget _buildFunctionsList(
|
||||
BuildContext context,
|
||||
List<ACFunction> acFunctions,
|
||||
ValueNotifier<String?> selectedFunctionNotifier,
|
||||
Function(String) onFunctionSelected,
|
||||
) {
|
||||
return ListView.separated(
|
||||
shrinkWrap: false,
|
||||
@ -146,7 +173,7 @@ class ACHelper {
|
||||
size: 16,
|
||||
color: ColorsManager.textGray,
|
||||
),
|
||||
onTap: () => selectedFunctionNotifier.value = function.code,
|
||||
onTap: () => onFunctionSelected(function.code),
|
||||
);
|
||||
},
|
||||
);
|
||||
@ -156,60 +183,57 @@ class ACHelper {
|
||||
static Widget _buildValueSelector(
|
||||
BuildContext context,
|
||||
String selectedFunction,
|
||||
ValueNotifier<dynamic> selectedValueNotifier,
|
||||
ValueNotifier<String?> selectedConditionNotifier,
|
||||
ValueNotifier<List<bool>> selectedConditionsNotifier,
|
||||
DeviceFunctionData? selectedFunctionData,
|
||||
Function(dynamic) onValueChanged,
|
||||
Function(String) onConditionChanged,
|
||||
List<ACFunction> acFunctions,
|
||||
) {
|
||||
// Handle temperature functions
|
||||
if (selectedFunction == 'temp_set' || selectedFunction == 'temp_current') {
|
||||
// Initialize with 20°C (200 in internal representation)
|
||||
if (selectedValueNotifier.value == null ||
|
||||
selectedValueNotifier.value is! int) {
|
||||
selectedValueNotifier.value = 200;
|
||||
}
|
||||
final initialValue = selectedFunctionData?.value ?? 200;
|
||||
return _buildTemperatureSelector(
|
||||
context,
|
||||
selectedValueNotifier,
|
||||
selectedConditionNotifier,
|
||||
selectedConditionsNotifier,
|
||||
initialValue,
|
||||
selectedFunctionData?.condition,
|
||||
onValueChanged,
|
||||
onConditionChanged,
|
||||
);
|
||||
}
|
||||
|
||||
// Handle other functions
|
||||
final selectedFn =
|
||||
acFunctions.firstWhere((f) => f.code == selectedFunction);
|
||||
final values = selectedFn.getOperationalValues();
|
||||
|
||||
// Don't set any default value for non-temperature functions
|
||||
return _buildOperationalValuesList(
|
||||
context,
|
||||
values,
|
||||
selectedValueNotifier,
|
||||
selectedFunctionData?.value,
|
||||
onValueChanged,
|
||||
);
|
||||
}
|
||||
|
||||
/// Build temperature selector for AC functions dialog
|
||||
static Widget _buildTemperatureSelector(
|
||||
BuildContext context,
|
||||
ValueNotifier<dynamic> selectedValueNotifier,
|
||||
ValueNotifier<String?> selectedConditionNotifier,
|
||||
ValueNotifier<List<bool>> selectedConditionsNotifier,
|
||||
dynamic initialValue,
|
||||
String? currentCondition,
|
||||
Function(dynamic) onValueChanged,
|
||||
Function(String) onConditionChanged,
|
||||
) {
|
||||
return Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
_buildConditionToggle(
|
||||
context,
|
||||
selectedConditionNotifier,
|
||||
selectedConditionsNotifier,
|
||||
currentCondition,
|
||||
onConditionChanged,
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
_buildTemperatureDisplay(context, selectedValueNotifier),
|
||||
_buildTemperatureDisplay(context, initialValue),
|
||||
const SizedBox(height: 20),
|
||||
_buildTemperatureSlider(
|
||||
context,
|
||||
selectedValueNotifier,
|
||||
initialValue,
|
||||
onValueChanged,
|
||||
),
|
||||
],
|
||||
);
|
||||
@ -218,79 +242,61 @@ class ACHelper {
|
||||
/// Build condition toggle for AC functions dialog
|
||||
static Widget _buildConditionToggle(
|
||||
BuildContext context,
|
||||
ValueNotifier<String?> selectedConditionNotifier,
|
||||
ValueNotifier<List<bool>> selectedConditionsNotifier,
|
||||
String? currentCondition,
|
||||
Function(String) onConditionChanged,
|
||||
) {
|
||||
return ValueListenableBuilder<List<bool>>(
|
||||
valueListenable: selectedConditionsNotifier,
|
||||
builder: (context, selectedConditions, _) {
|
||||
return ToggleButtons(
|
||||
onPressed: (int index) {
|
||||
final newConditions = [false, false, false];
|
||||
newConditions[index] = true;
|
||||
selectedConditionsNotifier.value = newConditions;
|
||||
selectedConditionNotifier.value = index == 0
|
||||
? "<"
|
||||
: index == 1
|
||||
? "=="
|
||||
: ">";
|
||||
},
|
||||
borderRadius: const BorderRadius.all(Radius.circular(8)),
|
||||
selectedBorderColor: ColorsManager.primaryColorWithOpacity,
|
||||
selectedColor: Colors.white,
|
||||
fillColor: ColorsManager.primaryColorWithOpacity,
|
||||
color: ColorsManager.primaryColorWithOpacity,
|
||||
constraints: const BoxConstraints(
|
||||
minHeight: 40.0,
|
||||
minWidth: 40.0,
|
||||
),
|
||||
isSelected: selectedConditions,
|
||||
children: const [Text("<"), Text("="), Text(">")],
|
||||
);
|
||||
final conditions = ["<", "==", ">"];
|
||||
|
||||
return ToggleButtons(
|
||||
onPressed: (int index) {
|
||||
onConditionChanged(conditions[index]);
|
||||
},
|
||||
borderRadius: const BorderRadius.all(Radius.circular(8)),
|
||||
selectedBorderColor: ColorsManager.primaryColorWithOpacity,
|
||||
selectedColor: Colors.white,
|
||||
fillColor: ColorsManager.primaryColorWithOpacity,
|
||||
color: ColorsManager.primaryColorWithOpacity,
|
||||
constraints: const BoxConstraints(
|
||||
minHeight: 40.0,
|
||||
minWidth: 40.0,
|
||||
),
|
||||
isSelected:
|
||||
conditions.map((c) => c == (currentCondition ?? "==")).toList(),
|
||||
children: conditions.map((c) => Text(c)).toList(),
|
||||
);
|
||||
}
|
||||
|
||||
/// Build temperature display for AC functions dialog
|
||||
static Widget _buildTemperatureDisplay(
|
||||
BuildContext context, ValueNotifier<dynamic> selectedValueNotifier) {
|
||||
BuildContext context, dynamic initialValue) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
|
||||
decoration: BoxDecoration(
|
||||
color: ColorsManager.primaryColorWithOpacity.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
),
|
||||
child: ValueListenableBuilder(
|
||||
valueListenable: selectedValueNotifier,
|
||||
builder: (context, selectedValue, child) {
|
||||
return Text(
|
||||
'${(selectedValue ?? 200) / 10}°C',
|
||||
style: context.textTheme.headlineMedium!.copyWith(
|
||||
color: ColorsManager.primaryColorWithOpacity,
|
||||
),
|
||||
);
|
||||
},
|
||||
child: Text(
|
||||
'${(initialValue ?? 200) / 10}°C',
|
||||
style: context.textTheme.headlineMedium!.copyWith(
|
||||
color: ColorsManager.primaryColorWithOpacity,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
static Widget _buildTemperatureSlider(
|
||||
BuildContext context,
|
||||
ValueNotifier<dynamic> selectedValueNotifier,
|
||||
dynamic initialValue,
|
||||
Function(dynamic) onValueChanged,
|
||||
) {
|
||||
return ValueListenableBuilder(
|
||||
valueListenable: selectedValueNotifier,
|
||||
builder: (context, selectedValue, child) {
|
||||
final currentValue =
|
||||
selectedValue is int ? selectedValue.toDouble() : 200.0;
|
||||
return Slider(
|
||||
value: currentValue,
|
||||
min: 160,
|
||||
max: 300,
|
||||
divisions: 14,
|
||||
label: '${(currentValue / 10).toInt()}°C',
|
||||
onChanged: (value) => selectedValueNotifier.value = value.toInt(),
|
||||
);
|
||||
return Slider(
|
||||
value: initialValue is int ? initialValue.toDouble() : 200.0,
|
||||
min: 160,
|
||||
max: 300,
|
||||
divisions: 14,
|
||||
label: '${((initialValue ?? 200) / 10).toInt()}°C',
|
||||
onChanged: (value) {
|
||||
onValueChanged(value.toInt());
|
||||
},
|
||||
);
|
||||
}
|
||||
@ -298,40 +304,44 @@ class ACHelper {
|
||||
static Widget _buildOperationalValuesList(
|
||||
BuildContext context,
|
||||
List<dynamic> values,
|
||||
ValueNotifier<dynamic> selectedValueNotifier,
|
||||
dynamic selectedValue,
|
||||
Function(dynamic) onValueChanged,
|
||||
) {
|
||||
return ValueListenableBuilder<dynamic>(
|
||||
valueListenable: selectedValueNotifier,
|
||||
builder: (context, selectedValue, _) {
|
||||
return ListView.builder(
|
||||
shrinkWrap: false,
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
itemCount: values.length,
|
||||
itemBuilder: (context, index) {
|
||||
final value = values[index];
|
||||
return ListTile(
|
||||
leading: SvgPicture.asset(
|
||||
value.icon,
|
||||
width: 24,
|
||||
height: 24,
|
||||
placeholderBuilder: (BuildContext context) => Container(
|
||||
width: 24,
|
||||
height: 24,
|
||||
color: Colors.transparent,
|
||||
),
|
||||
),
|
||||
title: Text(
|
||||
value.description,
|
||||
style: context.textTheme.bodyMedium,
|
||||
),
|
||||
trailing: Radio<dynamic>(
|
||||
value: value.value,
|
||||
groupValue: selectedValue,
|
||||
onChanged: (_) => selectedValueNotifier.value = value.value,
|
||||
activeColor: ColorsManager.primaryColorWithOpacity,
|
||||
),
|
||||
onTap: () => selectedValueNotifier.value = value.value,
|
||||
);
|
||||
return ListView.builder(
|
||||
shrinkWrap: false,
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
itemCount: values.length,
|
||||
itemBuilder: (context, index) {
|
||||
final value = values[index];
|
||||
final isSelected = selectedValue == value.value;
|
||||
return ListTile(
|
||||
leading: SvgPicture.asset(
|
||||
value.icon,
|
||||
width: 24,
|
||||
height: 24,
|
||||
placeholderBuilder: (BuildContext context) => Container(
|
||||
width: 24,
|
||||
height: 24,
|
||||
color: Colors.transparent,
|
||||
),
|
||||
),
|
||||
title: Text(
|
||||
value.description,
|
||||
style: context.textTheme.bodyMedium,
|
||||
),
|
||||
trailing: Icon(
|
||||
isSelected
|
||||
? Icons.radio_button_checked
|
||||
: Icons.radio_button_unchecked,
|
||||
size: 24,
|
||||
color: isSelected
|
||||
? ColorsManager.primaryColorWithOpacity
|
||||
: ColorsManager.textGray,
|
||||
),
|
||||
onTap: () {
|
||||
if (!isSelected) {
|
||||
onValueChanged(value.value);
|
||||
}
|
||||
},
|
||||
);
|
||||
},
|
||||
|
Reference in New Issue
Block a user