diff options
author | Allan Wang <me@allanwang.ca> | 2020-01-19 21:37:28 -0800 |
---|---|---|
committer | Allan Wang <me@allanwang.ca> | 2020-01-19 21:37:28 -0800 |
commit | 588b722a9b94305b2fb40954195c3707912e2d75 (patch) | |
tree | 0fd3a1c6e4f606f7acea80bbc0220a62ea6565cc /core/src/main/kotlin | |
parent | 0ddc12ab95c8a750b2738ada06073830fd7cae8a (diff) | |
parent | b7fd1c5c7e848d7cf12c49a1e9e319d4e11d0b5c (diff) | |
download | kau-588b722a9b94305b2fb40954195c3707912e2d75.tar.gz kau-588b722a9b94305b2fb40954195c3707912e2d75.tar.bz2 kau-588b722a9b94305b2fb40954195c3707912e2d75.zip |
Merge branch 'progress-animator' into misc
Diffstat (limited to 'core/src/main/kotlin')
-rw-r--r-- | core/src/main/kotlin/ca/allanwang/kau/ui/ProgressAnimator.kt | 103 |
1 files changed, 60 insertions, 43 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 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<ProgressDisposableAction> = 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) |