108 lines
4.3 KiB
Python
108 lines
4.3 KiB
Python
"""Меняет местами кнопки лайка и дизлайка у коментария и иконки
|
||
|
||
"comment_vote": {
|
||
"enabled": true,
|
||
"replace": true,
|
||
"custom_icons": true,
|
||
"icons_size": "14.0dip"
|
||
}
|
||
"""
|
||
|
||
__author__ = "wowlikon <wowlikon@gmail.com>"
|
||
__version__ = "1.0.0"
|
||
import os
|
||
import shutil
|
||
from typing import Any, Dict
|
||
|
||
from lxml import etree
|
||
from pydantic import Field
|
||
from tqdm import tqdm
|
||
|
||
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="Размер иконки")
|
||
|
||
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", self.icon_size)
|
||
# icon.set(f"{{{base['xml_ns']['android']}}}layout_height", self.icon_size)
|
||
|
||
if self.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 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
|