load episode mirrors

This commit is contained in:
LagradOst 2021-05-20 22:56:21 +02:00
parent c5364ecd9a
commit eef78f8275
4 changed files with 57 additions and 15 deletions

View file

@ -207,7 +207,7 @@ class ShiroProvider : MainAPI() {
override fun loadLinks(data: Any, isCasting: Boolean, callback: (ExtractorLink) -> Unit): Boolean {
if (data is ShiroEpisodes) {
return Vidstream().getUrl(data._id, isCasting) {
return Vidstream().getUrl(data.videos[0].video_id, isCasting) {
callback.invoke(it)
}
}

View file

@ -13,12 +13,16 @@ import com.lagradost.cloudstream3.APIHolder.getApiFromName
import com.lagradost.cloudstream3.utils.ExtractorLink
import kotlinx.android.synthetic.main.result_episode.view.*
const val ACTION_PLAY_EPISODE = 1
const val ACTION_RELOAD_EPISODE = 2
data class EpisodeClickEvent(val action: Int, val data: ResultEpisode)
class EpisodeAdapter(
private var activity: Activity,
var cardList: ArrayList<ResultEpisode>,
val resView: RecyclerView,
val clickCallback: (ResultEpisode) -> Unit
val clickCallback: (EpisodeClickEvent) -> Unit,
) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
@ -44,7 +48,12 @@ class EpisodeAdapter(
}
class CardViewHolder
constructor(itemView: View, _activity: Activity, resView: RecyclerView, clickCallback: (ResultEpisode) -> Unit) : RecyclerView.ViewHolder(itemView) {
constructor(
itemView: View,
_activity: Activity,
resView: RecyclerView,
clickCallback: (EpisodeClickEvent) -> Unit,
) : RecyclerView.ViewHolder(itemView) {
val activity = _activity
val episode_view_procentage: View = itemView.episode_view_procentage
val episode_view_procentage_off: View = itemView.episode_view_procentage_off
@ -68,7 +77,7 @@ class EpisodeAdapter(
setWidth(episode_view_procentage_off, 1 - card.watchProgress)
episode_play.setOnClickListener {
clickCallback.invoke(card)
clickCallback.invoke(EpisodeClickEvent(ACTION_PLAY_EPISODE, card))
}
}
}

View file

@ -56,6 +56,7 @@ class ResultFragment : Fragment() {
}
private lateinit var viewModel: ResultViewModel
private var allEpisodes: HashMap<Int, ArrayList<ExtractorLink>> = HashMap()
override fun onCreateView(
inflater: LayoutInflater,
@ -91,6 +92,10 @@ class ResultFragment : Fragment() {
activity?.onBackPressed()
}
observe(viewModel.allEpisodes) {
allEpisodes = it
}
observe(viewModel.resultResponse) { data ->
when (data) {
is Resource.Success -> {
@ -121,13 +126,39 @@ class ResultFragment : Fragment() {
}
}
val adapter: RecyclerView.Adapter<RecyclerView.ViewHolder>? = activity?.let {
fun playEpisode(data: ArrayList<ExtractorLink>?) {
if (data != null) {
for (d in data) {
println(d)
}
}
}
val adapter: RecyclerView.Adapter<RecyclerView.ViewHolder>? = activity?.let { it ->
EpisodeAdapter(
it,
ArrayList(),
result_episodes,
) {
viewModel.loadEpisode(it)
) { episodeClick ->
val id = episodeClick.data.id
when (episodeClick.action) {
ACTION_PLAY_EPISODE -> {
if (allEpisodes.containsKey(id)) {
playEpisode(allEpisodes[id])
} else {
viewModel.loadEpisode(episodeClick.data) { res ->
if (res is Resource.Success) {
playEpisode(allEpisodes[id])
}
}
}
}
ACTION_RELOAD_EPISODE -> viewModel.loadEpisode(episodeClick.data) { res ->
if (res is Resource.Success) {
playEpisode(allEpisodes[id])
}
}
}
}
}

View file

@ -24,19 +24,21 @@ class ResultViewModel : ViewModel() {
_resultResponse.postValue(data)
}
val allEpisodes : MutableLiveData<HashMap<Int,ArrayList<ExtractorLink>>> = MutableLiveData()
private val _episodeResponse: MutableLiveData<Resource<Boolean>> = MutableLiveData()
val episodeResponse: LiveData<Resource<Boolean>> get() = _episodeResponse
private val _allEpisodes: MutableLiveData<HashMap<Int, ArrayList<ExtractorLink>>> = MutableLiveData(HashMap())
val allEpisodes: LiveData<HashMap<Int, ArrayList<ExtractorLink>>> get() = _allEpisodes
fun loadEpisode(episode: ResultEpisode) = viewModelScope.launch {
if(allEpisodes.value?.contains(episode.id) == true) {
allEpisodes.value?.remove(episode.id)
fun loadEpisode(episode: ResultEpisode, callback: (Resource<Boolean>) -> Unit) = viewModelScope.launch {
if (_allEpisodes.value?.contains(episode.id) == true) {
_allEpisodes.value?.remove(episode.id)
}
val links = ArrayList<ExtractorLink>()
val data = safeApiCall {
getApiFromName(episode.apiName).loadLinks(episode.data, false) { //TODO IMPLEMENT CASTING
allEpisodes.value?.get(episode.id)?.add(it)
links.add(it)
_allEpisodes.value?.set(episode.id, links)
// _allEpisodes.value?.get(episode.id)?.add(it)
}
}
_episodeResponse.postValue(data)
callback.invoke(data)
}
}