in Jetcaster/app/src/main/java/com/example/jetcaster/data/PodcastsRepository.kt [43:70]
suspend fun updatePodcasts(force: Boolean) {
if (refreshingJob?.isActive == true) {
refreshingJob?.join()
} else if (force || podcastStore.isEmpty()) {
refreshingJob = scope.launch {
// Now fetch the podcasts, and add each to each store
podcastsFetcher(SampleFeeds)
.filter { it is PodcastRssResponse.Success }
.map { it as PodcastRssResponse.Success }
.collect { (podcast, episodes, categories) ->
transactionRunner {
podcastStore.addPodcast(podcast)
episodeStore.addEpisodes(episodes)
categories.forEach { category ->
// First insert the category
val categoryId = categoryStore.addCategory(category)
// Now we can add the podcast to the category
categoryStore.addPodcastToCategory(
podcastUri = podcast.uri,
categoryId = categoryId
)
}
}
}
}
}
}