Fixed sub/dub preference applying inside info page

This commit is contained in:
Blatzar 2022-05-22 00:10:33 +02:00
parent 0e69a4ff37
commit 022aebd376
2 changed files with 54 additions and 44 deletions

View file

@ -59,7 +59,6 @@ import com.lagradost.cloudstream3.ui.player.SubtitleData
import com.lagradost.cloudstream3.ui.quicksearch.QuickSearchFragment import com.lagradost.cloudstream3.ui.quicksearch.QuickSearchFragment
import com.lagradost.cloudstream3.ui.search.SearchAdapter import com.lagradost.cloudstream3.ui.search.SearchAdapter
import com.lagradost.cloudstream3.ui.search.SearchHelper import com.lagradost.cloudstream3.ui.search.SearchHelper
import com.lagradost.cloudstream3.ui.settings.SettingsFragment
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTrueTvSettings import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTrueTvSettings
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTvSettings import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTvSettings
import com.lagradost.cloudstream3.ui.subtitles.SubtitlesFragment.Companion.getDownloadSubsLanguageISO639_1 import com.lagradost.cloudstream3.ui.subtitles.SubtitlesFragment.Companion.getDownloadSubsLanguageISO639_1
@ -1584,14 +1583,14 @@ class ResultFragment : Fragment(), PanelsChildGestureRegionObserver.GestureRegio
result_dub_select?.text = status.toString() result_dub_select?.text = status.toString()
} }
val preferDub = context?.getApiDubstatusSettings()?.all { it == DubStatus.Dubbed } == true // val preferDub = context?.getApiDubstatusSettings()?.all { it == DubStatus.Dubbed } == true
observe(viewModel.dubSubSelections) { range -> observe(viewModel.dubSubSelections) { range ->
dubRange = range dubRange = range
if (preferDub && dubRange?.contains(DubStatus.Dubbed) == true) { // if (preferDub && dubRange?.contains(DubStatus.Dubbed) == true) {
viewModel.changeDubStatus(DubStatus.Dubbed) // viewModel.changeDubStatus(DubStatus.Dubbed)
} // }
result_dub_select?.visibility = if (range.size <= 1) GONE else VISIBLE result_dub_select?.visibility = if (range.size <= 1) GONE else VISIBLE

View file

@ -6,9 +6,11 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.lagradost.cloudstream3.* import com.lagradost.cloudstream3.*
import com.lagradost.cloudstream3.APIHolder.getApiDubstatusSettings
import com.lagradost.cloudstream3.APIHolder.getApiFromNameNull import com.lagradost.cloudstream3.APIHolder.getApiFromNameNull
import com.lagradost.cloudstream3.APIHolder.getApiFromUrlNull import com.lagradost.cloudstream3.APIHolder.getApiFromUrlNull
import com.lagradost.cloudstream3.APIHolder.getId import com.lagradost.cloudstream3.APIHolder.getId
import com.lagradost.cloudstream3.AcraApplication.Companion.context
import com.lagradost.cloudstream3.AcraApplication.Companion.setKey import com.lagradost.cloudstream3.AcraApplication.Companion.setKey
import com.lagradost.cloudstream3.LoadResponse.Companion.addTrailer import com.lagradost.cloudstream3.LoadResponse.Companion.addTrailer
import com.lagradost.cloudstream3.animeproviders.GogoanimeProvider import com.lagradost.cloudstream3.animeproviders.GogoanimeProvider
@ -25,7 +27,6 @@ import com.lagradost.cloudstream3.ui.player.SubtitleData
import com.lagradost.cloudstream3.utils.DOWNLOAD_HEADER_CACHE import com.lagradost.cloudstream3.utils.DOWNLOAD_HEADER_CACHE
import com.lagradost.cloudstream3.utils.DataStoreHelper import com.lagradost.cloudstream3.utils.DataStoreHelper
import com.lagradost.cloudstream3.utils.DataStoreHelper.getBookmarkedData import com.lagradost.cloudstream3.utils.DataStoreHelper.getBookmarkedData
import com.lagradost.cloudstream3.utils.DataStoreHelper.getDub
import com.lagradost.cloudstream3.utils.DataStoreHelper.getResultSeason import com.lagradost.cloudstream3.utils.DataStoreHelper.getResultSeason
import com.lagradost.cloudstream3.utils.DataStoreHelper.getResultWatchState import com.lagradost.cloudstream3.utils.DataStoreHelper.getResultWatchState
import com.lagradost.cloudstream3.utils.DataStoreHelper.getViewPos import com.lagradost.cloudstream3.utils.DataStoreHelper.getViewPos
@ -355,9 +356,9 @@ class ResultViewModel : ViewModel() {
when (data) { when (data) {
is Resource.Success -> { is Resource.Success -> {
val d = applyMeta(data.value, lastMeta) val loadResponse = applyMeta(data.value, lastMeta)
page.postValue(d) page.postValue(loadResponse)
val mainId = d.getId() val mainId = loadResponse.getId()
id.postValue(mainId) id.postValue(mainId)
loadWatchStatus(mainId) loadWatchStatus(mainId)
@ -367,30 +368,39 @@ class ResultViewModel : ViewModel() {
VideoDownloadHelper.DownloadHeaderCached( VideoDownloadHelper.DownloadHeaderCached(
apiName, apiName,
validUrl, validUrl,
d.type, loadResponse.type,
d.name, loadResponse.name,
d.posterUrl, loadResponse.posterUrl,
mainId, mainId,
System.currentTimeMillis(), System.currentTimeMillis(),
) )
) )
when (d) { when (loadResponse) {
is AnimeLoadResponse -> { is AnimeLoadResponse -> {
if (d.episodes.isEmpty()) { if (loadResponse.episodes.isEmpty()) {
_dubSubEpisodes.postValue(emptyMap()) _dubSubEpisodes.postValue(emptyMap())
return@launch return@launch
} }
val status = getDub(mainId) // val status = getDub(mainId)
val statuses = d.episodes.map { it.key } val statuses = loadResponse.episodes.map { it.key }
val dubStatus = if (statuses.contains(status)) status else statuses.first()
// Extremely bruh to have to take in context here, but I'm not sure how to do this in a better way :(
val preferDub = context?.getApiDubstatusSettings()
?.contains(DubStatus.Dubbed) == true
// 3 statements because there can be only dub even if you do not prefer it.
val dubStatus =
if (preferDub && statuses.contains(DubStatus.Dubbed)) DubStatus.Dubbed
else if (!preferDub && statuses.contains(DubStatus.Subbed)) DubStatus.Subbed
else statuses.first()
val fillerEpisodes = val fillerEpisodes =
if (showFillers) safeApiCall { getFillerEpisodes(d.name) } else null if (showFillers) safeApiCall { getFillerEpisodes(loadResponse.name) } else null
val existingEpisodes = HashSet<Int>() val existingEpisodes = HashSet<Int>()
val res = d.episodes.map { ep -> val res = loadResponse.episodes.map { ep ->
val episodes = ArrayList<ResultEpisode>() val episodes = ArrayList<ResultEpisode>()
val idIndex = ep.key.id val idIndex = ep.key.id
for ((index, i) in ep.value.withIndex()) { for ((index, i) in ep.value.withIndex()) {
@ -399,7 +409,7 @@ class ResultViewModel : ViewModel() {
if (!existingEpisodes.contains(episode)) { if (!existingEpisodes.contains(episode)) {
existingEpisodes.add(id) existingEpisodes.add(id)
episodes.add(buildResultEpisode( episodes.add(buildResultEpisode(
d.name, loadResponse.name,
filterName(i.name), filterName(i.name),
i.posterUrl, i.posterUrl,
episode, episode,
@ -413,7 +423,7 @@ class ResultViewModel : ViewModel() {
if (fillerEpisodes is Resource.Success) fillerEpisodes.value?.let { if (fillerEpisodes is Resource.Success) fillerEpisodes.value?.let {
it.contains(episode) && it[episode] == true it.contains(episode) && it[episode] == true
} ?: false else false, } ?: false else false,
d.type, loadResponse.type,
mainId mainId
)) ))
} }
@ -428,34 +438,35 @@ class ResultViewModel : ViewModel() {
updateEpisodes(mainId, episodes, -1) updateEpisodes(mainId, episodes, -1)
} }
_dubStatus.postValue(dubStatus) _dubStatus.postValue(dubStatus)
_dubSubSelections.postValue(d.episodes.keys) _dubSubSelections.postValue(loadResponse.episodes.keys)
} }
is TvSeriesLoadResponse -> { is TvSeriesLoadResponse -> {
val episodes = ArrayList<ResultEpisode>() val episodes = ArrayList<ResultEpisode>()
val existingEpisodes = HashSet<Int>() val existingEpisodes = HashSet<Int>()
for ((index, i) in d.episodes.sortedBy { for ((index, episode) in loadResponse.episodes.sortedBy {
(it.season?.times(10000) ?: 0) + (it.episode ?: 0) (it.season?.times(10000) ?: 0) + (it.episode ?: 0)
}.withIndex()) { }.withIndex()) {
val episode = i.episode ?: (index + 1) val episodeIndex = episode.episode ?: (index + 1)
val id = mainId + (i.season?.times(100000) ?: 0) + episode + 1 val id =
mainId + (episode.season?.times(100000) ?: 0) + episodeIndex + 1
if (!existingEpisodes.contains(id)) { if (!existingEpisodes.contains(id)) {
existingEpisodes.add(id) existingEpisodes.add(id)
episodes.add( episodes.add(
buildResultEpisode( buildResultEpisode(
d.name, loadResponse.name,
filterName(i.name), filterName(episode.name),
i.posterUrl, episode.posterUrl,
episode, episodeIndex,
i.season, episode.season,
i.data, episode.data,
apiName, apiName,
id, id,
index, index,
i.rating, episode.rating,
i.description, episode.description,
null, null,
d.type, loadResponse.type,
mainId mainId
) )
) )
@ -465,19 +476,19 @@ class ResultViewModel : ViewModel() {
} }
is MovieLoadResponse -> { is MovieLoadResponse -> {
buildResultEpisode( buildResultEpisode(
d.name, loadResponse.name,
d.name, loadResponse.name,
null, null,
0, 0,
null, null,
d.dataUrl, loadResponse.dataUrl,
d.apiName, loadResponse.apiName,
(mainId), // HAS SAME ID (mainId), // HAS SAME ID
0, 0,
null, null,
null, null,
null, null,
d.type, loadResponse.type,
mainId mainId
).let { ).let {
updateEpisodes(mainId, listOf(it), -1) updateEpisodes(mainId, listOf(it), -1)
@ -487,19 +498,19 @@ class ResultViewModel : ViewModel() {
updateEpisodes( updateEpisodes(
mainId, listOf( mainId, listOf(
buildResultEpisode( buildResultEpisode(
d.name, loadResponse.name,
d.name, loadResponse.name,
null, null,
0, 0,
null, null,
d.torrent ?: d.magnet ?: "", loadResponse.torrent ?: loadResponse.magnet ?: "",
d.apiName, loadResponse.apiName,
(mainId), // HAS SAME ID (mainId), // HAS SAME ID
0, 0,
null, null,
null, null,
null, null,
d.type, loadResponse.type,
mainId mainId
) )
), -1 ), -1