Improve rendering of cache size (and fix compilation issue)
This commit is contained in:
parent
c847ac3a98
commit
573de1c168
10 changed files with 22 additions and 17 deletions
|
|
@ -34,7 +34,7 @@ dependencies {
|
|||
anvil(projects.anvilcodegen)
|
||||
api(projects.features.messages.api)
|
||||
implementation(projects.libraries.androidutils)
|
||||
implementation(projects.libraries.androidtools.api)
|
||||
api(projects.libraries.androidtools.api)
|
||||
implementation(projects.libraries.core)
|
||||
implementation(projects.libraries.architecture)
|
||||
implementation(projects.libraries.matrix.api)
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@ dependencies {
|
|||
implementation(projects.anvilannotations)
|
||||
anvil(projects.anvilcodegen)
|
||||
implementation(projects.libraries.androidutils)
|
||||
api(projects.libraries.androidtools.api)
|
||||
implementation(projects.libraries.core)
|
||||
implementation(projects.libraries.architecture)
|
||||
implementation(projects.libraries.matrix.api)
|
||||
|
|
|
|||
|
|
@ -56,7 +56,7 @@ class DeveloperSettingsPresenter @Inject constructor(
|
|||
mutableStateMapOf<String, Boolean>()
|
||||
}
|
||||
val cacheSize = remember {
|
||||
mutableStateOf<Async<Long>>(Async.Uninitialized)
|
||||
mutableStateOf<Async<String>>(Async.Uninitialized)
|
||||
}
|
||||
val clearCacheAction = remember {
|
||||
mutableStateOf<Async<Unit>>(Async.Uninitialized)
|
||||
|
|
@ -88,7 +88,7 @@ class DeveloperSettingsPresenter @Inject constructor(
|
|||
|
||||
return DeveloperSettingsState(
|
||||
features = featureUiModels.toImmutableList(),
|
||||
cacheSizeInBytes = cacheSize.value,
|
||||
cacheSize = cacheSize.value,
|
||||
clearCacheAction = clearCacheAction.value,
|
||||
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 {
|
||||
computeCacheSizeUseCase.execute()
|
||||
}.execute(cacheSize)
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ import kotlinx.collections.immutable.ImmutableList
|
|||
|
||||
data class DeveloperSettingsState constructor(
|
||||
val features: ImmutableList<FeatureUiModel>,
|
||||
val cacheSizeInBytes: Async<Long>,
|
||||
val cacheSize: Async<String>,
|
||||
val clearCacheAction: Async<Unit>,
|
||||
val eventSink: (DeveloperSettingsEvents) -> Unit
|
||||
)
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ open class DeveloperSettingsStateProvider : PreviewParameterProvider<DeveloperSe
|
|||
|
||||
fun aDeveloperSettingsState() = DeveloperSettingsState(
|
||||
features = aFeatureUiModelList(),
|
||||
cacheSizeInBytes = Async.Success(0L),
|
||||
cacheSize = Async.Success("1.2 MB"),
|
||||
clearCacheAction = Async.Uninitialized,
|
||||
eventSink = {}
|
||||
)
|
||||
|
|
|
|||
|
|
@ -56,15 +56,13 @@ fun DeveloperSettingsView(
|
|||
onClick = onOpenShowkase
|
||||
)
|
||||
}
|
||||
val cache = state.cacheSizeInBytes
|
||||
val cache = state.cacheSize
|
||||
PreferenceCategory(title = "Cache") {
|
||||
PreferenceText(
|
||||
title = "Clear cache",
|
||||
icon = Icons.Default.Delete,
|
||||
currentValue = if (cache is Async.Success) {
|
||||
"${cache.state} bytes"
|
||||
} else null,
|
||||
loadingCurrentValue = state.cacheSizeInBytes.isLoading() || state.clearCacheAction.isLoading(),
|
||||
currentValue = cache.dataOrNull(),
|
||||
loadingCurrentValue = state.cacheSize.isLoading() || state.clearCacheAction.isLoading(),
|
||||
onClick = {
|
||||
if (state.clearCacheAction.isLoading().not()) {
|
||||
state.eventSink(DeveloperSettingsEvents.ClearCache)
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ package io.element.android.features.preferences.impl.tasks
|
|||
|
||||
import android.content.Context
|
||||
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.core.coroutine.CoroutineDispatchers
|
||||
import io.element.android.libraries.di.ApplicationContext
|
||||
|
|
@ -27,7 +28,7 @@ import kotlinx.coroutines.withContext
|
|||
import javax.inject.Inject
|
||||
|
||||
interface ComputeCacheSizeUseCase {
|
||||
suspend fun execute(): Long
|
||||
suspend fun execute(): String
|
||||
}
|
||||
|
||||
@ContributesBinding(SessionScope::class)
|
||||
|
|
@ -35,11 +36,13 @@ class DefaultComputeCacheSizeUseCase @Inject constructor(
|
|||
@ApplicationContext private val context: Context,
|
||||
private val matrixClient: MatrixClient,
|
||||
private val coroutineDispatchers: CoroutineDispatchers,
|
||||
private val fileSizeFormatter: FileSizeFormatter,
|
||||
) : ComputeCacheSizeUseCase {
|
||||
override suspend fun execute(): Long = withContext(coroutineDispatchers.io) {
|
||||
override suspend fun execute(): String = withContext(coroutineDispatchers.io) {
|
||||
var cumulativeSize = 0L
|
||||
cumulativeSize += matrixClient.getCacheSize()
|
||||
cumulativeSize += context.cacheDir.getSizeOfFiles()
|
||||
cumulativeSize
|
||||
// - 4096 to not include the size fo the folder
|
||||
cumulativeSize += (context.cacheDir.getSizeOfFiles() - 4096).coerceAtLeast(0)
|
||||
fileSizeFormatter.format(cumulativeSize)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ class DeveloperSettingsPresenterTest {
|
|||
val initialState = awaitItem()
|
||||
assertThat(initialState.features).isEmpty()
|
||||
assertThat(initialState.clearCacheAction).isEqualTo(Async.Uninitialized)
|
||||
assertThat(initialState.cacheSizeInBytes).isEqualTo(Async.Uninitialized)
|
||||
assertThat(initialState.cacheSize).isEqualTo(Async.Uninitialized)
|
||||
cancelAndIgnoreRemainingEvents()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,7 +26,9 @@ import io.element.android.libraries.di.ApplicationContext
|
|||
import javax.inject.Inject
|
||||
|
||||
@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 {
|
||||
// Since Android O, the system considers that 1ko = 1000 bytes instead of 1024 bytes.
|
||||
// We want to avoid that.
|
||||
|
|
|
|||
|
|
@ -77,6 +77,7 @@ private fun DependencyHandlerScope.addImplementationProjects(
|
|||
fun DependencyHandlerScope.allLibrariesImpl() {
|
||||
implementation(project(":libraries:androidutils"))
|
||||
implementation(project(":libraries:deeplink"))
|
||||
implementation(project(":libraries:androidtools:impl"))
|
||||
implementation(project(":libraries:designsystem"))
|
||||
implementation(project(":libraries:matrix:impl"))
|
||||
implementation(project(":libraries:matrixui"))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue