-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemoComposables.kt
More file actions
67 lines (63 loc) · 2.62 KB
/
Copy pathDemoComposables.kt
File metadata and controls
67 lines (63 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.pureswift.swiftandroid
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.geometry.CornerRadius
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.PathEffect
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import android.widget.RatingBar
import com.pureswift.swiftui.ComposableRegistry
/// Registers the demo app's custom composables into the interpreter's registry.
/// Called once before the first render. This is exactly what an app author
/// does to expose a native Android view or a Compose function to SwiftUI.
fun registerDemoComposables() {
// A native Android widget (android.widget.RatingBar) bridged through
// Compose's AndroidView — the canonical "custom Android view" case.
ComposableRegistry.register("RatingBar") { props, _ ->
val rating = props.float("rating") ?: 0f
val max = props.int("max") ?: 5
val onChanged = props.doubleAction("onRatingChanged")
AndroidView(
factory = { context ->
RatingBar(context).apply {
numStars = max
stepSize = 0.5f
// dragging the stars sends the new rating back to Swift;
// `fromUser` filters out our own programmatic updates
setOnRatingBarChangeListener { _, value, fromUser ->
if (fromUser) onChanged?.invoke(value.toDouble())
}
}
},
update = { bar ->
bar.numStars = max
if (bar.rating != rating) bar.rating = rating
},
)
}
// A pure Compose function drawing a dashed border around whatever SwiftUI
// child content is passed into the slot.
ComposableRegistry.register("DashedBorder") { props, children ->
val color = props.color("color") ?: Color(0xFF6750A4.toInt())
Box(
modifier = Modifier
.drawBehind {
drawRoundRect(
color = color,
style = Stroke(
width = 5f,
pathEffect = PathEffect.dashPathEffect(floatArrayOf(24f, 14f)),
),
cornerRadius = CornerRadius(20f, 20f),
)
}
.padding(18.dp),
) {
children()
}
}
}