60 lines
2.5 KiB
Python
60 lines
2.5 KiB
Python
from PySide6.QtWidgets import QWidget, QListWidget, QVBoxLayout, QLabel, QListWidgetItem
|
||
from PySide6.QtCore import Qt
|
||
from typing import List
|
||
from app.core.models.chat_models import PrivateChatListItem
|
||
|
||
class ChatListView(QWidget):
|
||
def __init__(self):
|
||
super().__init__()
|
||
self.init_ui()
|
||
|
||
def init_ui(self):
|
||
"""Инициализирует пользовательский интерфейс."""
|
||
layout = QVBoxLayout(self)
|
||
layout.setContentsMargins(0, 0, 0, 0)
|
||
|
||
self.chat_list = QListWidget()
|
||
self.chat_list.setStyleSheet("QListWidget { border: none; }")
|
||
layout.addWidget(self.chat_list)
|
||
|
||
# Изначальное состояние
|
||
self.show_placeholder_message("Загрузка чатов...")
|
||
|
||
def show_placeholder_message(self, text):
|
||
"""Очищает список и показывает одно сообщение (например, "Загрузка..." или "Чатов нет")."""
|
||
self.chat_list.clear()
|
||
item = QListWidgetItem(text)
|
||
item.setTextAlignment(Qt.AlignCenter)
|
||
self.chat_list.addItem(item)
|
||
|
||
def populate_chats(self, chat_items: List[PrivateChatListItem]):
|
||
"""
|
||
Заполняет список чатов данными, полученными от сервера.
|
||
"""
|
||
self.chat_list.clear()
|
||
|
||
if not chat_items:
|
||
self.show_placeholder_message("У вас пока нет чатов")
|
||
return
|
||
|
||
for chat in chat_items:
|
||
# Определяем имя собеседника
|
||
if chat.chat_type == "self":
|
||
companion_name = "Избранное"
|
||
elif chat.chat_data and 'login' in chat.chat_data:
|
||
companion_name = chat.chat_data['login']
|
||
else:
|
||
companion_name = "Неизвестный"
|
||
|
||
# Получаем текст последнего сообщения
|
||
if chat.last_message and chat.last_message.content:
|
||
last_msg = chat.last_message.content
|
||
else:
|
||
last_msg = "Нет сообщений"
|
||
|
||
# Создаем кастомный виджет для элемента списка (можно будет улучшить)
|
||
# Пока просто текстом
|
||
item_text = f"{companion_name}\n{last_msg}"
|
||
list_item = QListWidgetItem(item_text)
|
||
self.chat_list.addItem(list_item)
|