1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.systemui.util
18 
19 import androidx.core.animation.Animator
20 
21 /**
22  * Add an action which will be invoked when the animation has ended.
23  *
24  * @return the [Animator.AnimatorListener] added to the Animator
25  * @see Animator.end
26  */
doOnEndnull27 inline fun Animator.doOnEnd(
28     crossinline action: (animator: Animator) -> Unit
29 ): Animator.AnimatorListener = addListener(onEnd = action)
30 
31 /**
32  * Add an action which will be invoked when the animation has started.
33  *
34  * @return the [Animator.AnimatorListener] added to the Animator
35  * @see Animator.start
36  */
37 inline fun Animator.doOnStart(
38     crossinline action: (animator: Animator) -> Unit
39 ): Animator.AnimatorListener = addListener(onStart = action)
40 
41 /**
42  * Add an action which will be invoked when the animation has been cancelled.
43  *
44  * @return the [Animator.AnimatorListener] added to the Animator
45  * @see Animator.cancel
46  */
47 inline fun Animator.doOnCancel(
48     crossinline action: (animator: Animator) -> Unit
49 ): Animator.AnimatorListener = addListener(onCancel = action)
50 
51 /**
52  * Add an action which will be invoked when the animation has repeated.
53  *
54  * @return the [Animator.AnimatorListener] added to the Animator
55  */
56 inline fun Animator.doOnRepeat(
57     crossinline action: (animator: Animator) -> Unit
58 ): Animator.AnimatorListener = addListener(onRepeat = action)
59 
60 /**
61  * Add a listener to this Animator using the provided actions.
62  *
63  * @return the [Animator.AnimatorListener] added to the Animator
64  */
65 inline fun Animator.addListener(
66     crossinline onEnd: (animator: Animator) -> Unit = {},
<lambda>null67     crossinline onStart: (animator: Animator) -> Unit = {},
<lambda>null68     crossinline onCancel: (animator: Animator) -> Unit = {},
<lambda>null69     crossinline onRepeat: (animator: Animator) -> Unit = {}
70 ): Animator.AnimatorListener {
71     val listener =
72         object : Animator.AnimatorListener {
onAnimationRepeatnull73             override fun onAnimationRepeat(animator: Animator) = onRepeat(animator)
74             override fun onAnimationEnd(animator: Animator) = onEnd(animator)
75             override fun onAnimationCancel(animator: Animator) = onCancel(animator)
76             override fun onAnimationStart(animator: Animator) = onStart(animator)
77         }
78     addListener(listener)
79     return listener
80 }
81