[optimize] Optimize API call logic, do not create crashes

This commit is contained in:
acite
2025-08-25 17:22:09 +08:00
parent 0398caf3e5
commit 8fa9dfc809
4 changed files with 41 additions and 14 deletions

View File

@@ -13,22 +13,38 @@ object MediaManager
suspend fun listVideoKlasses(): List<String> suspend fun listVideoKlasses(): List<String>
{ {
val j = ApiClient.api.getVideoClasses(token) try
return j.toList() {
val j = ApiClient.api.getVideoClasses(token)
return j.toList()
}catch(e: Exception)
{
return listOf()
}
} }
suspend fun listVideos(klass: String): List<Video> suspend fun listVideos(klass: String): List<Video>
{ {
val j = ApiClient.api.queryVideoClasses(klass, token) try {
return j.map{ val j = ApiClient.api.queryVideoClasses(klass, token)
queryVideo(klass, it) return j.map{
}.toList() queryVideo(klass, it)!!
}.toList()
}catch (e: Exception)
{
return listOf()
}
} }
suspend fun queryVideo(klass: String, id: String): Video suspend fun queryVideo(klass: String, id: String): Video?
{ {
val j = ApiClient.api.queryVideo(klass, id, token) try {
return Video(klass = klass, id = id, token=token, j) val j = ApiClient.api.queryVideo(klass, id, token)
return Video(klass = klass, id = id, token=token, j)
}catch (e: Exception)
{
return null
}
} }
suspend fun listComics() : List<String> suspend fun listComics() : List<String>

View File

@@ -50,9 +50,11 @@ object RecentManager
try{ try{
val r = Json.decodeFromString<List<VideoQueryIndex>>(content) val r = Json.decodeFromString<List<VideoQueryIndex>>(content)
_recent.value = r.map{ val vn = r.map{
MediaManager.queryVideo(it.klass, it.id) MediaManager.queryVideo(it.klass, it.id)
} }.filter { it != null }
_recent.value = vn.map { it!! }
return r return r
}catch (e: Exception) }catch (e: Exception)
{ {
@@ -82,9 +84,12 @@ object RecentManager
if(o.size >= 21) if(o.size >= 21)
o.removeAt(o.size - 1) o.removeAt(o.size - 1)
_recent.value = o.map{
val vn = o.map{
MediaManager.queryVideo(it.klass, it.id) MediaManager.queryVideo(it.klass, it.id)
} }.filter { it != null }
_recent.value = vn.map { it!! }
writeFile(context, "recent.json", Json.encodeToString(o)) writeFile(context, "recent.json", Json.encodeToString(o))
} }
} }

View File

@@ -914,6 +914,7 @@ fun VideoPlayerLandscape(videoPlayerViewModel: VideoPlayerViewModel)
@Composable @Composable
fun MiniVideoCard(modifier: Modifier, video: Video, onClick: () -> Unit) fun MiniVideoCard(modifier: Modifier, video: Video, onClick: () -> Unit)
{ {
var isImageLoaded by remember { mutableStateOf(false) }
Card( Card(
modifier = modifier.height(80.dp).fillMaxWidth(), modifier = modifier.height(80.dp).fillMaxWidth(),
colors = CardColors( colors = CardColors(
@@ -932,6 +933,11 @@ fun MiniVideoCard(modifier: Modifier, video: Video, onClick: () -> Unit)
.data(video.getCover()) .data(video.getCover())
.memoryCacheKey("${video.klass}/${video.id}/cover") .memoryCacheKey("${video.klass}/${video.id}/cover")
.diskCacheKey("${video.klass}/${video.id}/cover") .diskCacheKey("${video.klass}/${video.id}/cover")
.listener(
onStart = { },
onSuccess = { _, _ -> isImageLoaded = true },
onError = { _, _ -> }
)
.build(), .build(),
contentDescription = null, contentDescription = null,
modifier = Modifier modifier = Modifier

View File

@@ -55,7 +55,7 @@ class VideoPlayerViewModel() : ViewModel()
remember { remember {
viewModelScope.launch { viewModelScope.launch {
video = MediaManager.queryVideo(v.split("/")[0], v.split("/")[1]) video = MediaManager.queryVideo(v.split("/")[0], v.split("/")[1])!!
RecentManager.Push(context, VideoQueryIndex(v.split("/")[0], v.split("/")[1])) RecentManager.Push(context, VideoQueryIndex(v.split("/")[0], v.split("/")[1]))
_player = ExoPlayer.Builder(context).build().apply { _player = ExoPlayer.Builder(context).build().apply {
val url = video?.getVideo() ?: "" val url = video?.getVideo() ?: ""