refactored helpers into a helper class to release some complexity out of the widget.

This commit is contained in:
Faris Armoush
2025-04-14 14:32:22 +03:00
parent 9e3a78f6b7
commit 06383018b9

View File

@ -31,10 +31,13 @@ class CpsDialogSliderSelector extends StatelessWidget {
return SliderValueSelector(
currentCondition: selectedFunctionData.condition,
dialogType: dialogType,
sliderRange: _sliderRange,
displayedValue: _displayText,
sliderRange: CpsSliderHelpers.sliderRange(selectedFunctionData.functionCode),
displayedValue: CpsSliderHelpers.displayText(
value: selectedFunctionData.value,
functionCode: selectedFunctionData.functionCode,
),
initialValue: selectedFunctionData.value ?? 0,
unit: _unit,
unit: CpsSliderHelpers.unit(selectedFunctionData.functionCode),
onConditionChanged: (condition) => context.read<FunctionBloc>().add(
AddFunction(
functionData: DeviceFunctionData(
@ -57,11 +60,50 @@ class CpsDialogSliderSelector extends StatelessWidget {
),
),
),
dividendOfRange: _dividendOfRange,
dividendOfRange: CpsSliderHelpers.dividendOfRange(
selectedFunctionData.functionCode,
),
);
}
(double, double) get _sliderRange => switch (selectedFunctionData.functionCode) {
double reverseMappedValue(double value) {
final (inputMin, inputMax) =
CpsSliderHelpers.sliderRange(selectedFunctionData.functionCode);
final (outputMin, outputMax, outputStep) = CpsSliderHelpers.mappedRange(
selectedFunctionData.functionCode,
);
final clampedValue = value.clamp(outputMin, outputMax);
final stepsFromMin = ((clampedValue - outputMin) / outputStep).round();
final mappedValue = inputMin +
(stepsFromMin *
CpsSliderHelpers.dividendOfRange(selectedFunctionData.functionCode));
return mappedValue.clamp(inputMin, inputMax);
}
}
abstract final class CpsSliderHelpers {
static (double min, double max, double step) mappedRange(String functionCode) {
final (defaultMin, defaultMax) = sliderRange(functionCode);
final defaultDivdidend = dividendOfRange(functionCode);
return switch (functionCode) {
'static_max_dis' => (0, 500, 50),
'presence_reference' => (0, 255, 50),
'moving_reference' => (0, 255, 5),
'perceptual_boundary' => (0, 500, 50),
'moving_boundary' => (0, 500, 50),
'moving_rigger_time' => (0, 2000, 100),
'moving_static_time' => (0, 6000, 100),
'none_body_time' => (0, 300000, 500),
_ => (defaultMin, defaultMax, defaultDivdidend),
};
}
static (double min, double max) sliderRange(String functionCode) =>
switch (functionCode) {
'moving_speed' => (0, 32),
'sensitivity' => (0, 10),
'space_static_val' => (0, 255),
@ -80,11 +122,40 @@ class CpsDialogSliderSelector extends StatelessWidget {
_ => (0, 300),
};
String get _displayText {
final value = selectedFunctionData.value;
static double dividendOfRange(String functionCode) => switch (functionCode) {
'presence_reference' => 5,
'moving_reference' => 5,
'moving_max_dis' => 0.5,
'static_max_dis' => 0.5,
'moving_range' => 0.1,
'presence_range' => 0.1,
'perceptual_boundary' => 0.5,
'moving_boundary' => 0.5,
'moving_rigger_time' => 0.1,
'moving_static_time' => 1.0,
'none_body_time' => 5.0,
_ => 1,
};
static String unit(String functionCode) => switch (functionCode) {
'moving_max_dis' ||
'static_max_dis' ||
'moving_range' ||
'presence_range' ||
'perceptual_boundary' ||
'moving_boundary' =>
'M',
'moving_rigger_time' || 'moving_static_time' || 'none_body_time' => 'sec',
_ => '',
};
static String displayText({
required dynamic value,
required String functionCode,
}) {
final parsedValue = double.tryParse('$value');
return switch (selectedFunctionData.functionCode) {
return switch (functionCode) {
'moving_max_dis' ||
'static_max_dis' ||
'moving_range' ||
@ -99,38 +170,4 @@ class CpsDialogSliderSelector extends StatelessWidget {
_ => '${parsedValue?.toStringAsFixed(0) ?? 0}',
};
}
String get _unit {
return switch (selectedFunctionData.functionCode) {
'moving_max_dis' ||
'static_max_dis' ||
'moving_range' ||
'presence_range' ||
'perceptual_boundary' ||
'moving_boundary' =>
'M',
'moving_rigger_time' || 'moving_static_time' || 'none_body_time' => 'sec',
_ => '',
};
}
double get _dividendOfRange => switch (selectedFunctionData.functionCode) {
'sensitivity' => 1,
'moving_speed' => 1,
'space_static_val' => 1,
'space_move_val' => 1,
'presence_reference' => 5,
'moving_reference' => 5,
'moving_max_dis' => 0.5,
'static_max_dis' => 0.5,
'moving_range' => 0.1,
'presence_range' => 0.1,
'perceptual_boundary' => 0.5,
'moving_boundary' => 0.5,
'moving_rigger_time' => 0.1,
'moving_static_time' => 1.0,
'none_body_time' => 5.0,
'sports_para' => 1.0,
_ => 1,
};
}