From d338639290beebf32c58e69df75cef1280f2e623 Mon Sep 17 00:00:00 2001 From: Jace <54625750+Jacekun@users.noreply.github.com> Date: Sat, 6 Nov 2021 06:08:47 +0800 Subject: [PATCH] Fix: Reload homepage after changing 'preferred media' setting (#221) --- .../cloudstream3/ui/home/HomeFragment.kt | 17 +++----- .../cloudstream3/ui/home/HomeViewModel.kt | 21 ++++------ .../ui/settings/SettingsFragment.kt | 41 ++++++++++--------- .../lagradost/cloudstream3/utils/AppUtils.kt | 17 ++++++++ 4 files changed, 51 insertions(+), 45 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeFragment.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeFragment.kt index 3d636e86..a4687103 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeFragment.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeFragment.kt @@ -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(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) } } } \ No newline at end of file diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeViewModel.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeViewModel.kt index 5d573c41..e51cb088 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeViewModel.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeViewModel.kt @@ -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) } diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsFragment.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsFragment.kt index 70ec1af7..34d21f00 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsFragment.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsFragment.kt @@ -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 { diff --git a/app/src/main/java/com/lagradost/cloudstream3/utils/AppUtils.kt b/app/src/main/java/com/lagradost/cloudstream3/utils/AppUtils.kt index facb9b8e..c4fd1b9e 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/utils/AppUtils.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/utils/AppUtils.kt @@ -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, currentPrefMedia: Int): List { + 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 + } + } } \ No newline at end of file