-
Notifications
You must be signed in to change notification settings - Fork 921
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create PirSubscriptionManager and use states to render view
The PirSubscriptionManager mimics our Manager for the VPN, exposing the status of PIR so we can render the viewstates correctly
- Loading branch information
1 parent
da4a323
commit 8b82a19
Showing
3 changed files
with
115 additions
and
14 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
...ptions-impl/src/main/java/com/duckduckgo/subscriptions/impl/pir/PirSubscriptionManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright (c) 2024 DuckDuckGo | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.duckduckgo.subscriptions.impl.pir | ||
|
||
import com.duckduckgo.di.scopes.AppScope | ||
import com.duckduckgo.subscriptions.api.Product.PIR | ||
import com.duckduckgo.subscriptions.api.SubscriptionStatus | ||
import com.duckduckgo.subscriptions.api.Subscriptions | ||
import com.duckduckgo.subscriptions.impl.pir.PirSubscriptionManager.PirStatus | ||
import com.squareup.anvil.annotations.ContributesBinding | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
|
||
interface PirSubscriptionManager { | ||
fun pirStatus(): Flow<PirStatus> | ||
|
||
enum class PirStatus { | ||
ACTIVE, | ||
EXPIRED, | ||
SIGNED_OUT, | ||
INACTIVE, | ||
WAITING, | ||
INELIGIBLE, | ||
} | ||
} | ||
|
||
@ContributesBinding(AppScope::class) | ||
class RealPirSubscriptionManager @Inject constructor( | ||
private val subscriptions: Subscriptions, | ||
) : PirSubscriptionManager { | ||
|
||
override fun pirStatus(): Flow<PirStatus> = hasPirEntitlement().map { getPirStatusInternal(it) } | ||
|
||
private fun hasPirEntitlement(): Flow<Boolean> = subscriptions.getEntitlementStatus().map { it.contains(PIR) } | ||
|
||
private suspend fun getPirStatusInternal(hasValidEntitlement: Boolean): PirStatus = when { | ||
!hasValidEntitlement -> PirStatus.INELIGIBLE | ||
else -> when (subscriptions.getSubscriptionStatus()) { | ||
SubscriptionStatus.INACTIVE, SubscriptionStatus.EXPIRED -> PirStatus.EXPIRED | ||
SubscriptionStatus.UNKNOWN -> PirStatus.SIGNED_OUT | ||
SubscriptionStatus.AUTO_RENEWABLE, SubscriptionStatus.NOT_AUTO_RENEWABLE, SubscriptionStatus.GRACE_PERIOD -> PirStatus.ACTIVE | ||
SubscriptionStatus.WAITING -> PirStatus.WAITING | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters