[fix&feat] Fix local judgment logic to prevent partial downloaded projects from being judged as local, Better playlists
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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 {
|
||||
@@ -152,7 +156,7 @@ private fun VideoDownloadCard(
|
||||
|
||||
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}")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,9 +108,11 @@ fun VideoPlayerPortal(
|
||||
val duration by videoPlayerViewModel.currentDuration
|
||||
|
||||
ToggleFullScreen(false)
|
||||
Column(Modifier
|
||||
.nestedScroll(nestedScrollConnection)
|
||||
.fillMaxHeight())
|
||||
Column(
|
||||
Modifier
|
||||
.nestedScroll(nestedScrollConnection)
|
||||
.fillMaxHeight()
|
||||
)
|
||||
{
|
||||
Box {
|
||||
PortalCorePlayer(
|
||||
@@ -194,13 +196,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 +221,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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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