Skip to content

Commit

Permalink
Merge pull request #126 from MohamedRejeb/0.5.x
Browse files Browse the repository at this point in the history
Use initialSelectedDateMillis as the initial date on iOS if it exists.
  • Loading branch information
MohamedRejeb authored Jul 25, 2024
2 parents bc0c3e8 + 6ceabcf commit a3883d7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -29,6 +31,8 @@ actual fun AdaptiveDatePicker(
}
val datePickerManager = remember {
DatePickerManager(
initialSelectedDateMillis = state.selectedDateMillis,
colors = colors,
datePicker = datePicker,
displayMode = state.initialUIKitDisplayMode,
onSelectionChanged = { dateMillis ->
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand All @@ -28,15 +40,18 @@ class DatePickerManager internal constructor(
@ObjCAction
fun dateSelection() {
onSelectionChanged(
stripTimeFromDate(datePicker.date).timeIntervalSince1970.toLong() * 1000
datePicker.date.timeIntervalSince1970.toLong() * 1000
)
}

val datePickerWidth = mutableStateOf(0f)
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) {
Expand Down

0 comments on commit a3883d7

Please sign in to comment.