You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

59 lines
1.8 KiB

package com.cookiejarapps.smartcookieweb_ytdl.models
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.cookiejarapps.smartcookieweb_ytdl.item.VideoInfoItem
import com.yausername.youtubedl_android.YoutubeDL
import com.yausername.youtubedl_android.mapper.VideoInfo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class VideoInfoViewModel : ViewModel() {
val vidFormats: MutableLiveData<VideoInfo> = MutableLiveData()
val loadState: MutableLiveData<LoadState> = MutableLiveData(LoadState.INITIAL)
private val thumbnail: MutableLiveData<String> = MutableLiveData()
lateinit var selectedItem: VideoInfoItem.VideoFormatItem
private fun submit(vidInfoItems: VideoInfo?) {
vidFormats.postValue(vidInfoItems)
}
private fun updateLoading(loadState: LoadState) {
this.loadState.postValue(loadState)
}
private fun updateThumbnail(thumbnail: String?) {
this.thumbnail.postValue(thumbnail)
}
fun fetchInfo(url: String) {
viewModelScope.launch {
updateLoading(LoadState.LOADING)
submit(null)
updateThumbnail(null)
lateinit var vidInfo: VideoInfo
try {
withContext(Dispatchers.IO) {
vidInfo = YoutubeDL.getInstance().getInfo(url)
}
} catch (e: Exception) {
updateLoading(LoadState.ERRORED)
return@launch
}
updateLoading(LoadState.LOADED)
updateThumbnail(vidInfo.thumbnail)
submit(vidInfo)
}
}
}
enum class LoadState {
INITIAL, LOADING, LOADED, ERRORED
}