Skip to content

Commit 5050413

Browse files
authored
Merge pull request #129 from superwall/ir/feature/config-dsl
Add idiomatic DSL for prettier configuration
2 parents a4ea425 + 770d0d2 commit 5050413

File tree

3 files changed

+105
-3
lines changed

3 files changed

+105
-3
lines changed

CHANGELOG.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,37 @@
22

33
The changelog for `Superwall`. Also see the [releases](https://github.com/superwall/Superwall-Android/releases) on GitHub.
44

5-
## 1.1.9
5+
## 1.2.0
66

7-
### Deprecations
7+
### Enhancements
8+
- Adds DSL methods for configuring the SDK. You can now use a configuration block:
9+
```kotlin
10+
fun Application.configureSuperwall(
11+
apiKey: String,
12+
configure: SuperwallBuilder.() -> Unit,
13+
)
14+
```
15+
16+
This allows you to configure the SDK in a more idiomatic way:
17+
```kotlin
18+
configureSuperwall(CONSTANT_API_KEY){
19+
options {
20+
logging {
21+
level = LogLevel.debug
22+
}
23+
paywalls {
24+
shouldPreload = false
25+
}
26+
}
27+
}
28+
```
829

30+
### Deprecations
931

1032
This release includes multiple deprecations that will be removed in upcoming versions.
1133
Most are internal and will not affect the public API, those that will are marked as such and a simple migration
1234
path is provided. The notable ones in the public API are as follows:
1335

14-
- Deprecated configuration method `Superwall.configure(applicationContext: Context, ...)` in favor of `Superwall.configure(applicationContext: Application, ...)` to enforce type safety. The rest of the method signature remains the same.
1536
- Deprecated `DebugViewControllerActivity` in favor of `DebugViewActivity`
1637
- Deprecated `PaywallViewController` in favor of `PaywallView`
1738
- Deprecated belonging methods:
@@ -38,6 +59,12 @@ path is provided. The notable ones in the public API are as follows:
3859
- Deprecated `LogScope.debugViewController` in favor of `LogScope.debugView`
3960
- Deprecated `PaywallPresentationRequestStatus.NoPaywallViewController` in favor of `NoPaywallView`
4061

62+
## 1.1.9
63+
64+
### Deprecations
65+
66+
- Deprecated configuration method `Superwall.configure(applicationContext: Context, ...)` in favor of `Superwall.configure(applicationContext: Application, ...)` to enforce type safety. The rest of the method signature remains the same.
67+
4168
### Fixes
4269

4370
- SW-2878: and it's related leaks. The `PaywallViewController` was not being properly detached when activity was stopped, causing memory leaks.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.superwall.sdk.utilities
2+
3+
@DslMarker
4+
annotation class SuperwallDSL

0 commit comments

Comments
 (0)