desktop_app/app/controllers/main_controller.py
2025-09-26 03:42:57 +03:00

72 lines
3.0 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 # эмуляция задержки от сервера
from app.core.database import get_last_login, get_session, set_last_login
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):
"""Проверяет наличие сессии для автологина."""
last_login = get_last_login()
if last_login:
session = get_session(last_login)
if session:
# Если сессия найдена, сразу переходим к списку чатов
self.handle_login_success(last_login)
return
# Если сессия не найдена, показываем экран входа
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):
"""Обрабатывает успешный вход в систему."""
# Сохраняем пользователя как последнего вошедшего
set_last_login(username)
if self.login_view:
self.login_view.close()
self.removeWidget(self.login_view)
self.login_view = None
# 🔹 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)