Skip to content

Commit 8052e15

Browse files
committed
Make the demo RatingBar interactive and report changes
1 parent f60796b commit 8052e15

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

Demo/app/src/main/java/com/pureswift/swiftandroid/DemoComposables.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,22 @@ fun registerDemoComposables() {
2323
ComposableRegistry.register("RatingBar") { props, _ ->
2424
val rating = props.float("rating") ?: 0f
2525
val max = props.int("max") ?: 5
26+
val onChanged = props.doubleAction("onRatingChanged")
2627
AndroidView(
2728
factory = { context ->
2829
RatingBar(context).apply {
2930
numStars = max
3031
stepSize = 0.5f
31-
setIsIndicator(true)
32+
// dragging the stars sends the new rating back to Swift;
33+
// `fromUser` filters out our own programmatic updates
34+
setOnRatingBarChangeListener { _, value, fromUser ->
35+
if (fromUser) onChanged?.invoke(value.toDouble())
36+
}
3237
}
3338
},
3439
update = { bar ->
3540
bar.numStars = max
36-
bar.rating = rating
41+
if (bar.rating != rating) bar.rating = rating
3742
},
3843
)
3944
}

0 commit comments

Comments
 (0)