Delay init EBegging

This commit is contained in:
Mygod
2022-09-24 19:34:20 -04:00
parent 28b1a1a0fc
commit e44d0ca295
2 changed files with 43 additions and 30 deletions

View File

@@ -83,7 +83,6 @@ class App : Application() {
} }
}) })
ServiceNotification.updateNotificationChannels() ServiceNotification.updateNotificationChannels()
EBegFragment.init()
if (DhcpWorkaround.shouldEnable) DhcpWorkaround.enable(true) if (DhcpWorkaround.shouldEnable) DhcpWorkaround.enable(true)
} }

View File

@@ -23,28 +23,30 @@ import timber.log.Timber
*/ */
class EBegFragment : AppCompatDialogFragment() { class EBegFragment : AppCompatDialogFragment() {
companion object : BillingClientStateListener, PurchasesUpdatedListener { companion object : BillingClientStateListener, PurchasesUpdatedListener {
private lateinit var billingClient: BillingClient private val billingClient by lazy {
BillingClient.newBuilder(app).apply {
fun init() {
billingClient = BillingClient.newBuilder(app).apply {
enablePendingPurchases() enablePendingPurchases()
}.setListener(this).build().also { it.startConnection(this) } }.setListener(this).build()
} }
private var instance: EBegFragment? = null
override fun onBillingSetupFinished(billingResult: BillingResult) { override fun onBillingSetupFinished(billingResult: BillingResult) {
if (billingResult.responseCode != BillingClient.BillingResponseCode.OK) { if (billingResult.responseCode != BillingClient.BillingResponseCode.OK) {
Timber.e("onBillingSetupFinished: ${billingResult.responseCode}") Timber.e("onBillingSetupFinished: ${billingResult.responseCode}")
} else GlobalScope.launch(Dispatchers.Main.immediate) { } else {
val result = billingClient.queryPurchasesAsync(QueryPurchasesParams.newBuilder().apply { instance?.onBillingConnected()
setProductType(BillingClient.ProductType.INAPP) GlobalScope.launch(Dispatchers.Main.immediate) {
}.build()) val result = billingClient.queryPurchasesAsync(QueryPurchasesParams.newBuilder().apply {
onPurchasesUpdated(result.billingResult, result.purchasesList) setProductType(BillingClient.ProductType.INAPP)
}.build())
onPurchasesUpdated(result.billingResult, result.purchasesList)
}
} }
} }
override fun onBillingServiceDisconnected() { override fun onBillingServiceDisconnected() {
Timber.e("onBillingServiceDisconnected") Timber.e("onBillingServiceDisconnected")
billingClient.startConnection(this) if (instance != null) billingClient.startConnection(this)
} }
override fun onPurchasesUpdated(billingResult: BillingResult, purchases: List<Purchase>?) { override fun onPurchasesUpdated(billingResult: BillingResult, purchases: List<Purchase>?) {
@@ -84,24 +86,6 @@ class EBegFragment : AppCompatDialogFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
dialog!!.setTitle(R.string.settings_misc_donate) dialog!!.setTitle(R.string.settings_misc_donate)
viewLifecycleOwner.lifecycleScope.launchWhenCreated {
billingClient.queryProductDetails(QueryProductDetailsParams.newBuilder().apply {
setProductList(listOf(
"donate001", "donate002", "donate005", "donate010", "donate020", "donate050",
"donate100", "donate200", "donatemax",
).map {
QueryProductDetailsParams.Product.newBuilder().apply {
setProductId(it)
setProductType(BillingClient.ProductType.INAPP)
}.build()
})
}.build()).apply {
if (billingResult.responseCode != BillingClient.BillingResponseCode.OK) {
Timber.e("queryProductDetails: ${billingResult.responseCode}")
SmartSnackbar.make(R.string.donations__google_android_market_not_supported).show()
} else productDetails = productDetailsList
}
}
binding.donationsGoogleAndroidMarketDonateButton.setOnClickListener { binding.donationsGoogleAndroidMarketDonateButton.setOnClickListener {
val product = productDetails?.getOrNull(binding.donationsGoogleAndroidMarketSpinner.selectedItemPosition) val product = productDetails?.getOrNull(binding.donationsGoogleAndroidMarketSpinner.selectedItemPosition)
if (product != null) billingClient.launchBillingFlow(requireActivity(), BillingFlowParams.newBuilder().apply { if (product != null) billingClient.launchBillingFlow(requireActivity(), BillingFlowParams.newBuilder().apply {
@@ -115,4 +99,34 @@ class EBegFragment : AppCompatDialogFragment() {
requireContext().launchUrl("https://mygod.be/donate/") requireContext().launchUrl("https://mygod.be/donate/")
} }
} }
override fun onStart() {
super.onStart()
instance = this
billingClient.startConnection(EBegFragment)
}
private fun onBillingConnected() = viewLifecycleOwner.lifecycleScope.launchWhenStarted {
billingClient.queryProductDetails(QueryProductDetailsParams.newBuilder().apply {
setProductList(listOf(
"donate001", "donate002", "donate005", "donate010", "donate020", "donate050",
"donate100", "donate200", "donatemax",
).map {
QueryProductDetailsParams.Product.newBuilder().apply {
setProductId(it)
setProductType(BillingClient.ProductType.INAPP)
}.build()
})
}.build()).apply {
if (billingResult.responseCode != BillingClient.BillingResponseCode.OK) {
Timber.e("queryProductDetails: ${billingResult.responseCode}")
SmartSnackbar.make(R.string.donations__google_android_market_not_supported).show()
} else productDetails = productDetailsList
}
}
override fun onStop() {
instance = null
super.onStop()
}
} }