Skip to content

Commit

Permalink
feat: Theme Patch (#440)
Browse files Browse the repository at this point in the history
* 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
Sculas and oSumAtrIX committed Sep 8, 2022
1 parent 2431d65 commit ae145ab
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,27 @@ package app.revanced.patches.youtube.layout.amoled.patch

import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.annotation.PatchDeprecated
import app.revanced.patcher.annotation.Version
import app.revanced.patcher.data.impl.ResourceData
import app.revanced.patcher.patch.PatchResult
import app.revanced.patcher.patch.PatchResultSuccess
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.amoled.annotations.AmoledCompatibility
import app.revanced.patches.youtube.layout.theme.patch.ThemePatch
import app.revanced.patches.youtube.misc.manifest.patch.FixLocaleConfigErrorPatch
import org.w3c.dom.Element

@Patch
@DependsOn([FixLocaleConfigErrorPatch::class])
@Name("amoled")
@Description("Enables pure black theme.")
@AmoledCompatibility
@Version("0.0.1")
@PatchDeprecated("Theme patch already includes the Amoled theme.", ThemePatch::class)
class AmoledPatch : ResourcePatch() {
override fun execute(data: ResourceData): PatchResult {
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)
if (node !is Element) continue

val element = resourcesNode.childNodes.item(i) as Element
element.textContent = when (element.getAttribute("name")) {
"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 -> continue
}
}
}

return PatchResultSuccess()
ThemePatch.theme = ThemePatch.Themes.Amoled.name
return ThemePatch().execute(data)
}
}
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
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 }
}
}
}

0 comments on commit ae145ab

Please sign in to comment.