mirror of
https://github.com/nextcloud/talk-android
synced 2025-06-19 11:39:42 +01:00
avoid NPE in onLoadMore
For v21.0.0, the following crash was reported: Exception java.lang.NullPointerException: null cannot be cast to non-null type com.nextcloud.talk.chat.data.model.ChatMessage at com.nextcloud.talk.chat.ChatActivity.onLoadMore (ChatActivity.kt:3107) at com.stfalcon.chatkit.messages.MessagesListAdapter.onLoadMore (MessagesListAdapter.java:148) at com.stfalcon.chatkit.messages.RecyclerScrollMoreListener.onScrolled (RecyclerScrollMoreListener.java:82) at androidx.recyclerview.widget.RecyclerView.dispatchOnScrolled (RecyclerView.java:5688) at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep3 (RecyclerView.java:4741) at androidx.recyclerview.widget.RecyclerView.dispatchLayout (RecyclerView.java:4367) at androidx.recyclerview.widget.RecyclerView.onLayout (RecyclerView.java:4919) This is now improved: - lastOrNull prevents exceptions if no matching item is found - as? is a safe cast that returns null if the cast fails Whole expression becomes null-safe, and id will be null if anything along the way doesn't match. Only when not null, onLoadMore continues. Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
parent
14b6060500
commit
d0190158d5
@ -2818,21 +2818,23 @@ class ChatActivity :
|
||||
DateFormatter.isSameDay(message1.createdAt, message2.createdAt)
|
||||
|
||||
override fun onLoadMore(page: Int, totalItemsCount: Int) {
|
||||
val id = (
|
||||
adapter?.items?.last {
|
||||
it.item is ChatMessage
|
||||
}?.item as ChatMessage
|
||||
).jsonMessageId
|
||||
val messageId = (
|
||||
adapter?.items
|
||||
?.lastOrNull { it.item is ChatMessage }
|
||||
?.item as? ChatMessage
|
||||
)?.jsonMessageId
|
||||
|
||||
val urlForChatting = ApiUtils.getUrlForChat(chatApiVersion, conversationUser?.baseUrl, roomToken)
|
||||
messageId?.let {
|
||||
val urlForChatting = ApiUtils.getUrlForChat(chatApiVersion, conversationUser?.baseUrl, roomToken)
|
||||
|
||||
chatViewModel.loadMoreMessages(
|
||||
beforeMessageId = id.toLong(),
|
||||
withUrl = urlForChatting,
|
||||
withCredentials = credentials!!,
|
||||
withMessageLimit = MESSAGE_PULL_LIMIT,
|
||||
roomToken = currentConversation!!.token
|
||||
)
|
||||
chatViewModel.loadMoreMessages(
|
||||
beforeMessageId = it.toLong(),
|
||||
withUrl = urlForChatting,
|
||||
withCredentials = credentials!!,
|
||||
withMessageLimit = MESSAGE_PULL_LIMIT,
|
||||
roomToken = currentConversation!!.token
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override fun format(date: Date): String =
|
||||
|
Loading…
Reference in New Issue
Block a user