Android jetpack 上的分页组合 LazyColumn
我正在使用 jetpack compose 开发 Android 聊天应用程序。
聊天消息来自 WebSocket。 (我使用 OkHttp 库)
并且所有消息都应存储在本地数据库中(我使用 Room)
当用户滚动到顶部时,应用程序会尝试从本地数据库获取消息。 如果本地数据库没有数据,它会尝试从远程 API 服务器获取消息。
而这个逻辑是通过Repository模式来实现的。
class ChatMessageRepository (
private val localMessageDao: LocalMessageDao,
private val remoteApiServer: RemoteApiServer
) {
fun getMessages(offset: Int) {
val cachedMessages = localMessageDao.get(offset)
if (cachedMessages.isNotEmpty()) {
return cachedMessages
}
return remoteApiServer.getMessages(offset)
.also {
localMessageDao.insert(it)
}
}
}
我可以将其与 PagingSource 集成。
class ChatPagingSource(
private val repo: ChatMessageRepository
): PagingSource<Int, Chat>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Chat> {
val offset = params.key ?: 1
val messages = repo.getMessages(offset)
return LoadResult.Page(
data = messages ,
prevKey = if (offset == 1) null else offset - messages .size,
nextKey = null
)
}
}
但问题是我不知道如何将WebSocket数据流与PagingSource
集成。
class WebSocketService: WebSocketListener() {
val messageSubject: PublishSubject<WsMsg> = PublishSubject.create()
override fun onMessage(webSocket: WebSocket, text: String) {
super.onMessage(webSocket, text)
messageSubject.onNext(friendStatus)
}
}
我正在使用 OkHttp 库回调方法接收聊天消息。
我该怎么做?
我是否应该放弃使用 Paging 库,而只使用 LazyColumn 滚动检测进行开发?
I am developing an Android Chat App using jetpack compose.
The chat messages are coming from WebSocket. (I use OkHttp library)
And all of the messages should be stored in the local database (I use Room)
When a user scrolls to the top, the app tries to get the messages from the local database.
If the local database has no data, it tries to get the messages from the Remote API server.
And this logic is implemented by the Repository pattern.
class ChatMessageRepository (
private val localMessageDao: LocalMessageDao,
private val remoteApiServer: RemoteApiServer
) {
fun getMessages(offset: Int) {
val cachedMessages = localMessageDao.get(offset)
if (cachedMessages.isNotEmpty()) {
return cachedMessages
}
return remoteApiServer.getMessages(offset)
.also {
localMessageDao.insert(it)
}
}
}
And I can integrate this with the PagingSource
.
class ChatPagingSource(
private val repo: ChatMessageRepository
): PagingSource<Int, Chat>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Chat> {
val offset = params.key ?: 1
val messages = repo.getMessages(offset)
return LoadResult.Page(
data = messages ,
prevKey = if (offset == 1) null else offset - messages .size,
nextKey = null
)
}
}
But the problem is that I don't know how can I integrate the WebSocket data stream with PagingSource
.
class WebSocketService: WebSocketListener() {
val messageSubject: PublishSubject<WsMsg> = PublishSubject.create()
override fun onMessage(webSocket: WebSocket, text: String) {
super.onMessage(webSocket, text)
messageSubject.onNext(friendStatus)
}
}
I am receiving the chat message using the OkHttp library callback method.
How can I do this?
Should I give up to use Paging library, and just develop using LazyColumn scroll detect?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论