Skip to content

Commit

Permalink
Merge pull request #128 from MohamedRejeb/0.5.x
Browse files Browse the repository at this point in the history
AdaptiveDatePicker: use DatePickerColors on iOS
  • Loading branch information
MohamedRejeb authored Jul 25, 2024
2 parents c13614a + 714f98c commit 868d63b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.compose.material3.DatePickerColors
import androidx.compose.material3.DatePickerFormatter
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.interop.UIKitView
Expand All @@ -29,6 +30,7 @@ actual fun AdaptiveDatePicker(
val datePicker = remember {
UIDatePicker()
}

val datePickerManager = remember {
DatePickerManager(
initialSelectedDateMillis = state.selectedDateMillis,
Expand All @@ -41,10 +43,15 @@ actual fun AdaptiveDatePicker(
)
}

LaunchedEffect(colors) {
datePickerManager.applyColors(colors)
}

UIKitView(
factory = {
datePicker
},
background = colors.containerColor,
modifier = modifier
.then(
if (datePickerManager.datePickerWidth.value > 0f)
Expand All @@ -59,4 +66,4 @@ actual fun AdaptiveDatePicker(
Modifier
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,17 @@ class DatePickerManager @OptIn(ExperimentalMaterial3Api::class) internal constru
datePickerWidth.value = this.size.width.toFloat()
datePickerHeight.value = this.size.height.toFloat()
}
applyColors(colors)
}

private fun stripTimeFromDate(originalDate: NSDate): NSDate {
val components = NSCalendar.currentCalendar.components(
NSCalendarUnitYear or NSCalendarUnitMonth or NSCalendarUnitDay,
originalDate
)
return NSCalendar.currentCalendar.dateFromComponents(components) ?: originalDate
fun applyColors(colors: DatePickerColors) {
applyTheme(isDark = !isDark(colors.dayContentColor))
datePicker.tintColor = colors.selectedDayContentColor.toUIColor()
datePicker.backgroundColor = colors.containerColor.toUIColor()
}

fun applyTheme(isDark: Boolean) {
datePicker.applyTheme(isDark)
}

}

0 comments on commit 868d63b

Please sign in to comment.