Skip to content

Commit

Permalink
Merge pull request #10 from apphud/development
Browse files Browse the repository at this point in the history
fix subscriptions not updating immediately after purchase or restore
  • Loading branch information
ABIvan-Tech authored Apr 6, 2021
2 parents 25b16a1 + db765f0 commit cc9a814
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/apphud/app/App.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.apphud.app

import android.app.Application
import android.util.Log
import com.apphud.sdk.Apphud

class App : Application() {
Expand All @@ -14,7 +15,6 @@ class App : Application() {
app = this

Apphud.enableDebugLogs()
// Apphud.disableAdTracking()
Apphud.start(this, Constants.API_KEY)
}
}
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_EzgZvMy2qRcSsU2uRuDxd9GaqU8BCV"
const val API_KEY = "app_DZzKAkuBWhe9nz3qeofJMU9PJezkxn"
}
6 changes: 5 additions & 1 deletion app/src/main/java/com/apphud/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class MainActivity : AppCompatActivity() {
val listener = object : ApphudListener {
override fun apphudSubscriptionsUpdated(subscriptions: List<ApphudSubscription>) {

Log.d("apphud","SUBSCRIPTIONS UPDATED: ${Apphud.subscriptions() }. Has active subscription: ${Apphud.hasActiveSubscription()}")

subscriptions.forEach { subscription ->
val model = when (val product = products[subscription.productId]) {
null -> mapper.map(subscription)
Expand Down Expand Up @@ -68,7 +70,9 @@ 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) { _ ->
Log.d("apphud","PURCHASE RESULT: ${Apphud.subscriptions() }. Has active subscription: ${Apphud.hasActiveSubscription()}")
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ android.enableJetifier=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official

sdkVersion=0.8.3
sdkVersion=0.8.5
10 changes: 6 additions & 4 deletions sdk/src/main/java/com/apphud/sdk/ApphudInternal.kt
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,13 @@ internal object ApphudInternal {

client.purchased(mkPurchasesBody(purchases)) { customer ->
handler.post {
storage.customer = customer
callback.invoke(purchases.map { it.purchase })
apphudListener?.apphudSubscriptionsUpdated(customer.subscriptions)
apphudListener?.apphudNonRenewingPurchasesUpdated(customer.purchases)
}
}

callback.invoke(purchases.map { it.purchase })

purchases.forEach {

when (it.purchase.purchaseState) {
Expand All @@ -244,14 +244,16 @@ internal object ApphudInternal {
storage.isNeedSync = true
billing.restoreCallback = { records ->

ApphudLog.log("syncPurchases: $records")
ApphudLog.log("restoreCallback: $records")

when {
prevPurchases.containsAll(records) -> ApphudLog.log("syncPurchases: Don't send equal purchases from prev state")
else -> client.purchased(mkPurchaseBody(records)) { customer ->
handler.post {
prevPurchases.addAll(records)
storage.isNeedSync = false
storage.customer = customer
ApphudLog.log("syncPurchases: customer updated $customer")
apphudListener?.apphudSubscriptionsUpdated(customer.subscriptions)
apphudListener?.apphudNonRenewingPurchasesUpdated(customer.purchases)
}
Expand All @@ -260,7 +262,7 @@ internal object ApphudInternal {
}
}
billing.historyCallback = { purchases ->
ApphudLog.log("syncPurchases: history purchases: $purchases")
ApphudLog.log("historyCallback: $purchases")
billing.restore(BillingClient.SkuType.SUBS, purchases)
billing.restore(BillingClient.SkuType.INAPP, purchases)
}
Expand Down

0 comments on commit cc9a814

Please sign in to comment.