desktop_app/app/controllers/main_controller.py
2025-09-09 18:08:46 +03:00

60 lines
2.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from PySide6.QtWidgets import QStackedWidget
from app.ui.views.login_view import LoginView
from app.ui.views.chat_list_view import ChatListView
from app.core.models.chat_models import PrivateChatListItem
from app.core.models.mock_data import get_mock_chats # ← заглушка
from typing import Optional
from threading import Thread
import time # эмуляция задержки от сервера
class MainController(QStackedWidget):
def __init__(self):
super().__init__()
self.login_view: Optional[LoginView] = None
self.chat_list_view: Optional[ChatListView] = None
self.init_app()
def init_app(self):
session_found = False # ← здесь позже будет логика автологина
if session_found:
self.handle_login_success("user_from_session")
else:
self.show_login()
def show_login(self):
self.login_view = LoginView(on_login=self.handle_login_success)
self.addWidget(self.login_view)
self.setCurrentWidget(self.login_view)
self.login_view.show()
def handle_login_success(self, username: str):
if self.login_view:
self.login_view.close()
# 🔹 1. Загружаем чаты локально
chat_items: list[PrivateChatListItem] = self.load_local_chats()
# 🔹 2. Отображаем список
self.chat_list_view = ChatListView(username=username, chat_items=chat_items)
self.addWidget(self.chat_list_view)
self.setCurrentWidget(self.chat_list_view)
self.chat_list_view.show()
# 🔹 3. Обновляем в фоне с сервера
Thread(target=self.update_chats_from_server, args=(username,), daemon=True).start()
def load_local_chats(self) -> list[PrivateChatListItem]:
# Позже можешь заменить это чтением из JSON или SQLite
return get_mock_chats()
def update_chats_from_server(self, username: str):
# Эмуляция запроса
time.sleep(2) # ⏳ как будто идёт запрос
print(f"[Sync] Обновляем чаты пользователя: {username}")
# Здесь должен быть запрос к серверу и обновление UI:
# new_chats = api.get_chats(username)
# self.chat_list_view.update_chat_items(new_chats)