cloudstream/app/src/main/java/com/lagradost/cloudstream3/ui/player/PlayerGeneratorViewModel.kt

182 lines
5.8 KiB
Kotlin
Raw Normal View History

2022-01-07 19:27:25 +00:00
package com.lagradost.cloudstream3.ui.player
2022-01-30 22:02:57 +00:00
import android.util.Log
2022-01-07 19:27:25 +00:00
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.lagradost.cloudstream3.mvvm.Resource
2022-08-18 00:54:05 +00:00
import com.lagradost.cloudstream3.mvvm.launchSafe
2022-01-07 19:27:25 +00:00
import com.lagradost.cloudstream3.mvvm.normalSafeApiCall
import com.lagradost.cloudstream3.mvvm.safeApiCall
import com.lagradost.cloudstream3.ui.result.ResultEpisode
import com.lagradost.cloudstream3.utils.Coroutines.ioSafe
import com.lagradost.cloudstream3.utils.EpisodeSkip
2022-01-07 19:27:25 +00:00
import com.lagradost.cloudstream3.utils.ExtractorLink
import com.lagradost.cloudstream3.utils.ExtractorUri
2022-01-17 16:17:19 +00:00
import kotlinx.coroutines.Job
2022-01-07 19:27:25 +00:00
class PlayerGeneratorViewModel : ViewModel() {
2022-01-30 22:02:57 +00:00
companion object {
val TAG = "PlayViewGen"
}
2022-01-07 19:27:25 +00:00
private var generator: IGenerator? = null
private val _currentLinks = MutableLiveData<Set<Pair<ExtractorLink?, ExtractorUri?>>>(setOf())
val currentLinks: LiveData<Set<Pair<ExtractorLink?, ExtractorUri?>>> = _currentLinks
private val _currentSubs = MutableLiveData<Set<SubtitleData>>(setOf())
val currentSubs: LiveData<Set<SubtitleData>> = _currentSubs
private val _loadingLinks = MutableLiveData<Resource<Boolean?>>()
val loadingLinks: LiveData<Resource<Boolean?>> = _loadingLinks
private val _currentStamps = MutableLiveData<List<EpisodeSkip.SkipStamp>>(emptyList())
val currentStamps: LiveData<List<EpisodeSkip.SkipStamp>> = _currentStamps
2023-01-17 15:57:46 +00:00
private val _currentSubtitleYear = MutableLiveData<Int?>(null)
val currentSubtitleYear: LiveData<Int?> = _currentSubtitleYear
fun setSubtitleYear(year: Int?) {
_currentSubtitleYear.postValue(year)
}
2022-01-07 19:27:25 +00:00
fun getId(): Int? {
return generator?.getCurrentId()
}
fun loadLinks(episode: Int) {
generator?.goto(episode)
loadLinks()
}
fun loadLinksPrev() {
2022-01-30 22:02:57 +00:00
Log.i(TAG, "loadLinksPrev")
2022-01-07 19:27:25 +00:00
if (generator?.hasPrev() == true) {
generator?.prev()
loadLinks()
}
}
fun loadLinksNext() {
2022-01-30 22:02:57 +00:00
Log.i(TAG, "loadLinksNext")
2022-01-07 19:27:25 +00:00
if (generator?.hasNext() == true) {
generator?.next()
loadLinks()
}
}
fun hasNextEpisode(): Boolean? {
return generator?.hasNext()
}
2022-01-17 16:17:19 +00:00
fun preLoadNextLinks() {
2022-01-30 22:02:57 +00:00
Log.i(TAG, "preLoadNextLinks")
2022-01-17 16:17:19 +00:00
currentJob?.cancel()
2022-08-18 00:54:05 +00:00
currentJob = viewModelScope.launchSafe {
2022-01-07 19:27:25 +00:00
if (generator?.hasCache == true && generator?.hasNext() == true) {
2022-01-17 16:17:19 +00:00
safeApiCall {
2022-01-30 22:02:57 +00:00
generator?.generateLinks(
clearCache = false,
isCasting = false,
{},
{},
offset = 1
)
2022-01-17 16:17:19 +00:00
}
2022-01-07 19:27:25 +00:00
}
}
}
fun getMeta(): Any? {
return normalSafeApiCall { generator?.getCurrent() }
}
fun getAllMeta(): List<Any>? {
return normalSafeApiCall { generator?.getAll() }
}
2022-01-07 19:27:25 +00:00
fun getNextMeta(): Any? {
return normalSafeApiCall {
if (generator?.hasNext() == false) return@normalSafeApiCall null
2022-01-30 22:02:57 +00:00
generator?.getCurrent(offset = 1)
2022-01-07 19:27:25 +00:00
}
}
fun attachGenerator(newGenerator: IGenerator?) {
if (generator == null) {
generator = newGenerator
}
}
2022-05-20 18:25:56 +00:00
/**
* If duplicate nothing will happen
* */
2022-01-07 19:27:25 +00:00
fun addSubtitles(file: Set<SubtitleData>) {
2022-05-20 18:25:56 +00:00
val currentSubs = _currentSubs.value ?: emptySet()
// Prevent duplicates
val allSubs = (currentSubs + file).distinct().toSet()
// Do not post if there's nothing new
// Posting will refresh subtitles which will in turn
// make the subs to english if previously unselected
if (allSubs != currentSubs) {
2022-05-20 18:25:56 +00:00
_currentSubs.postValue(allSubs)
}
2022-01-07 19:27:25 +00:00
}
2022-01-17 16:17:19 +00:00
private var currentJob: Job? = null
private var currentStampJob: Job? = null
fun loadStamps(duration: Long) {
//currentStampJob?.cancel()
currentStampJob = ioSafe {
val meta = generator?.getCurrent()
val page = (generator as? RepoLinkGenerator?)?.page
if (page != null && meta is ResultEpisode) {
_currentStamps.postValue(listOf())
_currentStamps.postValue(
EpisodeSkip.getStamps(
page,
meta,
duration,
hasNextEpisode() ?: false
)
)
}
}
}
2022-01-17 16:17:19 +00:00
fun loadLinks(clearCache: Boolean = false, isCasting: Boolean = false) {
2022-01-30 22:02:57 +00:00
Log.i(TAG, "loadLinks")
2022-01-17 16:17:19 +00:00
currentJob?.cancel()
2022-08-18 00:54:05 +00:00
currentJob = viewModelScope.launchSafe {
2022-01-17 16:17:19 +00:00
val currentLinks = mutableSetOf<Pair<ExtractorLink?, ExtractorUri?>>()
val currentSubs = mutableSetOf<SubtitleData>()
2022-01-27 20:19:31 +00:00
// clear old data
_currentSubs.postValue(currentSubs)
_currentLinks.postValue(currentLinks)
// load more data
2022-01-17 16:17:19 +00:00
_loadingLinks.postValue(Resource.Loading())
val loadingState = safeApiCall {
generator?.generateLinks(clearCache = clearCache, isCasting = isCasting, {
currentLinks.add(it)
_currentLinks.postValue(currentLinks)
}, {
currentSubs.add(it)
// _currentSubs.postValue(currentSubs) // this causes ConcurrentModificationException, so fuck it
})
}
2022-01-07 19:27:25 +00:00
2022-01-17 16:17:19 +00:00
_loadingLinks.postValue(loadingState)
_currentLinks.postValue(currentLinks)
_currentSubs.postValue(
currentSubs.union(_currentSubs.value ?: emptySet())
)
2022-01-17 16:17:19 +00:00
}
2022-01-07 19:27:25 +00:00
}
}