Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions app/spotify/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ def get_current_device(
def next_device(
spotify_service: SpotifyService = Depends(Provide[Container.spotify_service])
) -> CurrentDevice | dict:
spotify_service.refresh_devices()
spotify_service.next_device()
updated = spotify_service.refresh_devices()

if not updated:
spotify_service.next_device()

current_device = spotify_service.get_current_device()

if current_device is None:
Expand All @@ -56,8 +59,11 @@ def next_device(
def previous_device(
spotify_service: SpotifyService = Depends(Provide[Container.spotify_service])
) -> CurrentDevice | dict:
spotify_service.refresh_devices()
spotify_service.previous_device()
updated = spotify_service.refresh_devices()

if not updated:
spotify_service.previous_device()

current_device = spotify_service.get_current_device()

if current_device is None:
Expand Down
10 changes: 8 additions & 2 deletions app/spotify/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,20 @@ def play(self, track: Track) -> None:
elif "Invalid track uri" in e.msg:
raise TrackNotFoundError(e.msg)

def refresh_devices(self) -> list[Device]:
def refresh_devices(self) -> bool:
"""
Refreshes the devices from the API and returns whether the devices have changed

:return: Whether the devices have changed
"""
devices_from_api = self.__get_devices_from_api()

if devices_from_api != self.__devices:
self.__devices = devices_from_api
self.__current_device_index = 0
return True

return self.__devices
return False

def next_device(self) -> Device | None:
if len(self.__devices) == 0:
Expand Down