commit ebc5469fd0a42cd49dadc0ca2621bf58f1ed84c9
parent a1dc03c9584de9fb8ac2751338ad96c3392f8ebc
Author: rhunk <101876869+rhunk@users.noreply.github.com>
Date:   Fri,  3 Jan 2025 01:38:29 +0100

perf(app): logger history cache

Signed-off-by: rhunk <101876869+rhunk@users.noreply.github.com>

Diffstat:
Mapp/src/main/kotlin/me/rhunk/snapenhance/ui/manager/pages/LoggerHistoryRoot.kt | 16++++++++++++----
Mcommon/src/main/kotlin/me/rhunk/snapenhance/common/bridge/wrapper/LoggerWrapper.kt | 17++++++++---------
2 files changed, 20 insertions(+), 13 deletions(-)

diff --git a/app/src/main/kotlin/me/rhunk/snapenhance/ui/manager/pages/LoggerHistoryRoot.kt b/app/src/main/kotlin/me/rhunk/snapenhance/ui/manager/pages/LoggerHistoryRoot.kt @@ -15,7 +15,6 @@ import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.focus.FocusRequester import androidx.compose.ui.focus.focusRequester -import androidx.compose.ui.graphics.Color import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.text.font.FontStyle import androidx.compose.ui.text.font.FontWeight @@ -48,6 +47,7 @@ import me.rhunk.snapenhance.download.DownloadProcessor import me.rhunk.snapenhance.storage.findFriend import me.rhunk.snapenhance.ui.manager.Routes import java.text.DateFormat +import java.util.concurrent.ConcurrentHashMap import kotlin.math.absoluteValue @@ -219,6 +219,8 @@ class LoggerHistoryRoot : Routes.Route() { loggerWrapper = LoggerWrapper(context.androidContext) } + val conversationInfoCache = remember { ConcurrentHashMap<String, String?>() } + Column { var expanded by remember { mutableStateOf(false) } @@ -241,11 +243,15 @@ class LoggerHistoryRoot : Routes.Route() { } val selectedConversationInfo by rememberAsyncMutableState(defaultValue = null, keys = arrayOf(selectedConversation)) { - selectedConversation?.let { loggerWrapper.getConversationInfo(it) } + selectedConversation?.let { + conversationInfoCache.getOrPut(it) { + formatConversationInfo(loggerWrapper.getConversationInfo(it)) + } + } } OutlinedTextField( - value = remember(selectedConversationInfo) { formatConversationInfo(selectedConversationInfo) ?: "Select a conversation" }, + value = selectedConversationInfo ?: "Select a conversation", onValueChange = {}, readOnly = true, modifier = Modifier @@ -264,7 +270,9 @@ class LoggerHistoryRoot : Routes.Route() { expanded = false }, text = { val conversationInfo by rememberAsyncMutableState(defaultValue = null, keys = arrayOf(conversationId)) { - formatConversationInfo(loggerWrapper.getConversationInfo(conversationId)) + conversationInfoCache.getOrPut(conversationId) { + formatConversationInfo(loggerWrapper.getConversationInfo(conversationId)) + } } Text( diff --git a/common/src/main/kotlin/me/rhunk/snapenhance/common/bridge/wrapper/LoggerWrapper.kt b/common/src/main/kotlin/me/rhunk/snapenhance/common/bridge/wrapper/LoggerWrapper.kt @@ -399,14 +399,6 @@ class LoggerWrapper( } fun getConversationInfo(conversationId: String): ConversationInfo? { - val participantSize = database.rawQuery("SELECT COUNT(DISTINCT user_id) FROM messages WHERE conversation_id = ?", arrayOf(conversationId)).use { - if (!it.moveToFirst()) return null - it.getInt(0) - } - val groupTitle = if (participantSize > 2) database.rawQuery("SELECT group_title FROM messages WHERE conversation_id = ? AND group_title IS NOT NULL LIMIT 1", arrayOf(conversationId)).use { - if (!it.moveToFirst()) return@use null - it.getStringOrNull("group_title") - } else null val usernames = database.rawQuery("SELECT DISTINCT username FROM messages WHERE conversation_id = ?", arrayOf(conversationId)).use { val usernames = mutableListOf<String>() while (it.moveToNext()) { @@ -415,7 +407,14 @@ class LoggerWrapper( usernames } - return ConversationInfo(conversationId, participantSize, groupTitle, usernames) + if (usernames.size > 2) { usernames.remove("myai") } + + val groupTitle = if (usernames.size > 2) database.rawQuery("SELECT group_title FROM messages WHERE conversation_id = ? AND group_title IS NOT NULL LIMIT 1", arrayOf(conversationId)).use { + if (!it.moveToFirst()) return@use null + it.getStringOrNull("group_title") + } else null + + return ConversationInfo(conversationId, usernames.size, groupTitle, usernames) } override fun getChatEdits(conversationId: String, messageId: Long): List<LoggedChatEdit> {