Date Preference
![]() |
This shows a simple date picker preference.
Check out the composable and it's documentation in the code snipplet below.
Example#
val now = LocalDate.now()
val date1 = dataStore.getLong("date1", now.toEpochDay())
.collectAsState(initial = now.toEpochDay())
PreferenceDate(
value = date1.value.let { LocalDate.ofEpochDay(it).toKotlinLocalDate() },
onValueChange = {
scope.launch(Dispatchers.IO) {
dataStore.update("date1", it.toJavaLocalDate().toEpochDay())
}
},
title = "Date 1",
subtitle = "First day of week: Monday",
icon = { Icon(Icons.Default.DateRange, null) }
)
Composable#
/**
* A date preference item - this item provides a date dialog to change this preference
*
*
*
* **Basic Parameters:** all params not described here are derived from [com.michaelflisar.composepreferences.core.composables.BasePreference], check it out for more details
*
* @param value the [MutableState] of this item
* @param firstDayOfWeek the first day of the week for the underlying date dialog
* @param formatter the formatter for the selected date
*/
@Composable
fun PreferenceScope.PreferenceDate(
// Special
value: MutableState<LocalDate>,
firstDayOfWeek: DayOfWeek = DayOfWeek.MONDAY,
formatter: (date: LocalDate) -> String = {
// comes from the ComposeDialog library
defaultFormatterSelectedDate(it)
},
// Base Preference
title: String,
enabled: Dependency = Dependency.Enabled,
visible: Dependency = Dependency.Enabled,
subtitle: String? = null,
icon: (@Composable () -> Unit)? = null,
itemStyle: PreferenceItemStyle = LocalPreferenceSettings.current.style.defaultItemStyle,
itemSetup: PreferenceItemSetup = PreferenceDateDefaults.itemSetup(),
titleRenderer: @Composable (text: AnnotatedString) -> Unit = { Text(it) },
subtitleRenderer: @Composable (text: AnnotatedString) -> Unit = { Text(it) },
filterTags: List<String> = emptyList(),
// Dialog
dialog: @Composable (state: DialogState) -> Unit = { dialogState ->
PreferenceDateDefaults.dialog(dialogState, value.value, { value.value = it }, firstDayOfWeek, formatter, title, icon)
}
)
/**
* A date preference item - this item provides a date dialog to change this preference
*
*
*
* **Basic Parameters:** all params not described here are derived from [com.michaelflisar.composepreferences.core.composables.BasePreference], check it out for more details
*
* @param value the value of this item
* @param onDateChange the value changed callback of this item
* @param firstDayOfWeek the first day of the week for the underlying date dialog
* @param formatter the formatter for the selected date
*/
@Composable
fun PreferenceScope.PreferenceDate(
// Special
value: LocalDate,
onValueChange: (date: LocalDate) -> Unit,
firstDayOfWeek: DayOfWeek = DayOfWeek.MONDAY,
formatter: (date: LocalDate) -> String = {
// comes from the ComposeDialog library
defaultFormatterSelectedDate(it)
},
// Base Preference
title: String,
enabled: Dependency = Dependency.Enabled,
visible: Dependency = Dependency.Enabled,
subtitle: String? = null,
icon: (@Composable () -> Unit)? = null,
itemStyle: PreferenceItemStyle = LocalPreferenceSettings.current.style.defaultItemStyle,
itemSetup: PreferenceItemSetup = PreferenceDateDefaults.itemSetup(),
titleRenderer: @Composable (text: AnnotatedString) -> Unit = { Text(it) },
subtitleRenderer: @Composable (text: AnnotatedString) -> Unit = { Text(it) },
filterTags: List<String> = emptyList(),
// Dialog
dialog: @Composable (state: DialogState) -> Unit = { state ->
PreferenceDateDefaults.dialog(state, value, onValueChange, firstDayOfWeek, formatter, title, icon)
}
)
Screenshots#
![]() | ![]() |
![]() |