Skip to content

Commit

Permalink
修复超大歌单加载失败
Browse files Browse the repository at this point in the history
  • Loading branch information
wangchenyan committed Mar 29, 2024
1 parent 038a7c8 commit 764a661
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 7 deletions.
32 changes: 31 additions & 1 deletion app/src/main/java/me/wcy/music/discover/DiscoverApi.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package me.wcy.music.discover

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import me.wcy.music.common.bean.LrcDataWrap
import me.wcy.music.common.bean.SongData
import me.wcy.music.common.bean.SongUrlData
import me.wcy.music.discover.banner.BannerListData
import me.wcy.music.discover.playlist.detail.bean.PlaylistDetailData
Expand Down Expand Up @@ -48,7 +51,8 @@ interface DiscoverApi {
@POST("playlist/track/all")
suspend fun getPlaylistSongList(
@Query("id") id: Long,
@Query("limit") limit: Long? = null,
@Query("limit") limit: Int? = null,
@Query("offset") offset: Int? = null,
@Query("timestamp") timestamp: Long? = null
): SongListData

Expand All @@ -69,6 +73,8 @@ interface DiscoverApi {
suspend fun getBannerList(): BannerListData

companion object {
private const val SONG_LIST_LIMIT = 800

private val api: DiscoverApi by lazy {
val retrofit = Retrofit.Builder()
.baseUrl(ConfigPreferences.apiDomain)
Expand All @@ -79,5 +85,29 @@ interface DiscoverApi {
}

fun get(): DiscoverApi = api

suspend fun getFullPlaylistSongList(id: Long, timestamp: Long? = null): SongListData {
return withContext(Dispatchers.IO) {
var offset = 0
val list = mutableListOf<SongData>()
while (true) {
val songList = get().getPlaylistSongList(
id,
limit = SONG_LIST_LIMIT,
offset = offset,
timestamp = timestamp
)
if (songList.code != 200) {
throw Exception("code = ${songList.code}")
}
if (songList.songs.isEmpty()) {
break
}
list.addAll(songList.songs)
offset = list.size
}
return@withContext SongListData(200, list)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ class DiscoverFragment : BaseMusicFragment() {
lifecycleScope.launch {
showLoading()
kotlin.runCatching {
DiscoverApi.get().getPlaylistSongList(playlistData.id)
DiscoverApi.getFullPlaylistSongList(playlistData.id)
}.onSuccess { songListData ->
dismissLoading()
if (songListData.code == 200 && songListData.songs.isNotEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class DiscoverViewModel @Inject constructor(
rankingList.forEach {
val d = async {
val songListRes = kotlin.runCatching {
DiscoverApi.get().getPlaylistSongList(it.id, 3)
DiscoverApi.get().getPlaylistSongList(it.id, limit = 3)
}
if (songListRes.getOrNull()?.code == 200) {
it.songList = songListRes.getOrThrow().songs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class PlaylistDetailFragment : BaseMusicFragment() {
viewBinding.tvName.text = playlistData.name
viewBinding.ivCreatorAvatar.loadAvatar(playlistData.creator.avatarUrl)
viewBinding.tvCreatorName.text = playlistData.creator.nickname
viewBinding.tvSongCount.text = "(${playlistData.trackCount})"

viewBinding.flTags.removeAllViews()
playlistData.tags.forEach { tag ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class PlaylistViewModel @Inject constructor() : ViewModel() {
}
val songListRes = kotlin.runCatching {
val timestamp = if (realtimeData) ServerTime.currentTimeMillis() else null
DiscoverApi.get().getPlaylistSongList(playlistId, timestamp = timestamp)
DiscoverApi.getFullPlaylistSongList(playlistId, timestamp = timestamp)
}
return if (detailRes.isSuccess.not() || detailRes.getOrThrow().code != 200) {
CommonResult.fail(msg = detailRes.exceptionOrNull()?.message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class RankingFragment : BaseMusicFragment() {
lifecycleScope.launch {
showLoading()
kotlin.runCatching {
DiscoverApi.get().getPlaylistSongList(playlistData.id)
DiscoverApi.getFullPlaylistSongList(playlistData.id)
}.onSuccess { songListData ->
dismissLoading()
if (songListData.code == 200 && songListData.songs.isNotEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import me.wcy.music.discover.DiscoverApi
import top.wangchenyan.common.ext.toUnMutable
import top.wangchenyan.common.model.CommonResult
import me.wcy.music.discover.DiscoverApi

/**
* Created by wangchenyan.top on 2023/10/25.
Expand All @@ -31,7 +31,7 @@ class RankingViewModel : ViewModel() {
officialList.forEach {
val d = async {
val songListRes = kotlin.runCatching {
DiscoverApi.get().getPlaylistSongList(it.id, 3)
DiscoverApi.get().getPlaylistSongList(it.id, limit = 3)
}
if (songListRes.getOrNull()?.code == 200) {
it.songList = songListRes.getOrThrow().songs
Expand Down

0 comments on commit 764a661

Please sign in to comment.