Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// DatePicker.swift
// AndroidSwiftUICore
//
// A control for choosing a calendar date. The selection round-trips the
// bridge as milliseconds since the Unix epoch — Compose's Material3
// DatePickerState currency, and a trivial conversion from Foundation's Date.
//

import Foundation

public struct DatePicker<Label: View>: View {

internal let label: Label
internal let selection: Binding<Date>

public init(selection: Binding<Date>, @ViewBuilder label: () -> Label) {
self.label = label()
self.selection = selection
}

public typealias Body = Never
}

public extension DatePicker where Label == Text {
init<S: StringProtocol>(_ title: S, selection: Binding<Date>) {
self.init(selection: selection) { Text(title) }
}
}

extension DatePicker: PrimitiveView {
public func _render(in context: ResolveContext) -> RenderNode {
let binding = selection
let callbackID = context.callbacks.register(.double { millis in
binding.wrappedValue = Date(timeIntervalSince1970: millis / 1000)
})
return RenderNode(
type: "DatePicker",
id: context.path,
props: [
"millis": .double(selection.wrappedValue.timeIntervalSince1970 * 1000),
"onChange": .int(Int(callbackID)),
],
children: Evaluator.resolveChildren(label, context.descending("label"))
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//

import Testing
import Foundation
@testable import AndroidSwiftUICore

#if canImport(Observation)
Expand Down Expand Up @@ -194,4 +195,23 @@ struct ControlTests {
#expect(node.props["text"] == .string("Coleman"))
}
#endif

@Test("DatePicker emits its selection in milliseconds and round-trips a change")
func datePicker() {
let initial = Date(timeIntervalSince1970: 1_768_435_200) // 2026-01-15T00:00:00Z
let changed = Date(timeIntervalSince1970: 1_772_323_200) // 2026-03-01T00:00:00Z
struct Screen: View {
@State var date: Date
var body: some View { DatePicker("Birthday", selection: $date) }
}
let host = ViewHost(Screen(date: initial))
var node = host.evaluate()
#expect(node.type == "DatePicker")
#expect(node.props["millis"] == .double(initial.timeIntervalSince1970 * 1000))
if case .int(let id)? = node.props["onChange"] {
host.callbacks.invokeDouble(Int64(id), changed.timeIntervalSince1970 * 1000)
}
node = host.evaluate()
#expect(node.props["millis"] == .double(changed.timeIntervalSince1970 * 1000))
}
}
6 changes: 6 additions & 0 deletions Demo/App.swiftpm/Sources/MoreControlsPlaygrounds.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@ import AndroidSwiftUI
import SwiftUI
#endif

import Foundation

struct MoreControlsPlayground: View {
@State private var quantity = 1
@State private var password = ""
@State private var choice = "None"
@State private var birthday = Date(timeIntervalSince1970: 946_684_800) // 2000-01-01T00:00:00Z
var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: 0) {
Example("Stepper") {
Stepper("Quantity: \(quantity)", value: $quantity, in: 0...10)
}
Example("DatePicker") {
DatePicker("Birthday", selection: $birthday)
}
Example("SecureField") {
VStack(alignment: .leading, spacing: 8) {
SecureField("Password", text: $password)
Expand Down
47 changes: 47 additions & 0 deletions Demo/swiftui/src/commonMain/kotlin/com/pureswift/swiftui/Render.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.DatePicker
import androidx.compose.material3.DatePickerDialog
import androidx.compose.material3.rememberDatePickerState
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.HorizontalDivider
Expand Down Expand Up @@ -247,6 +250,8 @@ fun Render(node: ViewNode) {

"Menu" -> RenderMenu(node)

"DatePicker" -> RenderDatePicker(node)

"Picker" -> RenderPicker(node)

"NavStack" -> RenderNavStack(node)
Expand Down Expand Up @@ -380,6 +385,48 @@ private fun RenderSection(node: ViewNode) {
}
}

// A label row with a trailing formatted-date button; tapping opens a Material3
// date picker dialog. Selection round-trips as UTC epoch millis, matching both
// Date's representation and DatePickerState's currency, so no conversion.
@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun RenderDatePicker(node: ViewNode) {
val onChange = node.long("onChange")
val millis = (node.double("millis") ?: 0.0).toLong()
var showDialog by remember { mutableStateOf(false) }
Row(verticalAlignment = Alignment.CenterVertically, modifier = node.composeModifiers().fillMaxWidth()) {
RenderChildren(node)
Spacer(modifier = Modifier.weight(1f))
TextButton(onClick = { showDialog = true }) { Text(formatDateMillis(millis)) }
}
if (showDialog) {
val state = rememberDatePickerState(initialSelectedDateMillis = millis)
DatePickerDialog(
onDismissRequest = { showDialog = false },
confirmButton = {
TextButton(onClick = {
showDialog = false
state.selectedDateMillis?.let { picked -> onChange?.let { SwiftBridge.sink.invokeDouble(it, picked.toDouble()) } }
}) { Text("OK") }
},
dismissButton = { TextButton(onClick = { showDialog = false }) { Text("Cancel") } },
) {
DatePicker(state = state)
}
}
}

private val monthAbbreviations = arrayOf("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")

private fun formatDateMillis(millis: Long): String {
val calendar = java.util.Calendar.getInstance(java.util.TimeZone.getTimeZone("UTC"))
calendar.timeInMillis = millis
val month = monthAbbreviations[calendar.get(java.util.Calendar.MONTH)]
val day = calendar.get(java.util.Calendar.DAY_OF_MONTH)
val year = calendar.get(java.util.Calendar.YEAR)
return "$month $day, $year"
}

// A trigger button opening a dropdown; each child Button becomes a menu item
// firing its own tap callback.
@Composable
Expand Down
Loading