From 3b27c2ccc14de1e4401153acc3e04d8400b5c1ca Mon Sep 17 00:00:00 2001 From: inotia00 <108592928+inotia00@users.noreply.github.com> Date: Sat, 23 Dec 2023 23:26:20 +0900 Subject: [PATCH] feat(Reddit): add `Custom branding name Reddit` patch --- .../branding/name/CustomBrandingNamePatch.kt | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/main/kotlin/app/revanced/patches/reddit/layout/branding/name/CustomBrandingNamePatch.kt diff --git a/src/main/kotlin/app/revanced/patches/reddit/layout/branding/name/CustomBrandingNamePatch.kt b/src/main/kotlin/app/revanced/patches/reddit/layout/branding/name/CustomBrandingNamePatch.kt new file mode 100644 index 000000000..b20e32d2f --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/reddit/layout/branding/name/CustomBrandingNamePatch.kt @@ -0,0 +1,62 @@ +package app.revanced.patches.reddit.layout.branding.name + +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 app.revanced.patcher.patch.options.PatchOption.PatchExtensions.stringPatchOption +import java.io.FileWriter +import java.nio.file.Files + +@Patch( + name = "Custom branding name Reddit", + description = "Rename the Reddit app to the name specified in options.json.", + compatiblePackages = [CompatiblePackage("com.reddit.frontpage")], + use = false +) +@Suppress("unused") +object CustomBrandingNamePatch : ResourcePatch() { + private const val APP_NAME = "RVX Reddit" + + private val AppName by stringPatchOption( + key = "AppName", + default = APP_NAME, + title = "App name", + description = "The name of the app." + ) + + override fun execute(context: ResourceContext) { + val appName = AppName + ?: throw PatchException("Invalid app name.") + + val resDirectory = context["res"] + + val valuesV24Directory = resDirectory.resolve("values-v24") + if (!valuesV24Directory.isDirectory) + Files.createDirectories(valuesV24Directory.toPath()) + + val stringsXml = valuesV24Directory.resolve("strings.xml") + + if (!stringsXml.exists()) { + FileWriter(stringsXml).use { + it.write("") + } + } + + context.xmlEditor["res/values-v24/strings.xml"].use { editor -> + val document = editor.file + + mapOf( + "app_name" to appName + ).forEach { (k, v) -> + val stringElement = document.createElement("string") + + stringElement.setAttribute("name", k) + stringElement.textContent = v + + document.getElementsByTagName("resources").item(0).appendChild(stringElement) + } + } + } +}