refactor: Improve XML performance

This commit is contained in:
LisoUseInAIKyrios 2025-02-11 15:18:35 +02:00
parent 0594887e3b
commit 0868e38ce6
6 changed files with 24 additions and 11 deletions

View File

@ -56,8 +56,9 @@ val customThemePatch = resourcePatch(
document("res/values/colors.xml").use { document ->
val resourcesNode = document.getElementsByTagName("resources").item(0) as Element
for (i in 0 until resourcesNode.childNodes.length) {
val node = resourcesNode.childNodes.item(i) as? Element ?: continue
val childNodes = resourcesNode.childNodes
for (i in 0 until childNodes.length) {
val node = childNodes.item(i) as? Element ?: continue
node.textContent =
when (node.getAttribute("name")) {

View File

@ -172,14 +172,16 @@ val changeHeaderPatch = resourcePatch(
// Instead change styles.xml to use the old drawable resources.
if (is_19_25_or_greater) {
document("res/values/styles.xml").use { document ->
val documentChildNodes = document.childNodes
arrayOf(
"CairoLightThemeRingo2Updates" to variants[0],
"CairoDarkThemeRingo2Updates" to variants[1]
).forEach { (styleName, theme) ->
val style = document.childNodes.findElementByAttributeValueOrThrow(
val styleNodes = documentChildNodes.findElementByAttributeValueOrThrow(
"name",
styleName,
)
).childNodes
val drawable = "@drawable/${HEADER_FILE_NAME}_${theme}"
@ -187,7 +189,7 @@ val changeHeaderPatch = resourcePatch(
"ytWordmarkHeader",
"ytPremiumWordmarkHeader"
).forEach { itemName ->
style.childNodes.findElementByAttributeValueOrThrow(
styleNodes.findElementByAttributeValueOrThrow(
"name",
itemName,
).textContent = drawable

View File

@ -125,8 +125,10 @@ private val seekbarColorResourcePatch = resourcePatch {
fun setSplashDrawablePathFillColor(xmlFileNames: Iterable<String>, vararg resourceNames: String) {
xmlFileNames.forEach { xmlFileName ->
document(xmlFileName).use { document ->
val childNodes = document.childNodes
resourceNames.forEach { elementId ->
val element = document.childNodes.findElementByAttributeValueOrThrow(
val element = childNodes.findElementByAttributeValueOrThrow(
"android:name",
elementId
)

View File

@ -76,10 +76,11 @@ val playerControlsResourcePatch = resourcePatch {
"android.support.constraint.ConstraintLayout",
).item(0)
var bottomInsertBeforeNode: Node = bottomTargetDocument.childNodes.findElementByAttributeValue(
val bottomTargetDocumentChildNodes = bottomTargetDocument.childNodes
var bottomInsertBeforeNode: Node = bottomTargetDocumentChildNodes.findElementByAttributeValue(
"android:inflatedId",
bottomLastLeftOf,
) ?: bottomTargetDocument.childNodes.findElementByAttributeValueOrThrow(
) ?: bottomTargetDocumentChildNodes.findElementByAttributeValueOrThrow(
"android:id", // Older targets use non-inflated id.
bottomLastLeftOf,
)
@ -143,11 +144,13 @@ val playerControlsResourcePatch = resourcePatch {
}
finalize {
val childNodes = bottomTargetDocument.childNodes
arrayOf(
"@id/bottom_end_container",
"@id/multiview_button",
).forEach {
bottomTargetDocument.childNodes.findElementByAttributeValue(
childNodes.findElementByAttributeValue(
"android:id",
it,
)?.setAttribute("yt:layout_constraintRight_toLeftOf", bottomLastLeftOf)

View File

@ -97,6 +97,8 @@ private val settingsResourcePatch = resourcePatch {
// Remove horizontal divider from the settings Preferences
// To better match the appearance of the stock YouTube settings.
document("res/values/styles.xml").use { document ->
val childNodes = document.childNodes
arrayOf(
"Theme.YouTube.Settings",
"Theme.YouTube.Settings.Dark",
@ -105,7 +107,7 @@ private val settingsResourcePatch = resourcePatch {
listDividerNode.setAttribute("name", "android:listDivider")
listDividerNode.appendChild(document.createTextNode("@null"))
document.childNodes.findElementByAttributeValueOrThrow(
childNodes.findElementByAttributeValueOrThrow(
"name",
value,
).appendChild(listDividerNode)

View File

@ -41,7 +41,10 @@ inline fun Node.forEachChildElement(action: (Element) -> Unit) =
*/
fun Node.doRecursively(action: (Node) -> Unit) {
action(this)
for (i in 0 until this.childNodes.length) this.childNodes.item(i).doRecursively(action)
val childNodes = this.childNodes
for (i in 0 until childNodes.length) {
childNodes.item(i).doRecursively(action)
}
}
fun Node.insertFirst(node: Node) {