From c9fb658a6b50ff7fa5cb20ea363e36a5f2c18277 Mon Sep 17 00:00:00 2001 From: kirill Date: Fri, 24 Jan 2025 22:40:08 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BF=D0=BE=D1=87=D1=82=D0=B8=20=D0=BF=D0=BE?= =?UTF-8?q?=D0=BB=D1=83=D1=87=D0=B8=D0=BB=D0=BE=D1=81=D1=8C=20=D1=81=D0=B4?= =?UTF-8?q?=D0=B5=D0=BB=D0=B0=D1=82=D1=8C=20=D0=BC=D0=BD=D0=BE=D0=B3=D0=BE?= =?UTF-8?q?=D1=8D=D0=BA=D1=80=D0=B0=D0=BD=D0=BD=D0=BE=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/app.py | 55 ++++++++++++++++++++++++++++++++-------------- telegram/client.py | 5 +++++ widgets/chat.py | 6 ++++- widgets/dialog.py | 2 ++ widgets/message.py | 2 ++ 5 files changed, 52 insertions(+), 18 deletions(-) diff --git a/app/app.py b/app/app.py index 6b15054..92f58a6 100644 --- a/app/app.py +++ b/app/app.py @@ -1,34 +1,30 @@ -#from telethon import TelegramClient, events +from telethon import TelegramClient, events from textual.app import App, ComposeResult from textual.containers import Horizontal, VerticalScroll from textual.widgets import Static, Footer from widgets.chat import Chat from widgets.dialog import Dialog -from telegram.client import TelegramClientWrapper +from textual.screen import Screen +#from telegram.client import TelegramClientWrapper from tokens import api_id, api_hash -class TelegramTUI(App): - CSS_PATH = "../tcss/style.tcss" +class ChatScreen(Screen): + """Класс экрана чатов, он же основной экран приложения""" - def __init__(self): - super().__init__() - + def __init__(self, name = None, id = None, classes = None, telegram_client = None): + super().__init__(name, id, classes) + self.telegram_client = telegram_client + self.telegram_client.on(events.NewMessage())(self.update_chat_list) - async def on_mount(self) -> None: - self.telegram_client = TelegramClientWrapper(api_id, api_hash, self.update_chat_list) + def on_mount(self): self.chat_container = self.query_one("#main_container").query_one("#chats").query_one("#chat_container") + self.limit = 100 for i in range(self.limit): - chat = Chat(id=f"chat-{i + 1}") + chat = Chat(id=f"chat-{i + 1}", notify_func=self.notify) self.chat_container.mount(chat) #self.mount_chats(limit=25) - await self.telegram_client.connect() - - await self.update_chat_list() - - # TODO: скоро сюда переедет маунт чатов из функции on_mount - # P.S. сделано, но неудачно def mount_chats(self, limit: int): self.limit = limit chats_amount = len(self.chat_container.query(Chat)) @@ -48,7 +44,7 @@ class TelegramTUI(App): chat.username = str(dialogs[i].name) chat.msg = str(dialogs[i].message.message) chat.peer_id = dialogs[i].id - #self.notify("Новое сообщение") #колхоз дебаг + self.notify("Новое сообщение") #колхоз дебаг def compose(self) -> ComposeResult: yield Footer() @@ -59,6 +55,31 @@ class TelegramTUI(App): yield Dialog() +class AuthScreen(Screen): + """Это будет классом экрана логина""" + + pass + +class TelegramTUI(App): + """Класс приложения""" + + CSS_PATH = "../tcss/style.tcss" + #SCREENS = {"chats": ChatScreen} + + def __init__(self): + super().__init__() + + async def on_mount(self) -> None: + self.telegram_client = TelegramClient("../user", api_id, api_hash) + #self.push_screen("chats") + chat_screen = ChatScreen(telegram_client=self.telegram_client) + self.install_screen(chat_screen, name="chats") + self.push_screen("chats") + self.telegram_client.on(events.NewMessage())(chat_screen.update_chat_list()) + + await self.telegram_client.start() + await self.update_chat_list() + async def on_exit_app(self): await self.telegram_client.disconnect() return super()._on_exit_app() diff --git a/telegram/client.py b/telegram/client.py index 03b5fbe..59ff039 100644 --- a/telegram/client.py +++ b/telegram/client.py @@ -1,6 +1,8 @@ from telethon import TelegramClient, events, utils class TelegramClientWrapper: + """Обёртка для метода TelegramClient из Telethon""" + def __init__(self, api_id, api_hash, message_handler): self.message_handler = message_handler self.client = TelegramClient('user', api_id, api_hash) @@ -10,6 +12,9 @@ class TelegramClientWrapper: await self.message_handler() async def connect(self): + await self.client.connect() + + async def start(self): await self.client.start() async def disconnect(self): diff --git a/widgets/chat.py b/widgets/chat.py index 74380d8..09dfdc0 100644 --- a/widgets/chat.py +++ b/widgets/chat.py @@ -4,15 +4,19 @@ from textual.widget import Widget from textual.reactive import Reactive class Chat(Widget): + """Класс виджета чата для панели чатов""" + username = Reactive(" ", recompose=True) msg = Reactive(" ", recompose=True) peer_id = Reactive(0) - def __init__(self, name: str | None = None, id: str | None = None, classes: str | None = None, disabled: bool = False): + def __init__(self, name: str | None = None, notify_func = None, id: str | None = None, classes: str | None = None, disabled: bool = False): super().__init__(name=str(name), id=id, classes=classes, disabled=disabled) + self.notify = notify_func def _on_click(self): self.msg = str(self.peer_id) + self.notify("нажат чат") def compose(self): with Horizontal(): diff --git a/widgets/dialog.py b/widgets/dialog.py index 3c5cbde..e8ed1b6 100644 --- a/widgets/dialog.py +++ b/widgets/dialog.py @@ -4,6 +4,8 @@ from textual.widget import Widget from widgets.message import Message class Dialog(Widget): + """Класс окна диалога""" + def __init__(self, id=None, classes=None, disabled=False): super().__init__(id=id, classes=classes, disabled=disabled) diff --git a/widgets/message.py b/widgets/message.py index b1fc27c..edda533 100644 --- a/widgets/message.py +++ b/widgets/message.py @@ -3,6 +3,8 @@ from textual.containers import Container from textual.widget import Widget class Message(Widget): + """Класс виджета сообщений для окна диалога""" + def __init__(self, name=None, message=None, is_me=None, id=None, classes=None, disabled=False): super().__init__(name=name, id=id, classes=classes, disabled=disabled) self.message = message