Skip to content

Commit 6238c74

Browse files
committed
refactor: rename coerce function in state
1 parent 3203695 commit 6238c74

File tree

9 files changed

+58
-43
lines changed

9 files changed

+58
-43
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
/.idea/workspace.xml
99
/.idea/navEditor.xml
1010
/.idea/assetWizardSettings.xml
11+
/.idea/misc.xml
12+
/.idea/deploymentTargetDropDown.xml
13+
/.idea/deploymentTargetSelector.xml
1114
.DS_Store
1215
/build
1316
/captures

.idea/deploymentTargetSelector.xml

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/src/main/java/io/monstarlab/mosaic/features/SliderDemo.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ import androidx.compose.ui.graphics.Color
3737
import androidx.compose.ui.text.style.TextAlign
3838
import androidx.compose.ui.tooling.preview.Preview
3939
import androidx.compose.ui.unit.dp
40-
import io.monstarlab.mosaic.slider.distribution.FragmentedLinearDistribution
4140
import io.monstarlab.mosaic.slider.Slider
4241
import io.monstarlab.mosaic.slider.SliderColors
4342
import io.monstarlab.mosaic.slider.distribution.SliderValueDistribution
43+
import io.monstarlab.mosaic.slider.distribution.CheckPointsValueDistribution
4444
import kotlin.math.roundToInt
4545
import androidx.compose.material3.Slider as MaterialSlider
4646

@@ -84,11 +84,14 @@ fun MosaicSliderDemo() {
8484
}
8585

8686
val fragmentedDistribution: SliderValueDistribution = remember {
87-
FragmentedLinearDistribution.Builder()
88-
.sliceAt(0.2f,0f)
89-
.sliceAt(2f,0.3f)
90-
.sliceAt(0.2f,0.7f)
91-
.build()
87+
CheckPointsValueDistribution(
88+
listOf(
89+
0f to 0f,
90+
0.2f to 500f,
91+
0.4f to 800f,
92+
1f to 1000f,
93+
),
94+
)
9295
}
9396

9497
Slider(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ internal fun Modifier.sliderSemantics(state: SliderState, enabled: Boolean): Mod
4646
if (!enabled) disabled()
4747
setProgress(
4848
action = { targetValue ->
49-
val coercedValue = state.coerceValue(targetValue)
49+
val coercedValue = state.coerceUserValue(targetValue)
5050
// This is to keep it consistent with AbsSeekbar.java: return false if no
5151
// change from current.
5252
if (coercedValue == state.value) {

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public class SliderState(
5757
public var value: Float
5858
get() = valueState
5959
set(value) {
60-
valueState = coerceValue(value)
60+
valueState = coerceUserValue(value)
6161
}
6262

6363
/**
@@ -116,21 +116,21 @@ public class SliderState(
116116
* Scales offset in to the value that user should see
117117
*/
118118
private fun scaleToUserValue(offset: Float): Float {
119-
120-
val value = valueDistribution.interpolate(offset / totalWidth)
119+
val coercedValue = (offset / totalWidth).coerceIn(0f..1f)
120+
val value = valueDistribution.interpolate(coercedValue)
121121
.fractionToValue(range)
122-
return coerceValue(value)
122+
return coerceUserValue(value)
123123
}
124124

125125
/**
126126
* Converts value of the user into the raw offset on the track
127127
*/
128128
private fun scaleToOffset(value: Float): Float {
129-
val valueAsFraction = coerceValue(value).valueToFraction(range)
129+
val valueAsFraction = coerceUserValue(value).valueToFraction(range)
130130
return valueDistribution.inverse(valueAsFraction).fractionToValue(0f, totalWidth)
131131
}
132132

133-
internal fun coerceValue(value: Float): Float {
133+
internal fun coerceUserValue(value: Float): Float {
134134
return value
135135
.coerceIn(range)
136136
.coerceIntoDisabledRange()

slider/src/main/java/io/monstarlab/mosaic/slider/distribution/ValueCheckPointDistribution.kt renamed to slider/src/main/java/io/monstarlab/mosaic/slider/distribution/CheckPointsValueDistribution.kt

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@ package io.monstarlab.mosaic.slider.distribution
22

33
import io.monstarlab.mosaic.slider.valueToFraction
44

5-
public class ValueCheckPointDistribution(valuesMap: List<Pair<Float, Float>>) :
5+
public class CheckPointsValueDistribution(
6+
valuesMap: List<Pair<Float, Float>>
7+
) :
68
SliderValueDistribution {
79

810
private var equations: List<RangedLinearEquation>
911

1012
init {
11-
require(valuesMap.isNotEmpty()) { "values map can't be empty" }
13+
require(valuesMap.isNotEmpty()) {
14+
"Values map can't be empty"
15+
}
16+
1217
val offsetRange = valuesMap.minOf { it.first }..valuesMap.maxOf { it.first }
1318
val valueRange = valuesMap.minOf { it.second }..valuesMap.maxOf { it.second }
1419
val zipped = valuesMap.sortedBy { it.first }
@@ -23,7 +28,7 @@ public class ValueCheckPointDistribution(valuesMap: List<Pair<Float, Float>>) :
2328
val x2Fraction = it.second.first.valueToFraction(offsetRange)
2429
val y1Fraction = it.first.second.valueToFraction(valueRange)
2530
val y2Fraction = it.second.second.valueToFraction(valueRange)
26-
val equation = LinearEquation.fromTowPoints(
31+
val equation = LinearEquation.fromTwoPoints(
2732
x1 = x1Fraction,
2833
x2 = x2Fraction,
2934
y1 = y1Fraction,
@@ -33,22 +38,27 @@ public class ValueCheckPointDistribution(valuesMap: List<Pair<Float, Float>>) :
3338
equation = equation,
3439
offsetRange = x1Fraction..x2Fraction,
3540
valueRange = y1Fraction..y2Fraction,
36-
37-
)
41+
)
3842
}
3943
}
4044

4145

42-
override fun interpolate(value: Float): Float =
43-
equations.first { it.offsetRange.contains(value) }.equation.valueFromOffset(value)
46+
override fun interpolate(value: Float): Float {
47+
val equation = equations.firstOrNull { it.offsetRange.contains(value) }?.equation
48+
checkNotNull(equation) { "No equation found for value $value during interpolate" }
49+
return equation.valueFromOffset(value)
50+
}
4451

4552

46-
override fun inverse(value: Float): Float =
47-
equations.first { it.valueRange.contains(value) }.equation.offsetFromValue(value)
53+
override fun inverse(value: Float): Float {
54+
val equation = equations.firstOrNull { it.valueRange.contains(value) }?.equation
55+
checkNotNull(equation) { "No equation found for value $value during inverse" }
56+
return equation.offsetFromValue(value)
57+
}
4858

4959

5060
public class DecreasingValueException(progressValuePair: Pair<Float, Float>) :
51-
Exception(
61+
IllegalStateException(
5262
"Values must be always increasing with increasing progress," +
5363
" item at progress ${progressValuePair.first} with value " +
5464
"${progressValuePair.second} is breaking this rule "

slider/src/main/java/io/monstarlab/mosaic/slider/distribution/LinearEquation.kt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ public data class LinearEquation(
88

99
public fun offsetFromValue(value: Float): Float = (value - c) / m
1010

11-
public companion object{
12-
public fun fromTowPoints(x1:Float,y1:Float,x2:Float,y2:Float): LinearEquation {
13-
require(x2!=x1){"can't calc equation from points with similar x value"}
14-
val slope = (y2-y1)/(x2-x1)
15-
val c = y2 - slope*x2
16-
return LinearEquation(slope,c)
17-
}
18-
}
11+
public companion object {
12+
public fun fromTwoPoints(
13+
x1: Float, y1: Float,
14+
x2: Float, y2: Float
15+
): LinearEquation {
16+
require(x2 != x1) { "can't calc equation from points with similar x value" }
17+
val slope = (y2 - y1) / (x2 - x1)
18+
val c = y2 - slope * x2
19+
return LinearEquation(slope, c)
20+
}
21+
}
1922
}

slider/src/test/java/io/monstarlab/mosaic/slider/ValueCheckPointDistributionTest.kt renamed to slider/src/test/java/io/monstarlab/mosaic/slider/CheckPointsValueDistributionTest.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
package io.monstarlab.mosaic.slider
22

3-
import io.monstarlab.mosaic.slider.distribution.ValueCheckPointDistribution
3+
import io.monstarlab.mosaic.slider.distribution.CheckPointsValueDistribution
44
import org.junit.Assert.*
55

66
import org.junit.After
77
import org.junit.Before
88
import org.junit.Test
99

10-
class ValueCheckPointDistributionTest {
10+
class CheckPointsValueDistributionTest {
1111

12-
private lateinit var valueCheckPointDistribution : ValueCheckPointDistribution
12+
private lateinit var checkPointsValueDistribution : CheckPointsValueDistribution
1313
private val accuracy = 0.0001f
1414

1515
@Before
1616
fun setUp() {
1717

18-
valueCheckPointDistribution = ValueCheckPointDistribution(
18+
checkPointsValueDistribution = CheckPointsValueDistribution(
1919
listOf(
2020
0f to 0f,
2121
25f to 25f,
@@ -31,8 +31,8 @@ class ValueCheckPointDistributionTest {
3131

3232
@Test
3333
fun `test create from pairs with decreasing value`() {
34-
assertThrows(ValueCheckPointDistribution.DecreasingValueException::class.java) {
35-
ValueCheckPointDistribution(
34+
assertThrows(CheckPointsValueDistribution.DecreasingValueException::class.java) {
35+
CheckPointsValueDistribution(
3636
listOf(
3737
0f to 0f,
3838
5f to 10f,
@@ -56,7 +56,7 @@ class ValueCheckPointDistributionTest {
5656
0.75f to 0.875f,
5757
)
5858
points.forEach {
59-
assertEquals(it.second, valueCheckPointDistribution.interpolate(it.first), accuracy)
59+
assertEquals(it.second, checkPointsValueDistribution.interpolate(it.first), accuracy)
6060
}
6161

6262
}
@@ -73,7 +73,7 @@ class ValueCheckPointDistributionTest {
7373
0.75f to 0.875f,
7474
)
7575
points.forEach {
76-
assertEquals(it.first, valueCheckPointDistribution.inverse(it.second), accuracy)
76+
assertEquals(it.first, checkPointsValueDistribution.inverse(it.second), accuracy)
7777
}
7878

7979
}

0 commit comments

Comments
 (0)