1/*
2 * Copyright (C) 2018 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@file:Suppress("NOTHING_TO_INLINE", "DeprecatedCallableAddReplaceWith", "DEPRECATION")
18
19package androidx.core.preference
20
21import android.preference.Preference
22import android.preference.PreferenceGroup
23
24/**
25 * Returns the preference with `key`.
26 *
27 * @throws NullPointerException if no preference is found with that key.
28 */
29@Deprecated("Use Jetpack preference library")
30inline operator fun PreferenceGroup.get(key: CharSequence): Preference = findPreference(key)
31
32/**
33 * Returns the preference at `index`.
34 *
35 * @throws IndexOutOfBoundsException if index is less than 0 or greater than or equal to the count.
36 */
37@Deprecated("Use Jetpack preference library")
38operator fun PreferenceGroup.get(index: Int): Preference = getPreference(index)
39        ?: throw IndexOutOfBoundsException("Index: $index, Size: $preferenceCount")
40
41/** Returns `true` if `preference` is found in this preference group. */
42@Deprecated("Use Jetpack preference library")
43operator fun PreferenceGroup.contains(preference: Preference): Boolean {
44    for (index in 0 until preferenceCount) {
45        if (getPreference(index) == preference) {
46            return true
47        }
48    }
49    return false
50}
51
52/** Adds `preference` to this preference group. */
53@Deprecated("Use Jetpack preference library")
54inline operator fun PreferenceGroup.plusAssign(preference: Preference) {
55    addPreference(preference)
56}
57
58/** Removes `preference` from this preference group. */
59@Deprecated("Use Jetpack preference library")
60inline operator fun PreferenceGroup.minusAssign(preference: Preference) {
61    removePreference(preference)
62}
63
64/** Returns the number of preferences in this preference group. */
65@Deprecated("Use Jetpack preference library")
66inline val PreferenceGroup.size: Int get() = preferenceCount
67
68/** Returns true if this preference group contains no preferences. */
69@Deprecated("Use Jetpack preference library")
70inline fun PreferenceGroup.isEmpty(): Boolean = size == 0
71
72/** Returns true if this preference group contains one or more preferences. */
73@Deprecated("Use Jetpack preference library")
74inline fun PreferenceGroup.isNotEmpty(): Boolean = size != 0
75
76/** Performs the given action on each preference in this preference group. */
77@Deprecated("Use Jetpack preference library")
78inline fun PreferenceGroup.forEach(action: (preference: Preference) -> Unit) {
79    for (index in 0 until preferenceCount) {
80        action(get(index))
81    }
82}
83
84/** Performs the given action on each preference in this preference group, providing its sequential index. */
85@Deprecated("Use Jetpack preference library")
86inline fun PreferenceGroup.forEachIndexed(action: (index: Int, preference: Preference) -> Unit) {
87    for (index in 0 until preferenceCount) {
88        action(index, get(index))
89    }
90}
91
92/** Returns a [MutableIterator] over the preferences in this preference group. */
93@Deprecated("Use Jetpack preference library")
94operator fun PreferenceGroup.iterator() = object : MutableIterator<Preference> {
95    private var index = 0
96    override fun hasNext() = index < size
97    override fun next() = getPreference(index++) ?: throw IndexOutOfBoundsException()
98    override fun remove() {
99        removePreference(getPreference(--index))
100    }
101}
102
103/** Returns a [Sequence] over the preferences in this preference group. */
104@Deprecated("Use Jetpack preference library")
105val PreferenceGroup.children: Sequence<Preference>
106    get() = object : Sequence<Preference> {
107        override fun iterator() = this@children.iterator()
108    }
109