diff --git a/zoomable/src/main/java/net/engawapg/lib/zoomable/ZoomState.kt b/zoomable/src/main/java/net/engawapg/lib/zoomable/ZoomState.kt index e72552b..603f078 100644 --- a/zoomable/src/main/java/net/engawapg/lib/zoomable/ZoomState.kt +++ b/zoomable/src/main/java/net/engawapg/lib/zoomable/ZoomState.kt @@ -16,6 +16,7 @@ package net.engawapg.lib.zoomable +import androidx.annotation.FloatRange import androidx.compose.animation.core.Animatable import androidx.compose.animation.core.DecayAnimationSpec import androidx.compose.animation.core.exponentialDecay @@ -40,10 +41,14 @@ import kotlin.math.abs */ @Stable class ZoomState( - private val maxScale: Float, + @FloatRange(from = 1.0) private val maxScale: Float = 5f, private var contentSize: Size = Size.Zero, private val velocityDecay: DecayAnimationSpec = exponentialDecay(), ) { + init { + require(maxScale >= 1.0f) { "maxScale must be at least 1.0." } + } + private var _scale = Animatable(1f).apply { updateBounds(0.9f, maxScale) } @@ -226,7 +231,7 @@ class ZoomState( */ @Composable fun rememberZoomState( - maxScale: Float = 5f, + @FloatRange(from = 1.0) maxScale: Float = 5f, contentSize: Size = Size.Zero, velocityDecay: DecayAnimationSpec = exponentialDecay(), ) = remember { diff --git a/zoomable/src/test/java/net/engawapg/lib/zoomable/ZoomStateTest.kt b/zoomable/src/test/java/net/engawapg/lib/zoomable/ZoomStateTest.kt new file mode 100644 index 0000000..73fb84b --- /dev/null +++ b/zoomable/src/test/java/net/engawapg/lib/zoomable/ZoomStateTest.kt @@ -0,0 +1,29 @@ +package net.engawapg.lib.zoomable + +import org.junit.Assert.* +import org.junit.Test + +class ZoomStateTest { + + @Test + fun zoomState_noArgs_instantiated() { + val zoomState = ZoomState() + assertNotNull(zoomState) + } + + @Test + fun zoomState_maxScale_1_instantiated() { + val zoomState = ZoomState(maxScale = 1.0f) + assertNotNull(zoomState) + } + + @Test + fun zoomState_maxScale_099_throwException() { + assertThrows( + "maxScale must be at least 1.0.", + IllegalArgumentException::class.java + ) { + ZoomState(maxScale = 0.99f) + } + } +} \ No newline at end of file