This commit is contained in:
LagradOst 2023-09-17 20:35:01 +02:00
parent 6957a8f95d
commit a82059cb57
1 changed files with 33 additions and 12 deletions

View File

@ -7,6 +7,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.lagradost.cloudstream3.mvvm.Resource import com.lagradost.cloudstream3.mvvm.Resource
import com.lagradost.cloudstream3.mvvm.launchSafe import com.lagradost.cloudstream3.mvvm.launchSafe
import com.lagradost.cloudstream3.mvvm.logError
import com.lagradost.cloudstream3.mvvm.normalSafeApiCall import com.lagradost.cloudstream3.mvvm.normalSafeApiCall
import com.lagradost.cloudstream3.mvvm.safeApiCall import com.lagradost.cloudstream3.mvvm.safeApiCall
import com.lagradost.cloudstream3.ui.result.ResultEpisode import com.lagradost.cloudstream3.ui.result.ResultEpisode
@ -15,6 +16,7 @@ import com.lagradost.cloudstream3.utils.EpisodeSkip
import com.lagradost.cloudstream3.utils.ExtractorLink import com.lagradost.cloudstream3.utils.ExtractorLink
import com.lagradost.cloudstream3.utils.ExtractorUri import com.lagradost.cloudstream3.utils.ExtractorUri
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
class PlayerGeneratorViewModel : ViewModel() { class PlayerGeneratorViewModel : ViewModel() {
companion object { companion object {
@ -38,6 +40,11 @@ class PlayerGeneratorViewModel : ViewModel() {
private val _currentSubtitleYear = MutableLiveData<Int?>(null) private val _currentSubtitleYear = MutableLiveData<Int?>(null)
val currentSubtitleYear: LiveData<Int?> = _currentSubtitleYear val currentSubtitleYear: LiveData<Int?> = _currentSubtitleYear
/**
* Save the Episode ID to prevent starting multiple link loading Jobs when preloading links.
*/
private var currentLoadingEpisodeId: Int? = null
fun setSubtitleYear(year: Int?) { fun setSubtitleYear(year: Int?) {
_currentSubtitleYear.postValue(year) _currentSubtitleYear.postValue(year)
} }
@ -72,9 +79,16 @@ class PlayerGeneratorViewModel : ViewModel() {
} }
fun preLoadNextLinks() { fun preLoadNextLinks() {
val id = getId()
// Do not preload if already loading
if (id == currentLoadingEpisodeId) return
Log.i(TAG, "preLoadNextLinks") Log.i(TAG, "preLoadNextLinks")
currentJob?.cancel() currentJob?.cancel()
currentJob = viewModelScope.launchSafe { currentLoadingEpisodeId = id
currentJob = viewModelScope.launch {
try {
if (generator?.hasCache == true && generator?.hasNext() == true) { if (generator?.hasCache == true && generator?.hasNext() == true) {
safeApiCall { safeApiCall {
generator?.generateLinks( generator?.generateLinks(
@ -86,6 +100,13 @@ class PlayerGeneratorViewModel : ViewModel() {
) )
} }
} }
} catch (t: Throwable) {
logError(t)
} finally {
if (currentLoadingEpisodeId == id) {
currentLoadingEpisodeId = null
}
}
} }
} }
@ -162,7 +183,7 @@ class PlayerGeneratorViewModel : ViewModel() {
// load more data // load more data
_loadingLinks.postValue(Resource.Loading()) _loadingLinks.postValue(Resource.Loading())
val loadingState = safeApiCall { val loadingState = safeApiCall {
generator?.generateLinks(type = type,clearCache = clearCache, callback = { generator?.generateLinks(type = type, clearCache = clearCache, callback = {
currentLinks.add(it) currentLinks.add(it)
// Clone to prevent ConcurrentModificationException // Clone to prevent ConcurrentModificationException
normalSafeApiCall { normalSafeApiCall {