Skip to content

Commit 68ec963

Browse files
committed
update SliderState
1 parent 4ee33b3 commit 68ec963

File tree

1 file changed

+14
-29
lines changed

1 file changed

+14
-29
lines changed

slider/src/main/java/io/monstarlab/mosaic/slider/SliderState.kt

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ public class SliderState(
6363
* Internal value returned as fraction, used for displaying and specifying
6464
* the "real" position of the thumb
6565
*/
66-
internal val valueAsFraction: Float
67-
get() {
68-
val inverted = valueDistribution.inverse(value)
69-
val invertedRange = valueDistribution.inverse(range)
70-
return calcFraction(invertedRange.start, invertedRange.endInclusive, inverted)
66+
internal val offsetAsFraction: Float
67+
get() = if (totalWidth == 0f) 0f else {
68+
val valueFraction = value.valueToFraction(range)
69+
valueDistribution.inverse(valueFraction)
7170
}
7271

72+
7373
internal val disabledRangeAsFractions: ClosedFloatingPointRange<Float>
7474
get() = coerceRangeIntoFractions(disabledRange)
7575

@@ -115,25 +115,18 @@ public class SliderState(
115115
* Scales offset in to the value that user should see
116116
*/
117117
private fun scaleToUserValue(offset: Float): Float {
118-
val invertedRange = valueDistribution.inverse(range)
119-
val value = scale(0f, totalWidth, offset, invertedRange.start, invertedRange.endInclusive)
120-
return coerceValue(valueDistribution.interpolate(value))
118+
119+
val value = valueDistribution.interpolate(offset / totalWidth)
120+
.fractionToValue(range)
121+
return coerceValue(value)
121122
}
122123

123124
/**
124125
* Converts value of the user into the raw offset on the track
125126
*/
126127
private fun scaleToOffset(value: Float): Float {
127-
val coerced = coerceValue(value)
128-
val invertedRange = valueDistribution.inverse(range)
129-
val invertedValue = valueDistribution.inverse(coerced)
130-
return scale(
131-
invertedRange.start,
132-
invertedRange.endInclusive,
133-
invertedValue,
134-
0f,
135-
totalWidth,
136-
)
128+
val valueAsFraction = coerceValue(value).valueToFraction(range)
129+
return valueDistribution.inverse(valueAsFraction).fractionToValue(0f, totalWidth)
137130
}
138131

139132
internal fun coerceValue(value: Float): Float {
@@ -156,17 +149,9 @@ public class SliderState(
156149
subrange: ClosedFloatingPointRange<Float>,
157150
): ClosedFloatingPointRange<Float> {
158151
if (subrange.isEmpty()) return subrange
159-
val interpolatedRange = valueDistribution.interpolate(range)
160-
val interpolatedSubrange = valueDistribution.interpolate(subrange)
161-
return calcFraction(
162-
interpolatedRange.start,
163-
interpolatedRange.endInclusive,
164-
interpolatedSubrange.start,
165-
)..calcFraction(
166-
interpolatedRange.start,
167-
interpolatedRange.endInclusive,
168-
interpolatedSubrange.endInclusive,
169-
)
152+
val start = valueDistribution.inverse(subrange.start.valueToFraction(range))
153+
val end = valueDistribution.inverse(subrange.endInclusive.valueToFraction(range))
154+
return start..end
170155
}
171156
}
172157

0 commit comments

Comments
 (0)