Browse Source

Improve rendering of cache size (and fix compilation issue)

feature/julioromano/geocoding_api
Benoit Marty 1 year ago committed by Benoit Marty
parent
commit
573de1c168
  1. 2
      features/messages/impl/build.gradle.kts
  2. 1
      features/preferences/impl/build.gradle.kts
  3. 6
      features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/developer/DeveloperSettingsPresenter.kt
  4. 2
      features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/developer/DeveloperSettingsState.kt
  5. 2
      features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/developer/DeveloperSettingsStateProvider.kt
  6. 8
      features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/developer/DeveloperSettingsView.kt
  7. 11
      features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/tasks/ComputeCacheSizeUseCase.kt
  8. 2
      features/preferences/impl/src/test/kotlin/io/element/android/features/preferences/impl/developer/DeveloperSettingsPresenterTest.kt
  9. 4
      libraries/androidtools/impl/src/main/kotlin/io/element/android/libraries/androidtools/impl/AndroidFileSizeFormatter.kt
  10. 1
      plugins/src/main/kotlin/extension/DependencyHandleScope.kt

2
features/messages/impl/build.gradle.kts

@ -34,7 +34,7 @@ dependencies { @@ -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)

1
features/preferences/impl/build.gradle.kts

@ -33,6 +33,7 @@ dependencies { @@ -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)

6
features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/developer/DeveloperSettingsPresenter.kt

@ -56,7 +56,7 @@ class DeveloperSettingsPresenter @Inject constructor( @@ -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( @@ -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( @@ -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)

2
features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/developer/DeveloperSettingsState.kt

@ -22,7 +22,7 @@ import kotlinx.collections.immutable.ImmutableList @@ -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
)

2
features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/developer/DeveloperSettingsStateProvider.kt

@ -30,7 +30,7 @@ open class DeveloperSettingsStateProvider : PreviewParameterProvider<DeveloperSe @@ -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 = {}
)

8
features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/developer/DeveloperSettingsView.kt

@ -56,15 +56,13 @@ fun DeveloperSettingsView( @@ -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)

11
features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/tasks/ComputeCacheSizeUseCase.kt

@ -18,6 +18,7 @@ package io.element.android.features.preferences.impl.tasks @@ -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 @@ -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( @@ -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)
}
}

2
features/preferences/impl/src/test/kotlin/io/element/android/features/preferences/impl/developer/DeveloperSettingsPresenterTest.kt

@ -42,7 +42,7 @@ class DeveloperSettingsPresenterTest { @@ -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()
}
}

4
libraries/androidtools/impl/src/main/kotlin/io/element/android/libraries/androidtools/impl/AndroidFileSizeFormatter.kt

@ -26,7 +26,9 @@ import io.element.android.libraries.di.ApplicationContext @@ -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.

1
plugins/src/main/kotlin/extension/DependencyHandleScope.kt

@ -77,6 +77,7 @@ private fun DependencyHandlerScope.addImplementationProjects( @@ -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…
Cancel
Save