Skip to content

Commit

Permalink
Handle case when user has no subscriptions or no downloads more reliably
Browse files Browse the repository at this point in the history
  • Loading branch information
mr3y-the-programmer committed Aug 17, 2024
1 parent 6682748 commit 62f1bea
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import app.cash.molecule.launchMolecule
import com.mr3y.podcaster.core.data.PodcastsRepository
import com.mr3y.podcaster.ui.presenter.BaseMoleculeViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.launch
Expand All @@ -37,21 +39,12 @@ internal fun DownloadsPresenter(
var isLoading by remember { mutableStateOf(true) }
val downloads by repository.getDownloads().collectAsState(initial = emptyList())

LaunchedEffect(Unit) {
launch {
// if the user has no downloads
repository.hasDownloads()
.filter { hasDownloads -> !hasDownloads }
.collect {
isLoading = false
}
}
launch {
snapshotFlow { downloads }
.drop(1)
.collect {
isLoading = false
}
LaunchedEffect(downloads) {
if (downloads.isNotEmpty()) {
isLoading = false
} else {
delay(1000)
isLoading = false
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import com.mr3y.podcaster.ui.presenter.RefreshResult
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -55,30 +57,19 @@ internal fun SubscriptionsPresenter(
val queueEpisodesIds by repository.getQueueEpisodesIds().collectAsState(initial = emptyList())
var refreshResult: RefreshResult? by remember { mutableStateOf(null) }

LaunchedEffect(Unit) {
launch {
// if the user has no subscriptions yet
repository.hasSubscriptions()
.filter { isSubscribedToAnyPodcast -> !isSubscribedToAnyPodcast }
.collect {
isSubscriptionsLoading = false
isEpisodesLoading = false
}
LaunchedEffect(podcasts, episodes) {
if (podcasts.isNotEmpty()) {
isSubscriptionsLoading = false
}
launch {
snapshotFlow { podcasts }
.drop(1) // Ignore initial value
.collect {
isSubscriptionsLoading = false
}

if (episodes.isNotEmpty()) {
isEpisodesLoading = false
}

launch {
snapshotFlow { episodes }
.drop(1) // Ignore initial value
.collect {
isEpisodesLoading = false
}
if (podcasts.isEmpty() || episodes.isEmpty()) {
delay(1000)
isSubscriptionsLoading = false
isEpisodesLoading = false
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ interface PodcastsRepository {

fun isPodcastFromSubscriptionsNonObservable(podcastId: Long): Boolean

fun hasSubscriptions(): Flow<Boolean>
fun countSubscriptions(): Flow<Long>

fun hasDownloads(): Flow<Boolean>
fun countDownloads(): Flow<Long>

suspend fun getEpisode(episodeId: Long, podcastArtworkUrl: String, forceRefresh: Boolean): Episode?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ class DefaultPodcastsRepository @Inject constructor(
return podcastsDao.isPodcastAvailableNonObservable(podcastId)
}

override fun hasSubscriptions(): Flow<Boolean> = podcastsDao.hasPodcasts()
override fun countSubscriptions(): Flow<Long> = podcastsDao.countPodcasts()

override fun hasDownloads(): Flow<Boolean> = podcastsDao.hasDownloads()
override fun countDownloads(): Flow<Long> = podcastsDao.countDownloads()

override suspend fun getEpisode(episodeId: Long, podcastArtworkUrl: String, forceRefresh: Boolean): Episode? {
suspend fun fetchFromNetworkAndRefresh(): Episode? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.mr3y.podcaster.core.local.dao

import app.cash.sqldelight.coroutines.asFlow
import app.cash.sqldelight.coroutines.mapToList
import app.cash.sqldelight.coroutines.mapToOne
import app.cash.sqldelight.coroutines.mapToOneOrNull
import com.mr3y.podcaster.CurrentlyPlayingEntity
import com.mr3y.podcaster.PodcasterDatabase
Expand Down Expand Up @@ -35,9 +36,9 @@ interface PodcastsDao {

fun isPodcastAvailable(podcastId: Long): Flow<Boolean>

fun hasPodcasts(): Flow<Boolean>
fun countPodcasts(): Flow<Long>

fun hasDownloads(): Flow<Boolean>
fun countDownloads(): Flow<Long>

fun isPodcastAvailableNonObservable(podcastId: Long): Boolean

Expand Down Expand Up @@ -145,18 +146,12 @@ class DefaultPodcastsDao @Inject constructor(
.map { it == 1L }
}

override fun hasPodcasts(): Flow<Boolean> {
return database.podcastEntityQueries.countPodcasts()
.asFlow()
.mapToOneOrNull(dispatcher)
.map { it != null && it != 0L }
override fun countPodcasts(): Flow<Long> {
return database.podcastEntityQueries.countPodcasts().asFlow().mapToOne(dispatcher)
}

override fun hasDownloads(): Flow<Boolean> {
return database.downloadableEpisodeEntityQueries.countDownloads()
.asFlow()
.mapToOneOrNull(dispatcher)
.map { it != null && it != 0L }
override fun countDownloads(): Flow<Long> {
return database.downloadableEpisodeEntityQueries.countDownloads().asFlow().mapToOne(dispatcher)
}

override fun isPodcastAvailableNonObservable(podcastId: Long): Boolean {
Expand Down

0 comments on commit 62f1bea

Please sign in to comment.