From dcec7bd786f6e9ff9fdcec32aae66a3dfa0bd05a Mon Sep 17 00:00:00 2001 From: Marcel Hibbe Date: Wed, 13 Nov 2024 15:20:39 +0100 Subject: [PATCH] fix crash when getting previousChatMessage when adapter has no items adapter!!.items[1].item will crash with IndexOutOfBoundsException This could happen e.g. when chat history is cleared. error was: Exception java.lang.IndexOutOfBoundsException: Index 1 out of bounds for length 0 at jdk.internal.util.Preconditions.outOfBounds (Preconditions.java:64) at jdk.internal.util.Preconditions.outOfBoundsCheckIndex (Preconditions.java:70) at jdk.internal.util.Preconditions.checkIndex (Preconditions.java:266) at java.util.Objects.checkIndex (Objects.java:359) at java.util.ArrayList.get (ArrayList.java:434) at com.nextcloud.talk.chat.ChatActivity.processMessagesFromTheFuture (ChatActivity.kt:2666) at com.nextcloud.talk.chat.ChatActivity.access$processMessagesFromTheFuture (ChatActivity.kt:209) at com.nextcloud.talk.chat.ChatActivity$initObservers$12$1.invokeSuspend (ChatActivity.kt:875) at com.nextcloud.talk.chat.ChatActivity$initObservers$12$1.invoke (Unknown Source:8) at com.nextcloud.talk.chat.ChatActivity$initObservers$12$1.invoke (Unknown Source:4) at kotlinx.coroutines.flow.FlowKt__TransformKt$onEach$$inlined$unsafeTransform$1$2.emit (Emitters.kt:219) at kotlinx.coroutines.flow.FlowKt__ErrorsKt$catchImpl$2.emit (Errors.kt:154) at kotlinx.coroutines.flow.FlowKt__TransformKt$onEach$$inlined$unsafeTransform$1$2.emit (Emitters.kt:220) at kotlinx.coroutines.flow.SharedFlowImpl.collect$suspendImpl (SharedFlow.kt:392) at kotlinx.coroutines.flow.SharedFlowImpl$collect$1.invokeSuspend (Unknown Source:15) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith (ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTask.run (DispatchedTask.kt:104) at android.os.Handler.handleCallback (Handler.java:942) at android.os.Handler.dispatchMessage (Handler.java:99) at android.os.Looper.loopOnce (Looper.java:201) at android.os.Looper.loop (Looper.java:288) at android.app.ActivityThread.main (ActivityThread.java:7932) at java.lang.reflect.Method.invoke at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:942) Signed-off-by: Marcel Hibbe --- app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt b/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt index 31042b6500..fdf5eb6e9c 100644 --- a/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt @@ -2663,8 +2663,8 @@ class ChatActivity : chatMessage.activeUser = conversationUser adapter?.let { - val previousChatMessage = adapter!!.items[1].item - if (previousChatMessage is ChatMessage) { + val previousChatMessage = it.items?.getOrNull(1)?.item + if (previousChatMessage != null && previousChatMessage is ChatMessage) { chatMessage.isGrouped = groupMessages(chatMessage, previousChatMessage) } chatMessage.isOneToOneConversation =