I am trying to configure the Android Paging library in my project to load a paginated list of messages into a RecyclerView. Since my API uses offset and max, I'm using a PositionalDataSource.
Here is my DataSource implementation, where DataStore is using RetroFit to load the messages, and I can see in the console that messages are being loaded properly, and converted to instances of MessageListItem:
class MessageDataSource: PositionalDataSource<MessageListItem>() {
override fun loadRange(params: LoadRangeParams, callback: LoadRangeCallback<MessageListItem>) {
DataStore.shared.loadMessages(params.startPosition, params.loadSize) { result, error ->
if(result != null) {
callback.onResult(result.items)
} else {
callback.onError(MessageDataSourceException(error))
}
}
}
override fun loadInitial(
params: LoadInitialParams,
callback: LoadInitialCallback<MessageListItem>
) {
DataStore.shared.loadMessages(params.requestedStartPosition, params.requestedLoadSize) { response, error ->
if(response != null) {
callback.onResult(response.items, response.offset, response.total)
} else {
callback.onError(MessageDataSourceException(error))
}
}
}
}
class MessageDataSourceException(rootCause: Throwable? = null): Exception(rootCause)
Here is my DataSourceFactory implementation:
class MessageDataSourceFactory: DataSource.Factory<Int, MessageListItem>() {
val messageLiveDataSource = MutableLiveData<MessageDataSource>()
private lateinit var messageDataSource: MessageDataSource
override fun create(): DataSource<Int, MessageListItem> {
messageDataSource = MessageDataSource()
messageLiveDataSource.postValue(messageDataSource)
return messageDataSource
}
}
Here is my MessageListAdapter implementation:
object MessageListItemDiff: DiffUtil.ItemCallback<MessageListItem>() {
override fun areItemsTheSame(oldItem: MessageListItem, newItem: MessageListItem): Boolean {
return oldItem.id == newItem.id
}
override fun areContentsTheSame(oldItem: MessageListItem, newItem: MessageListItem): Boolean {
return oldItem == newItem
}
}
class MessageListAdapter(private val clickListener: View.OnClickListener):
PagedListAdapter<MessageListItem, MessageListAdapter.MessageHolder>(MessageListItemDiff) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MessageHolder {
val inflatedView = LayoutInflater.from(parent.context).inflate(R.layout.item_message, parent, false)
return MessageHolder(inflatedView, clickListener)
}
override fun onBindViewHolder(holder: MessageHolder, position: Int) {
holder.bind(getItem(position)!!)
}
class MessageHolder(itemView: View, private val clickListener: View.OnClickListener) : RecyclerView.ViewHolder(itemView) {
val unreadIndicator = itemView.findViewById<ImageView>(R.id.unreadIndicator)
val title = itemView.findViewById<TextView>(R.id.title)
val dateSent = itemView.findViewById<TextView>(R.id.dateSent)
val cardView = itemView.findViewById<CardView>(R.id.card_view)
fun bind(message: MessageListItem) {
cardView.tag = message
cardView.setOnClickListener(clickListener)
title.text = message.title
dateSent.text = TimeAgo.using(message.dateSent.time)
if(message.isRead) {
unreadIndicator.setImageResource(0)
} else {
unreadIndicator.setImageResource(R.drawable.ic_unread)
}
}
}
}
And finally my ViewModel:
class MessageListViewModel: ViewModel() {
val messagePagedList: LiveData<PagedList<MessageListItem>>
val liveDataSource: LiveData<MessageDataSource>
init {
val messageDataSourceFactory = MessageDataSourceFactory()
liveDataSource = messageDataSourceFactory.messageLiveDataSource
val pagedListConfig = PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setPageSize(30)
.setPrefetchDistance(90)
.build()
messagePagedList = LivePagedListBuilder(messageDataSourceFactory, pagedListConfig).build()
}
}
And here is the onViewCreated implementation in the fragment that is supposed to display the recycler view called messageList:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
messageList.layoutManager = LinearLayoutManager(context!!)
messageList.setHasFixedSize(true)
messageListViewModel = ViewModelProvider(this).get(MessageListViewModel::class.java)
messageListAdapter = MessageListAdapter(this)
messageListViewModel.messagePagedList.observe(this, Observer { messages ->
messageListAdapter.submitList(messages)
})
messageList.adapter = messageListAdapter
}
The problem is that I can see that data is being loaded from the server, but it never reaches the recycler view. If I add a breakpoint on the observer line (messageListAdapter.submitList(messages)
), I get a call once with an empty message list, and that's it.
I have to admit I'm really confused with all these classes and what they are supposed to do, this is my first Paging implementation in Android, and I had to adapt code I found here and there because I didn't want to use a Room database, RxJava or a PageKeyedDataSource, which most samples out there do.
Any idea what might be going on?