From 4b59295bd334c8c6502b66891cf748b7341fdeb4 Mon Sep 17 00:00:00 2001 From: cb-pravin Date: Thu, 7 Dec 2023 19:35:03 +0530 Subject: [PATCH 1/2] fix: multiple callbacks --- .../billingservice/BillingClientManager.kt | 78 ++++++++++++------- 1 file changed, 50 insertions(+), 28 deletions(-) diff --git a/chargebee/src/main/java/com/chargebee/android/billingservice/BillingClientManager.kt b/chargebee/src/main/java/com/chargebee/android/billingservice/BillingClientManager.kt index ce7912b..0dd0a1f 100644 --- a/chargebee/src/main/java/com/chargebee/android/billingservice/BillingClientManager.kt +++ b/chargebee/src/main/java/com/chargebee/android/billingservice/BillingClientManager.kt @@ -16,6 +16,7 @@ import com.chargebee.android.models.CBNonSubscriptionResponse import com.chargebee.android.models.CBProduct import com.chargebee.android.network.CBReceiptResponse import com.chargebee.android.restore.CBRestorePurchaseManager +import java.util.concurrent.ConcurrentLinkedQueue import kotlin.collections.ArrayList class BillingClientManager(context: Context) : PurchasesUpdatedListener { @@ -31,6 +32,8 @@ class BillingClientManager(context: Context) : PurchasesUpdatedListener { private lateinit var restorePurchaseCallBack: CBCallback.RestorePurchaseCallback private var oneTimePurchaseCallback: CBCallback.OneTimePurchaseCallback? = null + private val requests = ConcurrentLinkedQueue Unit, (connectionError: CBException) -> Unit>>() + init { this.mContext = context } @@ -252,16 +255,16 @@ class BillingClientManager(context: Context) : PurchasesUpdatedListener { } } } - else -> { - if (product.skuDetails.type == ProductType.SUBS.value) - purchaseCallBack?.onError( - throwCBException(billingResult) - ) - else - oneTimePurchaseCallback?.onError( - throwCBException(billingResult) - ) - } + else -> { + if (product.skuDetails.type == ProductType.SUBS.value) + purchaseCallBack?.onError( + throwCBException(billingResult) + ) + else + oneTimePurchaseCallback?.onError( + throwCBException(billingResult) + ) + } } } @@ -492,34 +495,52 @@ class BillingClientManager(context: Context) : PurchasesUpdatedListener { private fun onConnected(status: (Boolean) -> Unit, connectionError: (CBException) -> Unit) { val billingClient = buildBillingClient(this) + requests.add(Pair(status, connectionError)) if (billingClient?.isReady == false) { - handler.postDelayed({ - billingClient.startConnection( - createBillingClientStateListener(status, connectionError) - ) - }, CONNECT_TIMER_START_MILLISECONDS) - } else status(true) + billingClient.startConnection( + createBillingClientStateListener() + ) + } else { + executeRequestsInQueue() + } + } + + @Synchronized + private fun executeRequestsInQueue() { + val head = requests.poll() + if (head != null) { + val successHandler = head.first + handler.post { + successHandler(true) + } + } + } + + @Synchronized + private fun sendErrorToRequestsInQueue(exception: CBException) { + val head = requests.poll() + if(head != null) { + val exceptionHandler = head.second + handler.post { + exceptionHandler(exception) + } + } } - private fun createBillingClientStateListener( - status: (Boolean) -> Unit, - connectionError: (CBException) -> Unit - ) = object : - BillingClientStateListener { + private fun createBillingClientStateListener() = object : BillingClientStateListener { + override fun onBillingServiceDisconnected() { Log.i(javaClass.simpleName, "onBillingServiceDisconnected") - status(false) } override fun onBillingSetupFinished(billingResult: BillingResult) { when (billingResult.responseCode) { OK -> { Log.i(TAG, "Google Billing Setup Done!") - status(true) - } - else -> { - connectionError(throwCBException(billingResult)) - } + executeRequestsInQueue() + } else -> { + sendErrorToRequestsInQueue(throwCBException(billingResult)) + } } } } @@ -624,4 +645,5 @@ class BillingClientManager(context: Context) : PurchasesUpdatedListener { completionCallback.onError(error) }) } -} + +} \ No newline at end of file From 524f578aa087e1bd3c58ce196e313cfd202c40cf Mon Sep 17 00:00:00 2001 From: cb-pravin Date: Thu, 7 Dec 2023 19:36:30 +0530 Subject: [PATCH 2/2] updated version --- README.md | 2 +- chargebee/build.gradle | 2 +- chargebee/src/main/java/com/chargebee/android/Chargebee.kt | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 115ca58..55593ab 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ The following requirements must be set up before installing Chargebee’s Androi The `Chargebee-Android` SDK can be installed by adding below dependency to the `build.gradle` file: ```kotlin -implementation 'com.chargebee:chargebee-android:1.2.0' +implementation 'com.chargebee:chargebee-android:1.2.1' ``` ## Example project diff --git a/chargebee/build.gradle b/chargebee/build.gradle index 7b2d19a..4198c6f 100644 --- a/chargebee/build.gradle +++ b/chargebee/build.gradle @@ -8,7 +8,7 @@ android { minSdkVersion 21 targetSdkVersion 31 versionCode 1 - versionName "1.2.0" + versionName "1.2.1" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" consumerProguardFiles "consumer-rules.pro" diff --git a/chargebee/src/main/java/com/chargebee/android/Chargebee.kt b/chargebee/src/main/java/com/chargebee/android/Chargebee.kt index 763ee1f..f652dd7 100644 --- a/chargebee/src/main/java/com/chargebee/android/Chargebee.kt +++ b/chargebee/src/main/java/com/chargebee/android/Chargebee.kt @@ -35,7 +35,7 @@ object Chargebee { var appName: String = "Chargebee" var environment: String = "cb_android_sdk" const val platform: String = "Android" - const val sdkVersion: String = "1.2.0" + const val sdkVersion: String = "1.2.1" const val limit: String = "100" private const val PLAY_STORE_SUBSCRIPTION_URL = "https://play.google.com/store/account/subscriptions"