Skip to content

Commit

Permalink
refactor the way we identify when has at least one podcast with auto …
Browse files Browse the repository at this point in the history
…download toggle enabled
  • Loading branch information
mebarbosa committed Dec 10, 2024
1 parent 3592ea0 commit 50220ce
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
}
}

Expand Down Expand Up @@ -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() {
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 @@ -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
Expand All @@ -31,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 Down

0 comments on commit 50220ce

Please sign in to comment.