Skip to content

Commit af46d50

Browse files
committed
Register demo custom composables (RatingBar, DashedBorder)
1 parent d592fed commit af46d50

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.pureswift.swiftandroid
2+
3+
import androidx.compose.foundation.layout.Box
4+
import androidx.compose.foundation.layout.padding
5+
import androidx.compose.ui.Modifier
6+
import androidx.compose.ui.draw.drawBehind
7+
import androidx.compose.ui.geometry.CornerRadius
8+
import androidx.compose.ui.graphics.Color
9+
import androidx.compose.ui.graphics.PathEffect
10+
import androidx.compose.ui.graphics.drawscope.Stroke
11+
import androidx.compose.ui.unit.dp
12+
import androidx.compose.ui.viewinterop.AndroidView
13+
import android.widget.RatingBar
14+
import com.pureswift.swiftui.ComposableRegistry
15+
16+
/// Registers the demo app's custom composables into the interpreter's registry.
17+
/// Called once before the first render. This is exactly what an app author
18+
/// does to expose a native Android view or a Compose function to SwiftUI.
19+
fun registerDemoComposables() {
20+
21+
// A native Android widget (android.widget.RatingBar) bridged through
22+
// Compose's AndroidView — the canonical "custom Android view" case.
23+
ComposableRegistry.register("RatingBar") { props, _ ->
24+
val rating = props.float("rating") ?: 0f
25+
val max = props.int("max") ?: 5
26+
AndroidView(
27+
factory = { context ->
28+
RatingBar(context).apply {
29+
numStars = max
30+
stepSize = 0.5f
31+
setIsIndicator(true)
32+
}
33+
},
34+
update = { bar ->
35+
bar.numStars = max
36+
bar.rating = rating
37+
},
38+
)
39+
}
40+
41+
// A pure Compose function drawing a dashed border around whatever SwiftUI
42+
// child content is passed into the slot.
43+
ComposableRegistry.register("DashedBorder") { props, children ->
44+
val color = props.color("color") ?: Color(0xFF6750A4.toInt())
45+
Box(
46+
modifier = Modifier
47+
.drawBehind {
48+
drawRoundRect(
49+
color = color,
50+
style = Stroke(
51+
width = 5f,
52+
pathEffect = PathEffect.dashPathEffect(floatArrayOf(24f, 14f)),
53+
),
54+
cornerRadius = CornerRadius(20f, 20f),
55+
)
56+
}
57+
.padding(18.dp),
58+
) {
59+
children()
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)