diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IAB.kt | 36 |
1 files changed, 22 insertions, 14 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 3b6eb53b..a15d0520 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 @@ -14,6 +14,12 @@ import com.pitchedapps.frost.utils.* /** * Created by Allan Wang on 2017-06-23. + * + * Helper singleton to handle all billing related queries + * NOTE + * Make sure you call [IAB.dispose] once an operation is done to release the resources + * Also make sure that it is called on the very LAST operation if there are a list of async calls. + * Otherwise the helper will be prematurely disposed */ object IAB { @@ -24,17 +30,16 @@ object IAB { * * [mustHavePlayStore] decides if dialogs should be shown if play store errors occur * - * [onStart] should return true if we wish to dispose the helper after the operation - * and false otherwise * */ - operator fun invoke(activity: Activity, mustHavePlayStore: Boolean = true, onFailed: () -> Unit = {}, onStart: (helper: IabHelper) -> Boolean) { + operator fun invoke(activity: Activity, mustHavePlayStore: Boolean = true, onFailed: () -> Unit = {}, onStart: (helper: IabHelper) -> Unit) { with(activity) { if (helper?.mDisposed ?: true) { helper = null L.d("IAB setup async") if (!isFrostPlay) { if (mustHavePlayStore) playStoreNotFound() + IAB.dispose() onFailed() return } @@ -46,13 +51,13 @@ object IAB { L.d("IAB setup finished; ${result.isSuccess}") if (result.isSuccess) { L.d("IAB setup success") - if (onStart(helper!!)) - helper!!.disposeWhenFinished() + onStart(helper!!) } else { L.d("IAB setup fail") if (mustHavePlayStore) activity.playStoreGenericError("Setup error: ${result.response} ${result.message}") onFailed() + IAB.dispose() } } } catch (e: Exception) { @@ -60,9 +65,9 @@ object IAB { if (mustHavePlayStore) playStoreGenericError(null) onFailed() + IAB.dispose() } - } else if (onStart(helper!!)) - helper!!.disposeWhenFinished() + } else onStart(helper!!) } } @@ -71,10 +76,10 @@ object IAB { /** * Call this after any execution to dispose the helper - * Ensure that async calls have already finished beforehand */ fun dispose() { - helper?.dispose() + helper?.disposeWhenFinished() + helper?.flagEndAsync() helper = null } @@ -103,7 +108,7 @@ fun SettingsActivity.restorePurchases() { } finishRestore(restore, false) } - getInventory(false, true, reset) { + getInventory(false, reset) { inv, _ -> val proSku = inv.hasPurchase(FROST_PRO) Prefs.pro = proSku @@ -120,6 +125,7 @@ private fun SettingsActivity.finishRestore(snackbar: Snackbar, hasPro: Boolean) positiveText(R.string.reload) dismissListener { adapter.notifyAdapterDataSetChanged() } } + IAB.dispose() } /** @@ -128,17 +134,18 @@ private fun SettingsActivity.finishRestore(snackbar: Snackbar, hasPro: Boolean) */ fun Activity.validatePro() { L.d("Validate pro") - getInventory(Prefs.pro, true, { if (Prefs.pro) playStoreNoLongerPro() }) { + getInventory(Prefs.pro, { if (Prefs.pro) playStoreNoLongerPro() }) { inv, _ -> 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() } } fun Activity.getInventory( mustHavePlayStore: Boolean = true, - disposeOnFinish: Boolean = true, onFailed: () -> Unit = {}, onSuccess: (inv: Inventory, helper: IabHelper) -> Unit) { IAB(this, mustHavePlayStore, onFailed) { @@ -149,7 +156,6 @@ fun Activity.getInventory( if (res.isFailure || inv == null) onFailed() else onSuccess(inv, helper) } - disposeOnFinish } } @@ -163,11 +169,12 @@ 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, false, { playStoreGenericError("Query res error") }) { + getInventory(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}") @@ -183,6 +190,7 @@ fun Activity.openPlayPurchase(key: String, code: Int, onSuccess: (key: String) - .putCustomAttribute("result", result.message) .putSuccess(result.isSuccess)) } + IAB.dispose() } } }
\ No newline at end of file |