<lambda>null1 package androidx.glance
2
3 import androidx.annotation.RestrictTo
4
5 /*
6 * Copyright 2021 The Android Open Source Project
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
22 inline fun <reified T> GlanceModifier.findModifier(): T? =
23 this.foldIn<T?>(null) { acc, cur ->
24 if (cur is T) {
25 cur
26 } else {
27 acc
28 }
29 }
30
31 /**
32 * Find the last modifier of the given type, and create a new [GlanceModifier] which is equivalent
33 * with the previous one, but without any modifiers of specified type.
34 */
35 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
extractModifiernull36 inline fun <reified T> GlanceModifier.extractModifier(): Pair<T?, GlanceModifier> =
37 if (any { it is T }) {
accnull38 foldIn<Pair<T?, GlanceModifier>>(null to GlanceModifier) { acc, cur ->
39 if (cur is T) {
40 cur to acc.second
41 } else {
42 acc.first to acc.second.then(cur)
43 }
44 }
45 } else {
46 null to this
47 }
48
49 /** Returns a GlanceModifier with all elements of type [T] removed. */
50 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
removeModifiersOfTypenull51 inline fun <reified T> GlanceModifier.removeModifiersOfType(): GlanceModifier where
52 T : GlanceModifier.Element =
53 foldIn(GlanceModifier) { acc: GlanceModifier, cur: GlanceModifier ->
54 cur.takeUnless { it is T }?.let { acc.then(cur) } ?: acc
55 }
56