From 2c2dd99bcc01c4735e02d8ae3b99b3b0e84cd7d5 Mon Sep 17 00:00:00 2001 From: Philip Simpson Date: Wed, 5 Feb 2025 17:53:46 +1030 Subject: [PATCH] Fix missing episodes with unfollowed podcasts --- .../podcast/PodcastManagerImpl.kt | 20 ++++++++++++++----- .../repositories/sync/PodcastRefresherImpl.kt | 3 ++- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/podcast/PodcastManagerImpl.kt b/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/podcast/PodcastManagerImpl.kt index 0602881f6a4..cff60cef939 100644 --- a/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/podcast/PodcastManagerImpl.kt +++ b/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/podcast/PodcastManagerImpl.kt @@ -37,7 +37,7 @@ import io.reactivex.Flowable import io.reactivex.Maybe import io.reactivex.Single import io.reactivex.schedulers.Schedulers -import java.util.Calendar +import java.util.concurrent.TimeUnit import javax.inject.Inject import kotlin.coroutines.CoroutineContext import kotlinx.coroutines.CoroutineScope @@ -224,18 +224,23 @@ class PodcastManagerImpl @Inject constructor( // we don't delete podcasts added to the phone in the last week. This is to prevent stuff you just leave open in discover from being removed val addedDate = podcast.addedDate - val calendar = Calendar.getInstance() - calendar.add(Calendar.DAY_OF_YEAR, -7) - val oneWeekAgo = calendar.time - if (addedDate != null && addedDate > oneWeekAgo) { + val oneWeekAgoMs = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(7) + if (addedDate != null && addedDate.time > oneWeekAgoMs) { return false } // podcasts can be deleted if all of the episodes are haven't been interacted with val episodes = episodeManager.findEpisodesByPodcastOrderedBlocking(podcast) var podcastHasChangedEpisodes = false + var latestPlaybackInteraction = 0L val deleteEpisodes = mutableListOf() for (episode in episodes) { + // find the latest playback interaction + episode.lastPlaybackInteraction?.let { interaction -> + if (interaction > latestPlaybackInteraction) { + latestPlaybackInteraction = interaction + } + } if (episodeManager.userHasInteractedWithEpisode(episode, playbackManager)) { podcastHasChangedEpisodes = true continue @@ -243,6 +248,11 @@ class PodcastManagerImpl @Inject constructor( // bulk delete or it takes 10 seconds on a large podcast deleteEpisodes.add(episode) } + // don't remove episodes or the podcast if the latest playback interaction was less than a month ago, + val oneMonthAgoMs = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(30) + if (latestPlaybackInteraction > oneMonthAgoMs) { + return false + } if (deleteEpisodes.isNotEmpty()) { episodeManager.deleteEpisodesWithoutSyncBlocking(deleteEpisodes, playbackManager) } diff --git a/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/sync/PodcastRefresherImpl.kt b/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/sync/PodcastRefresherImpl.kt index fe03121f780..f44ebd672ed 100644 --- a/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/sync/PodcastRefresherImpl.kt +++ b/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/sync/PodcastRefresherImpl.kt @@ -28,7 +28,8 @@ class PodcastRefresherImpl @Inject constructor( override suspend fun refreshPodcast(existingPodcast: Podcast, playbackManager: PlaybackManager) { try { val podcastResponse = cacheServiceManager.getPodcastResponse(existingPodcast.uuid) - if (podcastResponse.wasCached() || podcastResponse.notModified()) { + // podcasts that aren't subscribed can have episodes removed, so always refresh them + if (existingPodcast.isSubscribed && (podcastResponse.wasCached() || podcastResponse.notModified())) { LogBuffer.i(LogBuffer.TAG_BACKGROUND_TASKS, "Refreshing podcast ${existingPodcast.uuid} not required as cached") return }