Skip to content

Commit

Permalink
CustomEntitlementsComputation: disable first listener callback when s…
Browse files Browse the repository at this point in the history
…et (#1152)

Disables the first fire of the Customer Info listener when it's set
initially.
  • Loading branch information
aboedo authored Jul 20, 2023
1 parent 6ec7485 commit 3e9dc9a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
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

0 comments on commit 3e9dc9a

Please sign in to comment.