feat: Implement playlist reordering with position swapping and reload player on current playlist changes
This commit is contained in:
@@ -14,6 +14,33 @@ from app.repositories.sound import SoundRepository
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
async def _reload_player_playlist() -> None:
|
||||
"""Reload the player playlist after current playlist changes."""
|
||||
try:
|
||||
# Import here to avoid circular import issues
|
||||
from app.services.player import get_player_service # noqa: PLC0415
|
||||
|
||||
player = get_player_service()
|
||||
await player.reload_playlist()
|
||||
logger.debug("Player playlist reloaded after current playlist change")
|
||||
except Exception: # noqa: BLE001
|
||||
# Don't fail the playlist operation if player reload fails
|
||||
logger.warning("Failed to reload player playlist", exc_info=True)
|
||||
|
||||
|
||||
async def _is_current_playlist(session: AsyncSession, playlist_id: int) -> bool:
|
||||
"""Check if the given playlist is the current playlist."""
|
||||
try:
|
||||
from app.repositories.playlist import PlaylistRepository # noqa: PLC0415
|
||||
|
||||
playlist_repo = PlaylistRepository(session)
|
||||
current_playlist = await playlist_repo.get_current_playlist()
|
||||
return current_playlist is not None and current_playlist.id == playlist_id
|
||||
except Exception: # noqa: BLE001
|
||||
logger.warning("Failed to check if playlist is current", exc_info=True)
|
||||
return False
|
||||
|
||||
|
||||
class PlaylistService:
|
||||
"""Service for playlist operations."""
|
||||
|
||||
@@ -99,6 +126,11 @@ class PlaylistService:
|
||||
|
||||
playlist = await self.playlist_repo.create(playlist_data)
|
||||
logger.info("Created playlist '%s' for user %s", name, user_id)
|
||||
|
||||
# If this was set as current, reload player playlist
|
||||
if is_current:
|
||||
await _reload_player_playlist()
|
||||
|
||||
return playlist
|
||||
|
||||
async def update_playlist( # noqa: PLR0913
|
||||
@@ -145,6 +177,10 @@ class PlaylistService:
|
||||
playlist = await self.playlist_repo.update(playlist, update_data)
|
||||
logger.info("Updated playlist %s for user %s", playlist_id, user_id)
|
||||
|
||||
# If is_current was changed, reload player playlist
|
||||
if "is_current" in update_data:
|
||||
await _reload_player_playlist()
|
||||
|
||||
return playlist
|
||||
|
||||
async def delete_playlist(self, playlist_id: int, user_id: int) -> None:
|
||||
@@ -157,11 +193,15 @@ class PlaylistService:
|
||||
detail="This playlist cannot be deleted",
|
||||
)
|
||||
|
||||
# Check if this was the current playlist before deleting
|
||||
was_current = playlist.is_current
|
||||
|
||||
await self.playlist_repo.delete(playlist)
|
||||
logger.info("Deleted playlist %s for user %s", playlist_id, user_id)
|
||||
|
||||
# Note: If the deleted playlist was current, main playlist becomes fallback
|
||||
# No action needed as get_current_playlist() handles the fallback automatically
|
||||
# If the deleted playlist was current, reload player to use main playlist fallback
|
||||
if was_current:
|
||||
await _reload_player_playlist()
|
||||
|
||||
async def search_playlists(self, query: str, user_id: int) -> list[Playlist]:
|
||||
"""Search user's playlists by name."""
|
||||
@@ -210,6 +250,10 @@ class PlaylistService:
|
||||
user_id,
|
||||
)
|
||||
|
||||
# If this is the current playlist, reload player
|
||||
if await _is_current_playlist(self.session, playlist_id):
|
||||
await _reload_player_playlist()
|
||||
|
||||
async def remove_sound_from_playlist(
|
||||
self,
|
||||
playlist_id: int,
|
||||
@@ -235,6 +279,10 @@ class PlaylistService:
|
||||
user_id,
|
||||
)
|
||||
|
||||
# If this is the current playlist, reload player
|
||||
if await _is_current_playlist(self.session, playlist_id):
|
||||
await _reload_player_playlist()
|
||||
|
||||
async def reorder_playlist_sounds(
|
||||
self,
|
||||
playlist_id: int,
|
||||
@@ -256,6 +304,9 @@ class PlaylistService:
|
||||
await self.playlist_repo.reorder_playlist_sounds(playlist_id, sound_positions)
|
||||
logger.info("Reordered sounds in playlist %s for user %s", playlist_id, user_id)
|
||||
|
||||
# If this is the current playlist, reload player
|
||||
if await _is_current_playlist(self.session, playlist_id):
|
||||
await _reload_player_playlist()
|
||||
|
||||
async def get_playlist_stats(self, playlist_id: int) -> dict[str, Any]:
|
||||
"""Get statistics for a playlist."""
|
||||
@@ -281,18 +332,25 @@ class PlaylistService:
|
||||
msg = "Main playlist has no ID, cannot add sound"
|
||||
raise ValueError(msg)
|
||||
|
||||
# Extract ID before async operations to avoid session issues
|
||||
main_playlist_id = main_playlist.id
|
||||
|
||||
# Check if sound is already in main playlist
|
||||
if not await self.playlist_repo.is_sound_in_playlist(
|
||||
main_playlist.id,
|
||||
main_playlist_id,
|
||||
sound_id,
|
||||
):
|
||||
await self.playlist_repo.add_sound_to_playlist(main_playlist.id, sound_id)
|
||||
await self.playlist_repo.add_sound_to_playlist(main_playlist_id, sound_id)
|
||||
logger.info(
|
||||
"Added sound %s to main playlist for user %s",
|
||||
sound_id,
|
||||
user_id,
|
||||
)
|
||||
|
||||
# If main playlist is current, reload player
|
||||
if await _is_current_playlist(self.session, main_playlist_id):
|
||||
await _reload_player_playlist()
|
||||
|
||||
# Current playlist methods (global by default)
|
||||
async def set_current_playlist(self, playlist_id: int) -> Playlist:
|
||||
"""Set a playlist as the current playlist (app-wide)."""
|
||||
@@ -304,6 +362,10 @@ class PlaylistService:
|
||||
# Set new current playlist
|
||||
playlist = await self.playlist_repo.update(playlist, {"is_current": True})
|
||||
logger.info("Set playlist %s as current playlist", playlist_id)
|
||||
|
||||
# Reload player playlist to reflect the change
|
||||
await _reload_player_playlist()
|
||||
|
||||
return playlist
|
||||
|
||||
async def unset_current_playlist(self) -> None:
|
||||
@@ -311,9 +373,11 @@ class PlaylistService:
|
||||
await self._unset_current_playlist()
|
||||
logger.info("Unset current playlist, main playlist is now fallback")
|
||||
|
||||
# Reload player playlist to reflect the change (will fallback to main)
|
||||
await _reload_player_playlist()
|
||||
|
||||
async def _unset_current_playlist(self) -> None:
|
||||
"""Unset any current playlist globally."""
|
||||
current_playlist = await self.playlist_repo.get_current_playlist()
|
||||
if current_playlist:
|
||||
await self.playlist_repo.update(current_playlist, {"is_current": False})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user