Skip to content

Commit

Permalink
Merge pull request #96 from cb-pravinrajmohan/fix-multiple-callbacks
Browse files Browse the repository at this point in the history
Fix multiple callbacks
  • Loading branch information
cb-haripriyan authored Dec 7, 2023
2 parents dc74884 + 524f578 commit fd415f0
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion chargebee/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion chargebee/src/main/java/com/chargebee/android/Chargebee.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<Pair<(Boolean) -> Unit, (connectionError: CBException) -> Unit>>()

init {
this.mContext = context
}
Expand Down Expand Up @@ -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)
)
}
}
}

Expand Down Expand Up @@ -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))
}
}
}
}
Expand Down Expand Up @@ -624,4 +645,5 @@ class BillingClientManager(context: Context) : PurchasesUpdatedListener {
completionCallback.onError(error)
})
}
}

}

0 comments on commit fd415f0

Please sign in to comment.