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/src/main') 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 From f24a2cfb20d86b6dae8aa9ccf16eb4d038955874 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Mon, 13 Jan 2020 15:29:13 -0800 Subject: Misc fixes --- .../kotlin/ca/allanwang/kau/ui/ProgressAnimator.kt | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'core/src/main') 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 f735f4d..486dc7f 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/ui/ProgressAnimator.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/ui/ProgressAnimator.kt @@ -33,7 +33,7 @@ class ProgressAnimator private constructor() : ValueAnimator() { companion object { - fun ofFloat(builder: ProgressAnimator.() -> Unit): ProgressAnimator = ProgressAnimator().apply { + fun ofFloat(builder: ProgressAnimator.() -> Unit = {}): ProgressAnimator = ProgressAnimator().apply { setFloatValues(0f, 1f) addUpdateListener { apply(it.animatedValue as Float) } addListener(object : AnimatorListenerAdapter() { @@ -143,30 +143,36 @@ class ProgressAnimator private constructor() : ValueAnimator() { } } - 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") - } + fun withRangeAnimator(min: Float, max: Float, start: Float, end: Float, action: ProgressAction) { + require(min < max) { "Range animator must have min < max; currently min=$min, max=$max" } withDisposableAnimator { when { it > max -> true it < min -> false else -> { - action(progress(start, end, progress, min, max)) + action(progress(start, end, it, min, max)) false } } } } - fun withPointAnimator(point: Float, action: ProgressAction) { + @Deprecated(level = DeprecationLevel.WARNING, + message = "Renamed to withPointAction", + replaceWith = ReplaceWith("withPointAction(point, action)")) + fun withPointAnimator(point: Float, action: ProgressAction) = withPointAction(point, isAscendingProgress = true, action = action) + + fun withPointAction(point: Float, isAscendingProgress: Boolean = true, action: ProgressAction) { animators.add { - action.runIf(it >= point, it) + action.runIf((isAscendingProgress && (it >= point) || !isAscendingProgress && (it <= point)), it) } } fun withDelayedStartAction(skipCount: Int, action: ProgressAction) { var count = 0 + withStartAction { + count = 0 + } animators.add { action.runIf(count++ >= skipCount, it) } -- cgit v1.2.3 From d0963f367a23e6523af332e66afb1486cbf0f01b Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Fri, 17 Jan 2020 10:57:14 -0800 Subject: Ensure range animator applies change on last frame --- core/src/main/kotlin/ca/allanwang/kau/ui/ProgressAnimator.kt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'core/src/main') 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 486dc7f..04989d7 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/ui/ProgressAnimator.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/ui/ProgressAnimator.kt @@ -147,7 +147,10 @@ class ProgressAnimator private constructor() : ValueAnimator() { require(min < max) { "Range animator must have min < max; currently min=$min, max=$max" } withDisposableAnimator { when { - it > max -> true + it > max -> { + action(end) // apply action in case frames were skipped + true + } it < min -> false else -> { action(progress(start, end, it, min, max)) -- cgit v1.2.3 From b7fd1c5c7e848d7cf12c49a1e9e319d4e11d0b5c Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sun, 19 Jan 2020 21:37:19 -0800 Subject: Format code --- core-ui/src/main/res-public/values/public.xml | 6 +- .../kotlin/ca/allanwang/kau/ui/ProgressAnimator.kt | 103 ++++++++++++--------- core/src/main/res-public/values/public.xml | 34 +++---- 3 files changed, 80 insertions(+), 63 deletions(-) (limited to 'core/src/main') diff --git a/core-ui/src/main/res-public/values/public.xml b/core-ui/src/main/res-public/values/public.xml index f62ed25..c8c2e56 100644 --- a/core-ui/src/main/res-public/values/public.xml +++ b/core-ui/src/main/res-public/values/public.xml @@ -1,10 +1,10 @@ + + + - - - \ No newline at end of file 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 04989d7..dad825e 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/ui/ProgressAnimator.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/ui/ProgressAnimator.kt @@ -33,46 +33,50 @@ class ProgressAnimator private constructor() : ValueAnimator() { companion object { - fun ofFloat(builder: ProgressAnimator.() -> Unit = {}): ProgressAnimator = ProgressAnimator().apply { - setFloatValues(0f, 1f) - addUpdateListener { apply(it.animatedValue as Float) } - addListener(object : AnimatorListenerAdapter() { - override fun onAnimationStart(animation: Animator?, isReverse: Boolean) { - isCancelled = false - startActions.runAll() - } - - override fun onAnimationCancel(animation: Animator?) { - isCancelled = true - cancelActions.runAll() - } - - override fun onAnimationEnd(animation: Animator?) { - endActions.runAll() - isCancelled = false - } - }) - }.apply(builder) + fun ofFloat(builder: ProgressAnimator.() -> Unit = {}): ProgressAnimator = + ProgressAnimator().apply { + setFloatValues(0f, 1f) + addUpdateListener { apply(it.animatedValue as Float) } + addListener(object : AnimatorListenerAdapter() { + override fun onAnimationStart(animation: Animator?, isReverse: Boolean) { + isCancelled = false + startActions.runAll() + } + + override fun onAnimationCancel(animation: Animator?) { + isCancelled = true + cancelActions.runAll() + } + + override fun onAnimationEnd(animation: Animator?) { + endActions.runAll() + isCancelled = false + } + }) + }.apply(builder) /** * Gets output of a linear function starting at [start] when [progress] is 0 and [end] when [progress] is 1 at point [progress]. */ - fun progress(start: Float, end: Float, progress: Float): Float = start + (end - start) * progress - - fun progress(start: Float, end: Float, progress: Float, min: Float, max: Float): Float = when { - min == max -> throw IllegalArgumentException("Progress range cannot be 0 (min == max == $min") - progress <= min -> start - progress >= max -> end - else -> { - val trueProgress = (progress - min) / (max - min) - start + (end - start) * trueProgress + fun progress(start: Float, end: Float, progress: Float): Float = + start + (end - start) * progress + + fun progress(start: Float, end: Float, progress: Float, min: Float, max: Float): Float = + when { + min == max -> throw IllegalArgumentException("Progress range cannot be 0 (min == max == $min") + progress <= min -> start + progress >= max -> end + else -> { + val trueProgress = (progress - min) / (max - min) + 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() + fun progress(start: Int, end: Int, progress: Float): Int = + (start + (end - start) * progress).toInt() } private val animators: MutableList = mutableListOf() @@ -114,16 +118,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()) + withDisposableAnimator(from, to, action.asDisposable()) fun withDisposableAnimator(action: ProgressDisposableAction) = animators.add(action) @@ -143,13 +147,19 @@ class ProgressAnimator private constructor() : ValueAnimator() { } } - fun withRangeAnimator(min: Float, max: Float, start: Float, end: Float, action: ProgressAction) { + fun withRangeAnimator( + min: Float, + max: Float, + start: Float, + end: Float, + action: ProgressAction + ) { require(min < max) { "Range animator must have min < max; currently min=$min, max=$max" } withDisposableAnimator { when { it > max -> { - action(end) // apply action in case frames were skipped - true + action(end) // apply action in case frames were skipped + true } it < min -> false else -> { @@ -160,14 +170,20 @@ class ProgressAnimator private constructor() : ValueAnimator() { } } - @Deprecated(level = DeprecationLevel.WARNING, - message = "Renamed to withPointAction", - replaceWith = ReplaceWith("withPointAction(point, action)")) - fun withPointAnimator(point: Float, action: ProgressAction) = withPointAction(point, isAscendingProgress = true, action = action) + @Deprecated( + level = DeprecationLevel.WARNING, + message = "Renamed to withPointAction", + replaceWith = ReplaceWith("withPointAction(point, action)") + ) + fun withPointAnimator(point: Float, action: ProgressAction) = + withPointAction(point, isAscendingProgress = true, action = action) fun withPointAction(point: Float, isAscendingProgress: Boolean = true, action: ProgressAction) { animators.add { - action.runIf((isAscendingProgress && (it >= point) || !isAscendingProgress && (it <= point)), it) + action.runIf( + (isAscendingProgress && (it >= point) || !isAscendingProgress && (it <= point)), + it + ) } } @@ -188,7 +204,8 @@ class ProgressAnimator private constructor() : ValueAnimator() { fun withDisposableStartAction(action: ProgressDisposableRunnable) = startActions.add(action) - fun withCancelAction(action: ProgressRunnable) = withDisposableCancelAction(action.asDisposable()) + fun withCancelAction(action: ProgressRunnable) = + withDisposableCancelAction(action.asDisposable()) fun withDisposableCancelAction(action: ProgressDisposableRunnable) = cancelActions.add(action) diff --git a/core/src/main/res-public/values/public.xml b/core/src/main/res-public/values/public.xml index a912019..9f8780e 100644 --- a/core/src/main/res-public/values/public.xml +++ b/core/src/main/res-public/values/public.xml @@ -1,25 +1,20 @@ + - + + + + + - - - - - - - - - - - - - + + + @@ -104,7 +99,6 @@ - @@ -112,6 +106,12 @@ - - + + + + + + + + \ No newline at end of file -- cgit v1.2.3