Skip to content

Commit

Permalink
Add Options builder
Browse files Browse the repository at this point in the history
  • Loading branch information
twyatt committed Jul 26, 2024
1 parent 0640241 commit 1d6eb0c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 14 deletions.
2 changes: 1 addition & 1 deletion kable-core/src/jsMain/kotlin/Bluetooth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private fun Options.filters(): List<BluetoothLEScanFilterInit> =
if (filterSets?.isNotEmpty() == true) {
filterSets.toBluetoothLEScanFilterInit()
} else {
FiltersBuilder().apply(filters).build().toBluetoothLEScanFilterInit()
filterPredicates.toBluetoothLEScanFilterInit()
}

// Note: Web Bluetooth requires that UUIDs be provided as lowercase strings.
Expand Down
29 changes: 16 additions & 13 deletions kable-core/src/jsMain/kotlin/Options.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,19 @@ package com.juul.kable
import com.benasher44.uuid.Uuid

/** https://developer.mozilla.org/en-US/docs/Web/API/Bluetooth/requestDevice */
public data class Options(
public fun Options(builder: OptionsBuilder.() -> Unit): Options =
OptionsBuilder().apply(builder).build()

// Deprecation notice acts to inform consumer, `Options` class itself is not intended to be removed.
@Deprecated(
message = "Use Options builder instead.",
replaceWith = ReplaceWith("Options { }"),
)
public data class Options internal constructor(

@Deprecated(
message = "Replaced with filters builder DSL",
replaceWith = ReplaceWith("filters = { }"),
replaceWith = ReplaceWith("Options { filters { } }"),
level = DeprecationLevel.WARNING,
)
val filterSets: List<FilterSet>? = null,
Expand All @@ -17,17 +26,11 @@ public data class Options(
*
* https://webbluetoothcg.github.io/web-bluetooth/#device-discovery
*/
@Deprecated(
message = "Use Options builder instead.",
replaceWith = ReplaceWith("Options { optionalServices = listOf() }"),
)
val optionalServices: List<Uuid>? = null,

/**
* Filters to apply when requesting devices. If predicates are non-empty, then only devices
* that match at least one of the predicates will appear in the `requestDevice` picker.
*
* Filtering on Service Data is not supported because it is not implemented:
* https://github.com/WebBluetoothCG/web-bluetooth/blob/main/implementation-status.md
*
* Filtering on Manufacturer Data is supported and a good explanation can be found here:
* https://github.com/WebBluetoothCG/web-bluetooth/blob/main/data-filters-explainer.md
*/
val filters: FiltersBuilder.() -> Unit = { },
internal val filterPredicates: List<FilterPredicate>,
)
32 changes: 32 additions & 0 deletions kable-core/src/jsMain/kotlin/OptionsBuilder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.juul.kable

import com.benasher44.uuid.Uuid

public class OptionsBuilder internal constructor() {

private var filterPredicates: List<FilterPredicate> = emptyList()

/**
* Filters to apply when requesting devices. If predicates are non-empty, then only devices
* that match at least one of the predicates will appear in the `requestDevice` picker.
*
* Filtering on Service Data is not supported because it is not implemented:
* https://github.com/WebBluetoothCG/web-bluetooth/blob/main/implementation-status.md
*
* Filtering on Manufacturer Data is supported and a good explanation can be found here:
* https://github.com/WebBluetoothCG/web-bluetooth/blob/main/data-filters-explainer.md
*/
public fun filters(builder: FiltersBuilder.() -> Unit) {
filterPredicates = FiltersBuilder().apply(builder).build()
}

/**
* Access is only granted to services listed as [service filters][Filter.Service] in [filters].
* If any additional services need to be accessed, they must be specified in [optionalServices].
*
* https://webbluetoothcg.github.io/web-bluetooth/#device-discovery
*/
public var optionalServices: List<Uuid>? = null

internal fun build() = Options(null, optionalServices, filterPredicates)
}

0 comments on commit 1d6eb0c

Please sign in to comment.