From 50220cedfb319d17ad9782b86c113e5740765732 Mon Sep 17 00:00:00 2001 From: Eduarda Barbosa Date: Tue, 10 Dec 2024 14:20:14 -0300 Subject: [PATCH] refactor the way we identify when has at least one podcast with auto download toggle enabled --- .../settings/AutoDownloadSettingsFragment.kt | 44 +++++++++---------- .../AutoDownloadSettingsViewModel.kt | 12 +---- 2 files changed, 23 insertions(+), 33 deletions(-) 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 87f080f1c8a..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 @@ -171,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) @@ -276,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) @@ -290,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) + } } } @@ -414,17 +419,12 @@ class AutoDownloadSettingsFragment : ) } - @SuppressLint("CheckResult") private fun setupNewEpisodesToggleStatusCheck() { - viewModel.countPodcastsAutoDownloading() - .map { it > 0 } - .subscribeBy( - onError = { Timber.e(it) }, - onSuccess = { on -> - onNewEpisodesToggleChange(on.toAutoDownloadStatus()) - newEpisodesPreference?.isChecked = on - }, - ) + lifecycleScope.launch { + val newValue = viewModel.hasEpisodesWithAutoDownloadEnabled() + newEpisodesPreference?.isChecked = newValue + onNewEpisodesToggleChange(newValue.toAutoDownloadStatus()) + } } private fun setupOnFollowPodcastToggleStatusCheck() { 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 66d790ec757..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 @@ -16,8 +15,6 @@ 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 @@ -31,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) {