diff --git a/calf-ui/src/iosMain/kotlin/com/mohamedrejeb/calf/ui/datepicker/AdaptiveDatePicker.ios.kt b/calf-ui/src/iosMain/kotlin/com/mohamedrejeb/calf/ui/datepicker/AdaptiveDatePicker.ios.kt index 85060fb..ad85f2e 100644 --- a/calf-ui/src/iosMain/kotlin/com/mohamedrejeb/calf/ui/datepicker/AdaptiveDatePicker.ios.kt +++ b/calf-ui/src/iosMain/kotlin/com/mohamedrejeb/calf/ui/datepicker/AdaptiveDatePicker.ios.kt @@ -10,10 +10,12 @@ import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import androidx.compose.ui.interop.UIKitView import androidx.compose.ui.unit.dp +import com.mohamedrejeb.calf.core.InternalCalfApi +import kotlinx.cinterop.BetaInteropApi import kotlinx.cinterop.ExperimentalForeignApi import platform.UIKit.UIDatePicker -@OptIn(ExperimentalForeignApi::class, ExperimentalMaterial3Api::class) +@OptIn(ExperimentalForeignApi::class, ExperimentalMaterial3Api::class, InternalCalfApi::class, BetaInteropApi::class) @Composable actual fun AdaptiveDatePicker( state: AdaptiveDatePickerState, @@ -29,6 +31,8 @@ actual fun AdaptiveDatePicker( } val datePickerManager = remember { DatePickerManager( + initialSelectedDateMillis = state.selectedDateMillis, + colors = colors, datePicker = datePicker, displayMode = state.initialUIKitDisplayMode, onSelectionChanged = { dateMillis -> diff --git a/calf-ui/src/iosMain/kotlin/com/mohamedrejeb/calf/ui/datepicker/DatePickerManager.kt b/calf-ui/src/iosMain/kotlin/com/mohamedrejeb/calf/ui/datepicker/DatePickerManager.kt index 3d8d931..88fea8e 100644 --- a/calf-ui/src/iosMain/kotlin/com/mohamedrejeb/calf/ui/datepicker/DatePickerManager.kt +++ b/calf-ui/src/iosMain/kotlin/com/mohamedrejeb/calf/ui/datepicker/DatePickerManager.kt @@ -1,6 +1,12 @@ package com.mohamedrejeb.calf.ui.datepicker +import androidx.compose.material3.DatePickerColors +import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.runtime.mutableStateOf +import com.mohamedrejeb.calf.core.InternalCalfApi +import com.mohamedrejeb.calf.ui.utils.applyTheme +import com.mohamedrejeb.calf.ui.utils.isDark +import com.mohamedrejeb.calf.ui.utils.toUIColor import kotlinx.cinterop.BetaInteropApi import kotlinx.cinterop.ExperimentalForeignApi import kotlinx.cinterop.ObjCAction @@ -9,8 +15,14 @@ import platform.Foundation.* import platform.UIKit.* import platform.objc.sel_registerName -@OptIn(ExperimentalForeignApi::class) -class DatePickerManager internal constructor( +@OptIn( + ExperimentalForeignApi::class, + ExperimentalMaterial3Api::class +) +@InternalCalfApi +class DatePickerManager @OptIn(ExperimentalMaterial3Api::class) internal constructor( + initialSelectedDateMillis: Long?, + colors: DatePickerColors, private val datePicker: UIDatePicker, displayMode: UIKitDisplayMode, private val onSelectionChanged: (dateMillis: Long?) -> Unit, @@ -28,7 +40,7 @@ class DatePickerManager internal constructor( @ObjCAction fun dateSelection() { onSelectionChanged( - stripTimeFromDate(datePicker.date).timeIntervalSince1970.toLong() * 1000 + datePicker.date.timeIntervalSince1970.toLong() * 1000 ) } @@ -36,7 +48,10 @@ class DatePickerManager internal constructor( val datePickerHeight = mutableStateOf(0f) init { - datePicker.date = NSDate() + val date = initialSelectedDateMillis + ?.let { NSDate.dateWithTimeIntervalSince1970(it / 1000.0) } + ?: NSDate() + datePicker.setDate(date, animated = false) datePicker.locale = NSLocale.currentLocale datePicker.datePickerMode = UIDatePickerMode.UIDatePickerModeDate datePicker.preferredDatePickerStyle = when(displayMode) {