Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ffef7e77 authored by Sunny Shao's avatar Sunny Shao
Browse files

Add the addBefore and addAfer APIs

Test: manual test
Bug: 360061554
Flag: com.android.settings.flags.catalyst_legal_information
Change-Id: I01d59462250b6750a3671c29fbc2f4218b92fae8
parent fc270041
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -54,6 +54,20 @@ class PreferenceHierarchy internal constructor(metadata: PreferenceMetadata) :
        children.add(PreferenceHierarchyNode(metadata))
    }

    /** Adds a preference to the hierarchy before a specific preference. */
    fun addBefore(key: String, metadata: PreferenceMetadata) {
        var foundIndex = children.indexOfFirst { it.metadata.key == key }
        if (foundIndex == -1) foundIndex = children.size
        children.add(foundIndex, PreferenceHierarchyNode(metadata))
    }

    /** Adds a preference to the hierarchy after a specific preference. */
    fun addAfter(key: String, metadata: PreferenceMetadata) {
        var foundIndex = children.indexOfFirst { it.metadata.key == key }
        if (foundIndex == -1) foundIndex = children.size else foundIndex++
        children.add(foundIndex, PreferenceHierarchyNode(metadata))
    }

    /** Adds a preference group to the hierarchy. */
    operator fun PreferenceGroup.unaryPlus() = PreferenceHierarchy(this).also { children.add(it) }