From a16575b984354138b9ab175307be8d15de60b6a6 Mon Sep 17 00:00:00 2001 From: bennett-sh <110846042+bennett-sh@users.noreply.github.com> Date: Thu, 29 Sep 2022 22:08:00 +0200 Subject: [PATCH] feat: `dynamic-color` patch (#652) --- .../annotations/DynamicColorCompatibility.kt | 9 ++ .../dynamiccolor/patch/DynamicColorPatch.kt | 89 +++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 src/main/kotlin/app/revanced/patches/twitter/misc/dynamiccolor/annotations/DynamicColorCompatibility.kt create mode 100644 src/main/kotlin/app/revanced/patches/twitter/misc/dynamiccolor/patch/DynamicColorPatch.kt diff --git a/src/main/kotlin/app/revanced/patches/twitter/misc/dynamiccolor/annotations/DynamicColorCompatibility.kt b/src/main/kotlin/app/revanced/patches/twitter/misc/dynamiccolor/annotations/DynamicColorCompatibility.kt new file mode 100644 index 000000000..135276a1c --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/twitter/misc/dynamiccolor/annotations/DynamicColorCompatibility.kt @@ -0,0 +1,9 @@ +package app.revanced.patches.twitter.misc.dynamiccolor.annotations + +import app.revanced.patcher.annotation.Compatibility +import app.revanced.patcher.annotation.Package + +@Compatibility([Package("com.twitter.android")]) +@Target(AnnotationTarget.CLASS) +@Retention(AnnotationRetention.RUNTIME) +internal annotation class DynamicColorCompatibility \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/twitter/misc/dynamiccolor/patch/DynamicColorPatch.kt b/src/main/kotlin/app/revanced/patches/twitter/misc/dynamiccolor/patch/DynamicColorPatch.kt new file mode 100644 index 000000000..e6b5aa4b9 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/twitter/misc/dynamiccolor/patch/DynamicColorPatch.kt @@ -0,0 +1,89 @@ +package app.revanced.patches.twitter.misc.dynamiccolor.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.PatchResult +import app.revanced.patcher.patch.PatchResultError +import app.revanced.patcher.patch.PatchResultSuccess +import app.revanced.patcher.patch.annotations.Patch +import app.revanced.patcher.patch.impl.ResourcePatch +import app.revanced.patches.twitter.misc.dynamiccolor.annotations.DynamicColorCompatibility +import java.nio.file.Files + +@Patch +@Name("dynamic-color") +@Description("Replaces the default Twitter Blue with the users Material You palette.") +@DynamicColorCompatibility +@Version("0.0.1") +class DynamicColorPatch : ResourcePatch() { + override fun execute(data: ResourceData): PatchResult { + val resDirectory = data["res"] + if (!resDirectory.isDirectory) return PatchResultError("The res folder can not be found.") + + val valuesV31Directory = resDirectory.resolve("values-v31") + if (!valuesV31Directory.isDirectory) Files.createDirectories(valuesV31Directory.toPath()) + + val valuesNightV31Directory = resDirectory.resolve("values-night-v31") + if (!valuesNightV31Directory.isDirectory) Files.createDirectories(valuesNightV31Directory.toPath()) + + listOf(valuesV31Directory, valuesNightV31Directory).forEach { + val colorsXml = it.resolve("colors.xml") + + if(!colorsXml.exists()) { + Files.writeString( + colorsXml.toPath(), + "\n" + + "\n" + + "" + ) + } + } + + data.xmlEditor["res/values-v31/colors.xml"].use { editor -> + val document = editor.file + + mapOf( + "ps__twitter_blue" to "@color/twitter_blue", + "ps__twitter_blue_pressed" to "@color/twitter_blue_fill_pressed", + "twitter_blue" to "@android:color/system_accent1_400", + "twitter_blue_fill_pressed" to "@android:color/system_accent1_300", + "twitter_blue_opacity_30" to "@android:color/system_accent1_100", + "twitter_blue_opacity_50" to "@android:color/system_accent1_200", + "twitter_blue_opacity_58" to "@android:color/system_accent1_300", + "deep_transparent_twitter_blue" to "@android:color/system_accent1_200", + "ic_launcher_background" to "#1DA1F2" + ).forEach { (k, v) -> + val colorElement = document.createElement("color") + + colorElement.setAttribute("name", k) + colorElement.textContent = v + + document.getElementsByTagName("resources").item(0).appendChild(colorElement) + } + } + + data.xmlEditor["res/values-night-v31/colors.xml"].use { editor -> + val document = editor.file + + mapOf( + "twitter_blue" to "@android:color/system_accent1_200", + "twitter_blue_fill_pressed" to "@android:color/system_accent1_300", + "twitter_blue_opacity_30" to "@android:color/system_accent1_50", + "twitter_blue_opacity_50" to "@android:color/system_accent1_100", + "twitter_blue_opacity_58" to "@android:color/system_accent1_200", + "deep_transparent_twitter_blue" to "@android:color/system_accent1_200" + ).forEach { (k, v) -> + val colorElement = document.createElement("color") + + colorElement.setAttribute("name", k) + colorElement.textContent = v + + document.getElementsByTagName("resources").item(0).appendChild(colorElement) + } + } + + return PatchResultSuccess() + } +} \ No newline at end of file