Skip to content

Commit

Permalink
fix: add initilize(lifecycleOwner: LifecycleOwner)
Browse files Browse the repository at this point in the history
  • Loading branch information
ch4rl3x committed Jul 17, 2024
1 parent 882621a commit b16d167
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion billing/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ apply from: '../_ktlint.gradle'

ext {
PUBLISH_GROUP_ID = 'de.charlex.billing'
PUBLISH_VERSION = '7.0.0-1.0.0'
PUBLISH_VERSION = '7.0.0-1.0.1'
PUBLISH_ARTIFACT_ID = 'billing-suspend'
}

Expand Down
17 changes: 9 additions & 8 deletions billing/src/main/java/de/charlex/billing/BillingHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import kotlinx.coroutines.withTimeoutOrNull

class BillingHelper(
context: Context,
lifecycleOwner: LifecycleOwner,
billingClientBuilder: BillingClient.Builder.() -> Unit,
onPurchasesResult: (purchasesResult: PurchasesResult) -> Unit
) : BillingClientStateListener {
Expand Down Expand Up @@ -73,7 +72,9 @@ class BillingHelper(
onPurchasesResult(PurchasesResult(billingResult, purchases ?: emptyList()))
}
}.build()
}

fun initilize(lifecycleOwner: LifecycleOwner) {
billingClientStatus.tryEmit(BillingClient.BillingResponseCode.SERVICE_DISCONNECTED)
lifecycleOwner.lifecycleScope.launch {
// repeatOnLifecycle launches the block in a new coroutine every time the
Expand All @@ -93,7 +94,7 @@ class BillingHelper(
}
}

private suspend fun requireBillingClientSetup(): Boolean =
private suspend fun isBillingClientConnected(): Boolean =
withTimeoutOrNull(5_000) {
billingClientStatus.firstOrNull() == BillingClient.BillingResponseCode.OK
} ?: false
Expand Down Expand Up @@ -137,7 +138,7 @@ class BillingHelper(
*/
suspend fun acknowledgePurchase(purchaseToken: String): BillingResult? = withContext(Dispatchers.IO) {
val acknowledgePurchaseParams = AcknowledgePurchaseParams.newBuilder().setPurchaseToken(purchaseToken).build()
return@withContext if (requireBillingClientSetup()) {
return@withContext if (isBillingClientConnected()) {
val result = billingClient.acknowledgePurchase(acknowledgePurchaseParams)
if (result.responseCode == BillingClient.BillingResponseCode.OK) {
Log.d("BillingHelper", "Purchase acknowleged! (Token: $purchaseToken)")
Expand Down Expand Up @@ -167,7 +168,7 @@ class BillingHelper(
*/
suspend fun consume(purchaseToken: String): ConsumeResult? = withContext(Dispatchers.IO) {
val consumeParams = ConsumeParams.newBuilder().setPurchaseToken(purchaseToken).build()
return@withContext if (requireBillingClientSetup()) {
return@withContext if (isBillingClientConnected()) {
billingClient.consumePurchase(consumeParams)
} else {
null
Expand All @@ -192,7 +193,7 @@ class BillingHelper(
*/
suspend fun queryPurchases(@ProductType productType: String): PurchasesResult? = withContext(Dispatchers.IO) {
Log.d("BillingHelper", "queryPurchases")
return@withContext if (requireBillingClientSetup()) {
return@withContext if (isBillingClientConnected()) {
Log.d("BillingHelper", "queryPurchases on billingClient")
billingClient.queryPurchasesAsync(
QueryPurchasesParams.newBuilder()
Expand Down Expand Up @@ -258,7 +259,7 @@ class BillingHelper(
offerToken: String? = null,
isOfferPersonalized: Boolean = false
) {
if (requireBillingClientSetup()) {
if (isBillingClientConnected()) {
Log.d("BillingHelper", "purchase ${productDetails.name}")

val productDetailsParamsList = listOf(
Expand Down Expand Up @@ -305,7 +306,7 @@ class BillingHelper(
}

suspend fun queryProductDetails(productDetailsParams: QueryProductDetailsParams): List<ProductDetails>? = withContext(Dispatchers.IO) {
if (requireBillingClientSetup()) {
if (isBillingClientConnected()) {
val productDetailsResult = billingClient.queryProductDetails(productDetailsParams)
Log.d("BillingHelper", "Billing Result: ${productDetailsResult.productDetailsList?.size}")
return@withContext productDetailsResult.productDetailsList
Expand All @@ -315,7 +316,7 @@ class BillingHelper(
}

suspend fun queryPurchaseHistory(queryPurchaseHistoryParams: QueryPurchaseHistoryParams): PurchaseHistoryResult? = withContext(Dispatchers.IO) {
if (requireBillingClientSetup()) {
if (isBillingClientConnected()) {
return@withContext billingClient.queryPurchaseHistory(queryPurchaseHistoryParams)
} else {
return@withContext null
Expand Down

0 comments on commit b16d167

Please sign in to comment.