Skip to content

Commit 6ccf114

Browse files
fix(YouTube - Change header): Apply header changes to A/B layout (ReVanced#3907)
1 parent c3d00ce commit 6ccf114

File tree

1 file changed

+54
-13
lines changed

1 file changed

+54
-13
lines changed

patches/src/main/kotlin/app/revanced/patches/youtube/layout/branding/header/ChangeHeaderPatch.kt

+54-13
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ package app.revanced.patches.youtube.layout.branding.header
33
import app.revanced.patcher.patch.PatchException
44
import app.revanced.patcher.patch.resourcePatch
55
import app.revanced.patcher.patch.stringOption
6+
import app.revanced.patches.youtube.misc.playservice.is_19_25_or_greater
7+
import app.revanced.patches.youtube.misc.playservice.versionCheckPatch
68
import app.revanced.util.ResourceGroup
79
import app.revanced.util.Utils.trimIndentMultiline
810
import app.revanced.util.copyResources
11+
import app.revanced.util.findElementByAttributeValueOrThrow
912
import java.io.File
1013

1114
private const val HEADER_FILE_NAME = "yt_wordmark_header"
@@ -34,6 +37,8 @@ val changeHeaderPatch = resourcePatch(
3437
description = "Applies a custom header in the top left corner within the app. Defaults to the ReVanced header.",
3538
use = false,
3639
) {
40+
dependsOn(versionCheckPatch)
41+
3742
compatibleWith("com.google.android.youtube")
3843

3944
val header by stringOption(
@@ -79,7 +84,7 @@ val changeHeaderPatch = resourcePatch(
7984
/**
8085
* A function that overwrites both header variants in the target resource directories.
8186
*/
82-
val overwriteFromTo: (String, String) -> Unit = { from: String, to: String ->
87+
fun overwriteFromTo(from: String, to: String) {
8388
targetResourceDirectories.forEach { directory ->
8489
variants.forEach { variant ->
8590
val fromPath = directory.resolve("${from}_$variant.png")
@@ -91,23 +96,28 @@ val changeHeaderPatch = resourcePatch(
9196
}
9297

9398
// Functions to overwrite the header to the different variants.
94-
val toPremium = { overwriteFromTo(PREMIUM_HEADER_FILE_NAME, HEADER_FILE_NAME) }
95-
val toHeader = { overwriteFromTo(HEADER_FILE_NAME, PREMIUM_HEADER_FILE_NAME) }
96-
val toReVanced = {
99+
fun toPremium() { overwriteFromTo(PREMIUM_HEADER_FILE_NAME, HEADER_FILE_NAME) }
100+
fun toHeader() { overwriteFromTo(HEADER_FILE_NAME, PREMIUM_HEADER_FILE_NAME) }
101+
fun toReVanced() {
97102
// Copy the ReVanced header to the resource directories.
98103
targetResourceFiles.forEach { copyResources("change-header/revanced", it) }
99104

100105
// Overwrite the premium with the custom header as well.
101106
toHeader()
102107
}
103-
val toReVancedBorderless = {
108+
fun toReVancedBorderless() {
104109
// Copy the ReVanced borderless header to the resource directories.
105-
targetResourceFiles.forEach { copyResources("change-header/revanced-borderless", it) }
110+
targetResourceFiles.forEach {
111+
copyResources(
112+
"change-header/revanced-borderless",
113+
it
114+
)
115+
}
106116

107117
// Overwrite the premium with the custom header as well.
108118
toHeader()
109119
}
110-
val toCustom = {
120+
fun toCustom() {
111121
val sourceFolders = File(header!!).listFiles { file -> file.isDirectory }
112122
?: throw PatchException("The provided path is not a directory: $header")
113123

@@ -136,11 +146,42 @@ val changeHeaderPatch = resourcePatch(
136146
}
137147

138148
when (header) {
139-
HEADER_OPTION -> toHeader
140-
PREMIUM_HEADER_OPTION -> toPremium
141-
REVANCED_HEADER_OPTION -> toReVanced
142-
REVANCED_BORDERLESS_HEADER_OPTION -> toReVancedBorderless
143-
else -> toCustom
144-
}()
149+
HEADER_OPTION -> toHeader()
150+
PREMIUM_HEADER_OPTION -> toPremium()
151+
REVANCED_HEADER_OPTION -> toReVanced()
152+
REVANCED_BORDERLESS_HEADER_OPTION -> toReVancedBorderless()
153+
else -> toCustom()
154+
}
155+
156+
// Fix 19.25+ A/B layout with different header icons:
157+
// yt_ringo2_wordmark_header, yt_ringo2_premium_wordmark_header
158+
//
159+
// These images are webp and not png, so overwriting them is not so simple.
160+
// Instead change styles.xml to use the old drawable resources.
161+
if (is_19_25_or_greater) {
162+
document("res/values/styles.xml").use { document ->
163+
arrayOf(
164+
"CairoLightThemeRingo2Updates" to variants[0],
165+
"CairoDarkThemeRingo2Updates" to variants[1]
166+
).forEach { (styleName, theme) ->
167+
val style = document.childNodes.findElementByAttributeValueOrThrow(
168+
"name",
169+
styleName,
170+
)
171+
172+
val drawable = "@drawable/${HEADER_FILE_NAME}_${theme}"
173+
174+
arrayOf(
175+
"ytWordmarkHeader",
176+
"ytPremiumWordmarkHeader"
177+
).forEach { itemName ->
178+
style.childNodes.findElementByAttributeValueOrThrow(
179+
"name",
180+
itemName,
181+
).textContent = drawable
182+
}
183+
}
184+
}
185+
}
145186
}
146187
}

0 commit comments

Comments
 (0)