Merge pull request #7900 from element-hq/feature/bma/settingsMainThreadJank

Fix animation glitches when opening the settings
This commit is contained in:
Benoit Marty
2026-10-13 11:30:36 +02:00
committed by GitHub
3 changed files with 86 additions and 78 deletions
@@ -40,10 +40,7 @@ class PreferencesAccountPresenter(
@Composable
override fun present(): PreferencesAccountState {
val matrixUser = matrixClient.userProfile.collectAsState()
LaunchedEffect(Unit) {
// Force a refresh of the profile
matrixClient.getUserProfile()
}
// Note: no need to force a refresh of the profile here, PreferencesRootPresenter, which embeds this presenter, already does it.
val showLinkNewDevice by remember {
featureFlagService.isFeatureEnabledFlow(FeatureFlags.QrCodeLogin)
}.collectAsState(initial = false)
@@ -530,7 +530,7 @@ class RustMatrixClient(
* Store the user profile in the session storage, including a base64 encoded thumbnail of the avatar,
* so that the avatar can be rendered without having to use this client.
*/
private suspend fun storeUserProfile(matrixUser: MatrixUser) {
private suspend fun storeUserProfile(matrixUser: MatrixUser) = withContext(sessionDispatcher) {
val avatarUrl = matrixUser.avatarUrl
val storedSession = sessionStore.getSession(sessionId.value)
val avatarData = when {
@@ -23,6 +23,7 @@ import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import timber.log.Timber
@SingleIn(AppScope::class)
@@ -51,6 +52,7 @@ class DatabaseSessionStore(
}
override suspend fun addSession(sessionData: SessionData) {
withContext(dispatchers.io) {
sessionDataMutex.withLock {
val lastUsageIndex = getLastUsageIndex()
database.sessionDataQueries.insertSessionData(
@@ -65,8 +67,10 @@ class DatabaseSessionStore(
)
}
}
}
override suspend fun updateData(sessionData: SessionData) {
withContext(dispatchers.io) {
sessionDataMutex.withLock {
val result = database.sessionDataQueries.selectByUserId(sessionData.userId)
.executeAsOneOrNull()
@@ -74,7 +78,7 @@ class DatabaseSessionStore(
if (result == null) {
Timber.e("User ${sessionData.userId} not found in session database")
return
return@withContext
}
// Copy new data from SDK, but keep application data
database.sessionDataQueries.updateSession(
@@ -89,15 +93,17 @@ class DatabaseSessionStore(
)
}
}
}
override suspend fun updateUserProfile(sessionId: String, displayName: String?, avatarUrl: String?, avatarData: String?) {
withContext(dispatchers.io) {
sessionDataMutex.withLock {
val result = database.sessionDataQueries.selectByUserId(sessionId)
.executeAsOneOrNull()
?.toApiModel()
if (result == null) {
Timber.e("User $sessionId not found in session database")
return
return@withContext
}
database.sessionDataQueries.updateSession(
result.copy(
@@ -108,12 +114,14 @@ class DatabaseSessionStore(
)
}
}
}
override suspend fun setLatestSession(sessionId: String) {
withContext(dispatchers.io) {
val latestSession = getLatestSession()
if (latestSession?.userId == sessionId) {
// Already the latest session
return
return@withContext
}
val lastUsageIndex = latestSession?.lastUsageIndex ?: 0
val result = database.sessionDataQueries.selectByUserId(sessionId)
@@ -121,7 +129,7 @@ class DatabaseSessionStore(
?.toApiModel()
if (result == null) {
Timber.e("User $sessionId not found in session database")
return
return@withContext
}
sessionDataMutex.withLock {
// Update lastUsageIndex of the session
@@ -132,6 +140,7 @@ class DatabaseSessionStore(
)
}
}
}
private fun getLastUsageIndex(): Long {
return database.sessionDataQueries.selectLatest()
@@ -140,32 +149,32 @@ class DatabaseSessionStore(
?: -1L
}
override suspend fun getLatestSession(): SessionData? {
return sessionDataMutex.withLock {
override suspend fun getLatestSession(): SessionData? = withContext(dispatchers.io) {
sessionDataMutex.withLock {
database.sessionDataQueries.selectLatest()
.executeAsOneOrNull()
?.toApiModel()
}
}
override suspend fun getSession(sessionId: String): SessionData? {
return sessionDataMutex.withLock {
override suspend fun getSession(sessionId: String): SessionData? = withContext(dispatchers.io) {
sessionDataMutex.withLock {
database.sessionDataQueries.selectByUserId(sessionId)
.executeAsOneOrNull()
?.toApiModel()
}
}
override suspend fun getAllSessions(): List<SessionData> {
return sessionDataMutex.withLock {
override suspend fun getAllSessions(): List<SessionData> = withContext(dispatchers.io) {
sessionDataMutex.withLock {
database.sessionDataQueries.selectAll()
.executeAsList()
.map { it.toApiModel() }
}
}
override suspend fun numberOfSessions(): Int {
return sessionDataMutex.withLock {
override suspend fun numberOfSessions(): Int = withContext(dispatchers.io) {
sessionDataMutex.withLock {
database.sessionDataQueries.count()
.executeAsOneOrNull()
?.toInt()
@@ -181,8 +190,10 @@ class DatabaseSessionStore(
}
override suspend fun removeSession(sessionId: String) {
withContext(dispatchers.io) {
sessionDataMutex.withLock {
database.sessionDataQueries.removeSession(sessionId)
}
}
}
}