generated from ReVanced/revanced-patches-template
-
-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Theme Patch * refactor: deprecate AmoledPatch * Update src/main/kotlin/app/revanced/patches/youtube/layout/theme/patch/ThemePatch.kt Co-authored-by: oSumAtrIX <johan.melkonyan1@web.de> * refactor: apply requested changes Co-authored-by: oSumAtrIX <johan.melkonyan1@web.de>
- Loading branch information
Showing
3 changed files
with
77 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/app/revanced/patches/youtube/layout/theme/annotations/ThemeCompatibility.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package app.revanced.patches.youtube.layout.theme.annotations | ||
|
||
import app.revanced.patcher.annotation.Compatibility | ||
import app.revanced.patcher.annotation.Package | ||
|
||
@Compatibility([Package("com.google.android.youtube")]) | ||
@Target(AnnotationTarget.CLASS) | ||
@Retention(AnnotationRetention.RUNTIME) | ||
internal annotation class ThemeCompatibility |
63 changes: 63 additions & 0 deletions
63
src/main/kotlin/app/revanced/patches/youtube/layout/theme/patch/ThemePatch.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package app.revanced.patches.youtube.layout.theme.patch | ||
|
||
import app.revanced.patcher.annotation.Description | ||
import app.revanced.patcher.annotation.Name | ||
import app.revanced.patcher.annotation.Version | ||
import app.revanced.patcher.data.impl.ResourceData | ||
import app.revanced.patcher.patch.* | ||
import app.revanced.patcher.patch.annotations.DependsOn | ||
import app.revanced.patcher.patch.annotations.Patch | ||
import app.revanced.patcher.patch.impl.ResourcePatch | ||
import app.revanced.patches.youtube.layout.theme.annotations.ThemeCompatibility | ||
import app.revanced.patches.youtube.misc.manifest.patch.FixLocaleConfigErrorPatch | ||
import org.w3c.dom.Element | ||
|
||
@Patch | ||
@DependsOn([FixLocaleConfigErrorPatch::class]) | ||
@Name("theme") | ||
@Description("Enables a custom theme.") | ||
@ThemeCompatibility | ||
@Version("0.0.1") | ||
class ThemePatch : ResourcePatch() { | ||
override fun execute(data: ResourceData): PatchResult { | ||
val theme = Themes.of(theme) ?: return PatchResultError("Theme '$theme' not found.") | ||
|
||
data.xmlEditor["res/values/colors.xml"].use { editor -> | ||
val resourcesNode = editor.file.getElementsByTagName("resources").item(0) as Element | ||
|
||
for (i in 0 until resourcesNode.childNodes.length) { | ||
val node = resourcesNode.childNodes.item(i) as? Element ?: continue | ||
node.textContent = theme.apply(node.getAttribute("name")) ?: continue | ||
} | ||
} | ||
|
||
return PatchResultSuccess() | ||
} | ||
|
||
companion object : OptionsContainer() { | ||
var theme: String by PatchOption.StringListOption( | ||
key = "theme", | ||
default = null, | ||
options = Themes.names, | ||
title = "Theme", | ||
description = "Select a theme.", | ||
required = true | ||
) | ||
} | ||
|
||
enum class Themes(val apply: (String) -> String?) { | ||
Amoled({ attr -> | ||
when (attr) { | ||
"yt_black1", "yt_black1_opacity95", "yt_black2", "yt_black3", "yt_black4", "yt_status_bar_background_dark" -> "@android:color/black" | ||
"yt_selected_nav_label_dark" -> "#ffdf0000" | ||
else -> null | ||
} | ||
}); | ||
|
||
companion object { | ||
val names = values().map { it.name } | ||
|
||
fun of(name: String) = values().firstOrNull { it.name == name } | ||
} | ||
} | ||
} |