Skip to content

Commit

Permalink
feat(playlist_screen): add top 50 to playlist screen + fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Malopieds committed Apr 1, 2024
1 parent 7f77b6c commit 9b03933
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 9 deletions.
9 changes: 7 additions & 2 deletions app/src/main/java/com/zionhuang/music/ui/component/Items.kt
Original file line number Diff line number Diff line change
Expand Up @@ -884,10 +884,15 @@ fun PlaylistListItem(
title = playlist.playlist.name,
subtitle = pluralStringResource(R.plurals.n_song, playlist.songCount, playlist.songCount),
thumbnailContent = {
val auto = if (playlist.playlist.name == "Liked") 1 else if (playlist.playlist.name == "Downloaded") 2 else 0
val painter = when (playlist.playlist.name) {
"Liked" -> R.drawable.favorite
"Offline" -> R.drawable.offline
"My Top 50" -> R.drawable.trending_up
else -> R.drawable.queue_music
}
when (playlist.thumbnails.size) {
0 -> Icon(
painter = painterResource( if (auto == 0) R.drawable.queue_music else if (auto == 1) R.drawable.favorite else R.drawable.offline),
painter = painterResource(painter),
contentDescription = null,
modifier = Modifier.size(ListThumbnailSize)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ fun LibraryMixScreen(
)

val topSizeInt = topSize.toString().toInt()
if (topSongs != null)
println(topSongs!!.size)

val topPlaylist = Playlist(
playlist = PlaylistEntity(id = UUID.randomUUID().toString(), name = "My Top $topSize"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ fun LibraryPlaylistsScreen(

val likedSongs by viewModel.likedSongs.collectAsState()
val downloadSongs by viewModel.downloadSongs.collectAsState(initial = null)
val topSongs by viewModel.topSongs.collectAsState(initial = null)
val topSize by viewModel.topValue.collectAsState(initial = 50)
val likedPlaylist = Playlist(
playlist = PlaylistEntity(id = UUID.randomUUID().toString(), name = "Liked"),
songCount = if (likedSongs != null) likedSongs!!.size else 0,
Expand All @@ -94,6 +96,14 @@ fun LibraryPlaylistsScreen(
thumbnails = emptyList()
)

val topSizeInt = topSize.toString().toInt()

val topPlaylist = Playlist(
playlist = PlaylistEntity(id = UUID.randomUUID().toString(), name = "My Top $topSize"),
songCount = topSongs?.let { minOf(it.size, topSizeInt) } ?: 0,
thumbnails = emptyList()
)

val lazyListState = rememberLazyListState()
val lazyGridState = rememberLazyGridState()

Expand Down Expand Up @@ -201,7 +211,9 @@ fun LibraryPlaylistsScreen(
PlaylistMenu(
playlist = likedPlaylist,
coroutineScope = coroutineScope,
onDismiss = menuState::dismiss
onDismiss = menuState::dismiss,
autoPlaylist = true,
songList = likedSongs
)
}
}
Expand Down Expand Up @@ -235,7 +247,10 @@ fun LibraryPlaylistsScreen(
PlaylistMenu(
playlist = downloadPlaylist,
coroutineScope = coroutineScope,
onDismiss = menuState::dismiss
onDismiss = menuState::dismiss,
autoPlaylist = true,
downloadPlaylist = true,
songList = downloadSongs
)
}
}
Expand All @@ -255,6 +270,41 @@ fun LibraryPlaylistsScreen(
)
}

item(
key = "TopPlaylist",
contentType = { CONTENT_TYPE_PLAYLIST }
) {
PlaylistListItem(
playlist = topPlaylist,
trailingContent = {
IconButton(
onClick = {
menuState.show {
PlaylistMenu(
playlist = topPlaylist,
coroutineScope = coroutineScope,
onDismiss = menuState::dismiss,
autoPlaylist = true,
songList = topSongs
)
}
}
) {
Icon(
painter = painterResource(R.drawable.more_vert),
contentDescription = null
)
}
},
modifier = Modifier
.fillMaxWidth()
.clickable {
navController.navigate("top_playlist/$topSize")
}
.animateItemPlacement()
)
}

items(
items = playlists,
key = { it.id },
Expand Down Expand Up @@ -339,7 +389,9 @@ fun LibraryPlaylistsScreen(
PlaylistMenu(
playlist = likedPlaylist,
coroutineScope = coroutineScope,
onDismiss = menuState::dismiss
onDismiss = menuState::dismiss,
autoPlaylist = true,
songList = likedSongs
)
}
}
Expand All @@ -366,7 +418,39 @@ fun LibraryPlaylistsScreen(
PlaylistMenu(
playlist = downloadPlaylist,
coroutineScope = coroutineScope,
onDismiss = menuState::dismiss
onDismiss = menuState::dismiss,
autoPlaylist = true,
downloadPlaylist = true,
songList = downloadSongs
)
}
}
)
.animateItemPlacement()
)
}

item(
key = "TopPlaylist",
contentType = { CONTENT_TYPE_PLAYLIST }
) {
PlaylistGridItem(
playlist = topPlaylist,
fillMaxWidth = true,
modifier = Modifier
.fillMaxWidth()
.combinedClickable(
onClick = {
navController.navigate("top_playlist/$topSize")
},
onLongClick = {
menuState.show {
PlaylistMenu(
playlist = topPlaylist,
coroutineScope = coroutineScope,
onDismiss = menuState::dismiss,
autoPlaylist = true,
songList = topSongs?.subList(0, minOf(topSizeInt, topPlaylist.songCount))
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,6 @@ fun TopPlaylistScreen(
sortType = sortType,
sortDescending = false,
onSortTypeChange = {
println(it)
viewModel.topPeriod.value = it
},
onSortDescendingChange = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ class LibraryPlaylistsViewModel @Inject constructor(
}
}
}
@OptIn(ExperimentalCoroutinesApi::class)
val allPlaylists = context.dataStore.data
.map {
it[PlaylistSortTypeKey].toEnum(PlaylistSortType.CREATE_DATE) to (it[PlaylistSortDescendingKey] ?: true)
Expand All @@ -185,6 +186,8 @@ class LibraryPlaylistsViewModel @Inject constructor(
database.playlists(sortType, descending)
}
.stateIn(viewModelScope, SharingStarted.Lazily, emptyList())
val topSongs = database.mostPlayedSongs(0, 100)
val topValue = context.dataStore.data.map { it[TopSize] ?: "50"}.distinctUntilChanged()
}
@HiltViewModel
class ArtistSongsViewModel @Inject constructor(
Expand Down

0 comments on commit 9b03933

Please sign in to comment.