Skip to content

Commit

Permalink
Define the range requirement of maxScale. (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
usuiat committed Jan 12, 2023
1 parent 56b7282 commit df3b225
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
9 changes: 7 additions & 2 deletions zoomable/src/main/java/net/engawapg/lib/zoomable/ZoomState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Float> = exponentialDecay(),
) {
init {
require(maxScale >= 1.0f) { "maxScale must be at least 1.0." }
}

private var _scale = Animatable(1f).apply {
updateBounds(0.9f, maxScale)
}
Expand Down Expand Up @@ -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<Float> = exponentialDecay(),
) = remember {
Expand Down
29 changes: 29 additions & 0 deletions zoomable/src/test/java/net/engawapg/lib/zoomable/ZoomStateTest.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit df3b225

Please sign in to comment.