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 { class BillingManager @Inject constructor(context: Context) : PurchasesUpdatedListener {
enum class UpgradeStatus { REGULAR, LEGACY, SUPPORTER, DONOR, PHILANTHROPIST } enum class UpgradeStatus { REGULAR, UPGRADED }
companion object { companion object {
const val SKU_LEGACY = "remove_ads" const val SKU_PLUS = "remove_ads"
const val SKU_3 = "qksms_plus_3" const val SKU_PLUS_DONATE = "qksms_plus_donate"
const val SKU_5 = "qksms_plus_5"
const val SKU_10 = "qksms_plus_10"
} }
val subscriptions: Observable<List<SkuDetails>> = BehaviorSubject.create() val products: Observable<List<SkuDetails>> = BehaviorSubject.create()
val plusStatus: Observable<UpgradeStatus> 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 purchaseList = mutableListOf<Purchase>()
private val purchaseListObservable: Observable<List<Purchase>> = BehaviorSubject.create() private val purchaseListObservable: Observable<List<Purchase>> = BehaviorSubject.create()
@@ -58,11 +56,9 @@ class BillingManager @Inject constructor(context: Context) : PurchasesUpdatedLis
plusStatus = purchaseListObservable plusStatus = purchaseListObservable
.map { purchases -> .map { purchases ->
when { when {
purchases.any { it.sku == SKU_10 } -> UpgradeStatus.PHILANTHROPIST purchases.any { it.sku == SKU_PLUS } -> UpgradeStatus.UPGRADED
purchases.any { it.sku == SKU_5 } -> UpgradeStatus.DONOR purchases.any { it.sku == SKU_PLUS_DONATE } -> UpgradeStatus.UPGRADED
purchases.any { it.sku == SKU_3 } -> UpgradeStatus.SUPPORTER else -> UpgradeStatus.REGULAR
purchases.any { it.sku == SKU_LEGACY } -> UpgradeStatus.LEGACY
else -> UpgradeStatus.PHILANTHROPIST
} }
} }
} }
@@ -71,13 +67,6 @@ class BillingManager @Inject constructor(context: Context) : PurchasesUpdatedLis
executeServiceRequest { executeServiceRequest {
val purchasesResult = billingClient.queryPurchases(SkuType.INAPP) 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 // Handle purchase result
purchaseList.clear() purchaseList.clear()
purchaseList.addAll(purchasesResult.purchasesList) purchaseList.addAll(purchasesResult.purchasesList)
@@ -103,10 +92,10 @@ class BillingManager @Inject constructor(context: Context) : PurchasesUpdatedLis
private fun querySkuDetailsAsync() { private fun querySkuDetailsAsync() {
executeServiceRequest { 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 -> billingClient.querySkuDetailsAsync(subParams.build()) { responseCode, skuDetailsList ->
if (responseCode == BillingResponse.OK) { 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) { fun initiatePurchaseFlow(activity: Activity, sku: String) {
executeServiceRequest { executeServiceRequest {
val oldSkus = purchaseList val params = BillingFlowParams.newBuilder().setSku(sku).setType(SkuType.INAPP)
.filter { product -> subSkus.contains(product.sku) }
.map { product -> product.sku }
val params = BillingFlowParams.newBuilder().setSku(sku).setType(SkuType.SUBS).setOldSkus(ArrayList(oldSkus))
billingClient.launchBillingFlow(activity, params.build()) billingClient.launchBillingFlow(activity, params.build())
} }
} }
@@ -18,7 +18,6 @@
*/ */
package presentation.feature.plus package presentation.feature.plus
import android.content.res.ColorStateList
import android.graphics.Typeface import android.graphics.Typeface
import android.os.Bundle import android.os.Bundle
import com.jakewharton.rxbinding2.view.clicks import com.jakewharton.rxbinding2.view.clicks
@@ -31,7 +30,6 @@ import common.util.FontProvider
import common.util.extensions.setBackgroundTint import common.util.extensions.setBackgroundTint
import common.util.extensions.setTint import common.util.extensions.setTint
import common.util.extensions.setVisible import common.util.extensions.setVisible
import io.reactivex.rxkotlin.Observables
import kotlinx.android.synthetic.main.qksms_plus_activity.* import kotlinx.android.synthetic.main.qksms_plus_activity.*
import presentation.common.base.QkActivity import presentation.common.base.QkActivity
import javax.inject.Inject import javax.inject.Inject
@@ -41,9 +39,8 @@ class PlusActivity : QkActivity<PlusViewModel>(), PlusView {
@Inject lateinit var fontProvider: FontProvider @Inject lateinit var fontProvider: FontProvider
override val viewModelClass = PlusViewModel::class override val viewModelClass = PlusViewModel::class
override val supporterSelectedIntent by lazy { supporter.clicks() } override val upgradeIntent by lazy { upgrade.clicks() }
override val donorSelectedIntent by lazy { donor.clicks() } override val upgradeDonateIntent by lazy { upgradeDonate.clicks() }
override val philanthropistSelectedIntent by lazy { philanthropist.clicks() }
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
appComponent.inject(this) appComponent.inject(this)
@@ -72,63 +69,27 @@ class PlusActivity : QkActivity<PlusViewModel>(), PlusView {
colors.separator colors.separator
.autoDisposable(scope()) .autoDisposable(scope())
.subscribe { color -> thanks.setBackgroundTint(color) } .subscribe { color ->
upgradeDonate.setBackgroundTint(color)
thanks.setBackgroundTint(color)
}
colors.theme colors.theme
.autoDisposable(scope()) .autoDisposable(scope())
.subscribe { color -> .subscribe { color ->
upgrade.setBackgroundTint(color)
thanksIcon.setTint(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) { 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) 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) { override fun initiatePurchaseFlow(billingManager: BillingManager, sku: String) {
@@ -22,7 +22,7 @@ import common.util.BillingManager
data class PlusState( data class PlusState(
val currentPlan: BillingManager.UpgradeStatus = BillingManager.UpgradeStatus.REGULAR, val currentPlan: BillingManager.UpgradeStatus = BillingManager.UpgradeStatus.REGULAR,
val supporterPrice: String = "", val upgradePrice: String = "",
val donorPrice: String = "", val upgradeDonatePrice: String = "",
val philanthropistPrice: String = "" val currency: String = ""
) )
@@ -24,9 +24,8 @@ import presentation.common.base.QkView
interface PlusView : QkView<PlusState> { interface PlusView : QkView<PlusState> {
val supporterSelectedIntent: Observable<Unit> val upgradeIntent: Observable<Unit>
val donorSelectedIntent: Observable<Unit> val upgradeDonateIntent: Observable<Unit>
val philanthropistSelectedIntent: Observable<Unit>
fun initiatePurchaseFlow(billingManager: BillingManager, sku: String) fun initiatePurchaseFlow(billingManager: BillingManager, sku: String)
@@ -39,13 +39,13 @@ class PlusViewModel : QkViewModel<PlusView, PlusState>(PlusState()) {
newState { it.copy(currentPlan = plan) } newState { it.copy(currentPlan = plan) }
} }
disposables += billingManager.subscriptions disposables += billingManager.products
.subscribe { subs -> .subscribe { products ->
newState { newState {
it.copy( val upgrade = products.firstOrNull { it.sku == BillingManager.SKU_PLUS }
supporterPrice = subs.firstOrNull { it.sku == BillingManager.SKU_3 }?.price ?: "", val upgradeDonate = products.firstOrNull { it.sku == BillingManager.SKU_PLUS_DONATE }
donorPrice = subs.firstOrNull { it.sku == BillingManager.SKU_5 }?.price ?: "", it.copy(upgradePrice = upgrade?.price ?: "", upgradeDonatePrice = upgradeDonate?.price ?: "",
philanthropistPrice = subs.firstOrNull { it.sku == BillingManager.SKU_10 }?.price ?: "") currency = upgrade?.priceCurrencyCode ?: upgradeDonate?.priceCurrencyCode ?: "")
} }
} }
} }
@@ -54,9 +54,8 @@ class PlusViewModel : QkViewModel<PlusView, PlusState>(PlusState()) {
super.bindView(view) super.bindView(view)
Observable.merge( Observable.merge(
view.supporterSelectedIntent.map { BillingManager.SKU_3 }, view.upgradeIntent.map { BillingManager.SKU_PLUS },
view.donorSelectedIntent.map { BillingManager.SKU_5 }, view.upgradeDonateIntent.map { BillingManager.SKU_PLUS_DONATE })
view.philanthropistSelectedIntent.map { BillingManager.SKU_10 })
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { sku -> view.initiatePurchaseFlow(billingManager, sku) } .subscribe { sku -> view.initiatePurchaseFlow(billingManager, sku) }
} }
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
~
~ This file is part of QKSMS.
~
~ QKSMS is free software: you can redistribute it and/or modify
~ it under the terms of the GNU General Public License as published by
~ the Free Software Foundation, either version 3 of the License, or
~ (at your option) any later version.
~
~ QKSMS is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
~ GNU General Public License for more details.
~
~ You should have received a copy of the GNU General Public License
~ along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
-->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@android:color/transparent" />
<stroke
android:width="1dp"
android:color="@color/white" />
<corners android:radius="2dp" />
</shape>
</item>
</layer-list>
+23 -161
View File
@@ -77,19 +77,11 @@
android:textSize="14sp" android:textSize="14sp"
app:textColor="secondary" /> app:textColor="secondary" />
<presentation.common.widget.Separator
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp" />
<android.support.constraint.ConstraintLayout <android.support.constraint.ConstraintLayout
android:id="@+id/thanks" android:id="@+id/thanks"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="16dp" android:layout_margin="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:background="@drawable/rounded_rectangle_2dp" android:background="@drawable/rounded_rectangle_2dp"
android:padding="16dp" android:padding="16dp"
tools:backgroundTint="@color/separatorLight"> tools:backgroundTint="@color/separatorLight">
@@ -126,162 +118,32 @@
</android.support.constraint.ConstraintLayout> </android.support.constraint.ConstraintLayout>
<LinearLayout <presentation.common.widget.QkTextView
android:layout_width="match_parent" android:id="@+id/upgrade"
android:layout_height="wrap_content" style="@style/TextButton"
android:gravity="center" android:layout_marginLeft="32dp"
android:orientation="horizontal"> android:layout_marginRight="32dp"
android:layout_marginTop="24dp"
android:background="@drawable/rounded_rectangle_2dp"
android:text="@string/qksms_plus_upgrade"
app:textColor="primaryOnTheme"
tools:backgroundTint="@color/tools_theme" />
<android.support.constraint.ConstraintLayout <presentation.common.widget.QkTextView
android:id="@+id/supporter" android:id="@+id/upgradeDonate"
android:layout_width="wrap_content" style="@style/TextButton"
android:layout_height="wrap_content" android:layout_marginBottom="32dp"
android:background="@drawable/ripple" android:layout_marginLeft="32dp"
android:orientation="vertical" android:layout_marginRight="32dp"
android:paddingBottom="8dp" android:layout_marginTop="8dp"
android:paddingLeft="16dp" android:background="@drawable/rounded_rectangle_outline_2dp"
android:paddingRight="16dp" android:text="@string/qksms_plus_upgrade_donate"
android:paddingTop="8dp"> app:textColor="primary"
tools:backgroundTint="@color/separatorLight" />
<presentation.common.widget.QkTextView
android:id="@+id/supporterName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/qksms_plus_option_supporter"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:textColor="@color/textPrimary" />
<presentation.common.widget.QkTextView
android:id="@+id/supporterPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@id/supporterPeriod"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/supporterName"
tools:text="$2.99"
tools:textColor="@color/textPrimary" />
<presentation.common.widget.QkTextView
android:id="@+id/supporterPeriod"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/qksms_plus_renewal_year"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/supporterPrice"
app:layout_constraintTop_toBottomOf="@id/supporterName"
tools:textColor="@color/textSecondary" />
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:id="@+id/donor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ripple"
android:orientation="vertical"
android:paddingBottom="8dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="8dp">
<presentation.common.widget.QkTextView
android:id="@+id/donorName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/qksms_plus_option_donor"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:textColor="@color/textPrimary" />
<presentation.common.widget.QkTextView
android:id="@+id/donorPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@id/donorPeriod"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/donorName"
tools:text="$2.99"
tools:textColor="@color/textPrimary" />
<presentation.common.widget.QkTextView
android:id="@+id/donorPeriod"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/qksms_plus_renewal_year"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/donorPrice"
app:layout_constraintTop_toBottomOf="@id/donorName"
tools:textColor="@color/textSecondary" />
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:id="@+id/philanthropist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ripple"
android:orientation="vertical"
android:paddingBottom="8dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="8dp">
<presentation.common.widget.QkTextView
android:id="@+id/philanthropistName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/qksms_plus_option_philanthropist"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:textColor="@color/textPrimary" />
<presentation.common.widget.QkTextView
android:id="@+id/philanthropistPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@id/philanthropistPeriod"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/philanthropistName"
tools:text="$2.99"
tools:textColor="@color/textPrimary" />
<presentation.common.widget.QkTextView
android:id="@+id/philanthropistPeriod"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/qksms_plus_renewal_year"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/philanthropistPrice"
app:layout_constraintTop_toBottomOf="@id/philanthropistName"
tools:textColor="@color/textSecondary" />
</android.support.constraint.ConstraintLayout>
</LinearLayout>
<presentation.common.widget.Separator <presentation.common.widget.Separator
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="1dp" android:layout_height="1dp" />
android:layout_marginTop="16dp" />
<presentation.common.widget.PreferenceView <presentation.common.widget.PreferenceView
android:layout_width="match_parent" android:layout_width="match_parent"
+4 -7
View File
@@ -88,13 +88,10 @@
<string name="settings_sync_title">Sync messages</string> <string name="settings_sync_title">Sync messages</string>
<string name="settings_sync_summary">Re-sync your messages with the native Android SMS database</string> <string name="settings_sync_summary">Re-sync your messages with the native Android SMS database</string>
<string name="qksms_plus_description_title">Pay what you like, unlock everything</string> <string name="qksms_plus_description_title">Support development, unlock everything</string>
<string name="qksms_plus_description_summary">You can save a starving developer for as little as %s a year</string> <string name="qksms_plus_description_summary">You can save a starving developer for just %s</string>
<string name="qksms_plus_option_supporter">Supporter</string> <string name="qksms_plus_upgrade">Unlock for %1$s %2$s</string>
<string name="qksms_plus_option_donor">Donor</string> <string name="qksms_plus_upgrade_donate">Unlock + donate for %1$s %2$s</string>
<string name="qksms_plus_option_philanthropist">Philanthropist</string>
<string name="qksms_plus_renewal_year">/year</string>
<string name="qksms_plus_upgrade">Upgrade to QKSMS+</string>
<string name="qksms_plus_thanks_title">Thank you for supporting QKSMS!</string> <string name="qksms_plus_thanks_title">Thank you for supporting QKSMS!</string>
<string name="qksms_plus_thanks_summary">You now have access to all QKSMS+ features</string> <string name="qksms_plus_thanks_summary">You now have access to all QKSMS+ features</string>
<string name="qksms_plus_themes_title">Premium themes</string> <string name="qksms_plus_themes_title">Premium themes</string>
+11
View File
@@ -61,6 +61,17 @@
<item name="android:textStyle">bold</item> <item name="android:textStyle">bold</item>
</style> </style>
<style name="TextButton">
<item name="android:layout_height">36dp</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:minWidth">88dp</item>
<item name="android:paddingLeft">16dp</item>
<item name="android:paddingRight">16dp</item>
<item name="android:gravity">center</item>
<item name="android:textSize">14sp</item>
<item name="android:textStyle">bold</item>
</style>
<style name="CollapsingToolbarCollapsedText"> <style name="CollapsingToolbarCollapsedText">
<item name="android:textSize">20sp</item> <item name="android:textSize">20sp</item>
</style> </style>