aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/ca
diff options
context:
space:
mode:
authorAllan Wang <allanwang@google.com>2020-01-13 15:05:18 -0800
committerAllan Wang <allanwang@google.com>2020-01-13 15:05:18 -0800
commit986664c999cc4940161e84fdd271b1633d3f19c3 (patch)
tree3d96093021aa2b9cbcc3ec7e9bb0ff80e2f8e7a3 /core/src/main/kotlin/ca
parentf9dded97065f45848261e09e9358033c821312c9 (diff)
downloadkau-986664c999cc4940161e84fdd271b1633d3f19c3.tar.gz
kau-986664c999cc4940161e84fdd271b1633d3f19c3.tar.bz2
kau-986664c999cc4940161e84fdd271b1633d3f19c3.zip
Add int functions for ProgressAnimator
Diffstat (limited to 'core/src/main/kotlin/ca')
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/ui/ProgressAnimator.kt24
1 files changed, 21 insertions, 3 deletions
diff --git a/core/src/main/kotlin/ca/allanwang/kau/ui/ProgressAnimator.kt b/core/src/main/kotlin/ca/allanwang/kau/ui/ProgressAnimator.kt
index bb4cd88..f735f4d 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/ui/ProgressAnimator.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/ui/ProgressAnimator.kt
@@ -68,6 +68,11 @@ class ProgressAnimator private constructor() : ValueAnimator() {
start + (end - start) * trueProgress
}
}
+
+ /**
+ * Progress variant that takes in and returns int
+ */
+ fun progress(start: Int, end: Int, progress: Float): Int = (start + (end - start) * progress).toInt()
}
private val animators: MutableList<ProgressDisposableAction> = mutableListOf()
@@ -85,7 +90,7 @@ class ProgressAnimator private constructor() : ValueAnimator() {
/**
* Converts an action to a disposable action
*/
- private fun ProgressAction.asDisposable(): ProgressDisposableAction = { this(it); false }
+ private fun <T> ((T) -> Unit).asDisposable(): (T) -> Boolean = { this(it); false }
private fun ProgressRunnable.asDisposable(): ProgressDisposableRunnable = { this(); false }
@@ -109,13 +114,16 @@ class ProgressAnimator private constructor() : ValueAnimator() {
}
fun withAnimator(action: ProgressAction) =
- withDisposableAnimator(action.asDisposable())
+ withDisposableAnimator(action.asDisposable())
/**
* Range animator. Multiples the range by the current float progress before emission
*/
fun withAnimator(from: Float, to: Float, action: ProgressAction) =
- withDisposableAnimator(from, to, action.asDisposable())
+ withDisposableAnimator(from, to, action.asDisposable())
+
+ fun withAnimator(from: Int, to: Int, action: ProgressIntAction) =
+ withDisposableAnimator(from, to, action.asDisposable())
fun withDisposableAnimator(action: ProgressDisposableAction) = animators.add(action)
@@ -127,6 +135,14 @@ class ProgressAnimator private constructor() : ValueAnimator() {
}
}
+ fun withDisposableAnimator(from: Int, to: Int, action: ProgressIntDisposableAction) {
+ if (to != from) {
+ animators.add {
+ action(progress(from, to, it))
+ }
+ }
+ }
+
fun withRangeAnimator(min: Float, max: Float, start: Float, end: Float, progress: Float, action: ProgressAction) {
if (min >= max) {
throw IllegalArgumentException("Range animator must have min < max; currently min=$min, max=$max")
@@ -185,5 +201,7 @@ class ProgressAnimator private constructor() : ValueAnimator() {
private typealias ProgressAction = (Float) -> Unit
private typealias ProgressDisposableAction = (Float) -> Boolean
+private typealias ProgressIntAction = (Int) -> Unit
+private typealias ProgressIntDisposableAction = (Int) -> Boolean
private typealias ProgressRunnable = () -> Unit
private typealias ProgressDisposableRunnable = () -> Boolean