I have a situation where my BillingDataSource aka BillingClientWrapper needs a singleton BillingClient object, but creating the BillingClient object can't be completed without a listener that's in the BillingDataSource:
class BillingDataSource(private val client: BillingClient) {
private val _purchases = MutableStateFlow<List<Purchase>?>(mutableListOf())
val purchases = _purchases.asStateFlow()
val PURCHASES_UPDATED_LISTENER = PurchasesUpdatedListener { result, purchases ->
_purchases.value = purchases
}
}
And the dependency injection:
@Provides
@Singleton
fun provideBillingClient(
@ApplicationContext context: Context,
wrapper: BillingDataSource
) : BillingClient {
return BillingClient.newBuilder(context)
.setListener(wrapper.PURCHASES_UPDATED_LISTENER)
.enablePendingPurchases()
.build()
}
@Provides
@Singleton
fun provideBillingDataSource(
client: BillingClient
) : BillingDataSource = BillingDataSource(client)
The .setListener requirement when constructing the BillingClient is turning out to be a real headache. Of course, I can break the circular dependency by putting the listener outside of the BillingDataSource, but then I'd lose access to members (like _purchases) inside of BillingDataSource, and that's hardly ideal.
How do I solve this?
BillingClientsome kind ofinitmethod that creates and registers the listener..build()call.BillingClientis not a class under my control (it's part of the Billing Library provided by Google). So it's only theBillingDataSourceI'm able to modify.