diff options
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IAB.kt')
-rw-r--r-- | app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IAB.kt | 63 |
1 files changed, 27 insertions, 36 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IAB.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IAB.kt index 19b9b6f7..0fd10c5b 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IAB.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IAB.kt @@ -16,17 +16,20 @@ object IAB { var helper: IabHelper? = null - fun setupAsync(context: Context) { - if (!context.isFromGooglePlay) return + fun setupAsync(activity: Activity) { if (helper == null) { + L.d("IAB setup async") + if (!activity.isFromGooglePlay && !BuildConfig.DEBUG) return L.d("IAB not from google play") try { - helper = IabHelper(context.applicationContext, PUBLIC_BILLING_KEY) + helper = IabHelper(activity.applicationContext, PUBLIC_BILLING_KEY) helper!!.startSetup { result -> + L.d("IAB result ${result.message}") if (!result.isSuccess) L.eThrow("IAB Setup error: $result") } } catch (e: Exception) { L.e(e, "IAB error") + activity.playStoreNoLongerPro() } } } @@ -35,7 +38,7 @@ object IAB { private const val FROST_PRO = "frost_pro" val IS_FROST_PRO: Boolean - get() = (BuildConfig.DEBUG && Prefs.debugPro) || (IAB.helper?.queryInventory()?.getSkuDetails(FROST_PRO) != null) + get() = (BuildConfig.DEBUG && Prefs.debugPro) || Prefs.previouslyPro private fun Context.checkFromPlay(): Boolean { val isPlay = isFromGooglePlay || BuildConfig.DEBUG @@ -52,44 +55,32 @@ private fun Context.checkFromPlay(): Boolean { fun Activity.openPlayProPurchase(code: Int) = openPlayPurchase(FROST_PRO, code) fun Activity.openPlayPurchase(key: String, code: Int) { + L.d("Open play purchase $key $code") if (!checkFromPlay()) return frostAnswersCustom("PLAY_PURCHASE") { putCustomAttribute("Key", key) } - IAB.helper?.flagEndAsync() ?: playStoreErrorDialog() - IAB.helper?.queryInventoryAsync { + L.d("IAB flag end async") + IAB.helper?.flagEndAsync() ?: return playStoreGenericError("Null flag end async") + L.d("IAB query inv async") + IAB.helper!!.queryInventoryAsync { res, inv -> - if (res.isFailure) { - L.e("IAB error: ${res.message}") - playStoreErrorDialog() - } else if (inv == null) { - playStoreErrorDialog("Empty inventory") - } else { - val donation = inv.getSkuDetails(key) - if (donation != null) { - IAB.helper?.launchPurchaseFlow(this@openPlayPurchase, donation.sku, code) { - result, _ -> - if (result.isSuccess) materialDialogThemed { - title(R.string.play_thank_you) - content(R.string.play_purchased_pro) - positiveText(R.string.kau_ok) - } else playStoreErrorDialog("Result: ${result.message}") - frostAnswers { - logPurchase(PurchaseEvent() - .putItemId(key) - .putSuccess(result.isSuccess)) - } - } ?: playStoreErrorDialog("Launch Purchase Flow") + if (res.isFailure) return@queryInventoryAsync playStoreGenericError("Query res error") + if (inv == null) return@queryInventoryAsync playStoreGenericError("Empty inventory") + L.d("IAB: inventory ${inv.allOwnedSkus}") + val donation = inv.getSkuDetails(key) ?: return@queryInventoryAsync playStoreGenericError("Donation null") + IAB.helper!!.launchPurchaseFlow(this@openPlayPurchase, donation.sku, code) { + result, _ -> + if (result.isSuccess) materialDialogThemed { + title(R.string.play_thank_you) + content(R.string.play_purchased_pro) + positiveText(R.string.kau_ok) + } else playStoreGenericError("Result: ${result.message}") + frostAnswers { + logPurchase(PurchaseEvent() + .putItemId(key) + .putSuccess(result.isSuccess)) } } } -} - -private fun Context.playStoreErrorDialog(s: String = "Play Store Error") { - materialDialogThemed { - title(R.string.uh_oh) - content(R.string.play_store_billing_error) - positiveText(R.string.kau_ok) - } - L.e(Throwable(s), "Play Store Error") }
\ No newline at end of file |