Skip to content

Commit

Permalink
gui: introduce UISupportsLoginEvent protocol (#745)
Browse files Browse the repository at this point in the history
  • Loading branch information
cosven authored Jan 6, 2024
1 parent 2e7de10 commit b283838
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
17 changes: 14 additions & 3 deletions feeluown/gui/components/avatar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
17 changes: 17 additions & 0 deletions feeluown/gui/provider_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down
12 changes: 12 additions & 0 deletions feeluown/gui/uimain/sidebar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):

Expand Down
9 changes: 9 additions & 0 deletions feeluown/library/provider_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down

0 comments on commit b283838

Please sign in to comment.