Skip to content

Commit

Permalink
Fix: Disabled global Auto download new episodes setting overrides pod…
Browse files Browse the repository at this point in the history
…cast specifc one (#3342)
  • Loading branch information
mebarbosa authored Dec 11, 2024
1 parent c0bbc82 commit ccfa7c4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 63 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -154,10 +151,6 @@ class AutoDownloadSettingsFragment :
settings.bottomInset.collect {
view.updatePadding(bottom = it)
}
viewModel.hasEpisodesWithAutoDownloadEnabled.collect {
setupNewEpisodesToggleStatusCheck()
onNewEpisodesToggleChange(viewModel.getAutoDownloadNewEpisodes())
}
}
}
}
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
}
}

Expand Down Expand Up @@ -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<Int> {
return podcastManager.countDownloadStatusRxSingle(Podcast.AUTO_DOWNLOAD_NEW_EPISODES)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
}

private fun countPodcasts(): Single<Int> {
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) ->
Expand All @@ -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())
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -28,14 +28,7 @@ class AutoDownloadSettingsViewModel @Inject constructor(
override val coroutineContext = Dispatchers.Default
private var isFragmentChangingConfigurations: Boolean = false

private var _hasEpisodesWithAutoDownloadEnabled: MutableStateFlow<Boolean> = MutableStateFlow(false)
val hasEpisodesWithAutoDownloadEnabled: StateFlow<Boolean> = _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) {
Expand All @@ -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
Expand Down Expand Up @@ -125,6 +116,13 @@ class AutoDownloadSettingsViewModel @Inject constructor(
suspend fun updateAllAutoDownloadStatus(status: Int) {
podcastManager.updateAllAutoDownloadStatus(status)
}

fun countPodcastsAutoDownloading(): Single<Int> = podcastManager.countDownloadStatusRxSingle(Podcast.AUTO_DOWNLOAD_NEW_EPISODES)
.subscribeOn(Schedulers.io())

fun countPodcasts(): Single<Int> = podcastManager.countSubscribedRxSingle()
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
}

fun Boolean.toAutoDownloadStatus(): Int = when (this) {
Expand Down

0 comments on commit ccfa7c4

Please sign in to comment.