-
Notifications
You must be signed in to change notification settings - Fork 0
/
PickerExt.kt
47 lines (41 loc) · 1.6 KB
/
PickerExt.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package voiedev
import android.app.DatePickerDialog
import android.app.DatePickerDialog.OnDateSetListener
import android.app.TimePickerDialog
import android.content.Context
import org.threeten.bp.LocalDate
import org.threeten.bp.LocalDateTime
import org.threeten.bp.LocalTime
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
/***
* LocalDateTime month is 1-based; DatePicker month is 0-based
*/
suspend fun Context.pickDate(init: LocalDate): LocalDate =
suspendCoroutine { cont ->
DatePickerDialog(
this,
OnDateSetListener { datePicker, year, month, day ->
// Pass dialog result to continuation
cont.resume(LocalDate.of(year, month + 1, day))
},
init.year, init.monthValue - 1, init.dayOfMonth
).show()
}
suspend fun Context.pickTime(init: LocalTime, is24Hour: Boolean = true): LocalTime =
suspendCoroutine { cont ->
TimePickerDialog(
this,
TimePickerDialog.OnTimeSetListener { timePicker, hour, minute ->
// Pass dialog result to continuation
cont.resume(LocalTime.of(hour, minute))
},
init.hour, init.minute, is24Hour
).show()
}
suspend fun Context.pickDate(init: LocalDateTime): LocalDateTime =
init.with(pickDate(init.toLocalDate()))
suspend fun Context.pickTime(init: LocalDateTime, is24Hour: Boolean = true): LocalDateTime =
init.with(pickTime(init.toLocalTime(), is24Hour))
suspend fun Context.pickDateAndTime(init: LocalDateTime, is24Hour: Boolean = true) =
pickTime(pickDate(init))