From 62f1beab51c592a76abfe69c59fef349971ec661 Mon Sep 17 00:00:00 2001 From: MR3Y Date: Sat, 17 Aug 2024 22:56:22 +0300 Subject: [PATCH] Handle case when user has no subscriptions or no downloads more reliably --- .../presenter/downloads/DownloadsViewModel.kt | 23 +++++-------- .../subscriptions/SubscriptionsViewModel.kt | 33 +++++++------------ .../podcaster/core/data/PodcastsRepository.kt | 4 +-- .../internal/DefaultPodcastsRepository.kt | 4 +-- .../podcaster/core/local/dao/PodcastsDao.kt | 19 ++++------- 5 files changed, 31 insertions(+), 52 deletions(-) diff --git a/app/src/main/kotlin/com/mr3y/podcaster/ui/presenter/downloads/DownloadsViewModel.kt b/app/src/main/kotlin/com/mr3y/podcaster/ui/presenter/downloads/DownloadsViewModel.kt index 4050464c..144b00ca 100644 --- a/app/src/main/kotlin/com/mr3y/podcaster/ui/presenter/downloads/DownloadsViewModel.kt +++ b/app/src/main/kotlin/com/mr3y/podcaster/ui/presenter/downloads/DownloadsViewModel.kt @@ -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 @@ -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 } } diff --git a/app/src/main/kotlin/com/mr3y/podcaster/ui/presenter/subscriptions/SubscriptionsViewModel.kt b/app/src/main/kotlin/com/mr3y/podcaster/ui/presenter/subscriptions/SubscriptionsViewModel.kt index ecd66c9d..0603568c 100644 --- a/app/src/main/kotlin/com/mr3y/podcaster/ui/presenter/subscriptions/SubscriptionsViewModel.kt +++ b/app/src/main/kotlin/com/mr3y/podcaster/ui/presenter/subscriptions/SubscriptionsViewModel.kt @@ -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 @@ -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 } } diff --git a/core/data/src/main/kotlin/com/mr3y/podcaster/core/data/PodcastsRepository.kt b/core/data/src/main/kotlin/com/mr3y/podcaster/core/data/PodcastsRepository.kt index 08e54807..b7bc05a8 100644 --- a/core/data/src/main/kotlin/com/mr3y/podcaster/core/data/PodcastsRepository.kt +++ b/core/data/src/main/kotlin/com/mr3y/podcaster/core/data/PodcastsRepository.kt @@ -30,9 +30,9 @@ interface PodcastsRepository { fun isPodcastFromSubscriptionsNonObservable(podcastId: Long): Boolean - fun hasSubscriptions(): Flow + fun countSubscriptions(): Flow - fun hasDownloads(): Flow + fun countDownloads(): Flow suspend fun getEpisode(episodeId: Long, podcastArtworkUrl: String, forceRefresh: Boolean): Episode? diff --git a/core/data/src/main/kotlin/com/mr3y/podcaster/core/data/internal/DefaultPodcastsRepository.kt b/core/data/src/main/kotlin/com/mr3y/podcaster/core/data/internal/DefaultPodcastsRepository.kt index ef70316b..bdae3b55 100644 --- a/core/data/src/main/kotlin/com/mr3y/podcaster/core/data/internal/DefaultPodcastsRepository.kt +++ b/core/data/src/main/kotlin/com/mr3y/podcaster/core/data/internal/DefaultPodcastsRepository.kt @@ -73,9 +73,9 @@ class DefaultPodcastsRepository @Inject constructor( return podcastsDao.isPodcastAvailableNonObservable(podcastId) } - override fun hasSubscriptions(): Flow = podcastsDao.hasPodcasts() + override fun countSubscriptions(): Flow = podcastsDao.countPodcasts() - override fun hasDownloads(): Flow = podcastsDao.hasDownloads() + override fun countDownloads(): Flow = podcastsDao.countDownloads() override suspend fun getEpisode(episodeId: Long, podcastArtworkUrl: String, forceRefresh: Boolean): Episode? { suspend fun fetchFromNetworkAndRefresh(): Episode? { diff --git a/core/database/src/main/kotlin/com/mr3y/podcaster/core/local/dao/PodcastsDao.kt b/core/database/src/main/kotlin/com/mr3y/podcaster/core/local/dao/PodcastsDao.kt index 9d4d74f4..2b37f02e 100644 --- a/core/database/src/main/kotlin/com/mr3y/podcaster/core/local/dao/PodcastsDao.kt +++ b/core/database/src/main/kotlin/com/mr3y/podcaster/core/local/dao/PodcastsDao.kt @@ -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 @@ -35,9 +36,9 @@ interface PodcastsDao { fun isPodcastAvailable(podcastId: Long): Flow - fun hasPodcasts(): Flow + fun countPodcasts(): Flow - fun hasDownloads(): Flow + fun countDownloads(): Flow fun isPodcastAvailableNonObservable(podcastId: Long): Boolean @@ -145,18 +146,12 @@ class DefaultPodcastsDao @Inject constructor( .map { it == 1L } } - override fun hasPodcasts(): Flow { - return database.podcastEntityQueries.countPodcasts() - .asFlow() - .mapToOneOrNull(dispatcher) - .map { it != null && it != 0L } + override fun countPodcasts(): Flow { + return database.podcastEntityQueries.countPodcasts().asFlow().mapToOne(dispatcher) } - override fun hasDownloads(): Flow { - return database.downloadableEpisodeEntityQueries.countDownloads() - .asFlow() - .mapToOneOrNull(dispatcher) - .map { it != null && it != 0L } + override fun countDownloads(): Flow { + return database.downloadableEpisodeEntityQueries.countDownloads().asFlow().mapToOne(dispatcher) } override fun isPodcastAvailableNonObservable(podcastId: Long): Boolean {