Skip to content

Commit

Permalink
Merge release 7.76 into main (#3203)
Browse files Browse the repository at this point in the history
Co-authored-by: Eduarda Barbosa <[email protected]>
Co-authored-by: Olivier Halligon <[email protected]>
Co-authored-by: Michał Sikora <[email protected]>
Co-authored-by: Eduarda Barbosa <[email protected]>
  • Loading branch information
5 people authored Nov 11, 2024
1 parent 04a454d commit a1b9943
Show file tree
Hide file tree
Showing 32 changed files with 806 additions and 353 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class PodcastManagerTest {
val settings = mock<Settings> {
on { podcastGroupingDefault } doReturn UserSetting.Mock(PodcastGrouping.None, mock())
on { showArchivedDefault } doReturn UserSetting.Mock(false, mock())
on { autoDownloadNewEpisodes } doReturn UserSetting.Mock(true, mock())
on { autoDownloadNewEpisodes } doReturn UserSetting.Mock(Podcast.AUTO_DOWNLOAD_NEW_EPISODES, mock())
on { autoDownloadLimit } doReturn UserSetting.Mock(AutoDownloadLimitSetting.TWO_LATEST_EPISODE, mock())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class OnboardingCreateAccountViewModel @Inject constructor(
when (result) {
is LoginResult.Success -> {
podcastManager.refreshPodcastsAfterSignIn()
experimentProvider.refreshExperiments()
analyticsTracker.refreshMetadata()
experimentProvider.refreshExperiments()
onAccountCreated()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ import au.com.shiftyjelly.pocketcasts.localization.extensions.getStringPluralPod
import au.com.shiftyjelly.pocketcasts.models.entity.Podcast
import au.com.shiftyjelly.pocketcasts.models.type.AutoDownloadLimitSetting
import au.com.shiftyjelly.pocketcasts.preferences.Settings
import au.com.shiftyjelly.pocketcasts.preferences.Settings.Companion.GLOBAL_AUTO_DOWNLOAD_NONE
import au.com.shiftyjelly.pocketcasts.repositories.podcast.PlaylistManager
import au.com.shiftyjelly.pocketcasts.repositories.podcast.PlaylistProperty
import au.com.shiftyjelly.pocketcasts.repositories.podcast.PlaylistUpdateSource
import au.com.shiftyjelly.pocketcasts.repositories.podcast.PodcastManager
import au.com.shiftyjelly.pocketcasts.repositories.podcast.UserPlaylistUpdate
import au.com.shiftyjelly.pocketcasts.settings.viewmodel.AutoDownloadSettingsViewModel
import au.com.shiftyjelly.pocketcasts.settings.viewmodel.toAutoDownloadStatus
import au.com.shiftyjelly.pocketcasts.ui.theme.Theme
import au.com.shiftyjelly.pocketcasts.utils.featureflag.Feature
import au.com.shiftyjelly.pocketcasts.utils.featureflag.FeatureFlag
Expand Down Expand Up @@ -143,6 +145,10 @@ class AutoDownloadSettingsFragment :
settings.bottomInset.collect {
view.updatePadding(bottom = it)
}
viewModel.hasEpisodesWithAutoDownloadEnabled.collect {
setupNewEpisodesToggleStatusCheck()
onNewEpisodesToggleChange(viewModel.getAutoDownloadNewEpisodes())
}
}
}
}
Expand All @@ -163,7 +169,7 @@ class AutoDownloadSettingsFragment :
setOnPreferenceChangeListener { _, newValue ->
if (newValue is Boolean) {
viewModel.onNewEpisodesChange(newValue)
handleNewEpisodesToggle(newValue)
onNewEpisodesToggleChange(newValue.toAutoDownloadStatus())

viewLifecycleOwner.lifecycleScope.launch {
if (newValue && isDeviceRunningOnLowStorage()) lowStorageListener?.showModal(SourceView.AUTO_DOWNLOAD)
Expand Down Expand Up @@ -256,18 +262,28 @@ class AutoDownloadSettingsFragment :
lowStorageListener = null
}

private fun handleNewEpisodesToggle(isEnabled: Boolean) {
private fun onNewEpisodesToggleChange(status: Int) {
lifecycleScope.launch {
updateNewEpisodesSwitch(isEnabled)
if (status == Podcast.AUTO_DOWNLOAD_OFF) {
viewModel.updateAllAutoDownloadStatus(Podcast.AUTO_DOWNLOAD_OFF)
}
updateNewEpisodesPreferencesVisibility(status)
updatePodcastsSummary()
}
}

private fun updateNewEpisodesSwitch(on: Boolean) {
private fun updateNewEpisodesPreferencesVisibility(status: Int) {
val podcastsPreference = podcastsPreference ?: return
val podcastsLimitPreference = podcastsAutoDownloadLimitPreference ?: return
val podcastsCategory = podcastsCategory ?: return
if (on) {

val isAutoDownloadEnabled = if (status == GLOBAL_AUTO_DOWNLOAD_NONE) {
viewModel.hasEpisodesWithAutoDownloadEnabled.value
} else {
status == Podcast.AUTO_DOWNLOAD_NEW_EPISODES
}

if (isAutoDownloadEnabled) {
podcastsCategory.addPreference(podcastsPreference)
podcastsCategory.addPreference(podcastsLimitPreference)
} else {
Expand Down Expand Up @@ -360,13 +376,13 @@ class AutoDownloadSettingsFragment :
updateFiltersSelectedSummary()

upNextPreference.isChecked = viewModel.getAutoDownloadUpNext()
newEpisodesPreference?.isChecked = viewModel.getAutoDownloadNewEpisodes()
setupNewEpisodesToggleStatusCheck()
autoDownloadOnlyDownloadOnWifi.isChecked = viewModel.getAutoDownloadUnmeteredOnly()
autoDownloadOnlyWhenCharging.isChecked = viewModel.getAutoDownloadOnlyWhenCharging()
if (FeatureFlag.isEnabled(Feature.AUTO_DOWNLOAD)) {
newEpisodesPreference?.summary = getString(LR.string.settings_auto_download_new_episodes_description)
}
handleNewEpisodesToggle(viewModel.getAutoDownloadNewEpisodes())
onNewEpisodesToggleChange(viewModel.getAutoDownloadNewEpisodes())
}

private fun countPodcastsAutoDownloading(): Single<Int> {
Expand Down Expand Up @@ -399,6 +415,26 @@ 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
}
}
}

override fun onBackPressed(): Boolean {
if (childFragmentManager.backStackEntryCount > 0) {
childFragmentManager.popBackStack()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
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
import au.com.shiftyjelly.pocketcasts.models.type.AutoDownloadLimitSetting
import au.com.shiftyjelly.pocketcasts.preferences.Settings
import au.com.shiftyjelly.pocketcasts.repositories.download.DownloadManager
Expand All @@ -11,6 +13,8 @@ import dagger.hilt.android.lifecycle.HiltViewModel
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 @@ -24,6 +28,15 @@ 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)
}
}

fun onShown() {
if (!isFragmentChangingConfigurations) {
analyticsTracker.track(AnalyticsEvent.SETTINGS_AUTO_DOWNLOAD_SHOWN)
Expand All @@ -47,7 +60,7 @@ class AutoDownloadSettingsViewModel @Inject constructor(
fun getAutoDownloadNewEpisodes() = settings.autoDownloadNewEpisodes.value

fun onNewEpisodesChange(newValue: Boolean) {
settings.autoDownloadNewEpisodes.set(newValue, updateModifiedAt = true)
settings.autoDownloadNewEpisodes.set(newValue.toAutoDownloadStatus(), updateModifiedAt = true)

analyticsTracker.track(
AnalyticsEvent.SETTINGS_AUTO_DOWNLOAD_NEW_EPISODES_TOGGLED,
Expand Down Expand Up @@ -99,4 +112,13 @@ class AutoDownloadSettingsViewModel @Inject constructor(
}

fun getAutoDownloadOnlyWhenCharging() = settings.autoDownloadOnlyWhenCharging.value

suspend fun updateAllAutoDownloadStatus(status: Int) {
podcastManager.updateAllAutoDownloadStatus(status)
}
}

fun Boolean.toAutoDownloadStatus(): Int = when (this) {
true -> Podcast.AUTO_DOWNLOAD_NEW_EPISODES
false -> Podcast.AUTO_DOWNLOAD_OFF
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ProcessLifecycleOwner
import au.com.shiftyjelly.pocketcasts.analytics.AppLifecycleAnalytics
import au.com.shiftyjelly.pocketcasts.models.entity.Podcast.Companion.AUTO_DOWNLOAD_NEW_EPISODES
import au.com.shiftyjelly.pocketcasts.preferences.Settings
import au.com.shiftyjelly.pocketcasts.repositories.di.ApplicationScope
import au.com.shiftyjelly.pocketcasts.utils.AppPlatform
Expand Down Expand Up @@ -115,7 +116,7 @@ class AppLifecycleObserver constructor(
settings.autoPlayNextEpisodeOnEmpty.set(true, updateModifiedAt = false)

// For new users we want to auto download new episodes by default
settings.autoDownloadNewEpisodes.set(true, updateModifiedAt = false)
settings.autoDownloadNewEpisodes.set(AUTO_DOWNLOAD_NEW_EPISODES, updateModifiedAt = false)
}
}
} else if (previousVersionCode < versionCode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import au.com.shiftyjelly.pocketcasts.analytics.AppLifecycleAnalytics
import au.com.shiftyjelly.pocketcasts.models.entity.Podcast
import au.com.shiftyjelly.pocketcasts.preferences.Settings
import au.com.shiftyjelly.pocketcasts.preferences.UserSetting
import au.com.shiftyjelly.pocketcasts.utils.AppPlatform
Expand Down Expand Up @@ -39,7 +40,7 @@ class AppLifecycleObserverTest {

@Mock private lateinit var autoPlayNextEpisodeSetting: UserSetting<Boolean>

@Mock private lateinit var autoDownloadNewEpisodesSetting: UserSetting<Boolean>
@Mock private lateinit var autoDownloadNewEpisodesSetting: UserSetting<Int>

@Mock private lateinit var useUpNextDarkThemeSetting: UserSetting<Boolean>

Expand Down Expand Up @@ -94,7 +95,7 @@ class AppLifecycleObserverTest {

verify(appLifecycleAnalytics).onNewApplicationInstall()
verify(autoPlayNextEpisodeSetting).set(true, updateModifiedAt = false)
verify(autoDownloadNewEpisodesSetting).set(true, updateModifiedAt = false)
verify(autoDownloadNewEpisodesSetting).set(Podcast.AUTO_DOWNLOAD_NEW_EPISODES, updateModifiedAt = false)
verify(useUpNextDarkThemeSetting).set(false, updateModifiedAt = false)

verify(appLifecycleAnalytics, never()).onApplicationUpgrade(any())
Expand Down
Loading

0 comments on commit a1b9943

Please sign in to comment.