105 lines
4.0 KiB
Python
105 lines
4.0 KiB
Python
from PySide6.QtWidgets import QWidget, QListWidget, QVBoxLayout, QListWidgetItem
|
||
from PySide6.QtCore import Qt, QSize
|
||
from typing import List
|
||
from app.core.models.chat_models import PrivateChatListItem
|
||
from app.ui.widgets.chat_list_item_widget import ChatListItemWidget
|
||
from datetime import datetime
|
||
|
||
class ChatListView(QWidget):
|
||
def __init__(self):
|
||
super().__init__()
|
||
self.init_ui()
|
||
|
||
def init_ui(self):
|
||
"""Инициализирует пользовательский интерфейс."""
|
||
layout = QVBoxLayout(self)
|
||
layout.setContentsMargins(10, 10, 10, 10)
|
||
layout.setSpacing(0)
|
||
|
||
self.chat_list = QListWidget()
|
||
self.chat_list.setStyleSheet("""
|
||
QListWidget {
|
||
background-color: #ffffff;
|
||
border: none;
|
||
padding: 5px;
|
||
spacing: 8px;
|
||
outline: 0;
|
||
}
|
||
QListWidget::item {
|
||
background-color: #f5f5f5;
|
||
border-radius: 10px;
|
||
padding: 5px;
|
||
}
|
||
QListWidget::item:hover {
|
||
background-color: #e9e9e9;
|
||
}
|
||
QListWidget::item:selected {
|
||
background-color: #dcdcdc;
|
||
color: #000000;
|
||
}
|
||
QScrollBar:vertical {
|
||
border: none;
|
||
background: #f5f5f5;
|
||
width: 8px;
|
||
margin: 0px 0px 0px 0px;
|
||
}
|
||
QScrollBar::handle:vertical {
|
||
background: #cccccc;
|
||
min-height: 20px;
|
||
border-radius: 4px;
|
||
}
|
||
QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {
|
||
height: 0px;
|
||
}
|
||
""")
|
||
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)
|
||
item.setBackground(Qt.transparent)
|
||
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
|
||
|
||
# Преобразуем время
|
||
timestamp = chat.last_message.created_at.strftime('%H:%M')
|
||
else:
|
||
last_msg = "Нет сообщений"
|
||
timestamp = ""
|
||
|
||
# Создаем кастомный виджет
|
||
item_widget = ChatListItemWidget(companion_name, last_msg, timestamp)
|
||
|
||
# Создаем элемент списка и устанавливаем для него наш виджет
|
||
list_item = QListWidgetItem(self.chat_list)
|
||
list_item.setSizeHint(item_widget.sizeHint())
|
||
self.chat_list.addItem(list_item)
|
||
self.chat_list.setItemWidget(list_item, item_widget)
|