forked from upstream-mirrors/quik
Create BillingManager, query for products at launch
This commit is contained in:
+1
-3
@@ -27,9 +27,6 @@ android {
|
|||||||
minifyEnabled false
|
minifyEnabled false
|
||||||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
||||||
}
|
}
|
||||||
debug {
|
|
||||||
applicationIdSuffix ".debug"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
compileOptions {
|
compileOptions {
|
||||||
@@ -53,6 +50,7 @@ dependencies {
|
|||||||
})
|
})
|
||||||
compile "android.arch.lifecycle:extensions:1.0.0"
|
compile "android.arch.lifecycle:extensions:1.0.0"
|
||||||
compile "android.arch.lifecycle:runtime:1.0.3"
|
compile "android.arch.lifecycle:runtime:1.0.3"
|
||||||
|
compile 'com.android.billingclient:billing:1.0'
|
||||||
compile 'com.android.support:appcompat-v7:27.0.2'
|
compile 'com.android.support:appcompat-v7:27.0.2'
|
||||||
compile 'com.android.support.constraint:constraint-layout:1.1.0-beta4'
|
compile 'com.android.support.constraint:constraint-layout:1.1.0-beta4'
|
||||||
compile 'com.android.support:design:27.0.2'
|
compile 'com.android.support:design:27.0.2'
|
||||||
|
|||||||
@@ -0,0 +1,132 @@
|
|||||||
|
/*
|
||||||
|
* 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/>.
|
||||||
|
*/
|
||||||
|
package common.util
|
||||||
|
|
||||||
|
import android.app.Activity
|
||||||
|
import android.content.Context
|
||||||
|
import com.android.billingclient.api.*
|
||||||
|
import com.android.billingclient.api.BillingClient.BillingResponse
|
||||||
|
import com.android.billingclient.api.BillingClient.SkuType
|
||||||
|
import io.reactivex.Observable
|
||||||
|
import io.reactivex.subjects.BehaviorSubject
|
||||||
|
import io.reactivex.subjects.Subject
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
class BillingManager @Inject constructor(context: Context) : PurchasesUpdatedListener {
|
||||||
|
|
||||||
|
val purchases: Observable<List<Purchase>> = BehaviorSubject.create()
|
||||||
|
val iabs: Observable<List<SkuDetails>> = BehaviorSubject.create()
|
||||||
|
val subs: Observable<List<SkuDetails>> = BehaviorSubject.create()
|
||||||
|
|
||||||
|
private val iabSkus = listOf("remove_ads")
|
||||||
|
private val subSkus = listOf("qksms_plus", "qksms_plus_5", "qksms_plus_10")
|
||||||
|
private val purchaseList = mutableListOf<Purchase>()
|
||||||
|
|
||||||
|
private val billingClient: BillingClient = BillingClient.newBuilder(context).setListener(this).build()
|
||||||
|
private var isServiceConnected = false
|
||||||
|
private var billingClientResponseCode: Int? = null
|
||||||
|
|
||||||
|
init {
|
||||||
|
startServiceConnection {
|
||||||
|
queryPurchases()
|
||||||
|
querySkuDetailsAsync()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun queryPurchases() {
|
||||||
|
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)
|
||||||
|
(this.purchases as Subject).onNext(purchaseList)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private fun startServiceConnection(onSuccess: () -> Unit) {
|
||||||
|
billingClient.startConnection(object : BillingClientStateListener {
|
||||||
|
override fun onBillingSetupFinished(@BillingResponse billingResponseCode: Int) {
|
||||||
|
if (billingResponseCode == BillingResponse.OK) {
|
||||||
|
isServiceConnected = true
|
||||||
|
onSuccess()
|
||||||
|
}
|
||||||
|
billingClientResponseCode = billingResponseCode
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onBillingServiceDisconnected() {
|
||||||
|
isServiceConnected = false
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun querySkuDetailsAsync() {
|
||||||
|
executeServiceRequest {
|
||||||
|
val iabParams = SkuDetailsParams.newBuilder().setSkusList(iabSkus).setType(BillingClient.SkuType.INAPP)
|
||||||
|
billingClient.querySkuDetailsAsync(iabParams.build()) { responseCode, skuDetailsList ->
|
||||||
|
if (responseCode == BillingResponse.OK) {
|
||||||
|
(iabs as Subject).onNext(skuDetailsList)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val subParams = SkuDetailsParams.newBuilder().setSkusList(subSkus).setType(BillingClient.SkuType.SUBS)
|
||||||
|
billingClient.querySkuDetailsAsync(subParams.build()) { responseCode, skuDetailsList ->
|
||||||
|
if (responseCode == BillingResponse.OK) {
|
||||||
|
(subs as Subject).onNext(skuDetailsList)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun initiatePurchaseFlow(activity: Activity, skuId: String, oldSkus: ArrayList<String>, @BillingClient.SkuType billingType: String) {
|
||||||
|
executeServiceRequest {
|
||||||
|
val params = BillingFlowParams.newBuilder().setSku(skuId).setType(billingType).setOldSkus(oldSkus)
|
||||||
|
billingClient.launchBillingFlow(activity, params.build())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun executeServiceRequest(runnable: () -> Unit) {
|
||||||
|
when (isServiceConnected) {
|
||||||
|
true -> runnable()
|
||||||
|
false -> startServiceConnection(runnable)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onPurchasesUpdated(resultCode: Int, purchases: List<Purchase>?) {
|
||||||
|
when (resultCode) {
|
||||||
|
BillingResponse.OK -> {
|
||||||
|
purchases?.forEach { purchaseList.add(it) }
|
||||||
|
(this.purchases as Subject).onNext(purchaseList)
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> {
|
||||||
|
// Ignored
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -27,6 +27,7 @@ import com.moez.QKSMS.R
|
|||||||
import com.uber.autodispose.android.lifecycle.scope
|
import com.uber.autodispose.android.lifecycle.scope
|
||||||
import com.uber.autodispose.kotlin.autoDisposable
|
import com.uber.autodispose.kotlin.autoDisposable
|
||||||
import common.di.appComponent
|
import common.di.appComponent
|
||||||
|
import common.util.BillingManager
|
||||||
import common.util.filter.ConversationFilter
|
import common.util.filter.ConversationFilter
|
||||||
import data.model.MenuItem
|
import data.model.MenuItem
|
||||||
import data.model.SyncLog
|
import data.model.SyncLog
|
||||||
@@ -38,12 +39,14 @@ import io.reactivex.rxkotlin.withLatestFrom
|
|||||||
import io.realm.Realm
|
import io.realm.Realm
|
||||||
import presentation.common.Navigator
|
import presentation.common.Navigator
|
||||||
import presentation.common.base.QkViewModel
|
import presentation.common.base.QkViewModel
|
||||||
|
import timber.log.Timber
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class MainViewModel : QkViewModel<MainView, MainState>(MainState()) {
|
class MainViewModel : QkViewModel<MainView, MainState>(MainState()) {
|
||||||
|
|
||||||
@Inject lateinit var context: Context
|
@Inject lateinit var context: Context
|
||||||
|
@Inject lateinit var billingManager: BillingManager
|
||||||
@Inject lateinit var navigator: Navigator
|
@Inject lateinit var navigator: Navigator
|
||||||
@Inject lateinit var conversationFilter: ConversationFilter
|
@Inject lateinit var conversationFilter: ConversationFilter
|
||||||
@Inject lateinit var messageRepo: MessageRepository
|
@Inject lateinit var messageRepo: MessageRepository
|
||||||
@@ -94,6 +97,10 @@ class MainViewModel : QkViewModel<MainView, MainState>(MainState()) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
markAllSeen.execute(Unit)
|
markAllSeen.execute(Unit)
|
||||||
|
|
||||||
|
billingManager.purchases.subscribe { purchases -> Timber.v("Purchases: $purchases") }
|
||||||
|
billingManager.iabs.subscribe { iabs -> Timber.v("IABs: $iabs") }
|
||||||
|
billingManager.subs.subscribe { subs -> Timber.v("Subs: $subs") }
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun bindView(view: MainView) {
|
override fun bindView(view: MainView) {
|
||||||
@@ -139,7 +146,8 @@ class MainViewModel : QkViewModel<MainView, MainState>(MainState()) {
|
|||||||
DrawerItem.ARCHIVED -> newState { it.copy(page = Archived(messageRepo.getConversations(true))) }
|
DrawerItem.ARCHIVED -> newState { it.copy(page = Archived(messageRepo.getConversations(true))) }
|
||||||
DrawerItem.SCHEDULED -> newState { it.copy(page = Scheduled()) }
|
DrawerItem.SCHEDULED -> newState { it.copy(page = Scheduled()) }
|
||||||
DrawerItem.BLOCKED -> newState { it.copy(page = Blocked()) }
|
DrawerItem.BLOCKED -> newState { it.copy(page = Blocked()) }
|
||||||
else -> {} // Do nothing
|
else -> {
|
||||||
|
} // Do nothing
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.autoDisposable(view.scope())
|
.autoDisposable(view.scope())
|
||||||
|
|||||||
Reference in New Issue
Block a user