This commit is contained in:
+83
-75
@@ -8,92 +8,100 @@
|
||||
}
|
||||
"""
|
||||
|
||||
priority = 0
|
||||
|
||||
# imports
|
||||
__author__ = "wowlikon <wowlikon@gmail.com>"
|
||||
__version__ = "1.0.0"
|
||||
import os
|
||||
import shutil
|
||||
from tqdm import tqdm
|
||||
from typing import Any, Dict
|
||||
|
||||
from lxml import etree
|
||||
from pydantic import Field
|
||||
from typing import Dict, Any
|
||||
from utils.config import PatchConfig
|
||||
from tqdm import tqdm
|
||||
|
||||
#Config
|
||||
class Config(PatchConfig):
|
||||
from utils.config import PatchTemplate
|
||||
|
||||
|
||||
class Patch(PatchTemplate):
|
||||
priority: int = Field(frozen=True, exclude=True, default=0)
|
||||
replace: bool = Field(True, description="Менять местами лайк/дизлайк")
|
||||
custom_icons: bool = Field(True, description="Кастомные иконки")
|
||||
icon_size: str = Field("18.0dip", description="Размер иконки")
|
||||
|
||||
# Patch
|
||||
def apply(config, base: Dict[str, Any]) -> bool:
|
||||
file_path = "./decompiled/res/layout/item_comment.xml"
|
||||
parser = etree.XMLParser(remove_blank_text=True)
|
||||
tree = etree.parse(file_path, parser)
|
||||
root = tree.getroot()
|
||||
def apply(self, base: Dict[str, Any]) -> bool:
|
||||
file_path = "./decompiled/res/layout/item_comment.xml"
|
||||
parser = etree.XMLParser(remove_blank_text=True)
|
||||
tree = etree.parse(file_path, parser)
|
||||
root = tree.getroot()
|
||||
|
||||
tqdm.write("Меняем размер иконок лайка и дизлайка...")
|
||||
for icon in root.xpath(
|
||||
".//*[@android:id='@id/votePlusInactive']//ImageView | "
|
||||
".//*[@android:id='@id/votePlusActive']//ImageView | "
|
||||
".//*[@android:id='@id/voteMinusInactive']//ImageView | "
|
||||
".//*[@android:id='@id/voteMinusActive']//ImageView",
|
||||
namespaces=base['xml_ns'],
|
||||
):
|
||||
icon.set(f"{{{base['xml_ns']['android']}}}layout_width", config.icon_size)
|
||||
# icon.set(f"{{{base['xml_ns']['android']}}}layout_height", config.icon_size)
|
||||
|
||||
if config.replace:
|
||||
tqdm.write("Меняем местами лайк и дизлайк комментария...")
|
||||
|
||||
containers = root.xpath(
|
||||
".//LinearLayout[.//*[@android:id='@id/voteMinus'] and .//*[@android:id='@id/votePlus']]",
|
||||
namespaces=base["xml_ns"],
|
||||
)
|
||||
|
||||
found = False
|
||||
for container in containers:
|
||||
children = list(container)
|
||||
vote_plus = None
|
||||
vote_minus = None
|
||||
|
||||
for ch in children:
|
||||
cid = ch.get(f'{{{base["xml_ns"]["android"]}}}id')
|
||||
if cid == "@id/votePlus":
|
||||
vote_plus = ch
|
||||
elif cid == "@id/voteMinus":
|
||||
vote_minus = ch
|
||||
|
||||
if vote_plus is not None and vote_minus is not None:
|
||||
found = True
|
||||
i_plus = children.index(vote_plus)
|
||||
i_minus = children.index(vote_minus)
|
||||
children[i_plus], children[i_minus] = children[i_minus], children[i_plus]
|
||||
container[:] = children
|
||||
tqdm.write("Кнопки лайк и дизлайк поменялись местами.")
|
||||
break
|
||||
|
||||
if not found:
|
||||
tqdm.write("Не удалось найти оба узла votePlus/voteMinus даже в общих LinearLayout.")
|
||||
|
||||
if config.custom_icons:
|
||||
tqdm.write("Заменяем иконки лайка и дизлайка на кастомные...")
|
||||
for suffix in ["up", "up_40", "down", "down_40"]:
|
||||
shutil.copy(
|
||||
f"./resources/ic_chevron_{suffix}.xml",
|
||||
f"./decompiled/res/drawable/ic_chevron_{suffix}.xml",
|
||||
)
|
||||
|
||||
for inactive in root.xpath(
|
||||
".//*[@android:id='@id/votePlusInactive'] | .//*[@android:id='@id/voteMinusInactive']",
|
||||
tqdm.write("Меняем размер иконок лайка и дизлайка...")
|
||||
for icon in root.xpath(
|
||||
".//*[@android:id='@id/votePlusInactive']//ImageView | "
|
||||
".//*[@android:id='@id/votePlusActive']//ImageView | "
|
||||
".//*[@android:id='@id/voteMinusInactive']//ImageView | "
|
||||
".//*[@android:id='@id/voteMinusActive']//ImageView",
|
||||
namespaces=base["xml_ns"],
|
||||
):
|
||||
for img in inactive.xpath(".//ImageView[@android:src]", namespaces=base["xml_ns"]):
|
||||
src = img.get(f'{{{base["xml_ns"]["android"]}}}src', "")
|
||||
if src.startswith("@drawable/") and not src.endswith("_40"):
|
||||
img.set(f'{{{base["xml_ns"]["android"]}}}src', src + "_40")
|
||||
icon.set(f"{{{base['xml_ns']['android']}}}layout_width", self.icon_size)
|
||||
# icon.set(f"{{{base['xml_ns']['android']}}}layout_height", self.icon_size)
|
||||
|
||||
# Сохраняем
|
||||
tree.write(file_path, encoding="utf-8", xml_declaration=True, pretty_print=True)
|
||||
if self.replace:
|
||||
tqdm.write("Меняем местами лайк и дизлайк комментария...")
|
||||
|
||||
return True
|
||||
containers = root.xpath(
|
||||
".//LinearLayout[.//*[@android:id='@id/voteMinus'] and .//*[@android:id='@id/votePlus']]",
|
||||
namespaces=base["xml_ns"],
|
||||
)
|
||||
|
||||
found = False
|
||||
for container in containers:
|
||||
children = list(container)
|
||||
vote_plus = None
|
||||
vote_minus = None
|
||||
|
||||
for ch in children:
|
||||
cid = ch.get(f'{{{base["xml_ns"]["android"]}}}id')
|
||||
if cid == "@id/votePlus":
|
||||
vote_plus = ch
|
||||
elif cid == "@id/voteMinus":
|
||||
vote_minus = ch
|
||||
|
||||
if vote_plus is not None and vote_minus is not None:
|
||||
found = True
|
||||
i_plus = children.index(vote_plus)
|
||||
i_minus = children.index(vote_minus)
|
||||
children[i_plus], children[i_minus] = (
|
||||
children[i_minus],
|
||||
children[i_plus],
|
||||
)
|
||||
container[:] = children
|
||||
tqdm.write("Кнопки лайк и дизлайк поменялись местами.")
|
||||
break
|
||||
|
||||
if not found:
|
||||
tqdm.write(
|
||||
"Не удалось найти оба узла votePlus/voteMinus даже в общих LinearLayout."
|
||||
)
|
||||
|
||||
if self.custom_icons:
|
||||
tqdm.write("Заменяем иконки лайка и дизлайка на кастомные...")
|
||||
for suffix in ["up", "up_40", "down", "down_40"]:
|
||||
shutil.copy(
|
||||
f"./resources/ic_chevron_{suffix}.xml",
|
||||
f"./decompiled/res/drawable/ic_chevron_{suffix}.xml",
|
||||
)
|
||||
|
||||
for inactive in root.xpath(
|
||||
".//*[@android:id='@id/votePlusInactive'] | .//*[@android:id='@id/voteMinusInactive']",
|
||||
namespaces=base["xml_ns"],
|
||||
):
|
||||
for img in inactive.xpath(
|
||||
".//ImageView[@android:src]", namespaces=base["xml_ns"]
|
||||
):
|
||||
src = img.get(f'{{{base["xml_ns"]["android"]}}}src', "")
|
||||
if src.startswith("@drawable/") and not src.endswith("_40"):
|
||||
img.set(f'{{{base["xml_ns"]["android"]}}}src', src + "_40")
|
||||
|
||||
# Сохраняем
|
||||
tree.write(file_path, encoding="utf-8", xml_declaration=True, pretty_print=True)
|
||||
|
||||
return True
|
||||
|
||||
Reference in New Issue
Block a user