Now I have this code:
class MyFragment : DaggerFragment() {
...
private fun setTimePickerDialog() {
binding.timeButton.setOnClickListener{
val calendar = viewModel.calendar
val curHourOfDay = calendar.get(Calendar.HOUR_OF_DAY)
val curMinute = calendar.get(Calendar.MINUTE)
val dialog = TimePickerDialog(context, { _, hourOfDay, minute ->
val c = Calendar.getInstance()
c.set(1970, 0, 1, hourOfDay, minute)
viewModel.time.value = SimpleDateFormat("HH:mm:ss").format(c.time)
}, curHourOfDay, curMinute, true)
dialog.show()
}
}
...
}
I want to take advantage of the DataBinding library and not write setOnClickListener in my fragment. But I can't move this code to the ViewModel, because a context is required. What is a clean way to show DatePickerDialog by clicking on the button using DataBinding and MVVM?