Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

Commit

Permalink
fix: App Crashing on background for IAP users
Browse files Browse the repository at this point in the history
- Use `CancellableContinuation` instated of `Continuation`.
- Resume `continuation` with results if active and not completed.

fixes: LEARNER-9527
  • Loading branch information
farhan-arshad-dev authored and omerhabib26 committed Sep 12, 2023
1 parent fd873a1 commit 88275e0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.edx.mobile.extenstion

import kotlinx.coroutines.CancellableContinuation
import kotlin.coroutines.resume

fun CancellableContinuation<Boolean>.resumeIfActive(value: Boolean) {
if (isActive && isCompleted.not()) {
resume(value)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withContext
import org.edx.mobile.extenstion.encodeToString
import org.edx.mobile.extenstion.resumeIfActive
import org.edx.mobile.logger.Logger
import javax.inject.Inject
import javax.inject.Singleton
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine

/**
* The BillingProcessor implements all billing functionality for application.
Expand Down Expand Up @@ -69,14 +69,14 @@ class BillingProcessor @Inject constructor(
}

private suspend fun connect(): Boolean {
return suspendCoroutine { continuation ->
return suspendCancellableCoroutine { continuation ->
billingClientStateListener = object : BillingClientStateListener {
override fun onBillingSetupFinished(billingResult: BillingResult) {
logger.debug("BillingSetupFinished -> $billingResult")
if (billingResult.responseCode == BillingResponseCode.OK) {
continuation.resume(true)
continuation.resumeIfActive(true)
} else {
continuation.resume(false)
continuation.resumeIfActive(false)
}
}

Expand All @@ -89,7 +89,7 @@ class BillingProcessor @Inject constructor(
connectionTryCount++
retryBillingServiceConnectionWithExponentialBackoff()
}
continuation.resume(false)
continuation.resumeIfActive(false)
}
}
billingClient.startConnection(billingClientStateListener)
Expand Down

0 comments on commit 88275e0

Please sign in to comment.