diff --git a/app/app.py b/app/app.py index 41124a6..8a806b8 100644 --- a/app/app.py +++ b/app/app.py @@ -1,7 +1,7 @@ from telethon import TelegramClient, events from textual.app import App, ComposeResult from textual.containers import Horizontal, VerticalScroll, Vertical, Container -from textual.widgets import Placeholder, Label, Static, Rule, Input, Button +from textual.widgets import Placeholder, Label, Static, Input, Button from widgets.chat import Chat from widgets.dialog import Dialog from telegram.client import TelegramClientWrapper @@ -12,31 +12,37 @@ class TelegramTUI(App): def __init__(self): super().__init__() - self.telegram_client = TelegramClientWrapper(api_id, api_hash, self.handler) + self.telegram_client = TelegramClientWrapper(api_id, api_hash, self.update_chat_list) async def on_mount(self) -> None: - await self.telegram_client.connect() - await self.update_chat_list() + self.chat_container = self.query_one("#main_container").query_one("#chats").query_one("#chat_container") - async def handler(self, event): - await self.update_chat_list() + self.limit = 25 + for i in range(self.limit): + chat = Chat(id=f"chat-{i + 1}") + self.chat_container.mount(chat) + + await self.telegram_client.connect() + + # TODO: скоро сюда переедет маунт чатов из функции on_mount + def mount_chats(self): + pass async def update_chat_list(self): - dialogs = await self.telegram_client.get_dialogs(limit=10) - chat_container = self.query_one("#main_container").query_one("#chats").query_one("#chat_container") - chat_container.query(Chat).remove() + dialogs = await self.telegram_client.get_dialogs(limit=self.limit) - for dialog in dialogs: - name = dialog.name - msg = dialog.message.message - chat = Chat(name, msg, id=f"chat-{dialog.id}") - chat_container.mount(chat) + for i in range(len(dialogs)): + chat = self.chat_container.query_one(f"#chat-{i + 1}") + chat.username = str(dialogs[i].name) + chat.msg = str(dialogs[i].message.message) + chat.peer_id = dialogs[i].id + #self.notify("Новое сообщение") #колхоз дебаг def compose(self) -> ComposeResult: with Horizontal(id="main_container"): with Horizontal(id="chats"): yield VerticalScroll(Static(id="chat_container")) - yield Rule("vertical") + yield Dialog() async def on_exit_app(self): diff --git a/tcss/style.tcss b/tcss/style.tcss index d941030..1a50fb4 100644 --- a/tcss/style.tcss +++ b/tcss/style.tcss @@ -30,8 +30,14 @@ Message Container { #input_place { height: 3; + width: 70%; + align-horizontal: center; } #msg_input { - width: 70%; + width: 65%; +} + +#send { + } diff --git a/telegram/client.py b/telegram/client.py index 21935a4..ad759ce 100644 --- a/telegram/client.py +++ b/telegram/client.py @@ -3,7 +3,10 @@ from telethon import TelegramClient, events, utils class TelegramClientWrapper: def __init__(self, api_id, api_hash, message_handler): self.client = TelegramClient('user', api_id, api_hash) - self.client.add_event_handler(message_handler, events.NewMessage()) + #self.client.add_event_handler(message_handler, events.NewMessage()) + self.client.on(events.NewMessage())(message_handler) + + #ни то ни то не работает, костя спаси async def connect(self): await self.client.start() diff --git a/widgets/chat.py b/widgets/chat.py index 0f71c93..74380d8 100644 --- a/widgets/chat.py +++ b/widgets/chat.py @@ -1,18 +1,22 @@ from textual.widgets import Label from textual.containers import Horizontal, Vertical from textual.widget import Widget +from textual.reactive import Reactive class Chat(Widget): - def __init__(self, name: str | None = None, msg: str | None = None, id: str | None = None, classes: str | None = None, disabled: bool = False): + 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): super().__init__(name=str(name), id=id, classes=classes, disabled=disabled) - self.msg = str(msg) def _on_click(self): - pass + self.msg = str(self.peer_id) def compose(self): with Horizontal(): - yield Label(f"┌───┐\n│ {self.name[:1]} │\n└───┘") + yield Label(f"┌───┐\n│ {self.username[:1]} │\n└───┘") with Vertical(): - yield Label(self.name, id="name") + yield Label(self.username, id="name") yield Label(self.msg, id="last_msg")