Skip to content

Commit

Permalink
Update ObservePostSubscription implementation
Browse files Browse the repository at this point in the history
MAILANDR-2392
  • Loading branch information
nick0602 committed Dec 2, 2024
1 parent fb2e6a8 commit 8da3745
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
4 changes: 3 additions & 1 deletion app/src/main/kotlin/ch/protonmail/android/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.runtime.CompositionLocalProvider
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.lifecycle.lifecycleScope
import ch.protonmail.android.feature.postsubscription.ObservePostSubscription
import ch.protonmail.android.mailcommon.domain.system.DeviceCapabilities
import ch.protonmail.android.mailcommon.presentation.system.LocalDeviceCapabilitiesProvider
Expand Down Expand Up @@ -75,7 +76,8 @@ class MainActivity : AppCompatActivity() {
// Register activities for result.
launcherViewModel.register(this)

observePostSubscription.start(this)
// Pass this activity's lifecycleScope to allow proper cancellation.
observePostSubscription.start(this, lifecycleScope)

shareIntentObserver.onNewIntent(intent)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

package ch.protonmail.android.feature.postsubscription

import java.lang.ref.WeakReference
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import ch.protonmail.android.PostSubscriptionActivity
import ch.protonmail.android.mailcommon.domain.coroutines.AppScope
import ch.protonmail.android.mailcommon.domain.usecase.ObservePrimaryUserId
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.collectLatest
Expand All @@ -36,39 +36,45 @@ import me.proton.core.user.domain.extension.hasSubscriptionForMail
import javax.inject.Inject

class ObservePostSubscription @Inject constructor(
@AppScope private val coroutineScope: CoroutineScope,
private val isPostSubscriptionFlowEnabled: IsPostSubscriptionFlowEnabled,
private val observePrimaryUserId: ObservePrimaryUserId,
private val purchaseManager: PurchaseManager,
private val sessionManager: SessionManager,
private val userManager: UserManager
) {

fun start(activity: AppCompatActivity) = coroutineScope.launch {
observePrimaryUserId().filterNotNull().distinctUntilChanged().collectLatest { userId ->
// Cancel observation if the user already has a subscription.
if (userManager.getUser(userId).hasSubscriptionForMail()) return@collectLatest
fun start(activity: AppCompatActivity, scope: CoroutineScope) {
val activityReference = WeakReference(activity)

if (!isPostSubscriptionFlowEnabled(userId)) return@collectLatest
scope.launch {
observePrimaryUserId().filterNotNull().distinctUntilChanged().collectLatest { userId ->
// Cancel observation if the user already has a subscription.
if (userManager.getUser(userId).hasSubscriptionForMail()) return@collectLatest

val sessionId = sessionManager.getSessionId(userId)
if (!isPostSubscriptionFlowEnabled(userId)) return@collectLatest

purchaseManager.observePurchases().distinctUntilChanged().collectLatest purchases@{ purchases ->
// Make sure the purchase was completed from the app
val currentSessionPurchases = purchases.filter { it.sessionId == sessionId }
val isMailPlusCompletedPurchaseFound = currentSessionPurchases.any {
it.planName == MAIL_PLUS_PLAN_NAME && it.purchaseState == PurchaseState.Acknowledged
}
val sessionId = sessionManager.getSessionId(userId)

purchaseManager.observePurchases().distinctUntilChanged().collectLatest purchases@{ purchases ->
// Make sure the purchase was completed from the app
val currentSessionPurchases = purchases.filter { it.sessionId == sessionId }
val isMailPlusCompletedPurchaseFound = currentSessionPurchases.any {
it.planName == MAIL_PLUS_PLAN_NAME && it.purchaseState == PurchaseState.Acknowledged
}

if (isMailPlusCompletedPurchaseFound) {
val intent = Intent(activity, PostSubscriptionActivity::class.java)
activity.startActivity(intent)
activityReference.get()?.let { activity ->
if (isMailPlusCompletedPurchaseFound) {
val intent = Intent(activity, PostSubscriptionActivity::class.java)
activity.startActivity(intent)
}
}
}
}
}
}

companion object {

private const val MAIL_PLUS_PLAN_NAME = "mail2022"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ class ObservePostSubscriptionTest {
}

private val observePostSubscription = ObservePostSubscription(
coroutineScope = coroutineScope,
isPostSubscriptionFlowEnabled = isPostSubscriptionFlowEnabled,
observePrimaryUserId = observePrimaryUserId,
purchaseManager = purchaseManager,
Expand All @@ -69,7 +68,7 @@ class ObservePostSubscriptionTest {
} returns flowOf(listOf(basePurchase.copy(purchaseState = PurchaseState.Acknowledged)))

// When
observePostSubscription.start(activity)
observePostSubscription.start(activity, coroutineScope)
coroutineScope.advanceUntilIdle()

// Then
Expand All @@ -85,7 +84,7 @@ class ObservePostSubscriptionTest {
} returns flowOf(listOf(basePurchase.copy(purchaseState = PurchaseState.Pending)))

// When
observePostSubscription.start(activity)
observePostSubscription.start(activity, coroutineScope)
coroutineScope.advanceUntilIdle()

// Then
Expand Down

0 comments on commit 8da3745

Please sign in to comment.