Fix: Reload homepage after changing 'preferred media' setting (#221)

This commit is contained in:
Jace 2021-11-06 06:08:47 +08:00 committed by GitHub
parent 67cab252c0
commit d338639290
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 45 deletions

View file

@ -34,6 +34,7 @@ import com.lagradost.cloudstream3.ui.result.START_ACTION_RESUME_LATEST
import com.lagradost.cloudstream3.ui.search.*
import com.lagradost.cloudstream3.ui.search.SearchFragment.Companion.filterSearchResponse
import com.lagradost.cloudstream3.ui.search.SearchHelper.handleSearchClickCallback
import com.lagradost.cloudstream3.utils.AppUtils
import com.lagradost.cloudstream3.utils.AppUtils.loadSearchResult
import com.lagradost.cloudstream3.utils.DataStore.getKey
import com.lagradost.cloudstream3.utils.DataStore.setKey
@ -167,19 +168,9 @@ class HomeFragment : Fragment() {
}
private val apiChangeClickListener = View.OnClickListener { view ->
val allApis = apis.filter { api -> api.hasMainPage }.toMutableList()
var validAPIs = allApis
val settingsManager = PreferenceManager.getDefaultSharedPreferences(context)
val currentPrefMedia = settingsManager.getInt(getString(R.string.preferred_media_settings), 0)
// Filter API depending on preferred media type
if (currentPrefMedia > 0) {
val listEnumAnime = listOf(TvType.Anime, TvType.AnimeMovie, TvType.ONA)
val listEnumMovieTv = listOf(TvType.Movie, TvType.TvSeries, TvType.Cartoon)
val mediaTypeList = if (currentPrefMedia==1) listEnumMovieTv else listEnumAnime
validAPIs = allApis.filter { api -> api.supportedTypes.any { it in mediaTypeList } }.toMutableList()
}
val validAPIs = AppUtils.filterProviderByPreferredMedia(apis, currentPrefMedia).toMutableList()
validAPIs.add(0, randomApi)
validAPIs.add(0, noneApi)
@ -423,9 +414,11 @@ class HomeFragment : Fragment() {
reloadStored()
val apiName = context?.getKey<String>(HOMEPAGE_API)
val settingsManager = PreferenceManager.getDefaultSharedPreferences(context)
val currentPrefMedia = settingsManager.getInt(getString(R.string.preferred_media_settings), 0)
if (homeViewModel.apiName.value != apiName || apiName == null) {
//println("Caught home: " + homeViewModel.apiName.value + " at " + apiName)
homeViewModel.loadAndCancel(apiName, 0)
homeViewModel.loadAndCancel(apiName, currentPrefMedia)
}
}
}

View file

@ -7,25 +7,24 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.lagradost.cloudstream3.APIHolder.apis
import com.lagradost.cloudstream3.APIHolder.getApiFromNameNull
import com.lagradost.cloudstream3.AcraApplication.Companion.context
import com.lagradost.cloudstream3.HomePageResponse
import com.lagradost.cloudstream3.MainAPI
import com.lagradost.cloudstream3.SearchResponse
import com.lagradost.cloudstream3.TvType
import com.lagradost.cloudstream3.mvvm.Resource
import com.lagradost.cloudstream3.ui.APIRepository
import com.lagradost.cloudstream3.ui.APIRepository.Companion.noneApi
import com.lagradost.cloudstream3.ui.APIRepository.Companion.randomApi
import com.lagradost.cloudstream3.ui.WatchType
import com.lagradost.cloudstream3.utils.DOWNLOAD_HEADER_CACHE
import com.lagradost.cloudstream3.utils.*
import com.lagradost.cloudstream3.utils.DataStore.getKey
import com.lagradost.cloudstream3.utils.DataStoreHelper
import com.lagradost.cloudstream3.utils.DataStore.setKey
import com.lagradost.cloudstream3.utils.DataStoreHelper.getAllResumeStateIds
import com.lagradost.cloudstream3.utils.DataStoreHelper.getAllWatchStateIds
import com.lagradost.cloudstream3.utils.DataStoreHelper.getBookmarkedData
import com.lagradost.cloudstream3.utils.DataStoreHelper.getLastWatched
import com.lagradost.cloudstream3.utils.DataStoreHelper.getResultWatchState
import com.lagradost.cloudstream3.utils.DataStoreHelper.getViewPos
import com.lagradost.cloudstream3.utils.VideoDownloadHelper
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
@ -154,16 +153,10 @@ class HomeViewModel : ViewModel() {
if (preferredApiName == noneApi.name)
loadAndCancel(noneApi)
else if(preferredApiName == randomApi.name || api == null) {
val allApis = apis.filter { api -> api.hasMainPage }.toMutableList()
var validAPIs = allApis
if (currentPrefMedia > 0) {
val listEnumAnime = listOf(TvType.Anime, TvType.AnimeMovie, TvType.ONA)
val listEnumMovieTv = listOf(TvType.Movie, TvType.TvSeries, TvType.Cartoon)
val mediaTypeList = if (currentPrefMedia==1) listEnumMovieTv else listEnumAnime
validAPIs = allApis.filter { api -> api.supportedTypes.any { it in mediaTypeList } }.toMutableList()
}
loadAndCancel(validAPIs.random())
var validAPIs = AppUtils.filterProviderByPreferredMedia(apis, currentPrefMedia)
val apiRandom = validAPIs.random()
loadAndCancel(apiRandom)
context?.setKey(HOMEPAGE_API, apiRandom.name)
} else {
loadAndCancel(api)
}

View file

@ -22,6 +22,9 @@ import com.lagradost.cloudstream3.mvvm.logError
import com.lagradost.cloudstream3.network.initRequestClient
import com.lagradost.cloudstream3.ui.APIRepository
import com.lagradost.cloudstream3.ui.subtitles.SubtitlesFragment
import com.lagradost.cloudstream3.utils.AppUtils
import com.lagradost.cloudstream3.utils.DataStore.setKey
import com.lagradost.cloudstream3.utils.HOMEPAGE_API
import com.lagradost.cloudstream3.utils.InAppUpdater.Companion.runAutoUpdate
import com.lagradost.cloudstream3.utils.Qualities
import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showBottomDialog
@ -165,28 +168,28 @@ class SettingsFragment : PreferenceFragmentCompat() {
return@setOnPreferenceClickListener true
}
if (preferedMediaTypePreference != null) {
preferedMediaTypePreference.setOnPreferenceClickListener {
val prefNames = resources.getStringArray(R.array.media_type_pref)
val prefValues = resources.getIntArray(R.array.media_type_pref_values)
val settingsManager = PreferenceManager.getDefaultSharedPreferences(context)
preferedMediaTypePreference.setOnPreferenceClickListener {
val prefNames = resources.getStringArray(R.array.media_type_pref)
val prefValues = resources.getIntArray(R.array.media_type_pref_values)
val settingsManager = PreferenceManager.getDefaultSharedPreferences(context)
val currentPrefMedia =
settingsManager.getInt(getString(R.string.preferred_media_settings), 0)
val currentPrefMedia =
settingsManager.getInt(getString(R.string.preferred_media_settings), 0)
context?.showBottomDialog(
prefNames.toList(),
prefValues.indexOf(currentPrefMedia),
getString(R.string.preferred_media_settings),
true,
{}) {
settingsManager.edit()
.putInt(getString(R.string.preferred_media_settings), prefValues[it])
.apply()
context?.initRequestClient()
}
return@setOnPreferenceClickListener true
context?.showBottomDialog(
prefNames.toList(),
prefValues.indexOf(currentPrefMedia),
getString(R.string.preferred_media_settings),
true,
{}) {
settingsManager.edit()
.putInt(getString(R.string.preferred_media_settings), prefValues[it])
.apply()
val apiRandom = AppUtils.filterProviderByPreferredMedia(apis, prefValues[it]).random()
context?.setKey(HOMEPAGE_API, apiRandom.name)
context?.initRequestClient()
}
return@setOnPreferenceClickListener true
}
allLayoutPreference.setOnPreferenceClickListener {

View file

@ -18,8 +18,10 @@ import com.google.android.gms.cast.framework.CastState
import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.common.GoogleApiAvailability
import com.google.android.gms.common.wrappers.Wrappers
import com.lagradost.cloudstream3.MainAPI
import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.SearchResponse
import com.lagradost.cloudstream3.TvType
import com.lagradost.cloudstream3.ui.result.ResultFragment
import com.lagradost.cloudstream3.utils.UIHelper.navigate
@ -186,4 +188,19 @@ object AppUtils {
}
return currentAudioFocusRequest
}
fun filterProviderByPreferredMedia(apis: ArrayList<MainAPI>, currentPrefMedia: Int): List<MainAPI> {
val allApis = apis.filter { api -> api.hasMainPage }
return if (currentPrefMedia < 1) {
allApis
} else {
// Filter API depending on preferred media type
val listEnumAnime = listOf(TvType.Anime, TvType.AnimeMovie, TvType.ONA)
val listEnumMovieTv = listOf(TvType.Movie, TvType.TvSeries, TvType.Cartoon)
val mediaTypeList = if (currentPrefMedia==1) listEnumMovieTv else listEnumAnime
val filteredAPI = allApis.filter { api -> api.supportedTypes.any { it in mediaTypeList } }
filteredAPI
}
}
}