From 3dcd3e0ccdf71d52a6e2f8cd2978e3dd794ef613 Mon Sep 17 00:00:00 2001 From: Alterevit Date: Fri, 23 Mar 2018 15:02:27 +0300 Subject: [PATCH 1/5] add extensions for registration callback for Animatable2 --- .../androidx/core/animation/Animatable2.kt | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/main/java/androidx/core/animation/Animatable2.kt diff --git a/src/main/java/androidx/core/animation/Animatable2.kt b/src/main/java/androidx/core/animation/Animatable2.kt new file mode 100644 index 00000000..16313999 --- /dev/null +++ b/src/main/java/androidx/core/animation/Animatable2.kt @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package androidx.core.animation + +import android.graphics.drawable.Animatable2 +import android.graphics.drawable.Drawable +import android.support.annotation.RequiresApi + +@RequiresApi(23) +fun Animatable2.doOnEnd(action: (Drawable) -> Unit) = addCallback(onEnd = action) + +@RequiresApi(23) +fun Animatable2.doOnStart(action: (Drawable) -> Unit) = addCallback(onStart = action) + +@RequiresApi(23) +fun Animatable2.addCallback( + onStart: ((drawable: Drawable) -> Unit)? = null, + onEnd: ((drawable: Drawable) -> Unit)? = null +): Animatable2.AnimationCallback { + val callback = object : Animatable2.AnimationCallback() { + override fun onAnimationStart(drawable: Drawable) { + onStart?.invoke(drawable) + } + + override fun onAnimationEnd(drawable: Drawable) { + onEnd?.invoke(drawable) + } + } + registerAnimationCallback(callback) + return callback +} \ No newline at end of file From 6832b1674b22b711b10d50fa98475efd06f6f185 Mon Sep 17 00:00:00 2001 From: Alterevit Date: Fri, 23 Mar 2018 15:25:06 +0300 Subject: [PATCH 2/5] add comments --- src/main/java/androidx/core/animation/Animatable2.kt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/androidx/core/animation/Animatable2.kt b/src/main/java/androidx/core/animation/Animatable2.kt index 16313999..d06fed74 100644 --- a/src/main/java/androidx/core/animation/Animatable2.kt +++ b/src/main/java/androidx/core/animation/Animatable2.kt @@ -26,6 +26,9 @@ fun Animatable2.doOnEnd(action: (Drawable) -> Unit) = addCallback(onEnd = action @RequiresApi(23) fun Animatable2.doOnStart(action: (Drawable) -> Unit) = addCallback(onStart = action) +/** + * Add a callback to this Animatable2 using the provided actions. + */ @RequiresApi(23) fun Animatable2.addCallback( onStart: ((drawable: Drawable) -> Unit)? = null, From c97c556fee2cae0ee337f8dff84a75b99742ecc7 Mon Sep 17 00:00:00 2001 From: Alterevit Date: Fri, 30 Mar 2018 14:13:02 +0300 Subject: [PATCH 3/5] # add LayoutTransition extensions # add AnimatableTest, LayoutTransitionTest --- .../core/animation/Animatable2Test.kt | 75 +++++++++++++++++++ .../androidx/core/animation/AnimatorTest.kt | 2 +- .../core/animation/LayoutTransitionTest.kt | 57 ++++++++++++++ .../androidx/core/animation/Animatable2.kt | 12 +-- .../core/animation/LayoutTransition.kt | 57 ++++++++++++++ 5 files changed, 196 insertions(+), 7 deletions(-) create mode 100644 src/androidTest/java/androidx/core/animation/Animatable2Test.kt create mode 100644 src/androidTest/java/androidx/core/animation/LayoutTransitionTest.kt create mode 100644 src/main/java/androidx/core/animation/LayoutTransition.kt diff --git a/src/androidTest/java/androidx/core/animation/Animatable2Test.kt b/src/androidTest/java/androidx/core/animation/Animatable2Test.kt new file mode 100644 index 00000000..ac823f4f --- /dev/null +++ b/src/androidTest/java/androidx/core/animation/Animatable2Test.kt @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package androidx.core.animation + +import android.graphics.drawable.Animatable2 +import android.graphics.drawable.Animatable2.AnimationCallback +import android.support.test.runner.AndroidJUnit4 +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith + + +@RunWith(AndroidJUnit4::class) +class Animatable2Test { + + lateinit private var animatable: Animatable2 + + @Before fun init() { + animatable = object: Animatable2 { + + private var callback: AnimationCallback? = null + + override fun isRunning(): Boolean = false + + override fun registerAnimationCallback(callback: AnimationCallback?) { + this.callback = callback + } + + override fun start() { + callback?.onAnimationStart(null) + } + + override fun stop() { + callback?.onAnimationEnd(null) + } + + override fun clearAnimationCallbacks() { + callback = null + } + + override fun unregisterAnimationCallback(callback: AnimationCallback?) = false + + } + } + + @Test fun testDoOnStart() { + var called = false + animatable.doOnStart { called = true } + animatable.start() + assertTrue(called) + } + + @Test fun testDoOnEnd() { + var called = false + animatable.doOnEnd { called = true } + animatable.start() + animatable.stop() + assertTrue(called) + } +} \ No newline at end of file diff --git a/src/androidTest/java/androidx/core/animation/AnimatorTest.kt b/src/androidTest/java/androidx/core/animation/AnimatorTest.kt index de298a59..23242f24 100644 --- a/src/androidTest/java/androidx/core/animation/AnimatorTest.kt +++ b/src/androidTest/java/androidx/core/animation/AnimatorTest.kt @@ -30,6 +30,7 @@ import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) class AnimatorTest { + private val context = InstrumentationRegistry.getContext() private val view = View(context) @@ -44,7 +45,6 @@ class AnimatorTest { animator.doOnStart { called = true } - animator.listeners.forEach { it.onAnimationStart(animator) } assertTrue(called) } diff --git a/src/androidTest/java/androidx/core/animation/LayoutTransitionTest.kt b/src/androidTest/java/androidx/core/animation/LayoutTransitionTest.kt new file mode 100644 index 00000000..9436ec43 --- /dev/null +++ b/src/androidTest/java/androidx/core/animation/LayoutTransitionTest.kt @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package androidx.core.animation + +import android.animation.LayoutTransition +import android.support.test.runner.AndroidJUnit4 +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith + +@RunWith(AndroidJUnit4::class) +class LayoutTransitionTest { + + private lateinit var transition: LayoutTransition + + @Before fun before() { + transition = LayoutTransition() + } + + @Test fun testDoOnStart() { + var called = false + transition.doOnStart { _, _, _, _ -> + called = true + } + transition.transitionListeners.forEach { + it.startTransition(transition, null, null, LayoutTransition.DISAPPEARING) + } + assertTrue(called) + } + + @Test fun testDoOnEnd() { + var called = false + transition.doOnEnd { _, _, _, _ -> + called = true + } + transition.transitionListeners.forEach { + it.startTransition(transition, null, null, LayoutTransition.DISAPPEARING) + it.endTransition(transition, null, null, LayoutTransition.DISAPPEARING) + } + assertTrue(called) + } +} \ No newline at end of file diff --git a/src/main/java/androidx/core/animation/Animatable2.kt b/src/main/java/androidx/core/animation/Animatable2.kt index d06fed74..3007ab79 100644 --- a/src/main/java/androidx/core/animation/Animatable2.kt +++ b/src/main/java/androidx/core/animation/Animatable2.kt @@ -21,25 +21,25 @@ import android.graphics.drawable.Drawable import android.support.annotation.RequiresApi @RequiresApi(23) -fun Animatable2.doOnEnd(action: (Drawable) -> Unit) = addCallback(onEnd = action) +fun Animatable2.doOnStart(action: (Drawable?) -> Unit) = addCallback(onStart = action) @RequiresApi(23) -fun Animatable2.doOnStart(action: (Drawable) -> Unit) = addCallback(onStart = action) +fun Animatable2.doOnEnd(action: (Drawable?) -> Unit) = addCallback(onEnd = action) /** * Add a callback to this Animatable2 using the provided actions. */ @RequiresApi(23) fun Animatable2.addCallback( - onStart: ((drawable: Drawable) -> Unit)? = null, - onEnd: ((drawable: Drawable) -> Unit)? = null + onStart: ((drawable: Drawable?) -> Unit)? = null, + onEnd: ((drawable: Drawable?) -> Unit)? = null ): Animatable2.AnimationCallback { val callback = object : Animatable2.AnimationCallback() { - override fun onAnimationStart(drawable: Drawable) { + override fun onAnimationStart(drawable: Drawable?) { onStart?.invoke(drawable) } - override fun onAnimationEnd(drawable: Drawable) { + override fun onAnimationEnd(drawable: Drawable?) { onEnd?.invoke(drawable) } } diff --git a/src/main/java/androidx/core/animation/LayoutTransition.kt b/src/main/java/androidx/core/animation/LayoutTransition.kt new file mode 100644 index 00000000..e39d51c3 --- /dev/null +++ b/src/main/java/androidx/core/animation/LayoutTransition.kt @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package androidx.core.animation + +import android.animation.LayoutTransition +import android.animation.LayoutTransition.TransitionListener +import android.view.View +import android.view.ViewGroup + +fun LayoutTransition.doOnStart(action: (LayoutTransition, ViewGroup, View, Int) -> Unit) = + addListener(onStart = action) + +fun LayoutTransition.doOnEnd(action: (LayoutTransition, ViewGroup, View, Int) -> Unit) = + addListener(onEnd = action) + + +fun LayoutTransition.addListener( + onStart: ((LayoutTransition, ViewGroup, View, Int) -> Unit)? = null, + onEnd: ((LayoutTransition, ViewGroup, View, Int) -> Unit)? = null +) { + addTransitionListener( + object : TransitionListener { + override fun startTransition( + transition: LayoutTransition, + container: ViewGroup, + view: View, + transitionType: Int + ) { + onStart?.invoke(transition, container, view, transitionType) + } + + override fun endTransition( + transition: LayoutTransition, + container: ViewGroup, + view: View, + transitionType: Int + ) { + onEnd?.invoke(transition, container, view, transitionType) + } + + } + ) +} \ No newline at end of file From d025812b0621ef8536fef6d1b1374a8b6ffd14bf Mon Sep 17 00:00:00 2001 From: Alterevit Date: Fri, 30 Mar 2018 14:53:58 +0300 Subject: [PATCH 4/5] # update api/current.txt --- api/current.txt | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/api/current.txt b/api/current.txt index 4c56ac98..3ca99f23 100644 --- a/api/current.txt +++ b/api/current.txt @@ -1,5 +1,12 @@ package androidx.core.animation { + public final class Animatable2Kt { + ctor public Animatable2Kt(); + method @RequiresApi(23) public static android.graphics.drawable.Animatable2.AnimationCallback addCallback(android.graphics.drawable.Animatable2, kotlin.jvm.functions.Function1? onStart = "null", kotlin.jvm.functions.Function1? onEnd = "null"); + method @RequiresApi(23) public static android.graphics.drawable.Animatable2.AnimationCallback doOnEnd(android.graphics.drawable.Animatable2, kotlin.jvm.functions.Function1 action); + method @RequiresApi(23) public static android.graphics.drawable.Animatable2.AnimationCallback doOnStart(android.graphics.drawable.Animatable2, kotlin.jvm.functions.Function1 action); + } + public final class AnimatorKt { ctor public AnimatorKt(); method public static android.animation.Animator.AnimatorListener addListener(android.animation.Animator, kotlin.jvm.functions.Function1? onEnd = "null", kotlin.jvm.functions.Function1? onStart = "null", kotlin.jvm.functions.Function1? onCancel = "null", kotlin.jvm.functions.Function1? onRepeat = "null"); @@ -12,6 +19,13 @@ package androidx.core.animation { method public static android.animation.Animator.AnimatorListener doOnStart(android.animation.Animator, kotlin.jvm.functions.Function1 action); } + public final class LayoutTransitionKt { + ctor public LayoutTransitionKt(); + method public static void addListener(android.animation.LayoutTransition, kotlin.jvm.functions.Function4? onStart = "null", kotlin.jvm.functions.Function4? onEnd = "null"); + method public static void doOnEnd(android.animation.LayoutTransition, kotlin.jvm.functions.Function4 action); + method public static void doOnStart(android.animation.LayoutTransition, kotlin.jvm.functions.Function4 action); + } + } package androidx.core.content { From 9fb10df19071386d5fd40a88e8ae086279c61459 Mon Sep 17 00:00:00 2001 From: Alterevit Date: Fri, 30 Mar 2018 15:04:12 +0300 Subject: [PATCH 5/5] # fixed ktlint warnings --- .../java/androidx/core/animation/Animatable2Test.kt | 8 +++----- src/main/java/androidx/core/animation/LayoutTransition.kt | 2 -- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/androidTest/java/androidx/core/animation/Animatable2Test.kt b/src/androidTest/java/androidx/core/animation/Animatable2Test.kt index ac823f4f..de06a7a3 100644 --- a/src/androidTest/java/androidx/core/animation/Animatable2Test.kt +++ b/src/androidTest/java/androidx/core/animation/Animatable2Test.kt @@ -24,18 +24,17 @@ import org.junit.Before import org.junit.Test import org.junit.runner.RunWith - @RunWith(AndroidJUnit4::class) class Animatable2Test { - lateinit private var animatable: Animatable2 + private lateinit var animatable: Animatable2 @Before fun init() { - animatable = object: Animatable2 { + animatable = object : Animatable2 { private var callback: AnimationCallback? = null - override fun isRunning(): Boolean = false + override fun isRunning(): Boolean = false override fun registerAnimationCallback(callback: AnimationCallback?) { this.callback = callback @@ -54,7 +53,6 @@ class Animatable2Test { } override fun unregisterAnimationCallback(callback: AnimationCallback?) = false - } } diff --git a/src/main/java/androidx/core/animation/LayoutTransition.kt b/src/main/java/androidx/core/animation/LayoutTransition.kt index e39d51c3..3cc3094c 100644 --- a/src/main/java/androidx/core/animation/LayoutTransition.kt +++ b/src/main/java/androidx/core/animation/LayoutTransition.kt @@ -27,7 +27,6 @@ fun LayoutTransition.doOnStart(action: (LayoutTransition, ViewGroup, View, Int) fun LayoutTransition.doOnEnd(action: (LayoutTransition, ViewGroup, View, Int) -> Unit) = addListener(onEnd = action) - fun LayoutTransition.addListener( onStart: ((LayoutTransition, ViewGroup, View, Int) -> Unit)? = null, onEnd: ((LayoutTransition, ViewGroup, View, Int) -> Unit)? = null @@ -51,7 +50,6 @@ fun LayoutTransition.addListener( ) { onEnd?.invoke(transition, container, view, transitionType) } - } ) } \ No newline at end of file