Skip to content

Commit f0e5c06

Browse files
committed
Add DatePicker bound to a Foundation Date
1 parent 7e3521a commit f0e5c06

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// DatePicker.swift
3+
// AndroidSwiftUICore
4+
//
5+
// A control for choosing a calendar date. The selection round-trips the
6+
// bridge as milliseconds since the Unix epoch — Compose's Material3
7+
// DatePickerState currency, and a trivial conversion from Foundation's Date.
8+
//
9+
10+
import Foundation
11+
12+
public struct DatePicker<Label: View>: View {
13+
14+
internal let label: Label
15+
internal let selection: Binding<Date>
16+
17+
public init(selection: Binding<Date>, @ViewBuilder label: () -> Label) {
18+
self.label = label()
19+
self.selection = selection
20+
}
21+
22+
public typealias Body = Never
23+
}
24+
25+
public extension DatePicker where Label == Text {
26+
init<S: StringProtocol>(_ title: S, selection: Binding<Date>) {
27+
self.init(selection: selection) { Text(title) }
28+
}
29+
}
30+
31+
extension DatePicker: PrimitiveView {
32+
public func _render(in context: ResolveContext) -> RenderNode {
33+
let binding = selection
34+
let callbackID = context.callbacks.register(.double { millis in
35+
binding.wrappedValue = Date(timeIntervalSince1970: millis / 1000)
36+
})
37+
return RenderNode(
38+
type: "DatePicker",
39+
id: context.path,
40+
props: [
41+
"millis": .double(selection.wrappedValue.timeIntervalSince1970 * 1000),
42+
"onChange": .int(Int(callbackID)),
43+
],
44+
children: Evaluator.resolveChildren(label, context.descending("label"))
45+
)
46+
}
47+
}

0 commit comments

Comments
 (0)