Skip to content

Commit

Permalink
Merge pull request #13 from apphud/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
ABIvan-Tech authored Apr 29, 2021
2 parents cc9a814 + cdaa16e commit 8f78c56
Show file tree
Hide file tree
Showing 16 changed files with 419 additions and 137 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/apphud/app/Constants.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.apphud.app

object Constants {
const val API_KEY = "app_DZzKAkuBWhe9nz3qeofJMU9PJezkxn"
const val API_KEY = "app_4sY9cLggXpMDDQMmvc5wXUPGReMp8G"
}
12 changes: 9 additions & 3 deletions app/src/main/java/com/apphud/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ class MainActivity : AppCompatActivity() {
null -> mapper.map(subscription)
else -> mapper.map(product, subscription)
}
products[model.productId] = model
when (val existingSubscription = products[model.productId]?.subscription) {
null -> products[model.productId] = model
else -> Log.d("apphud","already has subscription, will not update")
}
}

adapter.products = products.values.toList()
Expand Down Expand Up @@ -70,15 +73,18 @@ class MainActivity : AppCompatActivity() {
Log.e("Apphud", "onClick model: $model")
when (model.details) {
null -> Log.e("Apphud", "details is empty")
else -> Apphud.purchase(this, model.details) { _ ->
else -> Apphud.purchase(this, model.details.sku) { result ->
Log.d("apphud","PURCHASE RESULT: ${Apphud.subscriptions() }. Has active subscription: ${Apphud.hasActiveSubscription()}")
Log.d("apphud", "${result.error?.toString()}")
}
}
}

val syncButton: Button = findViewById(R.id.syncButtonViewId)
syncButton.setOnClickListener {
Apphud.syncPurchases()
Apphud.restorePurchases { subscriptions, purchases, error ->
Log.d("apphud", "restorePurchases: subscriptions=${subscriptions?.toString()} purchases=${purchases?.toString()} error=${error?.toString()} ")
}
}
val recyclerView = findViewById<RecyclerView>(R.id.recyclerViewId)
recyclerView.adapter = adapter
Expand Down
64 changes: 55 additions & 9 deletions sdk/src/main/java/com/apphud/sdk/Apphud.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.apphud.sdk

import android.app.Activity
import android.content.Context
import com.android.billingclient.api.Purchase
import com.android.billingclient.api.SkuDetails
import com.apphud.sdk.domain.ApphudNonRenewingPurchase
import com.apphud.sdk.domain.ApphudSubscription
Expand Down Expand Up @@ -126,6 +125,20 @@ object Apphud {
@kotlin.jvm.JvmStatic
fun syncPurchases() = ApphudInternal.syncPurchases()

/**
* Implements `Restore Purchases` mechanism. Basically it just sends current Play Market Purchase Tokens to Apphud and returns subscriptions info.
*
* Even if callback returns some subscription, it doesn't mean that subscription is active. You should check `subscription.isActive()` value.
*
* @param callback: Required. Returns array of subscription (or subscriptions in case you have more than one subscription group), array of standard in-app purchases and an error. All of three parameters are optional.
*/
@kotlin.jvm.JvmStatic
fun restorePurchases(callback: (subscriptions: List<ApphudSubscription>?,
purchases: List<ApphudNonRenewingPurchase>?,
error: ApphudError?) -> Unit) {
ApphudInternal.restorePurchases(callback)
}

/**
* Returns an array of **SkuDetails** objects that you added in Apphud.
* Note that this method will return **null** if products are not yet fetched.
Expand Down Expand Up @@ -158,25 +171,58 @@ object Apphud {
}

/**
* Purchases product and automatically submit
* Purchase product by id and automatically submit Google Play purchase token to Apphud
* @param activity: current Activity for use
* @param productId: The identifier of the product you wish to purchase
* @param block: Optional. Returns `ApphudPurchaseResult` object.
*/
@kotlin.jvm.JvmStatic
fun purchase(activity: Activity, productId: String, block: ((ApphudPurchaseResult) -> Unit)?) =
ApphudInternal.purchase(activity, productId, true, block)

/**
* Purchase sku product and automatically submit Google Play purchase token to Apphud
*
* @param activity current Activity for use
* @param details The SkuDetails of the product you wish to purchase
* @param block Optional. Returns `ApphudPurchaseResult` object.
*/
@kotlin.jvm.JvmStatic
fun purchase(activity: Activity, details: SkuDetails, block: ((ApphudPurchaseResult) -> Unit)?) =
ApphudInternal.purchase(activity, details, true, block)

/**
* Purchase product by id and automatically submit Google Play purchase token to Apphud
*
* This method doesn't wait until Apphud validates purchase from Google Play and immediately returns result object.
* This method may be useful if you don't care about purchases validation in callback.
*
* Note: When using this method properties `subscription` and `nonRenewingPurchase` in `ApphudPurchaseResult` will always be `null` !
*
* @param activity: current Activity for use
* @param productId: The identifier of the product you wish to purchase
* @param block: The closure that will be called when purchase completes.
*/
@kotlin.jvm.JvmStatic
fun purchase(activity: Activity, productId: String, block: (List<Purchase>) -> Unit) =
ApphudInternal.purchase(activity, productId, block)
fun purchaseWithoutValidation(activity: Activity, productId: String, block: ((ApphudPurchaseResult) -> Unit)?) =
ApphudInternal.purchase(activity, productId, false, block)

/**
* Purchases product and automatically submit
* Purchase sku product and automatically submit Google Play purchase token to Apphud
*
* This method doesn't wait until Apphud validates purchase from Google Play and immediately returns result object.
* This method may be useful if you don't care about purchases validation in callback.
*
* When using this method properties `subscription` and `nonRenewingPurchase` in `ApphudPurchaseResult` will always be `null` !
*
* @param activity current Activity for use
* @param details The skuDetails of the product you wish to purchase
* @param details The SkuDetails of the product you wish to purchase
* @param block The closure that will be called when purchase completes.
*/
@kotlin.jvm.JvmStatic
fun purchase(activity: Activity, details: SkuDetails, block: (List<Purchase>) -> Unit) =
ApphudInternal.purchase(activity, details, block)

fun purchaseWithoutValidation(activity: Activity, details: SkuDetails, block: ((ApphudPurchaseResult) -> Unit)?) =
ApphudInternal.purchase(activity, details, false, block)

/**
* Set custom user property.
Expand Down
13 changes: 13 additions & 0 deletions sdk/src/main/java/com/apphud/sdk/ApphudError.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.apphud.sdk

data class ApphudError(
override val message: String,
/**
* Additional error message
* */
val secondErrorMessage: String? = null,
/**
* Additional error code
* */
val errorCode: Int? = null
) : Error(message)
Loading

0 comments on commit 8f78c56

Please sign in to comment.