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 | 91 |
1 files changed, 67 insertions, 24 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 b9213ae7..d511f773 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 @@ -3,13 +3,14 @@ package com.pitchedapps.frost.utils.iab import android.app.Activity import android.content.Context import android.content.Intent +import android.support.design.widget.Snackbar import ca.allanwang.kau.utils.isFromGooglePlay +import ca.allanwang.kau.utils.snackbar import com.crashlytics.android.answers.PurchaseEvent import com.pitchedapps.frost.BuildConfig +import com.pitchedapps.frost.R import com.pitchedapps.frost.SettingsActivity -import com.pitchedapps.frost.utils.L -import com.pitchedapps.frost.utils.Prefs -import com.pitchedapps.frost.utils.frostAnswers +import com.pitchedapps.frost.utils.* /** * Created by Allan Wang on 2017-06-23. @@ -27,13 +28,14 @@ object IAB { * and false otherwise * */ - operator fun invoke(activity: Activity, mustHavePlayStore: Boolean = true, onStart: (helper: IabHelper) -> Boolean) { + operator fun invoke(activity: Activity, mustHavePlayStore: Boolean = true, onFailed: () -> Unit = {}, onStart: (helper: IabHelper) -> Boolean) { with(activity) { if (helper?.mDisposed ?: true) { helper = null L.d("IAB setup async") if (!isFrostPlay) { if (mustHavePlayStore) playStoreNotFound() + onFailed() return } try { @@ -44,13 +46,17 @@ object IAB { if (result.isSuccess) { if (onStart(helper!!)) helper!!.disposeWhenFinished() - } else if (mustHavePlayStore) - activity.playStoreGenericError("Setup error: ${result.response} ${result.message}") + } else { + if (mustHavePlayStore) + activity.playStoreGenericError("Setup error: ${result.response} ${result.message}") + onFailed() + } } } catch (e: Exception) { L.e(e, "IAB error") if (mustHavePlayStore) playStoreGenericError(null) + onFailed() } } else if (onStart(helper!!)) helper!!.disposeWhenFinished() @@ -60,14 +66,21 @@ object IAB { fun handleActivityResult(requestCode: Int, resultCode: Int, data: Intent?): Boolean = helper?.handleActivityResult(requestCode, resultCode, data) ?: false + fun cancel() { + + } /** * Call this after any execution to dispose the helper + * Ensure that async calls have already finished beforehand */ fun dispose() { - helper?.disposeWhenFinished() + helper?.dispose() helper = null } + + val isInProgress: Boolean + get() = helper?.mAsyncInProgress ?: false } private const val FROST_PRO = "frost_pro" @@ -79,30 +92,60 @@ private val Context.isFrostPlay: Boolean get() = isFromGooglePlay || BuildConfig.DEBUG fun SettingsActivity.restorePurchases() { - validatePro(this) + //like validate, but with a snackbar and without other prompts + var restore: Snackbar? = null + restore = container.snackbar(R.string.restoring_purchases, Snackbar.LENGTH_INDEFINITE) { + setAction(R.string.kau_close) { restore?.dismiss() } + } + //called if inventory is not properly retrieved + val reset = { + if (Prefs.previouslyPro) { + Prefs.previouslyPro = false + Prefs.theme = Theme.DEFAULT.ordinal + } + finishRestore(restore) + } + getInventory(false, true, reset) { + val proSku = it.getSkuDetails(FROST_PRO) + Prefs.previouslyPro = proSku != null + finishRestore(restore) + } +} + +private fun SettingsActivity.finishRestore(snackbar: Snackbar?) { + snackbar?.dismiss() + materialDialogThemed { + title(R.string.purchases_restored) + content(if (Prefs.previouslyPro) R.string.purchases_restored_with_pro else R.string.purchases_restored_without_pro) + positiveText(R.string.reload) + dismissListener { adapter.notifyAdapterDataSetChanged() } + } } /** * If user has pro, check if it's valid and destroy the helper */ -fun Activity.validatePro(activity: Activity) { - IAB(activity, Prefs.previouslyPro) { //if pro, ensure that it is in inventory; if not, check quietly if it exists +fun Activity.validatePro() { + getInventory(Prefs.previouslyPro, true, { if (Prefs.previouslyPro) playStoreNoLongerPro() }) { + val proSku = it.getSkuDetails(FROST_PRO) + if (proSku == null && Prefs.previouslyPro) playStoreNoLongerPro() + else if (proSku != null && !Prefs.previouslyPro) playStoreFoundPro() + } +} + +fun Activity.getInventory( + mustHavePlayStore: Boolean = true, + disposeOnFinish: Boolean = true, + onFailed: () -> Unit = {}, + onSuccess: (inv: Inventory) -> Unit) { + IAB(this, mustHavePlayStore, onFailed) { helper -> - with(activity) { - helper.queryInventoryAsync { - res, inv -> - if (res.isFailure) return@queryInventoryAsync playStoreGenericError("Query res error") - if (inv?.getSkuDetails(FROST_PRO) != null) { - //owns pro - if (!Prefs.previouslyPro) - playStoreFoundPro() - } else if (Prefs.previouslyPro) { - //doesn't own pro but has it - playStoreNoLongerPro() - } - } + helper.queryInventoryAsync { + res, inv -> + if (res.isFailure || inv == null) onFailed() + else onSuccess(inv) } - true + disposeOnFinish } } |