[add] Implement Bilibili style

This commit is contained in:
acite
2025-08-25 04:26:34 +08:00
parent d0a6497dd6
commit 484f158e17
12 changed files with 766 additions and 424 deletions

View File

@@ -0,0 +1,94 @@
package com.acitelight.aether.service
import android.content.Context
import com.acitelight.aether.model.Video
import com.acitelight.aether.model.VideoQueryIndex
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import kotlinx.serialization.json.*
import java.io.File
import java.io.FileNotFoundException
import java.io.IOException
object RecentManager
{
private val mutex = Mutex()
suspend fun readFile(context: Context, filename: String): String {
return withContext(Dispatchers.IO) {
try {
val file = File(context.filesDir, filename)
val content = file.readText()
content
} catch (e: FileNotFoundException) {
"[]"
} catch (e: IOException) {
"[]"
}
}
}
suspend fun writeFile(context: Context, filename: String, content: String) {
withContext(Dispatchers.IO) {
try {
val file = File(context.filesDir, filename)
file.writeText(content)
} catch (e: IOException) {
e.printStackTrace()
}
}
}
suspend fun Query(context: Context): List<VideoQueryIndex>
{
val content = readFile(context, "recent.json")
try{
val r = Json.decodeFromString<List<VideoQueryIndex>>(content)
_recent.value = r.map{
MediaManager.queryVideo(it.klass, it.id)
}
return r
}catch (e: Exception)
{
print(e.message)
}
return listOf()
}
suspend fun Push(context: Context, video: VideoQueryIndex)
{
mutex.withLock{
val content = readFile(context, "recent.json")
var o = Json.decodeFromString<List<VideoQueryIndex>>(content).toMutableList();
if(o.contains(video))
{
val temp = o[0]
val index = o.indexOf(video)
o[0] = o[index]
o[index] = temp
}
else
{
o.add(0, video)
}
if(o.size >= 21)
o.removeAt(o.size - 1)
_recent.value = o.map{
MediaManager.queryVideo(it.klass, it.id)
}
writeFile(context, "recent.json", Json.encodeToString(o))
}
}
private val _recent = MutableStateFlow<List<Video>>(emptyList())
val recent: StateFlow<List<Video>> = _recent
}