Improve rendering of cache size (and fix compilation issue)

This commit is contained in:
Benoit Marty 2023-06-20 15:26:16 +02:00 committed by Benoit Marty
parent 2809d6fc2f
commit df4bc3d7ce
10 changed files with 22 additions and 17 deletions

View file

@ -34,7 +34,7 @@ dependencies {
anvil(projects.anvilcodegen) anvil(projects.anvilcodegen)
api(projects.features.messages.api) api(projects.features.messages.api)
implementation(projects.libraries.androidutils) implementation(projects.libraries.androidutils)
implementation(projects.libraries.androidtools.api) api(projects.libraries.androidtools.api)
implementation(projects.libraries.core) implementation(projects.libraries.core)
implementation(projects.libraries.architecture) implementation(projects.libraries.architecture)
implementation(projects.libraries.matrix.api) implementation(projects.libraries.matrix.api)

View file

@ -33,6 +33,7 @@ dependencies {
implementation(projects.anvilannotations) implementation(projects.anvilannotations)
anvil(projects.anvilcodegen) anvil(projects.anvilcodegen)
implementation(projects.libraries.androidutils) implementation(projects.libraries.androidutils)
api(projects.libraries.androidtools.api)
implementation(projects.libraries.core) implementation(projects.libraries.core)
implementation(projects.libraries.architecture) implementation(projects.libraries.architecture)
implementation(projects.libraries.matrix.api) implementation(projects.libraries.matrix.api)

View file

@ -56,7 +56,7 @@ class DeveloperSettingsPresenter @Inject constructor(
mutableStateMapOf<String, Boolean>() mutableStateMapOf<String, Boolean>()
} }
val cacheSize = remember { val cacheSize = remember {
mutableStateOf<Async<Long>>(Async.Uninitialized) mutableStateOf<Async<String>>(Async.Uninitialized)
} }
val clearCacheAction = remember { val clearCacheAction = remember {
mutableStateOf<Async<Unit>>(Async.Uninitialized) mutableStateOf<Async<Unit>>(Async.Uninitialized)
@ -88,7 +88,7 @@ class DeveloperSettingsPresenter @Inject constructor(
return DeveloperSettingsState( return DeveloperSettingsState(
features = featureUiModels.toImmutableList(), features = featureUiModels.toImmutableList(),
cacheSizeInBytes = cacheSize.value, cacheSize = cacheSize.value,
clearCacheAction = clearCacheAction.value, clearCacheAction = clearCacheAction.value,
eventSink = ::handleEvents eventSink = ::handleEvents
) )
@ -125,7 +125,7 @@ class DeveloperSettingsPresenter @Inject constructor(
} }
} }
private fun CoroutineScope.computeCacheSize(cacheSize: MutableState<Async<Long>>) = launch { private fun CoroutineScope.computeCacheSize(cacheSize: MutableState<Async<String>>) = launch {
suspend { suspend {
computeCacheSizeUseCase.execute() computeCacheSizeUseCase.execute()
}.execute(cacheSize) }.execute(cacheSize)

View file

@ -22,7 +22,7 @@ import kotlinx.collections.immutable.ImmutableList
data class DeveloperSettingsState constructor( data class DeveloperSettingsState constructor(
val features: ImmutableList<FeatureUiModel>, val features: ImmutableList<FeatureUiModel>,
val cacheSizeInBytes: Async<Long>, val cacheSize: Async<String>,
val clearCacheAction: Async<Unit>, val clearCacheAction: Async<Unit>,
val eventSink: (DeveloperSettingsEvents) -> Unit val eventSink: (DeveloperSettingsEvents) -> Unit
) )

View file

@ -30,7 +30,7 @@ open class DeveloperSettingsStateProvider : PreviewParameterProvider<DeveloperSe
fun aDeveloperSettingsState() = DeveloperSettingsState( fun aDeveloperSettingsState() = DeveloperSettingsState(
features = aFeatureUiModelList(), features = aFeatureUiModelList(),
cacheSizeInBytes = Async.Success(0L), cacheSize = Async.Success("1.2 MB"),
clearCacheAction = Async.Uninitialized, clearCacheAction = Async.Uninitialized,
eventSink = {} eventSink = {}
) )

View file

@ -56,15 +56,13 @@ fun DeveloperSettingsView(
onClick = onOpenShowkase onClick = onOpenShowkase
) )
} }
val cache = state.cacheSizeInBytes val cache = state.cacheSize
PreferenceCategory(title = "Cache") { PreferenceCategory(title = "Cache") {
PreferenceText( PreferenceText(
title = "Clear cache", title = "Clear cache",
icon = Icons.Default.Delete, icon = Icons.Default.Delete,
currentValue = if (cache is Async.Success) { currentValue = cache.dataOrNull(),
"${cache.state} bytes" loadingCurrentValue = state.cacheSize.isLoading() || state.clearCacheAction.isLoading(),
} else null,
loadingCurrentValue = state.cacheSizeInBytes.isLoading() || state.clearCacheAction.isLoading(),
onClick = { onClick = {
if (state.clearCacheAction.isLoading().not()) { if (state.clearCacheAction.isLoading().not()) {
state.eventSink(DeveloperSettingsEvents.ClearCache) state.eventSink(DeveloperSettingsEvents.ClearCache)

View file

@ -18,6 +18,7 @@ package io.element.android.features.preferences.impl.tasks
import android.content.Context import android.content.Context
import com.squareup.anvil.annotations.ContributesBinding import com.squareup.anvil.annotations.ContributesBinding
import io.element.android.libraries.androidtools.api.FileSizeFormatter
import io.element.android.libraries.androidutils.file.getSizeOfFiles import io.element.android.libraries.androidutils.file.getSizeOfFiles
import io.element.android.libraries.core.coroutine.CoroutineDispatchers import io.element.android.libraries.core.coroutine.CoroutineDispatchers
import io.element.android.libraries.di.ApplicationContext import io.element.android.libraries.di.ApplicationContext
@ -27,7 +28,7 @@ import kotlinx.coroutines.withContext
import javax.inject.Inject import javax.inject.Inject
interface ComputeCacheSizeUseCase { interface ComputeCacheSizeUseCase {
suspend fun execute(): Long suspend fun execute(): String
} }
@ContributesBinding(SessionScope::class) @ContributesBinding(SessionScope::class)
@ -35,11 +36,13 @@ class DefaultComputeCacheSizeUseCase @Inject constructor(
@ApplicationContext private val context: Context, @ApplicationContext private val context: Context,
private val matrixClient: MatrixClient, private val matrixClient: MatrixClient,
private val coroutineDispatchers: CoroutineDispatchers, private val coroutineDispatchers: CoroutineDispatchers,
private val fileSizeFormatter: FileSizeFormatter,
) : ComputeCacheSizeUseCase { ) : ComputeCacheSizeUseCase {
override suspend fun execute(): Long = withContext(coroutineDispatchers.io) { override suspend fun execute(): String = withContext(coroutineDispatchers.io) {
var cumulativeSize = 0L var cumulativeSize = 0L
cumulativeSize += matrixClient.getCacheSize() cumulativeSize += matrixClient.getCacheSize()
cumulativeSize += context.cacheDir.getSizeOfFiles() // - 4096 to not include the size fo the folder
cumulativeSize cumulativeSize += (context.cacheDir.getSizeOfFiles() - 4096).coerceAtLeast(0)
fileSizeFormatter.format(cumulativeSize)
} }
} }

View file

@ -42,7 +42,7 @@ class DeveloperSettingsPresenterTest {
val initialState = awaitItem() val initialState = awaitItem()
assertThat(initialState.features).isEmpty() assertThat(initialState.features).isEmpty()
assertThat(initialState.clearCacheAction).isEqualTo(Async.Uninitialized) assertThat(initialState.clearCacheAction).isEqualTo(Async.Uninitialized)
assertThat(initialState.cacheSizeInBytes).isEqualTo(Async.Uninitialized) assertThat(initialState.cacheSize).isEqualTo(Async.Uninitialized)
cancelAndIgnoreRemainingEvents() cancelAndIgnoreRemainingEvents()
} }
} }

View file

@ -26,7 +26,9 @@ import io.element.android.libraries.di.ApplicationContext
import javax.inject.Inject import javax.inject.Inject
@ContributesBinding(AppScope::class) @ContributesBinding(AppScope::class)
class AndroidFileSizeFormatter @Inject constructor(@ApplicationContext private val context: Context) : FileSizeFormatter { class AndroidFileSizeFormatter @Inject constructor(
@ApplicationContext private val context: Context,
) : FileSizeFormatter {
override fun format(fileSize: Long, useShortFormat: Boolean): String { override fun format(fileSize: Long, useShortFormat: Boolean): String {
// Since Android O, the system considers that 1ko = 1000 bytes instead of 1024 bytes. // Since Android O, the system considers that 1ko = 1000 bytes instead of 1024 bytes.
// We want to avoid that. // We want to avoid that.

View file

@ -77,6 +77,7 @@ private fun DependencyHandlerScope.addImplementationProjects(
fun DependencyHandlerScope.allLibrariesImpl() { fun DependencyHandlerScope.allLibrariesImpl() {
implementation(project(":libraries:androidutils")) implementation(project(":libraries:androidutils"))
implementation(project(":libraries:deeplink")) implementation(project(":libraries:deeplink"))
implementation(project(":libraries:androidtools:impl"))
implementation(project(":libraries:designsystem")) implementation(project(":libraries:designsystem"))
implementation(project(":libraries:matrix:impl")) implementation(project(":libraries:matrix:impl"))
implementation(project(":libraries:matrixui")) implementation(project(":libraries:matrixui"))