Skip to content
This repository has been archived by the owner on Nov 14, 2018. It is now read-only.

add extensions for registration callback for Animatable2 and LayoutTransition #457

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions api/current.txt
Original file line number Diff line number Diff line change
@@ -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<? super android.graphics.drawable.Drawable,kotlin.Unit>? onStart = "null", kotlin.jvm.functions.Function1<? super android.graphics.drawable.Drawable,kotlin.Unit>? onEnd = "null");
method @RequiresApi(23) public static android.graphics.drawable.Animatable2.AnimationCallback doOnEnd(android.graphics.drawable.Animatable2, kotlin.jvm.functions.Function1<? super android.graphics.drawable.Drawable,kotlin.Unit> action);
method @RequiresApi(23) public static android.graphics.drawable.Animatable2.AnimationCallback doOnStart(android.graphics.drawable.Animatable2, kotlin.jvm.functions.Function1<? super android.graphics.drawable.Drawable,kotlin.Unit> action);
}

public final class AnimatorKt {
ctor public AnimatorKt();
method public static android.animation.Animator.AnimatorListener addListener(android.animation.Animator, kotlin.jvm.functions.Function1<? super android.animation.Animator,kotlin.Unit>? onEnd = "null", kotlin.jvm.functions.Function1<? super android.animation.Animator,kotlin.Unit>? onStart = "null", kotlin.jvm.functions.Function1<? super android.animation.Animator,kotlin.Unit>? onCancel = "null", kotlin.jvm.functions.Function1<? super android.animation.Animator,kotlin.Unit>? onRepeat = "null");
Expand All @@ -12,6 +19,13 @@ package androidx.core.animation {
method public static android.animation.Animator.AnimatorListener doOnStart(android.animation.Animator, kotlin.jvm.functions.Function1<? super android.animation.Animator,kotlin.Unit> action);
}

public final class LayoutTransitionKt {
ctor public LayoutTransitionKt();
method public static void addListener(android.animation.LayoutTransition, kotlin.jvm.functions.Function4<? super android.animation.LayoutTransition,? super android.view.ViewGroup,? super android.view.View,? super java.lang.Integer,kotlin.Unit>? onStart = "null", kotlin.jvm.functions.Function4<? super android.animation.LayoutTransition,? super android.view.ViewGroup,? super android.view.View,? super java.lang.Integer,kotlin.Unit>? onEnd = "null");
method public static void doOnEnd(android.animation.LayoutTransition, kotlin.jvm.functions.Function4<? super android.animation.LayoutTransition,? super android.view.ViewGroup,? super android.view.View,? super java.lang.Integer,kotlin.Unit> action);
method public static void doOnStart(android.animation.LayoutTransition, kotlin.jvm.functions.Function4<? super android.animation.LayoutTransition,? super android.view.ViewGroup,? super android.view.View,? super java.lang.Integer,kotlin.Unit> action);
}

}

package androidx.core.content {
Expand Down
73 changes: 73 additions & 0 deletions src/androidTest/java/androidx/core/animation/Animatable2Test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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 {

private lateinit 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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class AnimatorTest {

private val context = InstrumentationRegistry.getContext()
private val view = View(context)

Expand All @@ -44,7 +45,6 @@ class AnimatorTest {
animator.doOnStart {
called = true
}

animator.listeners.forEach { it.onAnimationStart(animator) }
assertTrue(called)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}
48 changes: 48 additions & 0 deletions src/main/java/androidx/core/animation/Animatable2.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.doOnStart(action: (Drawable?) -> Unit) = addCallback(onStart = action)

@RequiresApi(23)
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
): 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
}
55 changes: 55 additions & 0 deletions src/main/java/androidx/core/animation/LayoutTransition.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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)
}
}
)
}