Skip to content

Instantly share code, notes, and snippets.

Modifier.pointerInput(Unit) {
var moveSkipped = false
forEachGesture {
awaitPointerEventScope {
while (true) {
val event = awaitPointerEvent()
val firstChange = event.changes.firstOrNull() ?: continue
val position = firstChange.position
if(event.type == PointerEventType.Release && moveSkipped) {
moveSkipped = false
val context = LocalContext.current
val newContext = remember(locale) {
val res = context.resources
val conf = res.configuration
conf.setLocale(locale)
context.createConfigurationContext(conf)
}
val okText = remember(newContext) {
newContext.resources.getString(android.R.string.ok)
}
//firstDayOfWeek contains when the week starts in the locale.
//For example, UK locale will have Monday as first day of week
//English locale will have Sunday as first day of week.
val firstDayOfWeek = currentMonth.firstDayOfWeek
//weekDayList contains a list of week days with size 7
//that starts with the firstDayOfWeek.
//For example, the UK locale has MONDAY as first day of week.
//The value of Calendar.MONDAY is 2.
//So the list contains [2, 3, 4, 5, 6, 7, 1]
@V-Abhilash-1999
V-Abhilash-1999 / DatePicker.kt
Created October 30, 2023 16:16
Row and Column
Column(
modifier = Modifier
.fillMaxWidth()
) {
(1..6).forEach { _ ->
Row(
modifier = Modifier
.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceEvenly,
verticalAlignment = Alignment.CenterVertically
@V-Abhilash-1999
V-Abhilash-1999 / DatePicker.kt
Created October 30, 2023 16:14
Week Day Text
(0..6).forEach { weekDay ->
val dayIndex = (weekDay + (firstDayOfWeek - 1)) % 7
val currentDay = Calendar.getInstance(locale).apply {
set(2023, 0, 1 + dayIndex)
}
val weekText = remember(weekDay, locale) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val dayOrdinal = if (dayIndex == 0) 7 else dayIndex
DayOfWeek.of(dayOrdinal).getDisplayName(java.time.format.TextStyle.NARROW, locale).toString()
} else {
val year = currentSelectedDate.get(Calendar.YEAR).getFormattedNumber(locale)
val monthText = remember(year, month) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
java.time.YearMonth.of(year, month).month.getDisplayName(java.time.format.TextStyle.FULL, locale)
} else {
Calendar.getInstance(locale).apply {
set(year, month - 1, 1)
}.getDisplayName(Calendar.MONTH, Calendar.LONG, locale) ?: ""
}
}
@V-Abhilash-1999
V-Abhilash-1999 / DatePicker.kt
Created October 30, 2023 16:12
Current Month Index
val startIndex = remember(currentSelectedDate) {
val year = currentSelectedDate.get(Calendar.YEAR)
val month = currentSelectedDate.get(Calendar.MONTH)
((year - startYear) * 12) + month
}
@V-Abhilash-1999
V-Abhilash-1999 / DatePicker.kt
Created October 30, 2023 16:10
Date Picker Header Month Text
val dateFormat = DateFormat.getInstanceForSkeleton("EMMMd", locale)
dateFormat.setContext(DisplayContext.CAPITALIZATION_FOR_STANDALONE)
val monthText = dateFormat.format(currentSelectedDate.time)
@V-Abhilash-1999
V-Abhilash-1999 / DatePicker.kt
Last active October 30, 2023 16:09
Date Picker Header
Column(
modifier = Modifier
.fillMaxWidth()
.background(datePickerColor.datePickerHeaderColor)
.padding(16.dp)
) {
Text(
modifier = Modifier.clickable {
setMode(DatePickerMode.YEAR)
},
Column(
modifier = Modifier
.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
val hsv = remember {
val hsv = floatArrayOf(0f, 0f, 0f)
AndroidColor.colorToHSV(Color.Blue.toArgb(), hsv)
mutableStateOf(