Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CustomEntitlementsComputation: disable first listener callback when set #1152

Merged
merged 3 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.revenuecat.purchases

import android.os.Handler
import android.os.Looper
import com.revenuecat.purchases.common.AppConfig
import com.revenuecat.purchases.common.LogIntent
import com.revenuecat.purchases.common.caching.DeviceCache
import com.revenuecat.purchases.common.log
Expand All @@ -18,6 +19,7 @@ internal class CustomerInfoUpdateHandler(
private val deviceCache: DeviceCache,
private val identityManager: IdentityManager,
private val offlineEntitlementsManager: OfflineEntitlementsManager,
private val appConfig: AppConfig,
private val handler: Handler = Handler(Looper.getMainLooper()),
) {

Expand Down Expand Up @@ -57,8 +59,10 @@ internal class CustomerInfoUpdateHandler(
private fun afterSetListener(listener: UpdatedCustomerInfoListener?) {
if (listener != null) {
log(LogIntent.DEBUG, ConfigureStrings.LISTENER_SET)
getCachedCustomerInfo(identityManager.currentAppUserID)?.let {
notifyListeners(it)
if (!appConfig.customEntitlementComputation) {
getCachedCustomerInfo(identityManager.currentAppUserID)?.let {
notifyListeners(it)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ internal class PurchasesFactory(
cache,
identityManager,
offlineEntitlementsManager,
appConfig = appConfig,
)

val postReceiptHelper = PostReceiptHelper(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.revenuecat.purchases

import androidx.test.ext.junit.runners.AndroidJUnit4
import com.revenuecat.purchases.common.AppConfig
import com.revenuecat.purchases.common.caching.DeviceCache
import com.revenuecat.purchases.common.offlineentitlements.OfflineEntitlementsManager
import com.revenuecat.purchases.identity.IdentityManager
Expand All @@ -20,6 +21,7 @@ class CustomerInfoUpdateHandlerTest {
private lateinit var deviceCache: DeviceCache
private lateinit var identityManager: IdentityManager
private lateinit var offlineEntitlementsManager: OfflineEntitlementsManager
private lateinit var appConfig: AppConfig

private lateinit var customerInfoUpdateHandler: CustomerInfoUpdateHandler

Expand All @@ -31,16 +33,19 @@ class CustomerInfoUpdateHandlerTest {
deviceCache = mockk()
identityManager = mockk()
offlineEntitlementsManager = mockk()
appConfig = mockk()

every { identityManager.currentAppUserID } returns appUserId
every { deviceCache.getCachedCustomerInfo(appUserId) } returns mockInfo
every { deviceCache.cacheCustomerInfo(appUserId, mockInfo) } just Runs
every { offlineEntitlementsManager.offlineCustomerInfo } returns null
every { appConfig.customEntitlementComputation } returns false

customerInfoUpdateHandler = CustomerInfoUpdateHandler(
deviceCache,
identityManager,
offlineEntitlementsManager,
appConfig = appConfig,
)
}

Expand All @@ -55,6 +60,15 @@ class CustomerInfoUpdateHandlerTest {
verify(exactly = 1) { listenerMock.onReceived(mockInfo) }
}

@Test
fun `setting listener doesn't send cached value if custom entitlements computation enabled`() {
every { appConfig.customEntitlementComputation } returns true
val listenerMock = mockk<UpdatedCustomerInfoListener>(relaxed = true)
customerInfoUpdateHandler.updatedCustomerInfoListener = listenerMock

verify(exactly = 0) { listenerMock.onReceived(mockInfo) }
}

@Test
fun `setting listener sends offline customer info cached value if it exists over cached value`() {
val mockCustomerInfo2 = mockk<CustomerInfo>()
Expand All @@ -66,7 +80,7 @@ class CustomerInfoUpdateHandlerTest {
}

@Test
fun `setting listener does not send cached value if it does not exists`() {
fun `setting listener does not send cached value if it does not exist`() {
val listenerMock = mockk<UpdatedCustomerInfoListener>(relaxed = true)
every { deviceCache.getCachedCustomerInfo(any()) } returns null
customerInfoUpdateHandler.updatedCustomerInfoListener = listenerMock
Expand Down