diff options
author | Allan Wang <me@allanwang.ca> | 2017-07-06 17:13:04 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-06 17:13:04 -0400 |
commit | 2586e947f769740dd8cb8bf8b1dd0473b51e50e4 (patch) | |
tree | 3deedd5357cbf7f52a8576dd715babf7b5bd097d /app/src/main/kotlin/com/pitchedapps/frost/utils/iab | |
parent | bbe30297a216218ded4c2c6e3e8c2843652eb7a7 (diff) | |
download | frost-2586e947f769740dd8cb8bf8b1dd0473b51e50e4.tar.gz frost-2586e947f769740dd8cb8bf8b1dd0473b51e50e4.tar.bz2 frost-2586e947f769740dd8cb8bf8b1dd0473b51e50e4.zip |
Dev 1.1.4 - fixes for donations (#31)
* Remove cookie from error log
* Remove null check on login as it isn't possible
* Add exception to profile loading
* Remap billing logic
* Display snackbar only for user prompts
* Private disposed caller
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/utils/iab')
-rw-r--r-- | app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IAB.kt | 40 |
1 files changed, 27 insertions, 13 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 da735430..a2dc82f3 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 @@ -34,9 +34,14 @@ object IAB { * [mustHavePlayStore] decides if dialogs should be shown if play store errors occur * */ - operator fun invoke(activity: Activity, mustHavePlayStore: Boolean = true, onFailed: () -> Unit = {}, onStart: (helper: IabHelper) -> Unit) { + operator fun invoke(activity: Activity, + mustHavePlayStore: Boolean = true, + userRequest: Boolean = true, + onFailed: () -> Unit = {}, + onStart: (helper: IabHelper) -> Unit) { with(activity) { - if (helper?.mDisposed ?: helper?.mDisposeAfterAsync ?: true) { + if (isInProgress) if (userRequest) snackbar(R.string.iab_still_in_progress, Snackbar.LENGTH_LONG) + else if (helper?.disposed ?: true) { helper = null L.d("IAB setup async") if (!isFrostPlay) { @@ -80,16 +85,27 @@ object IAB { */ fun dispose() { helper?.disposeWhenFinished() - helper?.flagEndAsync() helper = null } + /** + * Dispose given helper and check if it matches with our own helper + */ + fun dispose(helper: IabHelper) { + helper.disposeWhenFinished() + if (IAB.helper?.disposed ?: true) + this.helper = null + } + val isInProgress: Boolean get() = helper?.mAsyncInProgress ?: false } private const val FROST_PRO = "frost_pro" +private val IabHelper.disposed: Boolean + get() = mDisposed || mDisposeAfterAsync + val IS_FROST_PRO: Boolean get() = (BuildConfig.DEBUG && Prefs.debugPro) || Prefs.pro @@ -109,8 +125,8 @@ fun SettingsActivity.restorePurchases() { } finishRestore(restore, false) } - getInventory(false, reset) { - inv, _ -> + getInventory(false, true, reset) { + inv, helper -> val proSku = inv.hasPurchase(FROST_PRO) Prefs.pro = proSku L.d("Restore found: ${Prefs.pro}") @@ -126,7 +142,6 @@ private fun SettingsActivity.finishRestore(snackbar: Snackbar, hasPro: Boolean) positiveText(R.string.reload) dismissListener { adapter.notifyAdapterDataSetChanged() } } - IAB.dispose() } /** @@ -135,21 +150,22 @@ private fun SettingsActivity.finishRestore(snackbar: Snackbar, hasPro: Boolean) */ fun Activity.validatePro() { L.d("Validate pro") - getInventory(Prefs.pro, { if (Prefs.pro) playStoreNoLongerPro() }) { - inv, _ -> + getInventory(Prefs.pro, false, { if (Prefs.pro) playStoreNoLongerPro() }) { + inv, helper -> val proSku = inv.hasPurchase(FROST_PRO) L.d("Validation finished: ${Prefs.pro} should be $proSku") if (!proSku && Prefs.pro) playStoreNoLongerPro() else if (proSku && !Prefs.pro) playStoreFoundPro() - IAB.dispose() + helper.disposeWhenFinished() } } fun Activity.getInventory( mustHavePlayStore: Boolean = true, + userRequest: Boolean = true, onFailed: () -> Unit = {}, onSuccess: (inv: Inventory, helper: IabHelper) -> Unit) { - IAB(this, mustHavePlayStore, onFailed) { + IAB(this, mustHavePlayStore, userRequest, onFailed) { helper -> helper.queryInventoryAsync { res, inv -> @@ -170,12 +186,11 @@ fun Activity.openPlayProPurchase(code: Int) { fun Activity.openPlayPurchase(key: String, code: Int, onSuccess: (key: String) -> Unit) { L.d("Open play purchase $key $code") - getInventory(true, { playStoreGenericError("Query res error") }) { + getInventory(true, true, { playStoreGenericError("Query res error") }) { inv, helper -> if (inv.hasPurchase(key)) { playStoreAlreadyPurchased(key) onSuccess(key) - IAB.dispose() return@getInventory } L.d("IAB: inventory ${inv.allOwnedSkus}") @@ -184,7 +199,6 @@ fun Activity.openPlayPurchase(key: String, code: Int, onSuccess: (key: String) - if (result.isSuccess) { onSuccess(key) playStorePurchasedSuccessfully(key) - IAB.dispose() } frostAnswers { logPurchase(PurchaseEvent() |