Skip to content

Commit

Permalink
SubscriptionsManager refactoring (#5205)
Browse files Browse the repository at this point in the history
Task/Issue URL:
https://app.asana.com/0/1205648422731273/1208643784411875/f

### Description

### Steps to test this PR

_Smoke test Privacy Pro (no behavior changes are expected)_
- [x] Purchase test subscription
- [x] Remove from device
- [x] Restore using Play
- [x] Cancel subscription
- [x] Wait for it to expire
- [x] Purchase again

### No UI changes
  • Loading branch information
lmac012 authored Oct 31, 2024
1 parent 02bd74e commit 7dfe600
Show file tree
Hide file tree
Showing 26 changed files with 585 additions and 411 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ import dagger.SingleInstanceIn
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

Expand All @@ -62,25 +61,15 @@ class RealSubscriptions @Inject constructor(
) : Subscriptions {
override suspend fun getAccessToken(): String? {
return when (val result = subscriptionsManager.getAccessToken()) {
is AccessToken.Success -> result.accessToken
is AccessToken.Failure -> null
is AccessTokenResult.Success -> result.accessToken
is AccessTokenResult.Failure -> null
}
}

override fun getEntitlementStatus(): Flow<List<Product>> {
return subscriptionsManager.entitlements.map {
if (!checkIfActive()) emptyList() else it
}
return subscriptionsManager.entitlements
}

private suspend fun checkIfActive(): Boolean {
return if (subscriptionsManager.subscriptionStatus().isActiveOrWaiting()) {
true
} else {
subscriptionsManager.removeEntitlements()
false
}
}
override suspend fun isEligible(): Boolean {
val supportsEncryption = subscriptionsManager.canSupportEncryption()
val isActive = subscriptionsManager.subscriptionStatus().isActiveOrWaiting()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ class SubscriptionsCheckWorker(
override suspend fun doWork(): Result {
return try {
if (subscriptionsManager.subscriptionStatus() != UNKNOWN) {
val subscription = subscriptionsManager.fetchAndStoreAllData()
subscriptionsManager.fetchAndStoreAllData()
val subscription = subscriptionsManager.getSubscription()
if (subscription?.status == null || subscription.status == UNKNOWN) {
workManager.cancelAllWorkByTag(TAG_WORKER_SUBSCRIPTION_CHECK)
}
Expand Down
Loading

0 comments on commit 7dfe600

Please sign in to comment.