Address that gplay api can emit multiple times (#114)

This commit is contained in:
Matthias Urhahn
2023-03-12 02:58:37 +01:00
committed by GitHub
parent a3e2c9e61d
commit b85ac778c0
3 changed files with 33 additions and 32 deletions
@@ -14,15 +14,15 @@ import eu.darken.capod.common.debug.logging.logTag
import eu.darken.capod.common.flow.setupCommonEventHandlers import eu.darken.capod.common.flow.setupCommonEventHandlers
import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.channels.trySendBlocking
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.callbackFlow import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.retryWhen import kotlinx.coroutines.flow.retryWhen
import kotlinx.coroutines.launch
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
@Singleton @Singleton
class BillingClientConnectionProvider @Inject constructor( class BillingClientConnectionProvider @Inject constructor(
@@ -48,36 +48,36 @@ class BillingClientConnectionProvider @Inject constructor(
} }
}.build() }.build()
val connectionResult = suspendCoroutine<BillingResult> { continuation ->
log(TAG, VERBOSE) { "startConnection(...)" } log(TAG, VERBOSE) { "startConnection(...)" }
client.startConnection(object : BillingClientStateListener { client.startConnection(object : BillingClientStateListener {
override fun onBillingSetupFinished(result: BillingResult) { override fun onBillingSetupFinished(result: BillingResult) {
log(TAG, VERBOSE) { log(TAG, VERBOSE) {
"onBillingSetupFinished(code=${result.responseCode}, message=${result.debugMessage})" "onBillingSetupFinished(code=${result.responseCode}, message=${result.debugMessage})"
}
val billingClientConnection = when (result.responseCode) {
BillingResponseCode.OK -> BillingClientConnection(client, purchasePublisher)
else -> throw BillingClientException(result)
}
trySendBlocking(billingClientConnection)
launch {
try {
purchasePublisher.value = billingClientConnection.queryPurchases()
log(TAG) { "Initial IAP query successful." }
} catch (e: Exception) {
log(TAG, ERROR) { "Initial IAP query failed:\n${e.asLog()}" }
} }
continuation.resume(result)
} }
}
override fun onBillingServiceDisconnected() { override fun onBillingServiceDisconnected() {
log(TAG, VERBOSE) { "onBillingServiceDisconnected() " } log(TAG, VERBOSE) { "onBillingServiceDisconnected() " }
close(CancellationException("Billing service disconnected")) error(BillingException("Billing service disconnected"))
} }
}) })
}
val billingClientConnection = when (connectionResult.responseCode) {
BillingResponseCode.OK -> BillingClientConnection(client, purchasePublisher)
else -> throw BillingClientException(connectionResult)
}
try {
purchasePublisher.value = billingClientConnection.queryPurchases()
log(TAG) { "Initial IAP query successful." }
} catch (e: Exception) {
log(TAG, ERROR) { "Initial IAP query failed:\n${e.asLog()}" }
}
send(billingClientConnection)
log(TAG) { "Awaiting close." } log(TAG) { "Awaiting close." }
awaitClose { awaitClose {
@@ -2,9 +2,7 @@ package eu.darken.capod.common.upgrade.core.client
import com.android.billingclient.api.BillingResult import com.android.billingclient.api.BillingResult
class BillingClientException(val result: BillingResult) : Exception() { class BillingClientException(val result: BillingResult) : BillingException(result.debugMessage) {
override val message: String?
get() = result.debugMessage
override fun toString(): String = override fun toString(): String =
"BillingClientException(code=${result.responseCode}, message=${result.debugMessage})" "BillingClientException(code=${result.responseCode}, message=${result.debugMessage})"
@@ -0,0 +1,3 @@
package eu.darken.capod.common.upgrade.core.client
open class BillingException(override val message: String) : Exception()