1 /*
2  * Copyright 2022 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 androidx.compose.foundation.lazy.staggeredgrid
18 
19 import androidx.compose.animation.core.FiniteAnimationSpec
20 import androidx.compose.animation.core.Spring
21 import androidx.compose.animation.core.VisibilityThreshold
22 import androidx.compose.animation.core.spring
23 import androidx.compose.foundation.lazy.layout.LazyLayoutAnimateItemElement
24 import androidx.compose.runtime.Stable
25 import androidx.compose.ui.Modifier
26 import androidx.compose.ui.unit.IntOffset
27 
28 /** Receiver scope for itemContent in [LazyStaggeredGridScope.item] */
29 @Stable
30 @LazyStaggeredGridScopeMarker
31 sealed interface LazyStaggeredGridItemScope {
32     /**
33      * This modifier animates the item appearance (fade in), disappearance (fade out) and placement
34      * changes (such as an item reordering).
35      *
36      * You should also provide a key via [LazyStaggeredGridScope.item]/
37      * [LazyStaggeredGridScope.items] for this modifier to enable animations.
38      *
39      * @sample androidx.compose.foundation.samples.StaggeredGridAnimateItemSample
40      * @param fadeInSpec an animation specs to use for animating the item appearance. When null is
41      *   provided the item will be appearing without animations.
42      * @param placementSpec an animation specs that will be used to animate the item placement.
43      *   Aside from item reordering all other position changes caused by events like arrangement or
44      *   alignment changes will also be animated. When null is provided no animations will happen.
45      * @param fadeOutSpec an animation specs to use for animating the item disappearance. When null
46      *   is provided the item will be disappearance without animations.
47      */
animateItemnull48     fun Modifier.animateItem(
49         fadeInSpec: FiniteAnimationSpec<Float>? = spring(stiffness = Spring.StiffnessMediumLow),
50         placementSpec: FiniteAnimationSpec<IntOffset>? =
51             spring(
52                 stiffness = Spring.StiffnessMediumLow,
53                 visibilityThreshold = IntOffset.VisibilityThreshold
54             ),
55         fadeOutSpec: FiniteAnimationSpec<Float>? = spring(stiffness = Spring.StiffnessMediumLow),
56     ): Modifier
57 }
58 
59 internal object LazyStaggeredGridItemScopeImpl : LazyStaggeredGridItemScope {
60     override fun Modifier.animateItem(
61         fadeInSpec: FiniteAnimationSpec<Float>?,
62         placementSpec: FiniteAnimationSpec<IntOffset>?,
63         fadeOutSpec: FiniteAnimationSpec<Float>?
64     ): Modifier =
65         if (fadeInSpec == null && placementSpec == null && fadeOutSpec == null) {
66             this
67         } else {
68             this then LazyLayoutAnimateItemElement(fadeInSpec, placementSpec, fadeOutSpec)
69         }
70 }
71