Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix latestMessageIndex out of bounds error #75

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import androidx.hilt.navigation.compose.hiltViewModel
import dev.chungjungsoo.gptmobile.R
import dev.chungjungsoo.gptmobile.data.database.entity.Message
import dev.chungjungsoo.gptmobile.data.model.ApiType
import dev.chungjungsoo.gptmobile.util.DefaultHashMap
import dev.chungjungsoo.gptmobile.util.collectManagedState
import dev.chungjungsoo.gptmobile.util.multiScrollStateSaver
import kotlinx.coroutines.delay
Expand Down Expand Up @@ -104,21 +105,10 @@ fun ChatScreen(
val canUseChat = (chatViewModel.enabledPlatformsInChat.toSet() - appEnabledPlatforms.toSet()).isEmpty()
val groupedMessages = remember(messages) { groupMessages(messages) }
val latestMessageIndex = groupedMessages.keys.maxOrNull() ?: 0
val chatBubbleScrollStates = rememberSaveable(saver = multiScrollStateSaver) { MutableList(latestMessageIndex + 2) { ScrollState(0) } }
val chatBubbleScrollStates = rememberSaveable(saver = multiScrollStateSaver) { DefaultHashMap<Int, ScrollState>({ ScrollState(0) }) }

val scope = rememberCoroutineScope()

LaunchedEffect(latestMessageIndex) {
val opponentBubbles = ((latestMessageIndex + 1) / 2) + 1
val scrollStatesToAdd = opponentBubbles - chatBubbleScrollStates.size

if (scrollStatesToAdd > 0) {
repeat(scrollStatesToAdd) {
chatBubbleScrollStates.add(ScrollState(0))
}
}
}

LaunchedEffect(isIdle) {
listState.animateScrollToItem(groupedMessages.keys.size)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dev.chungjungsoo.gptmobile.util

/**
Small implementation of HashMap, but with default values.
This way the get operator will not throw an error or null.
Inspired by Python collections DefaultDict.
*/
open class DefaultHashMap<K, V>(protected val defaultValueProvider: () -> V) : HashMap<K, V>() {
override operator fun get(key: K): V {
if (key in this) {
return super.get(key)!!
}

val defaultValue = defaultValueProvider()
this[key] = defaultValue
return super.get(key)!!
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ package dev.chungjungsoo.gptmobile.util
import androidx.compose.foundation.ScrollState
import androidx.compose.runtime.saveable.Saver

val multiScrollStateSaver: Saver<MutableList<ScrollState>, *> = Saver(
save = { it.map { scrollState -> scrollState.value } },
restore = { it.map { i -> ScrollState(i) }.toMutableList() }
val multiScrollStateSaver: Saver<DefaultHashMap<Int, ScrollState>, *> = Saver(
save = {
val saver = hashMapOf<Int, Int>()
it.forEach { i, scrollState -> saver[i] = scrollState.value }
saver
},
restore = {
val restored = DefaultHashMap<Int, ScrollState>({ ScrollState(0) })
it.forEach { i, v -> restored[i] = ScrollState(v) }
restored
}
)