Skip to content

Commit

Permalink
Merge pull request #3 from adrcotfas/separate_activity
Browse files Browse the repository at this point in the history
IapConnector: refactoring
  • Loading branch information
akshaaatt authored Mar 11, 2021
2 parents f79a67d + 38a7314 commit ac2fb19
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ iapConnector.setOnInAppEventsListener(object : InAppEventsListener {
#### Making a purchase

```kotlin
iapConnector.makePurchase("<sku>")
iapConnector.makePurchase(this, "<sku>")
```

## Sample App
Expand Down
26 changes: 12 additions & 14 deletions app/src/main/java/com/alphelios/superrich/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
setContentView(binding.root)

iapConnector = IapConnector(
this, "key" // License Key
this, "key" // License Key
)
.setInAppProductIds(listOf("base", "moderate", "plenty", "quite"))
.setConsumableProductIds(listOf("base", "moderate", "plenty", "quite"))
.setSubscriptionIds(listOf())
.autoAcknowledge()
.connect()
.setInAppProductIds(listOf("base", "moderate", "plenty", "quite"))
.autoAcknowledge()
.connect()

iapConnector.setOnInAppEventsListener(object : InAppEventsListener {

Expand Down Expand Up @@ -71,43 +69,43 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
}

override fun onError(
inAppConnector: IapConnector,
result: DataWrappers.BillingResponse?
inAppConnector: IapConnector,
result: DataWrappers.BillingResponse?
) {
Log.d(tag, "Error : ${result?.message}")
}
})

binding.btPurchaseCons.setOnClickListener {
if (fetchedSkuDetailsList.find { it.sku == "base" } != null) {
iapConnector.makePurchase("base")
iapConnector.makePurchase(this,"base")
}
}
binding.btnMonthly.setOnClickListener {
if (fetchedSkuDetailsList.find { it.sku == "subscribe" } != null) {
iapConnector.makePurchase("subscribe")
iapConnector.makePurchase(this, "subscribe")
}
}

binding.btnYearly.setOnClickListener {
if (fetchedSkuDetailsList.find { it.sku == "yearly" } != null) {
iapConnector.makePurchase("yearly")
iapConnector.makePurchase(this,"yearly")
}
}
binding.btnQuite.setOnClickListener {
if (fetchedSkuDetailsList.find { it.sku == "quite" } != null) {
iapConnector.makePurchase("quite")
iapConnector.makePurchase(this,"quite")
}
}
binding.btnModerate.setOnClickListener {
if (fetchedSkuDetailsList.find { it.sku == "moderate" } != null) {
iapConnector.makePurchase("moderate")
iapConnector.makePurchase(this,"moderate")
}
}

binding.btnUltimate.setOnClickListener {
if (fetchedSkuDetailsList.find { it.sku == "plenty" } != null) {
iapConnector.makePurchase("plenty")
iapConnector.makePurchase(this,"plenty")
}
}
}
Expand Down
21 changes: 12 additions & 9 deletions iap/src/main/java/com/alphelios/iap/IapConnector.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.alphelios.iap

import android.app.Activity
import android.content.Context
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.android.billingclient.api.*
import com.android.billingclient.api.BillingClient.BillingResponseCode.*
import com.android.billingclient.api.BillingClient.FeatureType.SUBSCRIPTIONS
Expand All @@ -14,7 +14,7 @@ import com.android.billingclient.api.BillingClient.SkuType.SUBS
* Handles vital processes while dealing with IAP.
* Works around a listener [InAppEventsListener] for delivering events back to the caller.
*/
class IapConnector(private val activity: AppCompatActivity, private val base64Key: String) {
class IapConnector(context: Context, private val base64Key: String) {
private var shouldAutoAcknowledge: Boolean = false
private var fetchedSkuDetailsList = mutableListOf<SkuDetails>()
private val tag = "InAppLog"
Expand All @@ -27,7 +27,7 @@ class IapConnector(private val activity: AppCompatActivity, private val base64Ke
private lateinit var iapClient: BillingClient

init {
init(activity)
init(context)
}

/**
Expand Down Expand Up @@ -68,13 +68,14 @@ class IapConnector(private val activity: AppCompatActivity, private val base64Ke
* Its result is received in [PurchasesUpdatedListener] which further is handled
* by [handleConsumableProducts] / [handleNonConsumableProducts].
*/
fun makePurchase(sku: String) {
fun makePurchase(activity: Activity, sku: String) {
if (fetchedSkuDetailsList.isEmpty())
inAppEventsListener?.onError(this, DataWrappers.BillingResponse("Products not fetched"))
else
iapClient.launchBillingFlow(
activity,
BillingFlowParams.newBuilder().setSkuDetails(fetchedSkuDetailsList.find { it.sku == sku }!!).build()
BillingFlowParams.newBuilder()
.setSkuDetails(fetchedSkuDetailsList.find { it.sku == sku }!!).build()
)
}

Expand Down Expand Up @@ -227,15 +228,17 @@ class IapConnector(private val activity: AppCompatActivity, private val base64Ke
* Returns all the **non-consumable** purchases of the user.
*/
fun getAllPurchases() {
if(iapClient.isReady) {
if (iapClient.isReady) {
val allPurchases = mutableListOf<Purchase>()
allPurchases.addAll(iapClient.queryPurchases(INAPP).purchasesList!!)
if (isSubSupportedOnDevice())
allPurchases.addAll(iapClient.queryPurchases(SUBS).purchasesList!!)
processPurchases(allPurchases)
}
else{
inAppEventsListener?.onError(this, DataWrappers.BillingResponse("Client not initialized yet."))
} else {
inAppEventsListener?.onError(
this,
DataWrappers.BillingResponse("Client not initialized yet.")
)
}
}

Expand Down

0 comments on commit ac2fb19

Please sign in to comment.