diff --git a/CHANGELOG.md b/CHANGELOG.md index e244768adec..63c86a4b705 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ 7.80 ----- - +* Bug Fixes + * Fix global auto download setting was incorrectly overriding the podcast auto download setting + ([#3342](https://github.com/Automattic/pocket-casts-android/pull/3342)) 7.79 ----- diff --git a/modules/features/settings/src/main/java/au/com/shiftyjelly/pocketcasts/settings/AutoDownloadSettingsFragment.kt b/modules/features/settings/src/main/java/au/com/shiftyjelly/pocketcasts/settings/AutoDownloadSettingsFragment.kt index 03e33e21996..a2d7fb457c5 100644 --- a/modules/features/settings/src/main/java/au/com/shiftyjelly/pocketcasts/settings/AutoDownloadSettingsFragment.kt +++ b/modules/features/settings/src/main/java/au/com/shiftyjelly/pocketcasts/settings/AutoDownloadSettingsFragment.kt @@ -46,11 +46,8 @@ import au.com.shiftyjelly.pocketcasts.views.fragments.PodcastSelectFragmentSourc import au.com.shiftyjelly.pocketcasts.views.helper.HasBackstack import au.com.shiftyjelly.pocketcasts.views.helper.NavigationIcon.BackArrow import dagger.hilt.android.AndroidEntryPoint -import io.reactivex.Single -import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.rxkotlin.subscribeBy import io.reactivex.rxkotlin.zipWith -import io.reactivex.schedulers.Schedulers import javax.inject.Inject import kotlin.coroutines.CoroutineContext import kotlinx.coroutines.CoroutineScope @@ -154,10 +151,6 @@ class AutoDownloadSettingsFragment : settings.bottomInset.collect { view.updatePadding(bottom = it) } - viewModel.hasEpisodesWithAutoDownloadEnabled.collect { - setupNewEpisodesToggleStatusCheck() - onNewEpisodesToggleChange(viewModel.getAutoDownloadNewEpisodes()) - } } } } @@ -178,7 +171,7 @@ class AutoDownloadSettingsFragment : setOnPreferenceChangeListener { _, newValue -> if (newValue is Boolean) { viewModel.onNewEpisodesChange(newValue) - onNewEpisodesToggleChange(newValue.toAutoDownloadStatus()) + onNewEpisodesToggleChange(newValue.toAutoDownloadStatus(), isUserChange = true) viewLifecycleOwner.lifecycleScope.launch { val lowStorageDialogPresenter = LowStorageDialogPresenter(requireContext(), analyticsTracker, settings) @@ -283,9 +276,12 @@ class AutoDownloadSettingsFragment : updateView() } - private fun onNewEpisodesToggleChange(status: Int) { + // The isUserChange parameter is used to identify when the user manually updates + // the global auto-download toggle. This ensures that we will toggle off all podcasts + // only when the user toggles the global auto-download toggle off. + private fun onNewEpisodesToggleChange(status: Int, isUserChange: Boolean = false) { lifecycleScope.launch { - if (status == Podcast.AUTO_DOWNLOAD_OFF) { + if (status == Podcast.AUTO_DOWNLOAD_OFF && isUserChange) { viewModel.updateAllAutoDownloadStatus(Podcast.AUTO_DOWNLOAD_OFF) } updateNewEpisodesPreferencesVisibility(status) @@ -297,16 +293,18 @@ class AutoDownloadSettingsFragment : val podcastsPreference = podcastsPreference ?: return val podcastsCategory = podcastsCategory ?: return - val isAutoDownloadEnabled = if (status == GLOBAL_AUTO_DOWNLOAD_NONE) { - viewModel.hasEpisodesWithAutoDownloadEnabled.value - } else { - status == Podcast.AUTO_DOWNLOAD_NEW_EPISODES - } + lifecycleScope.launch { + val isAutoDownloadEnabled = if (status == GLOBAL_AUTO_DOWNLOAD_NONE) { + viewModel.hasEpisodesWithAutoDownloadEnabled() + } else { + status == Podcast.AUTO_DOWNLOAD_NEW_EPISODES + } - if (isAutoDownloadEnabled) { - podcastsCategory.addPreference(podcastsPreference) - } else { - podcastsCategory.removePreference(podcastsPreference) + if (isAutoDownloadEnabled) { + podcastsCategory.addPreference(podcastsPreference) + } else { + podcastsCategory.removePreference(podcastsPreference) + } } } @@ -401,24 +399,11 @@ class AutoDownloadSettingsFragment : if (FeatureFlag.isEnabled(Feature.AUTO_DOWNLOAD)) { newEpisodesPreference?.summary = getString(LR.string.settings_auto_download_new_episodes_description) } - onNewEpisodesToggleChange(viewModel.getAutoDownloadNewEpisodes()) - } - - private fun countPodcastsAutoDownloading(): Single { - return podcastManager.countDownloadStatusRxSingle(Podcast.AUTO_DOWNLOAD_NEW_EPISODES) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - } - - private fun countPodcasts(): Single { - return podcastManager.countSubscribedRxSingle() - .observeOn(AndroidSchedulers.mainThread()) - .subscribeOn(Schedulers.io()) } @SuppressLint("CheckResult") private fun updatePodcastsSummary() { - countPodcastsAutoDownloading().zipWith(countPodcasts()) + viewModel.countPodcastsAutoDownloading().zipWith(viewModel.countPodcasts()) .subscribeBy( onError = { Timber.e(it) }, onSuccess = { (autoDownloadingCount, allCount) -> @@ -435,22 +420,10 @@ class AutoDownloadSettingsFragment : } private fun setupNewEpisodesToggleStatusCheck() { - val value = viewModel.getAutoDownloadNewEpisodes() - when (value) { - Podcast.AUTO_DOWNLOAD_OFF -> { - newEpisodesPreference?.isChecked = false - } - - Podcast.AUTO_DOWNLOAD_NEW_EPISODES -> { - newEpisodesPreference?.isChecked = true - } - - else -> { - // This is the case where users have not set this toggle yet. - // In this case, we check if the user has auto download enabled for any podcast - // so we can enable the global auto-download status. - newEpisodesPreference?.isChecked = viewModel.hasEpisodesWithAutoDownloadEnabled.value - } + lifecycleScope.launch { + val newValue = viewModel.hasEpisodesWithAutoDownloadEnabled() + newEpisodesPreference?.isChecked = newValue + onNewEpisodesToggleChange(newValue.toAutoDownloadStatus()) } } diff --git a/modules/features/settings/src/main/java/au/com/shiftyjelly/pocketcasts/settings/viewmodel/AutoDownloadSettingsViewModel.kt b/modules/features/settings/src/main/java/au/com/shiftyjelly/pocketcasts/settings/viewmodel/AutoDownloadSettingsViewModel.kt index 016749d9139..18b0b8ed2ac 100644 --- a/modules/features/settings/src/main/java/au/com/shiftyjelly/pocketcasts/settings/viewmodel/AutoDownloadSettingsViewModel.kt +++ b/modules/features/settings/src/main/java/au/com/shiftyjelly/pocketcasts/settings/viewmodel/AutoDownloadSettingsViewModel.kt @@ -1,7 +1,6 @@ package au.com.shiftyjelly.pocketcasts.settings.viewmodel import androidx.lifecycle.ViewModel -import androidx.lifecycle.viewModelScope import au.com.shiftyjelly.pocketcasts.analytics.AnalyticsEvent import au.com.shiftyjelly.pocketcasts.analytics.AnalyticsTracker import au.com.shiftyjelly.pocketcasts.models.entity.Podcast @@ -10,11 +9,12 @@ import au.com.shiftyjelly.pocketcasts.preferences.Settings import au.com.shiftyjelly.pocketcasts.repositories.download.DownloadManager import au.com.shiftyjelly.pocketcasts.repositories.podcast.PodcastManager import dagger.hilt.android.lifecycle.HiltViewModel +import io.reactivex.Single +import io.reactivex.android.schedulers.AndroidSchedulers +import io.reactivex.schedulers.Schedulers import javax.inject.Inject import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.flow.MutableStateFlow -import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.launch @HiltViewModel @@ -28,14 +28,7 @@ class AutoDownloadSettingsViewModel @Inject constructor( override val coroutineContext = Dispatchers.Default private var isFragmentChangingConfigurations: Boolean = false - private var _hasEpisodesWithAutoDownloadEnabled: MutableStateFlow = MutableStateFlow(false) - val hasEpisodesWithAutoDownloadEnabled: StateFlow = _hasEpisodesWithAutoDownloadEnabled - - init { - viewModelScope.launch(Dispatchers.IO) { - _hasEpisodesWithAutoDownloadEnabled.value = podcastManager.hasEpisodesWithAutoDownloadStatus(Podcast.AUTO_DOWNLOAD_NEW_EPISODES) - } - } + suspend fun hasEpisodesWithAutoDownloadEnabled() = podcastManager.hasEpisodesWithAutoDownloadStatus(Podcast.AUTO_DOWNLOAD_NEW_EPISODES) fun onShown() { if (!isFragmentChangingConfigurations) { @@ -57,8 +50,6 @@ class AutoDownloadSettingsViewModel @Inject constructor( fun getAutoDownloadUpNext() = settings.autoDownloadUpNext.value - fun getAutoDownloadNewEpisodes() = settings.autoDownloadNewEpisodes.value - fun getLimitDownload() = settings.autoDownloadLimit.value fun isAutoDownloadOnFollowPodcastEnabled() = settings.autoDownloadOnFollowPodcast.value @@ -125,6 +116,13 @@ class AutoDownloadSettingsViewModel @Inject constructor( suspend fun updateAllAutoDownloadStatus(status: Int) { podcastManager.updateAllAutoDownloadStatus(status) } + + fun countPodcastsAutoDownloading(): Single = podcastManager.countDownloadStatusRxSingle(Podcast.AUTO_DOWNLOAD_NEW_EPISODES) + .subscribeOn(Schedulers.io()) + + fun countPodcasts(): Single = podcastManager.countSubscribedRxSingle() + .observeOn(AndroidSchedulers.mainThread()) + .subscribeOn(Schedulers.io()) } fun Boolean.toAutoDownloadStatus(): Int = when (this) {