0

I'm using paging 3 to paginate my retrieved data. I want to show a badge on the bottom navigation bar when a new item has been added to the recycler view. imagine the user is watching recycler view in the situation below:

item A
item B
item C

now the user refresh the page using a swipe refreshing button which calls this function:

 binding.refresh.setOnRefreshListener {
            adapter.refresh()
    }

and a new item is added, so the situation would be like this:

item D
item A
item B
item C

the new data would be propagated using this line of code:

   viewModel.tickets.observe(viewLifecycleOwner, {
        viewLifecycleOwner.lifecycleScope.launch {
            adapter.submitData(it)
        }
   })

I want to show a badge at this moment. so I added an interface into my recycler paged list adapter like this:

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        ticketUtils.hasNewItem(getItem(0)?.ticket_id)
        holder.bind(getItem(position))
    }

which checks what is the first item of the recycler view. so I have implemented the body of this interface inside of my fragment like this:

    override fun hasNewItem(itemID: String?) {
        if (itemID.isNullOrEmpty())
            return
        if (itemID != firstTicketID)
            MainActivity.mainUtils.newTicket(showBadge = true)
    }

and MainActivity.mainUtils.newTicket is an interface that shows a badge on the desired icon.

but I have faced to the problem that it seems the recycler view won't be notified for new data till the user start scrolling. I mean the code written in onBindedViewHolder would be called just when the user scrolls the page. how can I solve that?

mohammad
  • 187
  • 1
  • 2
  • 15
  • Is it possible to show the badge based on when there is new data in the dataset? – Varsha Kulkarni Feb 16 '21 at 19:42
  • yes, but I don't know how to find out when new data is added to the database or the old ones are modified. the only thing I get is a pagedList type in `viewModel.tickets.observe` @VarshaKulkarni – mohammad Feb 17 '21 at 06:51
  • Will you please explain the use case here. From where exactly this new data is coming in? What event occurs when new data is available? – Varsha Kulkarni Feb 17 '21 at 10:59

1 Answers1

0
viewModel.tickets.observe(viewLifecycleOwner, {
//Here you get to know that there's update in your Datalist
    adapter.submitData(it)
})

When you insert a new data into the database, observe the data inserted by returning the id of the data. For example if you are using Room, it can return a long value, which is the new rowId for the inserted item. Compare with existing ones and then you can show a badge if there's new data in the list.

Varsha Kulkarni
  • 1,389
  • 2
  • 13
  • 17
  • it is not always true, because the paging library loads a chunk of data at a moment. for instance, imagine you have shown 10 items. when the user scrolls down, the paging library will load more data that was already in the database but were not loaded into the recycler view. so in this situation the mentioned function will be called but there is no new data. furthermore, the type of `it` in the mentioned function is `PagedList` and I have no direct access to the items. so I can't make a comparison. – mohammad Feb 17 '21 at 09:04