From b28383856aba7e362b0e6c3dae0a028bcf647218 Mon Sep 17 00:00:00 2001 From: Shaowen Yin Date: Sat, 6 Jan 2024 23:20:10 +0800 Subject: [PATCH] gui: introduce UISupportsLoginEvent protocol (#745) --- feeluown/gui/components/avatar.py | 17 ++++++++++++++--- feeluown/gui/provider_ui.py | 17 +++++++++++++++++ feeluown/gui/uimain/sidebar.py | 12 ++++++++++++ feeluown/library/provider_protocol.py | 9 +++++++++ 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/feeluown/gui/components/avatar.py b/feeluown/gui/components/avatar.py index c98dd13fd3..df9e808ad3 100644 --- a/feeluown/gui/components/avatar.py +++ b/feeluown/gui/components/avatar.py @@ -3,10 +3,11 @@ from PyQt5.QtWidgets import QMenu, QAction from PyQt5.QtGui import QPainter, QIcon, QPalette, QContextMenuEvent -from feeluown.library import NoUserLoggedIn +from feeluown.library import NoUserLoggedIn, UserModel from feeluown.models.uri import reverse from feeluown.utils.aio import run_afn, run_fn -from feeluown.gui.provider_ui import UISupportsLoginOrGoHome, ProviderUiItem +from feeluown.gui.provider_ui import UISupportsLoginOrGoHome, ProviderUiItem, \ + UISupportsLoginEvent from feeluown.gui.widgets import SelfPaintAbstractSquareButton from feeluown.gui.drawers import PixmapDrawer, AvatarIconDrawer @@ -70,8 +71,18 @@ def contextMenuEvent(self, e) -> None: menu.exec_(e.globalPos()) + def on_provider_ui_login_event(self, provider_ui, event): + if event in (1, 2): + run_afn(self.show_pvd_ui_current_user) + run_afn( + self._app.ui.sidebar.show_provider_current_user_playlists, + provider_ui.provider + ) + def on_pvd_ui_selected(self, pvd_ui): self._app.current_pvd_ui_mgr.set(pvd_ui) + if isinstance(pvd_ui, UISupportsLoginEvent): + pvd_ui.login_event.connect(self.on_provider_ui_login_event) if isinstance(pvd_ui, UISupportsLoginOrGoHome): pvd_ui.login_or_go_home() run_afn(self.show_pvd_ui_current_user) @@ -105,7 +116,7 @@ async def _show_provider_current_user(self, name): if user is None: return None - if user.avatar_url: + if isinstance(user, UserModel) and user.avatar_url: img_data = await run_afn(self._app.img_mgr.get, user.avatar_url, reverse(user)) if img_data: diff --git a/feeluown/gui/provider_ui.py b/feeluown/gui/provider_ui.py index 192cad265c..60c400f713 100644 --- a/feeluown/gui/provider_ui.py +++ b/feeluown/gui/provider_ui.py @@ -19,6 +19,23 @@ def login_or_go_home(self): ... +@runtime_checkable +class UISupportsLoginEvent(Protocol): + + @property + @abstractmethod + def login_event(self): + """ + event: + 0: first login failed + 1: first login ok + 2: re-login ok + + :return: Signal(provider_ui, event) + """ + ... + + @runtime_checkable class UISupportsDiscovery(Protocol): diff --git a/feeluown/gui/uimain/sidebar.py b/feeluown/gui/uimain/sidebar.py index 7bce864262..24cead71dd 100644 --- a/feeluown/gui/uimain/sidebar.py +++ b/feeluown/gui/uimain/sidebar.py @@ -13,6 +13,8 @@ ) from feeluown.collection import CollectionAlreadyExists, CollectionType from feeluown.utils import aio +from feeluown.utils.reader import create_reader +from feeluown.utils.aio import run_fn from feeluown.gui.widgets import ( DiscoveryButton, HomeButton, @@ -99,6 +101,16 @@ def sizeHint(self): width = min(self._app.width() * 22 // 100, 240) return QSize(width, size.height()) + async def show_provider_current_user_playlists(self, provider): + self.p.playlists_con.show() + self._app.pl_uimgr.clear() + + playlists = await run_fn(provider.current_user_list_playlists) + reader = await run_fn(provider.current_user_fav_create_playlists_rd) + fav_playlists = create_reader(reader).readall() + self._app.pl_uimgr.add(playlists) + self._app.pl_uimgr.add(fav_playlists, is_fav=True) + class _LeftPanel(QFrame): diff --git a/feeluown/library/provider_protocol.py b/feeluown/library/provider_protocol.py index cc03394f09..eb29ddb11e 100644 --- a/feeluown/library/provider_protocol.py +++ b/feeluown/library/provider_protocol.py @@ -315,6 +315,15 @@ def get_current_user(self) -> UserModel: """ +@runtime_checkable +class SupportsCurrentUserListPlaylists(Protocol): + @abstractmethod + def current_user_list_playlists(self): + """ + : raises NoUserLoggedIn: + """ + + # # Protocols for current user favorites/collections #