From 9b03933e14ab6117c7210a2ac80a7038380cec9d Mon Sep 17 00:00:00 2001 From: Malopieds Date: Mon, 1 Apr 2024 23:13:28 +0200 Subject: [PATCH] feat(playlist_screen): add top 50 to playlist screen + fix --- .../com/zionhuang/music/ui/component/Items.kt | 9 +- .../ui/screens/library/LibraryMixScreen.kt | 2 - .../screens/library/LibraryPlaylistsScreen.kt | 92 ++++++++++++++++++- .../ui/screens/playlist/TopPlaylistScreen.kt | 1 - .../music/viewmodels/LibraryViewModels.kt | 3 + 5 files changed, 98 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/zionhuang/music/ui/component/Items.kt b/app/src/main/java/com/zionhuang/music/ui/component/Items.kt index bc7c2fa50..0ce0dab72 100644 --- a/app/src/main/java/com/zionhuang/music/ui/component/Items.kt +++ b/app/src/main/java/com/zionhuang/music/ui/component/Items.kt @@ -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) ) diff --git a/app/src/main/java/com/zionhuang/music/ui/screens/library/LibraryMixScreen.kt b/app/src/main/java/com/zionhuang/music/ui/screens/library/LibraryMixScreen.kt index 2147a140a..aab15e43c 100644 --- a/app/src/main/java/com/zionhuang/music/ui/screens/library/LibraryMixScreen.kt +++ b/app/src/main/java/com/zionhuang/music/ui/screens/library/LibraryMixScreen.kt @@ -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"), diff --git a/app/src/main/java/com/zionhuang/music/ui/screens/library/LibraryPlaylistsScreen.kt b/app/src/main/java/com/zionhuang/music/ui/screens/library/LibraryPlaylistsScreen.kt index a015faa4f..9ea42cdb7 100644 --- a/app/src/main/java/com/zionhuang/music/ui/screens/library/LibraryPlaylistsScreen.kt +++ b/app/src/main/java/com/zionhuang/music/ui/screens/library/LibraryPlaylistsScreen.kt @@ -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, @@ -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() @@ -201,7 +211,9 @@ fun LibraryPlaylistsScreen( PlaylistMenu( playlist = likedPlaylist, coroutineScope = coroutineScope, - onDismiss = menuState::dismiss + onDismiss = menuState::dismiss, + autoPlaylist = true, + songList = likedSongs ) } } @@ -235,7 +247,10 @@ fun LibraryPlaylistsScreen( PlaylistMenu( playlist = downloadPlaylist, coroutineScope = coroutineScope, - onDismiss = menuState::dismiss + onDismiss = menuState::dismiss, + autoPlaylist = true, + downloadPlaylist = true, + songList = downloadSongs ) } } @@ -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 }, @@ -339,7 +389,9 @@ fun LibraryPlaylistsScreen( PlaylistMenu( playlist = likedPlaylist, coroutineScope = coroutineScope, - onDismiss = menuState::dismiss + onDismiss = menuState::dismiss, + autoPlaylist = true, + songList = likedSongs ) } } @@ -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)) ) } } diff --git a/app/src/main/java/com/zionhuang/music/ui/screens/playlist/TopPlaylistScreen.kt b/app/src/main/java/com/zionhuang/music/ui/screens/playlist/TopPlaylistScreen.kt index 4fae96012..c4d240e98 100644 --- a/app/src/main/java/com/zionhuang/music/ui/screens/playlist/TopPlaylistScreen.kt +++ b/app/src/main/java/com/zionhuang/music/ui/screens/playlist/TopPlaylistScreen.kt @@ -373,7 +373,6 @@ fun TopPlaylistScreen( sortType = sortType, sortDescending = false, onSortTypeChange = { - println(it) viewModel.topPeriod.value = it }, onSortDescendingChange = {}, diff --git a/app/src/main/java/com/zionhuang/music/viewmodels/LibraryViewModels.kt b/app/src/main/java/com/zionhuang/music/viewmodels/LibraryViewModels.kt index 7db62dd4a..c5394dcbd 100644 --- a/app/src/main/java/com/zionhuang/music/viewmodels/LibraryViewModels.kt +++ b/app/src/main/java/com/zionhuang/music/viewmodels/LibraryViewModels.kt @@ -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) @@ -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(