Skip to content

Commit

Permalink
fix(auto_playlist): wrong menu playlist for auto_playlists
Browse files Browse the repository at this point in the history
  • Loading branch information
Malopieds committed Mar 31, 2024
1 parent 33d2d13 commit d21bb03
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 72 deletions.
78 changes: 47 additions & 31 deletions app/src/main/java/com/zionhuang/music/ui/menu/PlaylistMenu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ fun PlaylistMenu(
playlist: Playlist,
coroutineScope: CoroutineScope,
onDismiss: () -> Unit,
autoPlaylist: Boolean? = false,
downloadPlaylist: Boolean? = false,
songList: List<Song>?= emptyList()
) {
val context = LocalContext.current
val database = LocalDatabase.current
Expand All @@ -74,8 +77,15 @@ fun PlaylistMenu(
}

LaunchedEffect(Unit) {
database.playlistSongs(playlist.id).collect {
songs = it.map(PlaylistSong::song)
if (autoPlaylist == false) {
database.playlistSongs(playlist.id).collect {
songs = it.map(PlaylistSong::song)
}
} else {
if (songList != null) {
songs = songList
}

}
}

Expand Down Expand Up @@ -255,39 +265,45 @@ fun PlaylistMenu(
playerConnection.addToQueue(songs.map { it.toMediaItem() })
}

GridMenuItem(
icon = R.drawable.edit,
title = R.string.edit
) {
showEditDialog = true
if (autoPlaylist != true) {
GridMenuItem(
icon = R.drawable.edit,
title = R.string.edit
) {
showEditDialog = true
}
}

DownloadGridMenu(
state = downloadState,
onDownload = {
songs.forEach { song ->
val downloadRequest = DownloadRequest.Builder(song.id, song.id.toUri())
.setCustomCacheKey(song.id)
.setData(song.song.title.toByteArray())
.build()
DownloadService.sendAddDownload(
context,
ExoDownloadService::class.java,
downloadRequest,
false
)
if (downloadPlaylist != true) {
DownloadGridMenu(
state = downloadState,
onDownload = {
songs.forEach { song ->
val downloadRequest = DownloadRequest.Builder(song.id, song.id.toUri())
.setCustomCacheKey(song.id)
.setData(song.song.title.toByteArray())
.build()
DownloadService.sendAddDownload(
context,
ExoDownloadService::class.java,
downloadRequest,
false
)
}
},
onRemoveDownload = {
showRemoveDownloadDialog = true
}
},
onRemoveDownload = {
showRemoveDownloadDialog = true
}
)
)
}

GridMenuItem(
icon = R.drawable.delete,
title = R.string.delete
) {
showDeletePlaylistDialog = true
if (autoPlaylist != true) {
GridMenuItem(
icon = R.drawable.delete,
title = R.string.delete
) {
showDeletePlaylistDialog = true
}
}

if (playlist.playlist.browseId != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ fun LibraryMixScreen(
PlaylistMenu(
playlist = likedPlaylist,
coroutineScope = coroutineScope,
onDismiss = menuState::dismiss
onDismiss = menuState::dismiss,
autoPlaylist = true,
songList = likedSongs
)
}
}
Expand All @@ -131,7 +133,10 @@ fun LibraryMixScreen(
PlaylistMenu(
playlist = downloadPlaylist,
coroutineScope = coroutineScope,
onDismiss = menuState::dismiss
onDismiss = menuState::dismiss,
autoPlaylist = true,
downloadPlaylist = true,
songList = downloadSongs
)
}
}
Expand All @@ -158,7 +163,9 @@ fun LibraryMixScreen(
PlaylistMenu(
playlist = topPlaylist,
coroutineScope = coroutineScope,
onDismiss = menuState::dismiss
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 @@ -27,7 +27,6 @@ import com.zionhuang.music.constants.ContentLanguageKey
import com.zionhuang.music.constants.CountryCodeToName
import com.zionhuang.music.constants.InnerTubeCookieKey
import com.zionhuang.music.constants.LanguageCodeToName
import com.zionhuang.music.constants.MyTopTypeKey
import com.zionhuang.music.constants.ProxyEnabledKey
import com.zionhuang.music.constants.ProxyTypeKey
import com.zionhuang.music.constants.ProxyUrlKey
Expand Down Expand Up @@ -135,8 +134,10 @@ fun ContentSettings(

EditTextPreference(
title = { Text(stringResource(R.string.top_length)) },
value = lengthTop.toString(),
isInputValid = {it.toIntOrNull() != null},
value = lengthTop,
isInputValid = {val number = it.toIntOrNull()
number != null && it.isNotEmpty() &&number > 0
},
onValueChange = onLengthTopChange
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,41 +227,6 @@ class LibraryMixViewModel @Inject constructor(
}
}
}
val allSongs = context.dataStore.data
.map {
Triple(
it[SongFilterKey].toEnum(SongFilter.LIBRARY),
it[SongSortTypeKey].toEnum(SongSortType.CREATE_DATE),
(it[SongSortDescendingKey] ?: true)
)
}
.distinctUntilChanged()
.flatMapLatest { (filter, sortType, descending) ->
when (filter) {
SongFilter.LIBRARY -> database.songs(sortType, descending)
SongFilter.LIKED -> database.likedSongs(sortType, descending)
SongFilter.DOWNLOADED -> downloadUtil.downloads.flatMapLatest { downloads ->
database.allSongs()
.flowOn(Dispatchers.IO)
.map { songs ->
songs.filter {
downloads[it.id]?.state == Download.STATE_COMPLETED
}
}
.map { songs ->
when (sortType) {
SongSortType.CREATE_DATE -> songs.sortedBy { downloads[it.id]?.updateTimeMs ?: 0L }
SongSortType.NAME -> songs.sortedBy { it.song.title }
SongSortType.ARTIST -> songs.sortedBy { song ->
song.artists.joinToString(separator = "") { it.name }
}

SongSortType.PLAY_TIME -> songs.sortedBy { it.song.totalPlayTime }
}.reversed(descending)
}
}
}
}.stateIn(viewModelScope, SharingStarted.Lazily, emptyList())
val topSongs = database.mostPlayedSongs(0, 100)
}

Expand Down

0 comments on commit d21bb03

Please sign in to comment.