thellmund / Android-Week-View

Display highly customizable calendar views in your Android app
Apache License 2.0
188 stars 98 forks source link

I am using it inside a composable function. how could I can display my events on the calendar? The documentation should be more informative #295

Open eliomaroun opened 2 years ago

eliomaroun commented 2 years ago
 class CalendarAdapter(private val viewModel: CalenderViewModel) :
  WeekViewSimpleAdapterJsr310<Appointment>() {
  val defaultDateTimeFormatter: DateTimeFormatter =
    DateTimeFormatter.ofLocalizedDateTime(MEDIUM, SHORT)

fun Appointment.toWeekViewEntity(): WeekViewEntity = toWeekViewEntity()

override fun onCreateEntity(item: Appointment): WeekViewEntity = item.toWeekViewEntity()

override fun onEmptyViewClick(time: LocalDateTime) {
    super.onEmptyViewClick(time)
    viewModel.onEvent(CalendarEvents.OnEmptyViewClicked(time))

}

override fun onEventClick(data: Appointment) {
    super.onEventClick(data)
    viewModel.onEvent(CalendarEvents.OnAppointmentClicked(data))
}

override fun onRangeChanged(firstVisibleDate: LocalDate, lastVisibleDate: LocalDate) {
    super.onRangeChanged(firstVisibleDate, lastVisibleDate)
    viewModel.onEvent(CalendarEvents.OnRangeChanged(firstVisibleDate, lastVisibleDate))

}

}

  @Composable
 fun CalXML(viewModel: CalenderViewModel = CalenderViewModel()) {
val weekdayFormatter = DateTimeFormatter.ofPattern("EEE", Locale.getDefault())
val dateFormatter = DateTimeFormatter.ofPattern("MM/dd", Locale.getDefault())

AndroidView(
    factory = { context ->

        val view = WeekView(context)
        val adapter = CalendarAdapter(viewModel)
        view.adapter = adapter
        if (viewModel.state.value.appointmentList.isNotEmpty()) {
            viewModel.state.value.appointmentList.map { adapter.onCreateEntity(it) }
        }
        adapter.submitList(viewModel.state.value.appointmentList)
        view
    },
    update = { view ->

        println("${viewModel.state.value.appointmentList}")
        //view.setBackgroundColor(if (viewModel.state.value.appointmentList.isEmpty()) 0 else 88)
        view.setDateFormatter { date: LocalDate ->
            weekdayFormatter.format(date)
            dateFormatter.format(date)
        }

    }
)

}