Switch from Subscription to IAP

This commit is contained in:
Moez Bhatti
2018-02-05 17:11:50 -05:00
parent fcf5f2d6e8
commit 228c0e3106
9 changed files with 104 additions and 260 deletions
@@ -30,19 +30,17 @@ import javax.inject.Inject
class BillingManager @Inject constructor(context: Context) : PurchasesUpdatedListener {
enum class UpgradeStatus { REGULAR, LEGACY, SUPPORTER, DONOR, PHILANTHROPIST }
enum class UpgradeStatus { REGULAR, UPGRADED }
companion object {
const val SKU_LEGACY = "remove_ads"
const val SKU_3 = "qksms_plus_3"
const val SKU_5 = "qksms_plus_5"
const val SKU_10 = "qksms_plus_10"
const val SKU_PLUS = "remove_ads"
const val SKU_PLUS_DONATE = "qksms_plus_donate"
}
val subscriptions: Observable<List<SkuDetails>> = BehaviorSubject.create()
val products: Observable<List<SkuDetails>> = BehaviorSubject.create()
val plusStatus: Observable<UpgradeStatus>
private val subSkus = listOf(SKU_3, SKU_5, SKU_10)
private val skus = listOf(SKU_PLUS, SKU_PLUS_DONATE)
private val purchaseList = mutableListOf<Purchase>()
private val purchaseListObservable: Observable<List<Purchase>> = BehaviorSubject.create()
@@ -58,11 +56,9 @@ class BillingManager @Inject constructor(context: Context) : PurchasesUpdatedLis
plusStatus = purchaseListObservable
.map { purchases ->
when {
purchases.any { it.sku == SKU_10 } -> UpgradeStatus.PHILANTHROPIST
purchases.any { it.sku == SKU_5 } -> UpgradeStatus.DONOR
purchases.any { it.sku == SKU_3 } -> UpgradeStatus.SUPPORTER
purchases.any { it.sku == SKU_LEGACY } -> UpgradeStatus.LEGACY
else -> UpgradeStatus.PHILANTHROPIST
purchases.any { it.sku == SKU_PLUS } -> UpgradeStatus.UPGRADED
purchases.any { it.sku == SKU_PLUS_DONATE } -> UpgradeStatus.UPGRADED
else -> UpgradeStatus.REGULAR
}
}
}
@@ -71,13 +67,6 @@ class BillingManager @Inject constructor(context: Context) : PurchasesUpdatedLis
executeServiceRequest {
val purchasesResult = billingClient.queryPurchases(SkuType.INAPP)
if (billingClient.isFeatureSupported(BillingClient.FeatureType.SUBSCRIPTIONS) == BillingResponse.OK) {
val subscriptionResult = billingClient.queryPurchases(SkuType.SUBS)
if (subscriptionResult.responseCode == BillingResponse.OK) {
purchasesResult.purchasesList.addAll(subscriptionResult.purchasesList)
}
}
// Handle purchase result
purchaseList.clear()
purchaseList.addAll(purchasesResult.purchasesList)
@@ -103,10 +92,10 @@ class BillingManager @Inject constructor(context: Context) : PurchasesUpdatedLis
private fun querySkuDetailsAsync() {
executeServiceRequest {
val subParams = SkuDetailsParams.newBuilder().setSkusList(subSkus).setType(BillingClient.SkuType.SUBS)
val subParams = SkuDetailsParams.newBuilder().setSkusList(skus).setType(BillingClient.SkuType.INAPP)
billingClient.querySkuDetailsAsync(subParams.build()) { responseCode, skuDetailsList ->
if (responseCode == BillingResponse.OK) {
(subscriptions as Subject).onNext(skuDetailsList)
(products as Subject).onNext(skuDetailsList)
}
}
}
@@ -114,11 +103,7 @@ class BillingManager @Inject constructor(context: Context) : PurchasesUpdatedLis
fun initiatePurchaseFlow(activity: Activity, sku: String) {
executeServiceRequest {
val oldSkus = purchaseList
.filter { product -> subSkus.contains(product.sku) }
.map { product -> product.sku }
val params = BillingFlowParams.newBuilder().setSku(sku).setType(SkuType.SUBS).setOldSkus(ArrayList(oldSkus))
val params = BillingFlowParams.newBuilder().setSku(sku).setType(SkuType.INAPP)
billingClient.launchBillingFlow(activity, params.build())
}
}
@@ -18,7 +18,6 @@
*/
package presentation.feature.plus
import android.content.res.ColorStateList
import android.graphics.Typeface
import android.os.Bundle
import com.jakewharton.rxbinding2.view.clicks
@@ -31,7 +30,6 @@ import common.util.FontProvider
import common.util.extensions.setBackgroundTint
import common.util.extensions.setTint
import common.util.extensions.setVisible
import io.reactivex.rxkotlin.Observables
import kotlinx.android.synthetic.main.qksms_plus_activity.*
import presentation.common.base.QkActivity
import javax.inject.Inject
@@ -41,9 +39,8 @@ class PlusActivity : QkActivity<PlusViewModel>(), PlusView {
@Inject lateinit var fontProvider: FontProvider
override val viewModelClass = PlusViewModel::class
override val supporterSelectedIntent by lazy { supporter.clicks() }
override val donorSelectedIntent by lazy { donor.clicks() }
override val philanthropistSelectedIntent by lazy { philanthropist.clicks() }
override val upgradeIntent by lazy { upgrade.clicks() }
override val upgradeDonateIntent by lazy { upgradeDonate.clicks() }
override fun onCreate(savedInstanceState: Bundle?) {
appComponent.inject(this)
@@ -72,63 +69,27 @@ class PlusActivity : QkActivity<PlusViewModel>(), PlusView {
colors.separator
.autoDisposable(scope())
.subscribe { color -> thanks.setBackgroundTint(color) }
.subscribe { color ->
upgradeDonate.setBackgroundTint(color)
thanks.setBackgroundTint(color)
}
colors.theme
.autoDisposable(scope())
.subscribe { color ->
upgrade.setBackgroundTint(color)
thanksIcon.setTint(color)
supporter.setBackgroundTint(color)
donor.setBackgroundTint(color)
philanthropist.setBackgroundTint(color)
}
val states = arrayOf(
intArrayOf(android.R.attr.state_selected),
intArrayOf(-android.R.attr.state_selected))
Observables
.combineLatest(colors.textPrimaryOnTheme, colors.textPrimary, { theme, textSecondary ->
ColorStateList(states, intArrayOf(theme, textSecondary))
})
.autoDisposable(scope())
.subscribe { colorStateList ->
supporterName.setTextColor(colorStateList)
supporterPrice.setTextColor(colorStateList)
donorName.setTextColor(colorStateList)
donorPrice.setTextColor(colorStateList)
philanthropistName.setTextColor(colorStateList)
philanthropistPrice.setTextColor(colorStateList)
}
Observables
.combineLatest(colors.textSecondaryOnTheme, colors.textSecondary, { theme, textSecondary ->
ColorStateList(states, intArrayOf(theme, textSecondary))
})
.autoDisposable(scope())
.subscribe { colorStateList ->
supporterPeriod.setTextColor(colorStateList)
donorPeriod.setTextColor(colorStateList)
philanthropistPeriod.setTextColor(colorStateList)
}
}
override fun render(state: PlusState) {
description.text = getString(R.string.qksms_plus_description_summary, state.supporterPrice)
description.text = getString(R.string.qksms_plus_description_summary, state.upgradePrice)
upgrade.text = getString(R.string.qksms_plus_upgrade, state.upgradePrice, state.currency)
upgradeDonate.text = getString(R.string.qksms_plus_upgrade_donate, state.upgradeDonatePrice, state.currency)
upgrade.setVisible(state.currentPlan == BillingManager.UpgradeStatus.REGULAR)
upgradeDonate.setVisible(state.currentPlan == BillingManager.UpgradeStatus.REGULAR)
thanks.setVisible(state.currentPlan != BillingManager.UpgradeStatus.REGULAR)
val supportedSelected = state.currentPlan == BillingManager.UpgradeStatus.SUPPORTER
supporter.isSelected = supportedSelected
supporterPrice.text = state.supporterPrice
val donorSelected = state.currentPlan == BillingManager.UpgradeStatus.DONOR
donor.isSelected = donorSelected
donorPrice.text = state.donorPrice
val philanthropistSelected = state.currentPlan == BillingManager.UpgradeStatus.PHILANTHROPIST
philanthropist.isSelected = philanthropistSelected
philanthropistPrice.text = state.philanthropistPrice
}
override fun initiatePurchaseFlow(billingManager: BillingManager, sku: String) {
@@ -22,7 +22,7 @@ import common.util.BillingManager
data class PlusState(
val currentPlan: BillingManager.UpgradeStatus = BillingManager.UpgradeStatus.REGULAR,
val supporterPrice: String = "",
val donorPrice: String = "",
val philanthropistPrice: String = ""
val upgradePrice: String = "",
val upgradeDonatePrice: String = "",
val currency: String = ""
)
@@ -24,9 +24,8 @@ import presentation.common.base.QkView
interface PlusView : QkView<PlusState> {
val supporterSelectedIntent: Observable<Unit>
val donorSelectedIntent: Observable<Unit>
val philanthropistSelectedIntent: Observable<Unit>
val upgradeIntent: Observable<Unit>
val upgradeDonateIntent: Observable<Unit>
fun initiatePurchaseFlow(billingManager: BillingManager, sku: String)
@@ -39,13 +39,13 @@ class PlusViewModel : QkViewModel<PlusView, PlusState>(PlusState()) {
newState { it.copy(currentPlan = plan) }
}
disposables += billingManager.subscriptions
.subscribe { subs ->
disposables += billingManager.products
.subscribe { products ->
newState {
it.copy(
supporterPrice = subs.firstOrNull { it.sku == BillingManager.SKU_3 }?.price ?: "",
donorPrice = subs.firstOrNull { it.sku == BillingManager.SKU_5 }?.price ?: "",
philanthropistPrice = subs.firstOrNull { it.sku == BillingManager.SKU_10 }?.price ?: "")
val upgrade = products.firstOrNull { it.sku == BillingManager.SKU_PLUS }
val upgradeDonate = products.firstOrNull { it.sku == BillingManager.SKU_PLUS_DONATE }
it.copy(upgradePrice = upgrade?.price ?: "", upgradeDonatePrice = upgradeDonate?.price ?: "",
currency = upgrade?.priceCurrencyCode ?: upgradeDonate?.priceCurrencyCode ?: "")
}
}
}
@@ -54,9 +54,8 @@ class PlusViewModel : QkViewModel<PlusView, PlusState>(PlusState()) {
super.bindView(view)
Observable.merge(
view.supporterSelectedIntent.map { BillingManager.SKU_3 },
view.donorSelectedIntent.map { BillingManager.SKU_5 },
view.philanthropistSelectedIntent.map { BillingManager.SKU_10 })
view.upgradeIntent.map { BillingManager.SKU_PLUS },
view.upgradeDonateIntent.map { BillingManager.SKU_PLUS_DONATE })
.autoDisposable(view.scope())
.subscribe { sku -> view.initiatePurchaseFlow(billingManager, sku) }
}