aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/utils/iab
diff options
context:
space:
mode:
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.kt19
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IABDialogs.kt32
2 files changed, 42 insertions, 9 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 431ba0b6..ec63cce3 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,6 +34,13 @@ object IAB {
}
}
}
+
+ /**
+ * If user has pro, check if it's valid and destroy the helper
+ */
+ fun validatePro(activity: Activity) {
+
+ }
}
private const val FROST_PRO = "frost_pro"
@@ -68,19 +75,15 @@ fun Activity.openPlayPurchase(key: String, code: Int) {
IAB.helper!!.queryInventoryAsync {
res, inv ->
if (res.isFailure) return@queryInventoryAsync playStoreGenericError("Query res error")
- if (inv == null) return@queryInventoryAsync playStoreGenericError("Empty inventory")
+ if (inv?.getSkuDetails(key) != null) return@queryInventoryAsync playStoreAlreadyPurchased(key)
L.d("IAB: inventory ${inv.allOwnedSkus}")
- val donation = inv.getSkuDetails(key) ?: return@queryInventoryAsync playStoreGenericError("Donation null")
- IAB.helper!!.launchPurchaseFlow(this@openPlayPurchase, donation.sku, code) {
+ IAB.helper!!.launchPurchaseFlow(this@openPlayPurchase, key, 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}")
+ if (result.isSuccess) playStorePurchasedSuccessfully(key)
frostAnswers {
logPurchase(PurchaseEvent()
.putItemId(key)
+ .putCustomAttribute("result", result.message)
.putSuccess(result.isSuccess))
}
}
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IABDialogs.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IABDialogs.kt
index e855138f..f0a544f7 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IABDialogs.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IABDialogs.kt
@@ -3,7 +3,10 @@ package com.pitchedapps.frost.utils.iab
import android.app.Activity
import ca.allanwang.kau.utils.restart
import ca.allanwang.kau.utils.startPlayStoreLink
+import ca.allanwang.kau.utils.string
+import com.pitchedapps.frost.MainActivity
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.materialDialogThemed
@@ -15,6 +18,14 @@ private fun playStoreLog(text: String) {
L.e(Throwable(text), "Play Store Exception")
}
+private fun Activity.playRestart() {
+ if (this is MainActivity) restart()
+ else if (this is SettingsActivity) {
+ setResult(MainActivity.REQUEST_RESTART)
+ finish()
+ }
+}
+
fun Activity.playStoreNoLongerPro() {
if (!Prefs.previouslyPro) return //never pro to begin with
Prefs.previouslyPro = false
@@ -24,7 +35,7 @@ fun Activity.playStoreNoLongerPro() {
content(R.string.play_store_not_pro)
positiveText(R.string.reload)
dismissListener {
- this@playStoreNoLongerPro.restart()
+ this@playStoreNoLongerPro.playRestart()
}
}
}
@@ -47,4 +58,23 @@ fun Activity.playStoreGenericError(text: String = "Store generic error") {
content(R.string.play_store_billing_error)
positiveText(R.string.kau_ok)
}
+}
+
+fun Activity.playStoreAlreadyPurchased(key: String) {
+ materialDialogThemed {
+ title(R.string.play_already_purchased)
+ content(String.format(string(R.string.play_already_purchased_content), key))
+ positiveText(R.string.reload)
+ dismissListener {
+ this@playStoreAlreadyPurchased.playRestart()
+ }
+ }
+}
+
+fun Activity.playStorePurchasedSuccessfully(key: String) {
+ materialDialogThemed {
+ title(R.string.play_thank_you)
+ content(R.string.play_purchased_pro)
+ positiveText(R.string.kau_ok)
+ }
} \ No newline at end of file