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

Added request trigger and attempt index headers to restore and purchase requests. #660

Merged
merged 3 commits into from
Mar 20, 2025
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
23 changes: 12 additions & 11 deletions config/detekt/baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<ID>EmptyFunctionBlock:QAutomationsManagerTest.kt$QAutomationsManagerTest.&lt;no name provided&gt;${}</ID>
<ID>EmptyFunctionBlock:QIdentityManagerTest.kt$QIdentityManagerTest.Identify.&lt;no name provided&gt;${}</ID>
<ID>EmptyFunctionBlock:QProductCenterManager.kt$QProductCenterManager.&lt;no name provided&gt;${}</ID>
<ID>EmptyFunctionBlock:QonversionInternal.kt$QonversionInternal.&lt;no name provided&gt;${}</ID>
<ID>Filename:com.qonversion.android.sdk.internal.storage.util.kt:1</ID>
<ID>FinalNewline:com.qonversion.android.sdk.automations.internal.AutomationsEventMapperTest.kt:1</ID>
<ID>FinalNewline:com.qonversion.android.sdk.automations.internal.QAutomationsManagerTest.kt:1</ID>
Expand Down Expand Up @@ -122,6 +121,7 @@
<ID>MaxLineLength:QonversionError.kt$QonversionErrorCode$RemoteConfigurationNotAvailable : QonversionErrorCode</ID>
<ID>MaxLineLength:QonversionInternal.kt$QonversionInternal$@Deprecated("Use the new purchase() method", replaceWith = ReplaceWith("purchase(context, TODO(\"pass product here\"), callback)"))</ID>
<ID>MaxLineLength:QonversionInternal.kt$QonversionInternal$@Deprecated("Use the new updatePurchase() method", replaceWith = ReplaceWith("updatePurchase(context, TODO(\"pass product here\"), TODO(\"pass purchase options here\"), callback)"))</ID>
<ID>MaxLineLength:QonversionInternal.kt$QonversionInternal$productCenterManager</ID>
<ID>MaxLineLength:QonversionRepositoryIntegrationTest.kt$QonversionRepositoryIntegrationTest$"""HTTP status code=400, data={"message":"Invalid access token received","code":10003,"status":400,"extra":[]}. """</ID>
<ID>MaxLineLength:QonversionRepositoryIntegrationTest.kt$QonversionRepositoryIntegrationTest$"efnkoceomiocidgigbalneag.AO-J1Ow8JyXX8gs8W9blILbU9Nqy3Mr-RMTLgFG2DLOhO1urkWpr80PUSE6eA0IuEzl_k4Guecep5JZJwcnSYOWzTZhqwusafayRCbv4kRMUR-rR9Ot11nQ"</ID>
<ID>MaxLineLength:QonversionRepositoryIntegrationTest.kt$QonversionRepositoryIntegrationTest$"lgeigljfpmeoddkcebkcepjc.AO-J1Oy305qZj99jXTPEVBN8UZGoYAtjDLj4uTjRQvUFaG0vie-nr6VBlN0qnNDMU8eJR-sI7o3CwQyMOEHKl8eJsoQ86KSFzxKBR07PSpHLI_o7agXhNKY"</ID>
Expand Down Expand Up @@ -157,20 +157,21 @@
<ID>MaximumLineLength:com.qonversion.android.sdk.dto.products.QProduct.kt:120</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.dto.products.QProduct.kt:139</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.dto.products.QProductStoreDetails.kt:130</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.OutagerIntegrationTest.kt:214</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.OutagerIntegrationTest.kt:371</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.OutagerIntegrationTest.kt:90</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.QProductCenterManager.kt:333</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.OutagerIntegrationTest.kt:216</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.OutagerIntegrationTest.kt:373</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.OutagerIntegrationTest.kt:91</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.QProductCenterManager.kt:341</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.QProductCenterManagerTest.kt:152</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.QProductCenterManagerTest.kt:153</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.QRemoteConfigManager.kt:225</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.QUserPropertiesManagerTest.kt:178</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.QonversionInternal.kt:164</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.QonversionInternal.kt:215</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.QonversionRepositoryIntegrationTest.kt:287</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.QonversionRepositoryIntegrationTest.kt:356</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.QonversionRepositoryIntegrationTest.kt:878</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.QonversionRepositoryIntegrationTest.kt:89</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.QonversionInternal.kt:144</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.QonversionInternal.kt:155</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.QonversionInternal.kt:206</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.QonversionRepositoryIntegrationTest.kt:290</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.QonversionRepositoryIntegrationTest.kt:359</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.QonversionRepositoryIntegrationTest.kt:882</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.QonversionRepositoryIntegrationTest.kt:90</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.api.ApiErrorMapper.kt:118</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.api.ApiErrorMapper.kt:119</ID>
<ID>MaximumLineLength:com.qonversion.android.sdk.internal.billing.QonversionBillingService.kt:253</ID>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.qonversion.android.sdk.dto.offerings.QOffering
import com.qonversion.android.sdk.dto.offerings.QOfferingTag
import com.qonversion.android.sdk.dto.offerings.QOfferings
import com.qonversion.android.sdk.dto.products.QProduct
import com.qonversion.android.sdk.internal.api.RequestTrigger
import com.qonversion.android.sdk.internal.di.QDependencyInjector
import com.qonversion.android.sdk.internal.dto.QLaunchResult
import com.qonversion.android.sdk.internal.dto.QPermission
Expand Down Expand Up @@ -124,7 +125,8 @@ internal class OutagerIntegrationTest {
override fun onError(error: QonversionError) {
fail("Shouldn't fail")
}
}
},
RequestTrigger.Init
)

val repository = initRepository(uid)
Expand Down Expand Up @@ -269,7 +271,7 @@ internal class OutagerIntegrationTest {
fail("Failed to create user")
}

repository.restoreRequest(installDate, history, callback)
repository.restoreRequest(installDate, history, callback, RequestTrigger.Restore)
}

signal.await()
Expand Down Expand Up @@ -525,7 +527,8 @@ internal class OutagerIntegrationTest {
override fun onError(error: QonversionError) {
onComplete(error)
}
}
},
RequestTrigger.Init
)
repository.init(data)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.qonversion.android.sdk.dto.offerings.QOfferingTag
import com.qonversion.android.sdk.dto.offerings.QOfferings
import com.qonversion.android.sdk.dto.products.QProduct
import com.qonversion.android.sdk.dto.properties.QUserProperty
import com.qonversion.android.sdk.internal.api.RequestTrigger
import com.qonversion.android.sdk.internal.di.QDependencyInjector
import com.qonversion.android.sdk.internal.dto.QLaunchResult
import com.qonversion.android.sdk.internal.dto.QPermission
Expand Down Expand Up @@ -119,7 +120,8 @@ internal class QonversionRepositoryIntegrationTest {
override fun onError(error: QonversionError) {
fail("Shouldn't fail")
}
}
},
RequestTrigger.Init
)

val repository = initRepository(uid)
Expand Down Expand Up @@ -150,7 +152,8 @@ internal class QonversionRepositoryIntegrationTest {
assertIncorrectProjectKeyError(error)
signal.countDown()
}
}
},
RequestTrigger.Init
)

val repository = initRepository(uid, INCORRECT_PROJECT_KEY)
Expand Down Expand Up @@ -339,7 +342,7 @@ internal class QonversionRepositoryIntegrationTest {
fail("Failed to create user")
}

repository.restoreRequest(installDate, history, callback)
repository.restoreRequest(installDate, history, callback, RequestTrigger.Restore)
}

signal.await()
Expand Down Expand Up @@ -373,7 +376,7 @@ internal class QonversionRepositoryIntegrationTest {
val repository = initRepository(uid, INCORRECT_PROJECT_KEY)

// when
repository.restoreRequest(installDate, history, callback)
repository.restoreRequest(installDate, history, callback, RequestTrigger.Restore)

signal.await()
}
Expand Down Expand Up @@ -850,7 +853,8 @@ internal class QonversionRepositoryIntegrationTest {
override fun onError(error: QonversionError) {
onComplete(error)
}
}
},
RequestTrigger.Init
)
repository.init(data)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import com.qonversion.android.sdk.dto.entitlements.QEntitlementsCacheLifetime
import com.qonversion.android.sdk.dto.offerings.QOfferings
import com.qonversion.android.sdk.dto.products.QProduct
import com.qonversion.android.sdk.dto.products.QProductType
import com.qonversion.android.sdk.internal.api.RequestTrigger
import com.qonversion.android.sdk.internal.dto.QProductRenewState
import com.qonversion.android.sdk.internal.billing.BillingError
import com.qonversion.android.sdk.internal.billing.BillingService
Expand Down Expand Up @@ -126,7 +127,7 @@ internal class QProductCenterManager internal constructor(
processPendingInitIfAvailable()
}

fun launch(callback: QonversionLaunchCallback? = null) {
fun launch(requestTrigger: RequestTrigger, callback: QonversionLaunchCallback? = null) {
val launchCallback: QonversionLaunchCallback = getLaunchCallback(callback)
launchError = null
launchResultCache.resetSessionCache()
Expand All @@ -136,15 +137,15 @@ internal class QProductCenterManager internal constructor(
adProvider.init(context, object : AdvertisingProvider.Callback {
override fun onSuccess(advertisingId: String) {
advertisingID = advertisingId
continueLaunchWithPurchasesInfo(launchCallback)
continueLaunchWithPurchasesInfo(launchCallback, requestTrigger)
}

override fun onFailure(t: Throwable) {
continueLaunchWithPurchasesInfo(launchCallback)
continueLaunchWithPurchasesInfo(launchCallback, requestTrigger)
}
})
} else {
continueLaunchWithPurchasesInfo(launchCallback)
continueLaunchWithPurchasesInfo(launchCallback, requestTrigger)
}
}

Expand All @@ -156,7 +157,7 @@ internal class QProductCenterManager internal constructor(

launchResultCache.sessionLaunchResult?.let {
loadStoreProductsIfPossible()
} ?: launch()
} ?: launch(RequestTrigger.Products)
}

fun offerings(
Expand Down Expand Up @@ -204,7 +205,13 @@ internal class QProductCenterManager internal constructor(
}
}

val initRequestData = InitRequestData(installDate, advertisingID, callback = launchCallback)
val initRequestData = InitRequestData(
installDate,
advertisingID,
null,
launchCallback,
RequestTrigger.Identify
)
repository.init(initRequestData)
} else {
processIdentity(identityId)
Expand All @@ -226,7 +233,7 @@ internal class QProductCenterManager internal constructor(
internalConfig.uid = qonversionUid
remoteConfigManager.onUserUpdate()
launchResultCache.clearPermissionsCache()
launch(object : QonversionLaunchCallback {
launch(RequestTrigger.Identify, object : QonversionLaunchCallback {
override fun onSuccess(launchResult: QLaunchResult) {
fireIdentitySuccess(identityId)
}
Expand Down Expand Up @@ -309,7 +316,8 @@ internal class QProductCenterManager internal constructor(
if (launchError != null) {
retryLaunch(
onSuccess = { tryToPurchase() },
onError = { tryToPurchase() }
onError = { tryToPurchase() },
requestTrigger = RequestTrigger.Purchase,
)
} else {
tryToPurchase()
Expand Down Expand Up @@ -394,7 +402,7 @@ internal class QProductCenterManager internal constructor(
handlePendingRequests()
}

fun restore(callback: QonversionEntitlementsCallback? = null) {
fun restore(requestTrigger: RequestTrigger, callback: QonversionEntitlementsCallback? = null) {
callback?.let { restoreCallbacks.add(it) }

if (isRestoreInProgress) {
Expand Down Expand Up @@ -422,12 +430,14 @@ internal class QProductCenterManager internal constructor(
executeRestoreBlocksOnError(error)
}
}
})
},
requestTrigger
)
}
}

fun syncPurchases() {
restore()
restore(RequestTrigger.SyncPurchases)
}

override fun onPurchasesCompleted(purchases: List<Purchase>) {
Expand Down Expand Up @@ -657,11 +667,12 @@ internal class QProductCenterManager internal constructor(
}

private fun continueLaunchWithPurchasesInfo(
callback: QonversionLaunchCallback?
callback: QonversionLaunchCallback,
requestTrigger: RequestTrigger,
) {
fun processInitDefault() {
val initRequestData =
InitRequestData(installDate, advertisingID, callback = callback)
InitRequestData(installDate, advertisingID, null, callback, requestTrigger)
processInit(initRequestData)
}

Expand All @@ -687,27 +698,28 @@ internal class QProductCenterManager internal constructor(
installDate,
advertisingID,
purchasesInfo,
handledPurchasesCallback
handledPurchasesCallback,
requestTrigger
)
processInit(initRequestData)
}
}

private fun getWrappedPurchasesCallback(
trackingPurchases: List<Purchase>,
outerCallback: QonversionLaunchCallback?
outerCallback: QonversionLaunchCallback
): QonversionLaunchCallback {
return object : QonversionLaunchCallback {
override fun onSuccess(launchResult: QLaunchResult) {
handledPurchasesCache.saveHandledPurchases(trackingPurchases)
trackingPurchases.forEach {
removePurchaseOptions(it.productId)
}
outerCallback?.onSuccess(launchResult)
outerCallback.onSuccess(launchResult)
}

override fun onError(error: QonversionError) {
outerCallback?.onError(error)
outerCallback.onError(error)
}
}
}
Expand Down Expand Up @@ -773,7 +785,7 @@ internal class QProductCenterManager internal constructor(

private fun handleLogout() {
unhandledLogoutAvailable = false
launch()
launch(RequestTrigger.Logout)
}

private fun updateLaunchResult(launchResult: QLaunchResult) {
Expand Down Expand Up @@ -918,9 +930,10 @@ internal class QProductCenterManager internal constructor(

private fun retryLaunch(
onSuccess: (QLaunchResult) -> Unit,
onError: (QonversionError) -> Unit
onError: (QonversionError) -> Unit,
requestTrigger: RequestTrigger,
) {
launch(object : QonversionLaunchCallback {
launch(requestTrigger, object : QonversionLaunchCallback {
override fun onSuccess(launchResult: QLaunchResult) = onSuccess(launchResult)
override fun onError(error: QonversionError) = onError(error)
})
Expand Down Expand Up @@ -954,7 +967,9 @@ internal class QProductCenterManager internal constructor(
cachedPermissions?.let {
onSuccess(it)
} ?: onError(error)
})
},
requestTrigger = RequestTrigger.ActualizePermissions
)
}

val permissions = launchResultCache.getActualPermissions() ?: emptyMap()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.qonversion.android.sdk.dto.properties.QUserPropertyKey
import com.qonversion.android.sdk.dto.QonversionError
import com.qonversion.android.sdk.dto.QonversionErrorCode
import com.qonversion.android.sdk.dto.properties.QUserProperties
import com.qonversion.android.sdk.internal.api.RequestTrigger
import com.qonversion.android.sdk.listeners.QonversionLaunchCallback
import com.qonversion.android.sdk.internal.dto.QLaunchResult
import com.qonversion.android.sdk.internal.logger.Logger
Expand Down Expand Up @@ -106,15 +107,18 @@ internal class QUserPropertiesManager @Inject internal constructor(
isRequestInProgress = false

if (it.code === QonversionErrorCode.InvalidClientUid) {
productCenterManager?.launch(callback = object : QonversionLaunchCallback {
override fun onSuccess(launchResult: QLaunchResult) {
retryPropertiesRequest()
productCenterManager?.launch(
RequestTrigger.UserProperties,
object : QonversionLaunchCallback {
override fun onSuccess(launchResult: QLaunchResult) {
retryPropertiesRequest()
}

override fun onError(error: QonversionError) {
retryPropertiesRequest()
}
}

override fun onError(error: QonversionError) {
retryPropertiesRequest()
}
})
)
} else {
retryPropertiesRequest()
}
Expand Down
Loading