thellmund / Android-Week-View

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

Refresh and add events is rendered with as old events would exist #275

Closed thecodeside closed 2 years ago

thecodeside commented 2 years ago

Describe the bug When you refresh the adapter and load events it is rendered as old events would exist. Screenshot before refresh and after refresh and resubmitted events attached.

To Reproduce Steps to reproduce the behavior:

  1. Submit Events
  2. Call refresh on the adapter
  3. loadMore() is called
  4. Events are submitted but rendered as old events would exist

Expected behavior Events rendered as it would be clear calendar instance.

Screenshots Before refresh: 1_before_refresh After refresh and added events resubmited: 2_after_refresh

Additional context Latest sample app on the master branch.

thellmund commented 2 years ago

Hi there 👋 Where are you calling refresh() in the sample app?

thecodeside commented 2 years ago

I just added listener on click in the BasicActivity to test it. I can push changes if you want, but this is a very simple code.

    private lateinit var adapter: BasicActivityWeekViewAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
      (...)
        adapter = BasicActivityWeekViewAdapter(
            dragHandler = viewModel::handleDrag,
            loadMoreHandler = viewModel::fetchEvents,
            onEmptyViewClick = {
                adapter.refresh()
            }
        )

private class BasicActivityWeekViewAdapter(
    private val dragHandler: (Long, LocalDateTime, LocalDateTime) -> Unit,
    private val loadMoreHandler: (List<YearMonth>) -> Unit,
    private val onEmptyViewClick: () -> Unit,
) : WeekViewPagingAdapterJsr310<CalendarEntity>() {  

    override fun onEmptyViewLongClick(time: LocalDateTime) {
        onEmptyViewClick()
        context.showToast("Empty view long-clicked at ${defaultDateTimeFormatter.format(time)}")
    }
thellmund commented 2 years ago

I see… the issue here is that viewModel.fetchEvents() never clears the already loaded events in the view state, therefore calling refresh() (which then calls loadMore()) will result in duplicate events. Can you try to update the view model’s viewState before calling adapter.refresh()?

thecodeside commented 2 years ago

You have 100% right! That fixes the bug! Thank you!