Skip to content

Commit

Permalink
move to IO dispatcher for reading FF state
Browse files Browse the repository at this point in the history
  • Loading branch information
lmac012 committed Dec 1, 2024
1 parent 6ee6020 commit 19a1aa7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.duckduckgo.subscriptions.impl
import androidx.lifecycle.LifecycleOwner
import com.duckduckgo.app.di.AppCoroutineScope
import com.duckduckgo.app.lifecycle.MainProcessLifecycleObserver
import com.duckduckgo.common.utils.DispatcherProvider
import com.duckduckgo.di.scopes.AppScope
import com.duckduckgo.subscriptions.impl.SubscriptionsConstants.BASIC_SUBSCRIPTION
import com.duckduckgo.subscriptions.impl.billing.PlayBillingManager
Expand All @@ -29,6 +30,7 @@ import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import logcat.logcat

@ContributesMultibinding(
Expand All @@ -41,13 +43,14 @@ class SubscriptionFeaturesFetcher @Inject constructor(
private val subscriptionsService: SubscriptionsService,
private val authRepository: AuthRepository,
private val privacyProFeature: PrivacyProFeature,
private val dispatcherProvider: DispatcherProvider,
) : MainProcessLifecycleObserver {

override fun onCreate(owner: LifecycleOwner) {
super.onCreate(owner)
appCoroutineScope.launch {
try {
if (privacyProFeature.featuresApi().isEnabled()) {
if (isFeaturesApiEnabled()) {
fetchSubscriptionFeatures()
}
} catch (e: Exception) {
Expand All @@ -56,6 +59,10 @@ class SubscriptionFeaturesFetcher @Inject constructor(
}
}

private suspend fun isFeaturesApiEnabled(): Boolean = withContext(dispatcherProvider.io()) {
privacyProFeature.featuresApi().isEnabled()
}

private suspend fun fetchSubscriptionFeatures() {
playBillingManager.productsFlow
.first { it.isNotEmpty() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.onSubscription
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import logcat.LogPriority
import logcat.logcat
import retrofit2.HttpException
Expand Down Expand Up @@ -266,8 +267,8 @@ class RealSubscriptionsManager @Inject constructor(
return authRepository.getRefreshTokenV2() != null
}

private suspend fun shouldUseAuthV2(): Boolean {
return privacyProFeature.get().authApiV2().isEnabled() || isSignedInV2()
private suspend fun shouldUseAuthV2(): Boolean = withContext(dispatcherProvider.io()) {
privacyProFeature.get().authApiV2().isEnabled() || isSignedInV2()
}

private fun emitEntitlementsValues() {
Expand Down Expand Up @@ -870,8 +871,9 @@ class RealSubscriptionsManager @Inject constructor(
}
}

private fun isLaunchedRow(): Boolean =
private suspend fun isLaunchedRow(): Boolean = withContext(dispatcherProvider.io()) {
privacyProFeature.get().isLaunchedROW().isEnabled()
}

private fun parseError(e: HttpException): ResponseError? {
return try {
Expand Down

0 comments on commit 19a1aa7

Please sign in to comment.