Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define the range requirement of maxScale. #4

Merged
merged 1 commit into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
}
}
}