generated from ReVanced/revanced-patches-template
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Twitter): Add
Dynamic color
patch from revanced
(cherry picked from commit 083bd4009231b9612394b4496ca1d329947d6577)
- Loading branch information
Showing
1 changed file
with
80 additions
and
0 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
src/main/kotlin/app/revanced/patches/twitter/misc/dynamiccolor/DynamicColorPatch.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,80 @@ | ||
package app.revanced.patches.twitter.misc.dynamiccolor | ||
|
||
import app.revanced.patcher.data.ResourceContext | ||
import app.revanced.patcher.patch.PatchException | ||
import app.revanced.patcher.patch.ResourcePatch | ||
import app.revanced.patcher.patch.annotation.CompatiblePackage | ||
import app.revanced.patcher.patch.annotation.Patch | ||
import java.io.FileWriter | ||
import java.nio.file.Files | ||
|
||
@Patch( | ||
name = "Dynamic color", | ||
description = "Replaces the default X (Formerly Twitter) Blue with the user's Material You palette.", | ||
compatiblePackages = [CompatiblePackage("com.twitter.android")], | ||
) | ||
@Suppress("unused") | ||
object DynamicColorPatch : ResourcePatch() { | ||
override fun execute(context: ResourceContext) { | ||
val resDirectory = context.get("res") | ||
if (!resDirectory.isDirectory) throw PatchException("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 { it -> | ||
val colorsXml = it.resolve("colors.xml") | ||
|
||
if (!colorsXml.exists()) { | ||
FileWriter(colorsXml).use { | ||
it.write("<?xml version=\"1.0\" encoding=\"utf-8\"?><resources></resources>") | ||
} | ||
} | ||
} | ||
|
||
context.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", | ||
).forEach { (k, v) -> | ||
val colorElement = document.createElement("color") | ||
|
||
colorElement.setAttribute("name", k) | ||
colorElement.textContent = v | ||
|
||
document.getElementsByTagName("resources").item(0).appendChild(colorElement) | ||
} | ||
} | ||
|
||
context.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) | ||
} | ||
} | ||
} | ||
} |