[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

@@ -12,23 +12,39 @@ object MediaManager
var token: String = "null"
suspend fun listVideoKlasses(): List<String>
{
try
{
val j = ApiClient.api.getVideoClasses(token)
return j.toList()
}catch(e: Exception)
{
return listOf()
}
}
suspend fun listVideos(klass: String): List<Video>
{
try {
val j = ApiClient.api.queryVideoClasses(klass, token)
return j.map{
queryVideo(klass, it)
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?
{
try {
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>

View File

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

View File

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

View File

@@ -55,7 +55,7 @@ class VideoPlayerViewModel() : ViewModel()
remember {
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]))
_player = ExoPlayer.Builder(context).build().apply {
val url = video?.getVideo() ?: ""