update
This commit is contained in:
@@ -3,20 +3,19 @@ package com.youtubeapp.data
|
||||
import retrofit2.http.GET
|
||||
import retrofit2.http.POST
|
||||
import retrofit2.http.Path
|
||||
import retrofit2.http.Query
|
||||
|
||||
interface VideoApi {
|
||||
@GET("videos")
|
||||
suspend fun getAllVideos(@Query("profileId") profileId: Int? = null): List<Video>
|
||||
|
||||
@GET("videos/downloaded")
|
||||
suspend fun getDownloadedVideos(@Query("profileId") profileId: Int? = null): List<Video>
|
||||
@GET("profiles/{profileId}/videos")
|
||||
suspend fun getAllVideos(@Path("profileId") profileId: Int): List<Video>
|
||||
|
||||
@POST("videos/{id}/download")
|
||||
suspend fun triggerDownload(@Path("id") id: Int): Map<String, String>
|
||||
|
||||
@POST("videos/cleanup")
|
||||
suspend fun cleanupVideos(@retrofit2.http.Body body: Map<String, @JvmSuppressWildcards Any>): Map<String, Int>
|
||||
@POST("profiles/{profileId}/videos/cleanup")
|
||||
suspend fun cleanupVideos(
|
||||
@Path("profileId") profileId: Int,
|
||||
@retrofit2.http.Body body: Map<String, @JvmSuppressWildcards Any>,
|
||||
): Map<String, Int>
|
||||
|
||||
@retrofit2.http.DELETE("videos/{id}/file")
|
||||
suspend fun deleteServerFile(@Path("id") id: Int): Map<String, String>
|
||||
|
||||
@@ -2,12 +2,9 @@ package com.youtubeapp.data
|
||||
|
||||
class VideoRepository(private val api: VideoApi = ApiClient.api) {
|
||||
|
||||
suspend fun getAllVideos(profileId: Int? = null): Result<List<Video>> =
|
||||
suspend fun getAllVideos(profileId: Int): Result<List<Video>> =
|
||||
runCatching { api.getAllVideos(profileId) }
|
||||
|
||||
suspend fun getDownloadedVideos(profileId: Int? = null): Result<List<Video>> =
|
||||
runCatching { api.getDownloadedVideos(profileId) }
|
||||
|
||||
suspend fun triggerDownload(videoId: Int): Result<String> = runCatching {
|
||||
val response = api.triggerDownload(videoId)
|
||||
response["status"] ?: "unknown"
|
||||
@@ -19,8 +16,8 @@ class VideoRepository(private val api: VideoApi = ApiClient.api) {
|
||||
}
|
||||
|
||||
suspend fun cleanupVideos(profileId: Int, excludeIds: List<Int>): Result<Int> = runCatching {
|
||||
val body = mapOf("profileId" to profileId, "excludeIds" to excludeIds)
|
||||
val response = api.cleanupVideos(body)
|
||||
val body = mapOf("excludeIds" to excludeIds)
|
||||
val response = api.cleanupVideos(profileId, body)
|
||||
response["deleted"] ?: 0
|
||||
}
|
||||
|
||||
|
||||
@@ -103,9 +103,10 @@ class VideoViewModel : ViewModel() {
|
||||
}
|
||||
|
||||
fun loadAllVideos() {
|
||||
val profileId = _state.value.selectedProfileId ?: return
|
||||
viewModelScope.launch {
|
||||
_state.value = _state.value.copy(isLoading = true, error = null)
|
||||
repository.getAllVideos(profileId = _state.value.selectedProfileId)
|
||||
repository.getAllVideos(profileId)
|
||||
.onSuccess { videos ->
|
||||
_state.value = _state.value.copy(allVideos = videos, isLoading = false)
|
||||
}
|
||||
@@ -135,10 +136,11 @@ class VideoViewModel : ViewModel() {
|
||||
|
||||
val status = result.getOrNull()
|
||||
if (status == "download_started") {
|
||||
val profileId = _state.value.selectedProfileId ?: 1
|
||||
var attempts = 0
|
||||
while (attempts < 150) {
|
||||
delay(2000)
|
||||
val videosResult = repository.getAllVideos()
|
||||
val videosResult = repository.getAllVideos(profileId)
|
||||
val video = videosResult.getOrNull()?.find { it.id == videoId }
|
||||
if (video?.isDownloaded == true) break
|
||||
attempts++
|
||||
|
||||
Reference in New Issue
Block a user