Skip to content

Commit

Permalink
Fix podcast ratings not loading (#3378)
Browse files Browse the repository at this point in the history
Co-authored-by: Eduarda Barbosa <[email protected]>
  • Loading branch information
geekygecko and mebarbosa authored Dec 18, 2024
1 parent 8dcd2e6 commit 54f638a
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 27 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand All @@ -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<String?>
fun emailFlow(): Flow<String?>
fun emailFlowable(): Flowable<Optional<String>>
fun getLoginIdentity(): LoginIdentity?
fun getRefreshToken(account: Account? = null): RefreshToken?
fun getSignInType(account: Account): AccountConstants.SignInType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,7 +43,7 @@ open class SyncAccountManagerImpl @Inject constructor(
return getAccount()?.name
}

override fun observeEmail() = callbackFlow<String?> {
override fun emailFlow() = callbackFlow<String?> {
val listener = object : OnAccountsUpdateListener {
override fun onAccountsUpdated(accounts: Array<out Account>?) {
val email = accounts?.find { it.type == AccountConstants.ACCOUNT_TYPE }?.name
Expand All @@ -54,6 +57,24 @@ open class SyncAccountManagerImpl @Inject constructor(
}
}

override fun emailFlowable(): Flowable<Optional<String>> {
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -50,6 +52,7 @@ interface SyncManager : NamedSettingsCaller {
fun getLoginIdentity(): LoginIdentity?
fun getEmail(): String?
fun emailFlow(): Flow<String?>
fun emailFlowable(): Flowable<Optional<String>>
fun signOut(action: () -> Unit = {})
suspend fun loginWithGoogle(idToken: String, signInSource: SignInSource): LoginResult
suspend fun loginWithEmailAndPassword(email: String, password: String, signInSource: SignInSource): LoginResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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<Optional<String>> = syncAccountManager.emailFlowable().distinctUntilChanged()

override suspend fun getAccessToken(account: Account): AccessToken =
syncAccountManager.peekAccessToken(account)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 54f638a

Please sign in to comment.