Android jetpack 上的分页组合 LazyColumn

发布于 2025-01-11 14:56:52 字数 1810 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文