Skip to content

Instantly share code, notes, and snippets.

@lion4ik
Last active December 7, 2018 20:00
Show Gist options
  • Save lion4ik/7346759585e4d12bd0f9cb90f791edfe to your computer and use it in GitHub Desktop.
Save lion4ik/7346759585e4d12bd0f9cb90f791edfe to your computer and use it in GitHub Desktop.
class SomeClass {
private val channel: BroadcastChannel<List<Purchase>> = BroadcastChannel(Channel.CONFLATED)
override fun onPurchasesUpdated(responseCode: Int, purchases: MutableList<Purchase>?) {
GlobalScope.launch {
purchases?.run { channel.send(toList()) }
}
}
fun getPurchasesUpdatesChannel(): ReceiveChannel<List<Purchase>> = channel.openSubscription()
}
---using
somewhere in another class...
private val billingManager: SomeClass = SomeClass()
init {
launch(coroutineContext + coroutineExceptionHandler) {
billingManager.getPurchasesUpdatesChannel().consumeEach { purchases ->
if (purchases.isEmpty()) {
if (questionRepository.hasSections()) {
questionRepository.removeSections()
}
} else {
if (!questionRepository.hasSections()) {
purchases.find { it.sku == SKU_ITEM }?.run { downloadTest(this) }
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment