diff --git a/CHANGELOG.md b/CHANGELOG.md index e244768adec..cdb1969bcc3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,9 @@ ([#3294](https://github.com/Automattic/pocket-casts-android/pull/3294)) * Improve the Up Next clear all button ([#3334](https://github.com/Automattic/pocket-casts-android/pull/3334)) +* Bug Fixes + * Fix podcast ratings not loading + ([#3378](https://github.com/Automattic/pocket-casts-android/pull/3378)) 7.78 ----- diff --git a/modules/features/podcasts/src/main/java/au/com/shiftyjelly/pocketcasts/podcasts/viewmodel/GiveRatingViewModel.kt b/modules/features/podcasts/src/main/java/au/com/shiftyjelly/pocketcasts/podcasts/viewmodel/GiveRatingViewModel.kt index 0d4fabb29b9..883f04d843f 100644 --- a/modules/features/podcasts/src/main/java/au/com/shiftyjelly/pocketcasts/podcasts/viewmodel/GiveRatingViewModel.kt +++ b/modules/features/podcasts/src/main/java/au/com/shiftyjelly/pocketcasts/podcasts/viewmodel/GiveRatingViewModel.kt @@ -7,22 +7,20 @@ 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.UserPodcastRating -import au.com.shiftyjelly.pocketcasts.models.to.SignInState import au.com.shiftyjelly.pocketcasts.podcasts.viewmodel.GiveRatingViewModel.State.Loaded.Stars import au.com.shiftyjelly.pocketcasts.repositories.podcast.PodcastManager import au.com.shiftyjelly.pocketcasts.repositories.ratings.PodcastRatingResult import au.com.shiftyjelly.pocketcasts.repositories.ratings.RatingsManager +import au.com.shiftyjelly.pocketcasts.repositories.sync.SyncManager import au.com.shiftyjelly.pocketcasts.repositories.user.UserManager import au.com.shiftyjelly.pocketcasts.utils.Network import au.com.shiftyjelly.pocketcasts.utils.log.LogBuffer import dagger.hilt.android.lifecycle.HiltViewModel import java.util.Date import javax.inject.Inject -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext import au.com.shiftyjelly.pocketcasts.localization.R as LR @HiltViewModel @@ -31,6 +29,7 @@ class GiveRatingViewModel @Inject constructor( private val userManager: UserManager, private val ratingManager: RatingsManager, private val analyticsTracker: AnalyticsTracker, + private val syncManager: SyncManager, ) : ViewModel() { private var shouldTrackDismissedEvent = false @@ -73,25 +72,22 @@ class GiveRatingViewModel @Inject constructor( ) { _state.value = State.Loading + if (syncManager.isLoggedIn().not()) { + onUserSignedOut() + return + } + viewModelScope.launch { - val signInState = userManager.getSignInState().blockingFirst() - if (signInState == SignInState.SignedOut) { - onUserSignedOut() - } else if (signInState is SignInState.SignedIn) { - withContext(Dispatchers.IO) { - val countPlayedEpisodes = podcastManager.countPlayedEpisodes(podcastUuid) - - if (countPlayedEpisodes < NUMBER_OF_EPISODES_LISTENED_REQUIRED_TO_RATE) { - val episodes = podcastManager.countEpisodesByPodcast(podcastUuid) - if (episodes == 1 && countPlayedEpisodes == 1) { - onSuccess() // This is the case an user wants to rate a podcast that has only one episode. - } else { - _state.value = State.NotAllowedToRate(podcastUuid) - } - } else { - onSuccess() - } + val countPlayedEpisodes = podcastManager.countPlayedEpisodes(podcastUuid) + if (countPlayedEpisodes < NUMBER_OF_EPISODES_LISTENED_REQUIRED_TO_RATE) { + val episodes = podcastManager.countEpisodesByPodcast(podcastUuid) + if (episodes == 1 && countPlayedEpisodes == 1) { + onSuccess() // This is the case an user wants to rate a podcast that has only one episode. + } else { + _state.value = State.NotAllowedToRate(podcastUuid) } + } else { + onSuccess() } } } diff --git a/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/sync/SyncAccountManager.kt b/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/sync/SyncAccountManager.kt index 1ecdd9a285e..721510aafa2 100644 --- a/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/sync/SyncAccountManager.kt +++ b/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/sync/SyncAccountManager.kt @@ -6,6 +6,8 @@ import au.com.shiftyjelly.pocketcasts.preferences.AccountConstants import au.com.shiftyjelly.pocketcasts.preferences.RefreshToken import au.com.shiftyjelly.pocketcasts.servers.sync.LoginIdentity import au.com.shiftyjelly.pocketcasts.servers.sync.TokenHandler +import au.com.shiftyjelly.pocketcasts.utils.Optional +import io.reactivex.Flowable import kotlinx.coroutines.flow.Flow /** @@ -17,7 +19,8 @@ interface SyncAccountManager : TokenHandler { fun addAccount(email: String, uuid: String, refreshToken: RefreshToken, accessToken: AccessToken, loginIdentity: LoginIdentity) fun getAccount(): Account? fun getEmail(): String? - fun observeEmail(): Flow + fun emailFlow(): Flow + fun emailFlowable(): Flowable> fun getLoginIdentity(): LoginIdentity? fun getRefreshToken(account: Account? = null): RefreshToken? fun getSignInType(account: Account): AccountConstants.SignInType diff --git a/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/sync/SyncAccountManagerImpl.kt b/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/sync/SyncAccountManagerImpl.kt index bbc4b5738a7..972f8bb888e 100644 --- a/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/sync/SyncAccountManagerImpl.kt +++ b/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/sync/SyncAccountManagerImpl.kt @@ -15,7 +15,10 @@ import au.com.shiftyjelly.pocketcasts.preferences.RefreshToken import au.com.shiftyjelly.pocketcasts.servers.sync.LoginIdentity import au.com.shiftyjelly.pocketcasts.servers.sync.TokenHandler import au.com.shiftyjelly.pocketcasts.servers.sync.exception.RefreshTokenExpiredException +import au.com.shiftyjelly.pocketcasts.utils.Optional import au.com.shiftyjelly.pocketcasts.utils.log.LogBuffer +import io.reactivex.BackpressureStrategy +import io.reactivex.Flowable import javax.inject.Inject import javax.inject.Singleton import kotlinx.coroutines.Dispatchers @@ -40,7 +43,7 @@ open class SyncAccountManagerImpl @Inject constructor( return getAccount()?.name } - override fun observeEmail() = callbackFlow { + override fun emailFlow() = callbackFlow { val listener = object : OnAccountsUpdateListener { override fun onAccountsUpdated(accounts: Array?) { val email = accounts?.find { it.type == AccountConstants.ACCOUNT_TYPE }?.name @@ -54,6 +57,24 @@ open class SyncAccountManagerImpl @Inject constructor( } } + override fun emailFlowable(): Flowable> { + return Flowable.create({ emitter -> + try { + val listener = OnAccountsUpdateListener { accounts -> + val email = accounts?.find { it.type == AccountConstants.ACCOUNT_TYPE }?.name + emitter.onNext(Optional.of(email)) + } + emitter.onNext(Optional.of(getEmail())) + accountManager.addOnAccountsUpdatedListener(listener, null, true) + emitter.setCancellable { + accountManager.removeOnAccountsUpdatedListener(listener) + } + } catch (e: Exception) { + emitter.tryOnError(e) + } + }, BackpressureStrategy.BUFFER) + } + override fun getUuid(): String? = getAccount()?.let { account -> accountManager.getUserData(account, AccountConstants.UUID) diff --git a/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/sync/SyncManager.kt b/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/sync/SyncManager.kt index c547e90b328..bd2af58a72c 100644 --- a/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/sync/SyncManager.kt +++ b/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/sync/SyncManager.kt @@ -26,6 +26,7 @@ import au.com.shiftyjelly.pocketcasts.servers.sync.UserChangeResponse import au.com.shiftyjelly.pocketcasts.servers.sync.history.HistoryYearResponse import au.com.shiftyjelly.pocketcasts.servers.sync.login.ExchangeSonosResponse import au.com.shiftyjelly.pocketcasts.servers.sync.update.SyncUpdateResponse +import au.com.shiftyjelly.pocketcasts.utils.Optional import com.jakewharton.rxrelay2.BehaviorRelay import com.pocketcasts.service.api.PodcastRatingResponse import com.pocketcasts.service.api.PodcastRatingsResponse @@ -34,6 +35,7 @@ import com.pocketcasts.service.api.ReferralRedemptionResponse import com.pocketcasts.service.api.ReferralValidationResponse import com.pocketcasts.service.api.UserPodcastListResponse import io.reactivex.Completable +import io.reactivex.Flowable import io.reactivex.Maybe import io.reactivex.Single import java.io.File @@ -50,6 +52,7 @@ interface SyncManager : NamedSettingsCaller { fun getLoginIdentity(): LoginIdentity? fun getEmail(): String? fun emailFlow(): Flow + fun emailFlowable(): Flowable> fun signOut(action: () -> Unit = {}) suspend fun loginWithGoogle(idToken: String, signInSource: SignInSource): LoginResult suspend fun loginWithEmailAndPassword(email: String, password: String, signInSource: SignInSource): LoginResult diff --git a/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/sync/SyncManagerImpl.kt b/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/sync/SyncManagerImpl.kt index 03d6a9457be..f2d4e6c4aae 100644 --- a/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/sync/SyncManagerImpl.kt +++ b/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/sync/SyncManagerImpl.kt @@ -42,6 +42,7 @@ import au.com.shiftyjelly.pocketcasts.servers.sync.login.ExchangeSonosResponse import au.com.shiftyjelly.pocketcasts.servers.sync.login.LoginTokenResponse import au.com.shiftyjelly.pocketcasts.servers.sync.parseErrorResponse import au.com.shiftyjelly.pocketcasts.servers.sync.update.SyncUpdateResponse +import au.com.shiftyjelly.pocketcasts.utils.Optional import au.com.shiftyjelly.pocketcasts.utils.log.LogBuffer import com.jakewharton.rxrelay2.BehaviorRelay import com.pocketcasts.service.api.PodcastRatingResponse @@ -53,6 +54,7 @@ import com.pocketcasts.service.api.UserPodcastListResponse import com.squareup.moshi.Moshi import dagger.hilt.android.qualifiers.ApplicationContext import io.reactivex.Completable +import io.reactivex.Flowable import io.reactivex.Maybe import io.reactivex.Single import java.io.File @@ -130,7 +132,9 @@ class SyncManagerImpl @Inject constructor( override fun getEmail(): String? = syncAccountManager.getEmail() - override fun emailFlow() = syncAccountManager.observeEmail().distinctUntilChanged() + override fun emailFlow() = syncAccountManager.emailFlow().distinctUntilChanged() + + override fun emailFlowable(): Flowable> = syncAccountManager.emailFlowable().distinctUntilChanged() override suspend fun getAccessToken(account: Account): AccessToken = syncAccountManager.peekAccessToken(account) diff --git a/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/user/UserManager.kt b/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/user/UserManager.kt index 5250dc22ae2..e67e2e91b9c 100644 --- a/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/user/UserManager.kt +++ b/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/user/UserManager.kt @@ -23,7 +23,6 @@ import au.com.shiftyjelly.pocketcasts.repositories.podcast.UserEpisodeManager import au.com.shiftyjelly.pocketcasts.repositories.searchhistory.SearchHistoryManager import au.com.shiftyjelly.pocketcasts.repositories.subscription.SubscriptionManager import au.com.shiftyjelly.pocketcasts.repositories.sync.SyncManager -import au.com.shiftyjelly.pocketcasts.utils.Optional import au.com.shiftyjelly.pocketcasts.utils.log.LogBuffer import com.automattic.android.tracks.crashlogging.CrashLogging import dagger.hilt.android.qualifiers.ApplicationContext @@ -35,10 +34,8 @@ import javax.inject.Inject import kotlin.coroutines.CoroutineContext import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.flow.map import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking -import kotlinx.coroutines.rx2.asFlowable import timber.log.Timber interface UserManager { @@ -100,7 +97,7 @@ class UserManagerImpl @Inject constructor( subscriptionManager.getSubscriptionStatus(allowCache = false) } } - .combineLatest(syncManager.emailFlow().map { Optional.of(it) }.asFlowable()) + .combineLatest(syncManager.emailFlowable()) .map { (status, maybeEmail) -> analyticsTracker.refreshMetadata() SignInState.SignedIn(email = maybeEmail.get() ?: "", subscriptionStatus = status)