From 9c72310cb4fdf70bed370c722cc174ad0b719fbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Mart=C3=ADn?= Date: Fri, 28 Nov 2025 07:58:06 +0100 Subject: [PATCH] Add developer option to optimize the SDK DBs --- .../impl/developer/DeveloperSettingsEvents.kt | 1 + .../developer/DeveloperSettingsPresenter.kt | 5 ++++ .../impl/developer/DeveloperSettingsView.kt | 8 ++++++ .../impl/tasks/VacuumStoresUseCase.kt | 27 +++++++++++++++++++ .../libraries/matrix/api/MatrixClient.kt | 2 ++ .../libraries/matrix/impl/RustMatrixClient.kt | 6 +++++ 6 files changed, 49 insertions(+) create mode 100644 features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/tasks/VacuumStoresUseCase.kt diff --git a/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/developer/DeveloperSettingsEvents.kt b/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/developer/DeveloperSettingsEvents.kt index 3bf4f375d5..1804d7e070 100644 --- a/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/developer/DeveloperSettingsEvents.kt +++ b/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/developer/DeveloperSettingsEvents.kt @@ -21,4 +21,5 @@ sealed interface DeveloperSettingsEvents { data class SetShowColorPicker(val show: Boolean) : DeveloperSettingsEvents data class ChangeBrandColor(val color: Color?) : DeveloperSettingsEvents data object ClearCache : DeveloperSettingsEvents + data object VacuumStores : DeveloperSettingsEvents } diff --git a/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/developer/DeveloperSettingsPresenter.kt b/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/developer/DeveloperSettingsPresenter.kt index 52e522dc89..db35a1dc83 100644 --- a/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/developer/DeveloperSettingsPresenter.kt +++ b/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/developer/DeveloperSettingsPresenter.kt @@ -29,6 +29,7 @@ import io.element.android.features.preferences.impl.developer.tracing.toLogLevel import io.element.android.features.preferences.impl.model.EnabledFeature import io.element.android.features.preferences.impl.tasks.ClearCacheUseCase import io.element.android.features.preferences.impl.tasks.ComputeCacheSizeUseCase +import io.element.android.features.preferences.impl.tasks.VacuumStoresUseCase import io.element.android.features.rageshake.api.preferences.RageshakePreferencesState import io.element.android.libraries.architecture.AsyncAction import io.element.android.libraries.architecture.AsyncData @@ -61,6 +62,7 @@ class DeveloperSettingsPresenter( private val appPreferencesStore: AppPreferencesStore, private val buildMeta: BuildMeta, private val enterpriseService: EnterpriseService, + private val vacuumStoresUseCase: VacuumStoresUseCase, ) : Presenter { @Composable override fun present(): DeveloperSettingsState { @@ -151,6 +153,9 @@ class DeveloperSettingsPresenter( is DeveloperSettingsEvents.SetShowColorPicker -> { showColorPicker = event.show } + DeveloperSettingsEvents.VacuumStores -> coroutineScope.launch { + vacuumStoresUseCase() + } } } diff --git a/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/developer/DeveloperSettingsView.kt b/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/developer/DeveloperSettingsView.kt index 6d34e97f63..2bfb2f086a 100644 --- a/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/developer/DeveloperSettingsView.kt +++ b/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/developer/DeveloperSettingsView.kt @@ -146,6 +146,14 @@ fun DeveloperSettingsView( } val cache = state.cacheSize PreferenceCategory(title = "Cache") { + ListItem( + headlineContent = { + Text("Vacuum stores") + }, + onClick = { + state.eventSink(DeveloperSettingsEvents.VacuumStores) + } + ) ListItem( headlineContent = { Text("Clear cache") diff --git a/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/tasks/VacuumStoresUseCase.kt b/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/tasks/VacuumStoresUseCase.kt new file mode 100644 index 0000000000..0e6068cb4d --- /dev/null +++ b/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/tasks/VacuumStoresUseCase.kt @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Element Creations Ltd. + * + * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial. + * Please see LICENSE files in the repository root for full details. + */ + +package io.element.android.features.preferences.impl.tasks + +import dev.zacsweers.metro.AppScope +import dev.zacsweers.metro.ContributesBinding +import io.element.android.libraries.matrix.api.MatrixClient +import timber.log.Timber + +interface VacuumStoresUseCase { + suspend operator fun invoke() +} + +@ContributesBinding(AppScope::class) +class DefaultVacuumStoresUseCase( + private val matrixClient: MatrixClient, +) : VacuumStoresUseCase { + override suspend fun invoke() { + matrixClient.vacuumStores() + .onFailure { Timber.e(it, "Failed to vacuum stores") } + } +} diff --git a/libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/MatrixClient.kt b/libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/MatrixClient.kt index 1718f810df..566434b6be 100644 --- a/libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/MatrixClient.kt +++ b/libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/MatrixClient.kt @@ -194,6 +194,8 @@ interface MatrixClient { * Use [Timeline.markAsRead] instead when possible. */ suspend fun markRoomAsFullyRead(roomId: RoomId, eventId: EventId): Result + + suspend fun vacuumStores(): Result } /** diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClient.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClient.kt index fca2aaa197..31e80cf442 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClient.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClient.kt @@ -726,6 +726,12 @@ class RustMatrixClient( } } + override suspend fun vacuumStores(): Result = withContext(sessionDispatcher) { + runCatchingExceptions { + innerClient.optimizeStores() + } + } + private suspend fun getCacheSize( includeCryptoDb: Boolean = false, ): Long = withContext(sessionDispatcher) {