2021-04-30 17:20:15 +00:00
|
|
|
package com.lagradost.cloudstream3.ui.home
|
|
|
|
|
|
|
|
import androidx.lifecycle.LiveData
|
|
|
|
import androidx.lifecycle.MutableLiveData
|
|
|
|
import androidx.lifecycle.ViewModel
|
2021-07-29 00:19:42 +00:00
|
|
|
import androidx.lifecycle.viewModelScope
|
|
|
|
import com.lagradost.cloudstream3.APIHolder.apis
|
|
|
|
import com.lagradost.cloudstream3.APIHolder.getApiFromNameNull
|
|
|
|
import com.lagradost.cloudstream3.HomePageResponse
|
2021-07-29 15:16:08 +00:00
|
|
|
import com.lagradost.cloudstream3.MainAPI
|
2021-07-29 00:19:42 +00:00
|
|
|
import com.lagradost.cloudstream3.mvvm.Resource
|
|
|
|
import com.lagradost.cloudstream3.ui.APIRepository
|
|
|
|
import kotlinx.coroutines.launch
|
2021-04-30 17:20:15 +00:00
|
|
|
|
|
|
|
class HomeViewModel : ViewModel() {
|
2021-07-29 00:19:42 +00:00
|
|
|
var repo: APIRepository? = null
|
2021-04-30 17:20:15 +00:00
|
|
|
|
2021-07-29 00:19:42 +00:00
|
|
|
private val _apiName = MutableLiveData<String>()
|
|
|
|
val apiName: LiveData<String> = _apiName
|
|
|
|
|
|
|
|
private val _page = MutableLiveData<Resource<HomePageResponse>>()
|
|
|
|
val page: LiveData<Resource<HomePageResponse>> = _page
|
|
|
|
|
|
|
|
private fun autoloadRepo(): APIRepository {
|
|
|
|
return APIRepository(apis.first { it.hasMainPage })
|
|
|
|
}
|
|
|
|
|
2021-07-29 15:16:08 +00:00
|
|
|
fun load(api : MainAPI?) = viewModelScope.launch {
|
2021-07-29 00:19:42 +00:00
|
|
|
repo = if (api?.hasMainPage == true) {
|
|
|
|
APIRepository(api)
|
|
|
|
} else {
|
|
|
|
autoloadRepo()
|
|
|
|
}
|
2021-07-29 15:44:16 +00:00
|
|
|
_apiName.postValue(repo?.name)
|
2021-07-29 00:19:42 +00:00
|
|
|
_page.postValue(Resource.Loading())
|
|
|
|
_page.postValue(repo?.getMainPage())
|
2021-04-30 17:20:15 +00:00
|
|
|
}
|
2021-07-29 15:16:08 +00:00
|
|
|
|
|
|
|
fun load(preferredApiName: String?) = viewModelScope.launch {
|
|
|
|
val api = getApiFromNameNull(preferredApiName)
|
|
|
|
load(api)
|
|
|
|
}
|
2021-04-30 17:20:15 +00:00
|
|
|
}
|