|
| 1 | +package com.superwall.sdk.utilities |
| 2 | + |
| 3 | +import android.app.Application |
| 4 | +import com.superwall.sdk.Superwall |
| 5 | +import com.superwall.sdk.config.options.PaywallOptions |
| 6 | +import com.superwall.sdk.config.options.SuperwallOptions |
| 7 | +import com.superwall.sdk.delegate.subscription_controller.PurchaseController |
| 8 | +import com.superwall.sdk.misc.ActivityProvider |
| 9 | + |
| 10 | +class SuperwallBuilder { |
| 11 | + var purchaseController: PurchaseController? = null |
| 12 | + var options: SuperwallOptions? = null |
| 13 | + private set |
| 14 | + var activityProvider: ActivityProvider? = null |
| 15 | + var completion: (() -> Unit)? = null |
| 16 | + |
| 17 | + @SuperwallDSL |
| 18 | + fun options(action: SuperwallOptions.() -> Unit) { |
| 19 | + options = SuperwallOptions().apply(action) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Configures a shared instance of [Superwall] for use throughout your app. |
| 25 | + * |
| 26 | + * Call this as soon as your app finishes launching in `onCreate` in your `MainApplication` |
| 27 | + * class. |
| 28 | + * Check out [Configuring the SDK](https://docs.superwall.com/docs/configuring-the-sdk) for |
| 29 | + * information about how to configure the SDK. |
| 30 | + * |
| 31 | + * @param apiKey Your Public API Key that you can get from the Superwall dashboard |
| 32 | + * settings. If you don't have an account, you can |
| 33 | + * [sign up for free](https://superwall.com/sign-up). |
| 34 | + * @param configure A lambda that allows you to configure the SDK. Inside the configuration block, |
| 35 | + * you can setup your [PurchaseController], [ActivityProvider], a completion |
| 36 | + * closure and [SuperwallOptions] via [SuperwallBuilder.options] closure. |
| 37 | + * @return The configured [Superwall] instance. |
| 38 | + */ |
| 39 | + |
| 40 | +@SuperwallDSL |
| 41 | +fun Application.configureSuperwall( |
| 42 | + apiKey: String, |
| 43 | + configure: SuperwallBuilder.() -> Unit, |
| 44 | +): Superwall { |
| 45 | + val builder = SuperwallBuilder().apply(configure) |
| 46 | + Superwall.configure( |
| 47 | + this, |
| 48 | + apiKey, |
| 49 | + builder.purchaseController, |
| 50 | + builder.options, |
| 51 | + builder.activityProvider, |
| 52 | + builder.completion, |
| 53 | + ) |
| 54 | + return Superwall.instance |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Enables you to define [PaywallOptions] via closure. |
| 59 | + * */ |
| 60 | +@SuperwallDSL |
| 61 | +fun SuperwallOptions.paywalls(action: PaywallOptions.() -> Unit) { |
| 62 | + paywalls = this.paywalls.apply(action) |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Enables you to define [SuperwallOptions.Logging] via closure. |
| 67 | + * */ |
| 68 | +@SuperwallDSL |
| 69 | +fun SuperwallOptions.logging(action: SuperwallOptions.Logging.() -> Unit) { |
| 70 | + logging = this.logging.apply(action) |
| 71 | +} |
0 commit comments