Skip to content

Commit

Permalink
Convert BillingService to Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Dibrivnyi committed May 30, 2020
1 parent 3c0c34b commit c2a7b13
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 91 deletions.
91 changes: 0 additions & 91 deletions library/src/main/java/com/billing/BillingService.java

This file was deleted.

75 changes: 75 additions & 0 deletions library/src/main/java/com/billing/BillingService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.billing

import android.app.Activity
import androidx.annotation.CallSuper

abstract class BillingService {

private val purchaseServiceListeners: MutableList<PurchaseServiceListener> = mutableListOf()
private val subscriptionServiceListeners: MutableList<SubscriptionServiceListener> = mutableListOf()

fun addPurchaseListener(purchaseServiceListener: PurchaseServiceListener) {
purchaseServiceListeners.add(purchaseServiceListener)
}

fun removePurchaseListener(purchaseServiceListener: PurchaseServiceListener) {
purchaseServiceListeners.remove(purchaseServiceListener)
}

fun addSubscriptionListener(subscriptionServiceListener: SubscriptionServiceListener) {
subscriptionServiceListeners.add(subscriptionServiceListener)
}

fun removeSubscriptionListener(subscriptionServiceListener: SubscriptionServiceListener) {
subscriptionServiceListeners.remove(subscriptionServiceListener)
}

/**
* @param sku - product specificator
* @param isRestore - a flag indicating whether it's a fresh purchase or restored product
*/
fun productOwned(sku: String?, isRestore: Boolean) {
for (purchaseServiceListener in purchaseServiceListeners) {
if (isRestore) {
purchaseServiceListener.onProductRestored(sku)
} else {
purchaseServiceListener.onProductPurchased(sku)
}
}
}

/**
* @param sku - subscription specificator
* @param isRestore - a flag indicating whether it's a fresh purchase or restored subscription
*/
fun subscriptionOwned(sku: String, isRestore: Boolean) {
for (subscriptionServiceListener in subscriptionServiceListeners) {
if (isRestore) {
subscriptionServiceListener.onSubscriptionRestored(sku)
} else {
subscriptionServiceListener.onSubscriptionPurchased(sku)
}
}
}

fun updatePrices(iapkeyPrices: Map<String, String>) {
for (billingServiceListener in purchaseServiceListeners) {
billingServiceListener.onPricesUpdated(iapkeyPrices)
}
for (billingServiceListener in subscriptionServiceListeners) {
billingServiceListener.onPricesUpdated(iapkeyPrices)
}
}

abstract fun init(key: String?)
abstract fun buy(activity: Activity, sku: String)
abstract fun subscribe(activity: Activity, sku: String)
abstract fun unsubscribe(activity: Activity, sku: String)
abstract fun enableDebugLogging(enable: Boolean)

@CallSuper
open fun close() {
subscriptionServiceListeners.clear()
purchaseServiceListeners.clear()
}
}

0 comments on commit c2a7b13

Please sign in to comment.