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 -> document("res/values/colors.xml").use { document ->
val resourcesNode = document.getElementsByTagName("resources").item(0) as Element val resourcesNode = document.getElementsByTagName("resources").item(0) as Element
for (i in 0 until resourcesNode.childNodes.length) { val childNodes = resourcesNode.childNodes
val node = resourcesNode.childNodes.item(i) as? Element ?: continue for (i in 0 until childNodes.length) {
val node = childNodes.item(i) as? Element ?: continue
node.textContent = node.textContent =
when (node.getAttribute("name")) { when (node.getAttribute("name")) {

View File

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

View File

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

View File

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

View File

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

View File

@ -41,7 +41,10 @@ inline fun Node.forEachChildElement(action: (Element) -> Unit) =
*/ */
fun Node.doRecursively(action: (Node) -> Unit) { fun Node.doRecursively(action: (Node) -> Unit) {
action(this) 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) { fun Node.insertFirst(node: Node) {