I'm writing chat app. Messages are load by pages (item keyed - message_id) using Android Paging Library. Also there is "reply" feature, which mean users can reply to any message in chat. In RecyclerView it should looks like that:
Part of message which was replied...
------------------------------------
Main message text
It is simple, when related message is already in local database. I can fetch all data from database and show it in app. But there can be situations when replied message is NOT in local database (for example, this is old message and we just installed app - database is empty). And message looks bad, until replied message won't be stored in local database:
------------------------------------
Main message text
What I've tried: if message's property reply_to is not null I load replied message from remote server by this id and store it in local database. Room react to changes and display all content fine. But this replied message become last in my messages array and BoundaryCallback load messages after that replied message skipping messages between REAL last message and that replied message.
For example, I have messages with ids 1..100.
(boundary callback makes initialLoad, say 20 items)
1 message
2 message
..
10 message with reply to 50 message (load message 50 and store it in db)
11 message
..
(early loaded messages)
..
20 message
(this should be end, but this is not because of 50 message which is stored in local database now)
50 message
(bounary callback makes loadAfter new items by key - message id)
51 message
..
So, we lost part of messages from 21 to 49.
Message room entity has following properties:
@Entity
class Message {
var id: Int
var text: String
var reply_to: Int?
}
MessageModel is returned by Room by relation and used in PagedAdapter
class MessageModel {
var message: Message
var repliedMessage: Message
}
How can I show replied message and don't break paging? How it should be done in right way?