Make sure to re-calculate step if not explicitly set

Summary:
This causes the step to be re-calculated on every update of min, max and step value,
to use the most up to date values for the calculation,
except if step is explicitly set to a non-zero value by the user.

Fixes #10253

**Test plan (required)**
1. Create example app
2. Create a view with a slider that has a `value`, `minimumValue` and `maximumValue` set, but no step value (or step value set to 0).

   For example:

   ```
   <Slider
       maximumValue={10}
       minimumValue={1}
       value={4}
       />
   ```
3. See slider working as expected
Closes https://github.com/facebook/react-native/pull/10343

Differential Revision: D4142646

Pulled By: hramos

fbshipit-source-id: a0df87bbdbbd4b2a291d89f5579f73f517a33dfc
This commit is contained in:
Rene Weber
2016-11-09 19:55:45 -08:00
committed by Facebook Github Bot
parent 1d9d8e93fb
commit 868fbeaa00
3 changed files with 115 additions and 4 deletions

View File

@@ -30,7 +30,7 @@ public class ReactSlider extends SeekBar {
/**
* If step is 0 (unset) we default to this total number of steps.
* Don't use 100 which leads to rounding errors (0.200000000001).
*/
*/
private static int DEFAULT_TOTAL_STEPS = 128;
/**
@@ -50,6 +50,7 @@ public class ReactSlider extends SeekBar {
* If zero it's determined automatically.
*/
private double mStep = 0;
private double mStepCalculated = 0;
public ReactSlider(Context context, @Nullable AttributeSet attrs, int style) {
super(context, attrs, style);
@@ -83,7 +84,7 @@ public class ReactSlider extends SeekBar {
if (seekBarProgress == getMax()) {
return mMaxValue;
}
return seekBarProgress * mStep + mMinValue;
return seekBarProgress * getStepValue() + mMinValue;
}
/**
@@ -91,7 +92,7 @@ public class ReactSlider extends SeekBar {
*/
private void updateAll() {
if (mStep == 0) {
mStep = (mMaxValue - mMinValue) / (double) DEFAULT_TOTAL_STEPS;
mStepCalculated = (mMaxValue - mMinValue) / (double) DEFAULT_TOTAL_STEPS;
}
setMax(getTotalSteps());
updateValue();
@@ -106,6 +107,10 @@ public class ReactSlider extends SeekBar {
}
private int getTotalSteps() {
return (int) Math.ceil((mMaxValue - mMinValue) / mStep);
return (int) Math.ceil((mMaxValue - mMinValue) / getStepValue());
}
private double getStepValue() {
return mStep > 0 ? mStep : mStepCalculated;
}
}