Compare commits
2 Commits
5b770a965d
...
8184ab211c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8184ab211c | ||
|
|
4e346a83ee |
@@ -4,6 +4,7 @@ import android.content.Context
|
||||
import com.acitelight.aether.model.BookMark
|
||||
import com.acitelight.aether.model.Comic
|
||||
import com.acitelight.aether.model.Video
|
||||
import com.acitelight.aether.model.VideoDownloadItemState
|
||||
import com.tonyodev.fetch2.Status
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import kotlinx.serialization.json.Json
|
||||
@@ -44,15 +45,34 @@ class MediaManager @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun queryVideo(klass: String, id: String, model: VideoDownloadItemState): Video?
|
||||
{
|
||||
if(model.status == Status.COMPLETED)
|
||||
{
|
||||
val jsonString = File(
|
||||
context.getExternalFilesDir(null),
|
||||
"videos/$klass/$id/summary.json"
|
||||
).readText()
|
||||
return Json.decodeFromString<Video>(jsonString).toLocal(context.getExternalFilesDir(null)?.path!!)
|
||||
}
|
||||
|
||||
try {
|
||||
val j = ApiClient.api!!.queryVideo(klass, id, token)
|
||||
return Video(klass = klass, id = id, token=token, isLocal = false, localBase = "", video = j)
|
||||
}catch (_: Exception)
|
||||
{
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun queryVideo(klass: String, id: String): Video?
|
||||
{
|
||||
val downloaded = fetchManager.getAllDownloadsAsync().filter {
|
||||
it.status == Status.COMPLETED &&
|
||||
it.extras.getString("id", "") == id &&
|
||||
it.extras.getString("class", "") == klass
|
||||
}
|
||||
|
||||
if(!downloaded.isEmpty())
|
||||
if(downloaded.all{ it.status == Status.COMPLETED })
|
||||
{
|
||||
val jsonString = File(
|
||||
context.getExternalFilesDir(null),
|
||||
@@ -72,16 +92,16 @@ class MediaManager @Inject constructor(
|
||||
|
||||
suspend fun queryVideoBulk(klass: String, id: List<String>): List<Video>? {
|
||||
return try {
|
||||
val completedDownloads = fetchManager.getAllDownloadsAsync()
|
||||
.filter { it.status == Status.COMPLETED }
|
||||
val downloads = fetchManager.getAllDownloadsAsync()
|
||||
|
||||
val localIds = mutableSetOf<String>()
|
||||
val remoteIds = mutableListOf<String>()
|
||||
|
||||
for (videoId in id) {
|
||||
if (completedDownloads.any {
|
||||
if (downloads.filter {
|
||||
it.extras.getString("id", "") == videoId &&
|
||||
it.extras.getString("class", "") == klass
|
||||
}) {
|
||||
}.all{ it.status == Status.COMPLETED} ) {
|
||||
localIds.add(videoId)
|
||||
} else {
|
||||
remoteIds.add(videoId)
|
||||
|
||||
@@ -82,7 +82,7 @@ fun HomeScreen(
|
||||
{
|
||||
updateRelate(homeScreenViewModel.recentManager.recentVideo, i)
|
||||
|
||||
val playList = mutableListOf("${i.klass}/${i.id}")
|
||||
val playList = mutableListOf<String>()
|
||||
val fv = homeScreenViewModel.videoLibrary.classesMap.map { it.value }.flatten()
|
||||
|
||||
val group = fv.filter { it.klass == i.klass && it.video.group == i.video.group }
|
||||
@@ -90,7 +90,7 @@ fun HomeScreen(
|
||||
playList.add("${i.klass}/${i.id}")
|
||||
}
|
||||
|
||||
val route = "video_player_route/${playList.joinToString(",").toHex()}"
|
||||
val route = "video_player_route/${(playList.joinToString(",") + "|${i.id}").toHex()}"
|
||||
navController.navigate(route)
|
||||
}, homeScreenViewModel.imageLoader!!)
|
||||
HorizontalDivider(Modifier.padding(vertical = 8.dp).alpha(0.4f), 1.dp, DividerDefaults.color)
|
||||
|
||||
@@ -12,6 +12,8 @@ import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.lazy.LazyRow
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CardDefaults
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
@@ -28,7 +30,9 @@ import kotlinx.coroutines.launch
|
||||
@Composable
|
||||
fun PlaylistPanel(modifier: Modifier, videoPlayerViewModel: VideoPlayerViewModel)
|
||||
{
|
||||
val colorScheme = MaterialTheme.colorScheme
|
||||
val name by videoPlayerViewModel.currentName
|
||||
val id by videoPlayerViewModel.currentId
|
||||
|
||||
LazyRow(
|
||||
modifier = modifier
|
||||
@@ -48,7 +52,13 @@ fun PlaylistPanel(modifier: Modifier, videoPlayerViewModel: VideoPlayerViewModel
|
||||
videoPlayerViewModel.viewModelScope.launch {
|
||||
videoPlayerViewModel.startPlay(it)
|
||||
}
|
||||
}
|
||||
},
|
||||
colors =
|
||||
if (it.id == id)
|
||||
CardDefaults.cardColors(containerColor = colorScheme.primary)
|
||||
else
|
||||
CardDefaults.cardColors()
|
||||
|
||||
) {
|
||||
Box(Modifier.padding(8.dp).fillMaxSize())
|
||||
{
|
||||
|
||||
@@ -50,6 +50,7 @@ import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.serialization.json.Json
|
||||
import java.io.File
|
||||
import kotlin.collections.sortedWith
|
||||
|
||||
@Composable
|
||||
fun TransmissionScreen(
|
||||
@@ -120,9 +121,12 @@ private fun VideoDownloadCard(
|
||||
{
|
||||
val downloaded = viewModel.fetchManager.getAllDownloadsAsync().filter {
|
||||
it.status == Status.COMPLETED && it.extras.getString(
|
||||
"isComic",
|
||||
"class",
|
||||
""
|
||||
) != "true"
|
||||
) != "comic" && it.extras.getString(
|
||||
"type",
|
||||
""
|
||||
) == "main"
|
||||
}
|
||||
|
||||
val jsonQuery = downloaded.map {
|
||||
@@ -146,18 +150,18 @@ private fun VideoDownloadCard(
|
||||
jsonQuery.first { it.id == model.vid && it.klass == model.klass }
|
||||
)
|
||||
|
||||
val playList = mutableListOf("${model.klass}/${model.vid}")
|
||||
val playList = mutableListOf<String>()
|
||||
val fv = viewModel.videoLibrary.classesMap.map { it.value }.flatten()
|
||||
val video = fv.firstOrNull { it.klass == model.klass && it.id == model.vid }
|
||||
|
||||
if (video != null) {
|
||||
val group = fv.filter { it.klass == video.klass && it.video.group == video.video.group }
|
||||
for (i in group) {
|
||||
for (i in group.sortedWith(compareBy(naturalOrder()) { it.video.name })) {
|
||||
playList.add("${i.klass}/${i.id}")
|
||||
}
|
||||
}
|
||||
|
||||
val route = "video_player_route/${playList.joinToString(",").toHex()}"
|
||||
val route = "video_player_route/${(playList.joinToString(",") + "|${model.vid}").toHex()}"
|
||||
withContext(Dispatchers.Main) {
|
||||
navigator.navigate(route)
|
||||
}
|
||||
|
||||
@@ -47,6 +47,7 @@ import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.navigation.NavHostController
|
||||
import com.acitelight.aether.Global
|
||||
import com.acitelight.aether.Global.updateRelate
|
||||
import com.acitelight.aether.ToggleFullScreen
|
||||
import com.acitelight.aether.viewModel.VideoPlayerViewModel
|
||||
|
||||
@@ -108,9 +109,11 @@ fun VideoPlayerPortal(
|
||||
val duration by videoPlayerViewModel.currentDuration
|
||||
|
||||
ToggleFullScreen(false)
|
||||
Column(Modifier
|
||||
.nestedScroll(nestedScrollConnection)
|
||||
.fillMaxHeight())
|
||||
Column(
|
||||
Modifier
|
||||
.nestedScroll(nestedScrollConnection)
|
||||
.fillMaxHeight()
|
||||
)
|
||||
{
|
||||
Box {
|
||||
PortalCorePlayer(
|
||||
@@ -194,13 +197,15 @@ fun VideoPlayerPortal(
|
||||
fontWeight = FontWeight.Bold,
|
||||
)
|
||||
|
||||
Row(Modifier
|
||||
.align(Alignment.Start)
|
||||
.padding(horizontal = 4.dp)
|
||||
.alpha(0.5f)) {
|
||||
Row(
|
||||
Modifier
|
||||
.align(Alignment.Start)
|
||||
.padding(horizontal = 4.dp)
|
||||
.alpha(0.5f)
|
||||
) {
|
||||
Text(
|
||||
modifier = Modifier.padding(horizontal = 8.dp),
|
||||
text = klass,
|
||||
text = "$klass.$id",
|
||||
fontSize = 14.sp,
|
||||
maxLines = 1,
|
||||
fontWeight = FontWeight.Bold,
|
||||
@@ -217,12 +222,18 @@ fun VideoPlayerPortal(
|
||||
|
||||
HorizontalDivider(Modifier.padding(vertical = 8.dp), 1.dp, DividerDefaults.color)
|
||||
|
||||
PlaylistPanel(
|
||||
Modifier,
|
||||
videoPlayerViewModel = videoPlayerViewModel
|
||||
)
|
||||
if (videoPlayerViewModel.videos.size > 1) {
|
||||
PlaylistPanel(
|
||||
Modifier,
|
||||
videoPlayerViewModel = videoPlayerViewModel
|
||||
)
|
||||
|
||||
HorizontalDivider(Modifier.padding(vertical = 8.dp), 1.dp, DividerDefaults.color)
|
||||
HorizontalDivider(
|
||||
Modifier.padding(vertical = 8.dp),
|
||||
1.dp,
|
||||
DividerDefaults.color
|
||||
)
|
||||
}
|
||||
|
||||
HorizontalGallery(videoPlayerViewModel)
|
||||
HorizontalDivider(Modifier.padding(vertical = 8.dp), 1.dp, DividerDefaults.color)
|
||||
@@ -237,7 +248,16 @@ fun VideoPlayerPortal(
|
||||
{
|
||||
videoPlayerViewModel.isPlaying = false
|
||||
videoPlayerViewModel.player?.pause()
|
||||
val route = "video_player_route/${"${i.klass}/${i.id}".toHex()}"
|
||||
|
||||
val playList = mutableListOf<String>()
|
||||
val fv = videoPlayerViewModel.videoLibrary.classesMap.map { it.value }.flatten()
|
||||
|
||||
val group = fv.filter { it.klass == i.klass && it.video.group == i.video.group }
|
||||
for (i in group) {
|
||||
playList.add("${i.klass}/${i.id}")
|
||||
}
|
||||
|
||||
val route = "video_player_route/${playList.joinToString(",").toHex()}"
|
||||
navController.navigate(route)
|
||||
}, videoPlayerViewModel.imageLoader!!
|
||||
)
|
||||
|
||||
@@ -11,6 +11,7 @@ import com.acitelight.aether.model.Video
|
||||
import com.acitelight.aether.model.VideoDownloadItemState
|
||||
import com.acitelight.aether.service.ApiClient.createOkHttp
|
||||
import com.acitelight.aether.service.FetchManager
|
||||
import com.acitelight.aether.service.MediaManager
|
||||
import com.acitelight.aether.service.VideoLibrary
|
||||
import com.tonyodev.fetch2.Download
|
||||
import com.tonyodev.fetch2.FetchListener
|
||||
@@ -27,6 +28,7 @@ class TransmissionScreenViewModel @Inject constructor(
|
||||
val fetchManager: FetchManager,
|
||||
@ApplicationContext val context: Context,
|
||||
val videoLibrary: VideoLibrary,
|
||||
val mediaManager: MediaManager,
|
||||
) : ViewModel() {
|
||||
var imageLoader: ImageLoader? = null
|
||||
val downloads: SnapshotStateList<VideoDownloadItemState> = mutableStateListOf()
|
||||
@@ -34,8 +36,7 @@ class TransmissionScreenViewModel @Inject constructor(
|
||||
// map id -> state object reference (no index bookkeeping)
|
||||
private val idToState: MutableMap<Int, VideoDownloadItemState> = mutableMapOf()
|
||||
|
||||
fun modelToVideo(model: VideoDownloadItemState): Video?
|
||||
{
|
||||
fun modelToVideo(model: VideoDownloadItemState): Video? {
|
||||
val fv = videoLibrary.classesMap.map { it.value }.flatten()
|
||||
return fv.firstOrNull { it.klass == model.klass && it.id == model.vid }
|
||||
}
|
||||
@@ -54,9 +55,7 @@ class TransmissionScreenViewModel @Inject constructor(
|
||||
}
|
||||
|
||||
override fun onProgress(
|
||||
download: Download,
|
||||
etaInMilliSeconds: Long,
|
||||
downloadedBytesPerSecond: Long
|
||||
download: Download, etaInMilliSeconds: Long, downloadedBytesPerSecond: Long
|
||||
) {
|
||||
handleUpsert(download)
|
||||
}
|
||||
@@ -73,12 +72,21 @@ class TransmissionScreenViewModel @Inject constructor(
|
||||
handleUpsert(download)
|
||||
|
||||
if (download.extras.getString("type", "") == "main") {
|
||||
val ii = videoLibrary.classesMap[download.extras.getString("class", "")]
|
||||
?.indexOfFirst { it.id == download.extras.getString("id", "") }!!
|
||||
val ii = videoLibrary.classesMap[download.extras.getString(
|
||||
"class",
|
||||
""
|
||||
)]?.indexOfFirst { it.id == download.extras.getString("id", "") }
|
||||
|
||||
val newi = videoLibrary.classesMap[download.extras.getString("class", "")]!![ii]
|
||||
videoLibrary.classesMap[download.extras.getString("class", "")]!![ii] =
|
||||
newi.toLocal(context.getExternalFilesDir(null)!!.path)
|
||||
if (ii != null) {
|
||||
val newi =
|
||||
videoLibrary.classesMap[download.extras.getString("class", "")]?.get(ii)
|
||||
if (newi != null) videoLibrary.classesMap[download.extras.getString(
|
||||
"class",
|
||||
""
|
||||
)]?.set(
|
||||
ii, newi.toLocal(context.getExternalFilesDir(null)!!.path)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,25 +103,19 @@ class TransmissionScreenViewModel @Inject constructor(
|
||||
}
|
||||
|
||||
override fun onDownloadBlockUpdated(
|
||||
download: Download,
|
||||
downloadBlock: DownloadBlock,
|
||||
totalBlocks: Int
|
||||
download: Download, downloadBlock: DownloadBlock, totalBlocks: Int
|
||||
) {
|
||||
handleUpsert(download)
|
||||
}
|
||||
|
||||
override fun onStarted(
|
||||
download: Download,
|
||||
downloadBlocks: List<DownloadBlock>,
|
||||
totalBlocks: Int
|
||||
download: Download, downloadBlocks: List<DownloadBlock>, totalBlocks: Int
|
||||
) {
|
||||
handleUpsert(download)
|
||||
}
|
||||
|
||||
override fun onError(
|
||||
download: Download,
|
||||
error: com.tonyodev.fetch2.Error,
|
||||
throwable: Throwable?
|
||||
download: Download, error: com.tonyodev.fetch2.Error, throwable: Throwable?
|
||||
) {
|
||||
handleUpsert(download)
|
||||
}
|
||||
@@ -123,6 +125,23 @@ class TransmissionScreenViewModel @Inject constructor(
|
||||
viewModelScope.launch(Dispatchers.Main) {
|
||||
upsertOnMain(download)
|
||||
}
|
||||
|
||||
val state = downloadToState(download)
|
||||
|
||||
if (videoLibrary.classes.contains(state.klass)) videoLibrary.classes.add(state.klass)
|
||||
|
||||
if (!videoLibrary.classesMap.containsKey(state.klass)) videoLibrary.classesMap[state.klass] =
|
||||
mutableStateListOf()
|
||||
|
||||
if (videoLibrary.classesMap[state.klass]?.any { it.id == state.vid } != true) {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
val v = mediaManager.queryVideo(state.klass, state.vid, state)
|
||||
if (v != null) {
|
||||
videoLibrary.classesMap[state.klass]?.add(v)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleRemove(id: Int) {
|
||||
@@ -198,25 +217,34 @@ class TransmissionScreenViewModel @Inject constructor(
|
||||
}
|
||||
|
||||
init {
|
||||
imageLoader = ImageLoader.Builder(context)
|
||||
.components {
|
||||
add(OkHttpNetworkFetcherFactory(createOkHttp()))
|
||||
}
|
||||
.build()
|
||||
imageLoader = ImageLoader.Builder(context).components {
|
||||
add(OkHttpNetworkFetcherFactory(createOkHttp()))
|
||||
}.build()
|
||||
|
||||
viewModelScope.launch {
|
||||
fetchManager.setListener(fetchListener)
|
||||
withContext(Dispatchers.Main) {
|
||||
fetchManager.getAllDownloads { list ->
|
||||
downloads.clear()
|
||||
idToState.clear()
|
||||
list.sortedBy { it.extras.getString("name", "") }.forEach { d ->
|
||||
val s = downloadToState(d)
|
||||
downloads.add(s)
|
||||
idToState[s.id] = s
|
||||
val downloaded = fetchManager.getAllDownloadsAsync()
|
||||
|
||||
downloads.clear()
|
||||
idToState.clear()
|
||||
downloaded.sortedWith(compareBy(naturalOrder()) { it.extras.getString("name", "") })
|
||||
.forEach { d ->
|
||||
val s = downloadToState(d)
|
||||
downloads.add(s)
|
||||
idToState[s.id] = s
|
||||
|
||||
if (videoLibrary.classes.contains(s.klass)) videoLibrary.classes.add(s.klass)
|
||||
|
||||
if (!videoLibrary.classesMap.containsKey(s.klass)) videoLibrary.classesMap[s.klass] =
|
||||
mutableStateListOf()
|
||||
|
||||
if (videoLibrary.classesMap[s.klass]?.any { it.id == s.vid } != true) {
|
||||
val v = mediaManager.queryVideo(s.klass, s.vid, s)
|
||||
if (v != null) {
|
||||
videoLibrary.classesMap[s.klass]?.add(v)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,6 +49,7 @@ import androidx.media3.datasource.DefaultDataSource
|
||||
import androidx.media3.exoplayer.trackselection.DefaultTrackSelector
|
||||
import com.acitelight.aether.Global
|
||||
import com.acitelight.aether.model.KeyImage
|
||||
import com.acitelight.aether.service.VideoLibrary
|
||||
import kotlinx.coroutines.flow.filter
|
||||
import kotlinx.coroutines.flow.first
|
||||
|
||||
@@ -56,7 +57,8 @@ import kotlinx.coroutines.flow.first
|
||||
class VideoPlayerViewModel @Inject constructor(
|
||||
@ApplicationContext private val context: Context,
|
||||
val mediaManager: MediaManager,
|
||||
val recentManager: RecentManager
|
||||
val recentManager: RecentManager,
|
||||
val videoLibrary: VideoLibrary,
|
||||
) : ViewModel() {
|
||||
var tabIndex by mutableIntStateOf(0)
|
||||
var isPlaying by mutableStateOf(true)
|
||||
@@ -100,7 +102,16 @@ class VideoPlayerViewModel @Inject constructor(
|
||||
return
|
||||
_init = true
|
||||
|
||||
val vs = videoId.hexToString().split(",").map { it.split("/") }.toMutableList()
|
||||
val oId = videoId.hexToString()
|
||||
var spec = "-1"
|
||||
var vs = mutableListOf<List<String>>()
|
||||
|
||||
if(oId.contains("|"))
|
||||
{
|
||||
vs = oId.split("|")[0].split(",").map { it.split("/") }.toMutableList()
|
||||
spec = oId.split("|")[1]
|
||||
}
|
||||
|
||||
imageLoader = ImageLoader.Builder(context)
|
||||
.components {
|
||||
add(OkHttpNetworkFetcherFactory(createOkHttp()))
|
||||
@@ -114,7 +125,9 @@ class VideoPlayerViewModel @Inject constructor(
|
||||
videos = mediaManager.queryVideoBulk(vs.first()[0], vs.map { it[1] })!!
|
||||
|
||||
startPlay(
|
||||
if (ix != null)
|
||||
if(spec != "-1")
|
||||
videos.first { it.id == spec}
|
||||
else if (ix != null)
|
||||
videos.first { it.id == ix.id }
|
||||
else videos.first()
|
||||
)
|
||||
|
||||
@@ -54,13 +54,16 @@ class VideoScreenViewModel @Inject constructor(
|
||||
|
||||
if (Global.loggedIn) {
|
||||
videoLibrary.classes.addAll(mediaManager.listVideoKlasses())
|
||||
videoLibrary.classes.distinct()
|
||||
|
||||
if(videoLibrary.classes.isEmpty())
|
||||
return
|
||||
|
||||
var i = 0
|
||||
for (it in videoLibrary.classes) {
|
||||
videoLibrary.updatingMap[i++] = false
|
||||
videoLibrary.classesMap[it] = mutableStateListOf<Video>()
|
||||
if(!videoLibrary.classesMap.containsKey(it))
|
||||
videoLibrary.classesMap[it] = mutableStateListOf()
|
||||
}
|
||||
videoLibrary.updatingMap[0] = true
|
||||
val vl =
|
||||
@@ -69,6 +72,7 @@ class VideoScreenViewModel @Inject constructor(
|
||||
if (vl != null) {
|
||||
val r = vl.sortedWith(compareBy(naturalOrder()) { it.video.name })
|
||||
videoLibrary.classesMap[videoLibrary.classes[0]]?.addAll(r)
|
||||
videoLibrary.classesMap[videoLibrary.classes[0]]?.distinctBy { it.id }
|
||||
}
|
||||
}
|
||||
else {
|
||||
@@ -77,7 +81,7 @@ class VideoScreenViewModel @Inject constructor(
|
||||
videoLibrary.classesMap["Offline"] = mutableStateListOf<Video>()
|
||||
|
||||
val downloaded = fetchManager.getAllDownloadsAsync().filter {
|
||||
it.status == Status.COMPLETED && it.extras.getString("isComic", "") != "true"
|
||||
it.status == Status.COMPLETED && it.extras.getString("class", "") != "comic"
|
||||
}
|
||||
|
||||
val jsonQuery = downloaded.map{ File(
|
||||
@@ -107,6 +111,7 @@ class VideoScreenViewModel @Inject constructor(
|
||||
if (vl != null) {
|
||||
val r = vl.sortedWith(compareBy(naturalOrder()) { it.video.name })
|
||||
videoLibrary.classesMap[videoLibrary.classes[index]]?.addAll(r)
|
||||
videoLibrary.classesMap[videoLibrary.classes[index]]?.distinctBy { it.id }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user