From 986664c999cc4940161e84fdd271b1633d3f19c3 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Mon, 13 Jan 2020 15:05:18 -0800 Subject: Add int functions for ProgressAnimator --- .../kotlin/ca/allanwang/kau/ui/ProgressAnimator.kt | 24 +++++++++++++-- core/src/main/res-public/values/public.xml | 34 +++++++++++----------- 2 files changed, 38 insertions(+), 20 deletions(-) (limited to 'core') 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 = 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) -> 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 diff --git a/core/src/main/res-public/values/public.xml b/core/src/main/res-public/values/public.xml index 9f8780e..a912019 100644 --- a/core/src/main/res-public/values/public.xml +++ b/core/src/main/res-public/values/public.xml @@ -1,20 +1,25 @@ - - + - - - - + - - - + + + + + + + + + + + + @@ -99,6 +104,7 @@ + @@ -106,12 +112,6 @@ - - - - - - - - + + \ No newline at end of file -- cgit v1.2.3