Browse Source

Move cache management to a dedicated class and in the setting module, for clarity.

feature/julioromano/geocoding_api
Benoit Marty 1 year ago committed by Benoit Marty
parent
commit
b6920afb7f
  1. 6
      appnav/src/main/kotlin/io/element/android/appnav/RootFlowNode.kt
  2. 28
      features/preferences/api/src/main/kotlin/io/element/android/features/preferences/api/CacheService.kt
  3. 39
      features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/DefaultCacheService.kt
  4. 6
      features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/tasks/ClearCacheUseCase.kt
  5. 7
      libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/auth/MatrixAuthenticationService.kt
  6. 10
      libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/auth/RustMatrixAuthenticationService.kt
  7. 10
      libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/auth/FakeAuthenticationService.kt

6
appnav/src/main/kotlin/io/element/android/appnav/RootFlowNode.kt

@ -44,6 +44,7 @@ import io.element.android.appnav.root.RootPresenter @@ -44,6 +44,7 @@ import io.element.android.appnav.root.RootPresenter
import io.element.android.appnav.root.RootView
import io.element.android.features.login.api.oidc.OidcAction
import io.element.android.features.login.api.oidc.OidcActionFlow
import io.element.android.features.preferences.api.CacheService
import io.element.android.features.rageshake.api.bugreport.BugReportEntryPoint
import io.element.android.libraries.architecture.BackstackNode
import io.element.android.libraries.architecture.animation.rememberDefaultTransitionHandler
@ -67,6 +68,7 @@ class RootFlowNode @AssistedInject constructor( @@ -67,6 +68,7 @@ class RootFlowNode @AssistedInject constructor(
@Assisted val buildContext: BuildContext,
@Assisted plugins: List<Plugin>,
private val authenticationService: MatrixAuthenticationService,
private val cacheService: CacheService,
private val matrixClientsHolder: MatrixClientsHolder,
private val presenter: RootPresenter,
private val bugReportEntryPoint: BugReportEntryPoint,
@ -91,7 +93,7 @@ class RootFlowNode @AssistedInject constructor( @@ -91,7 +93,7 @@ class RootFlowNode @AssistedInject constructor(
authenticationService.isLoggedIn()
.distinctUntilChanged()
.combine(
authenticationService.cacheIndex().onEach {
cacheService.cacheIndex().onEach {
Timber.v("cacheIndex=$it")
matrixClientsHolder.removeAll()
}
@ -245,7 +247,7 @@ class RootFlowNode @AssistedInject constructor( @@ -245,7 +247,7 @@ class RootFlowNode @AssistedInject constructor(
}
private suspend fun attachSession(sessionId: SessionId): LoggedInFlowNode {
val cacheIndex = authenticationService.cacheIndex().first()
val cacheIndex = cacheService.cacheIndex().first()
return attachChild {
backstack.newRoot(NavTarget.LoggedInFlow(sessionId, cacheIndex))
}

28
features/preferences/api/src/main/kotlin/io/element/android/features/preferences/api/CacheService.kt

@ -0,0 +1,28 @@ @@ -0,0 +1,28 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.features.preferences.api
import kotlinx.coroutines.flow.Flow
interface CacheService {
/**
* Returns a flow of the current cache index, can let the app to know when the
* cache has been cleared, for instance to restart the app.
* Will be a flow of Int, starting from 0, and incrementing each time the cache is cleared.
*/
fun cacheIndex(): Flow<Int>
}

39
features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/DefaultCacheService.kt

@ -0,0 +1,39 @@ @@ -0,0 +1,39 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.features.preferences.impl
import com.squareup.anvil.annotations.ContributesBinding
import io.element.android.features.preferences.api.CacheService
import io.element.android.libraries.di.AppScope
import io.element.android.libraries.di.SingleIn
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import javax.inject.Inject
@SingleIn(AppScope::class)
@ContributesBinding(AppScope::class)
class DefaultCacheService @Inject constructor() : CacheService {
private val cacheIndexState = MutableStateFlow(0)
override fun cacheIndex(): Flow<Int> {
return cacheIndexState
}
fun incrementCacheIndex() {
cacheIndexState.value++
}
}

6
features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/tasks/ClearCacheUseCase.kt

@ -22,11 +22,11 @@ import android.content.Context @@ -22,11 +22,11 @@ import android.content.Context
import coil.Coil
import coil.annotation.ExperimentalCoilApi
import com.squareup.anvil.annotations.ContributesBinding
import io.element.android.features.preferences.impl.DefaultCacheService
import io.element.android.libraries.core.coroutine.CoroutineDispatchers
import io.element.android.libraries.di.ApplicationContext
import io.element.android.libraries.di.SessionScope
import io.element.android.libraries.matrix.api.MatrixClient
import io.element.android.libraries.matrix.api.auth.MatrixAuthenticationService
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
import javax.inject.Inject
@ -41,7 +41,7 @@ class DefaultClearCacheUseCase @Inject constructor( @@ -41,7 +41,7 @@ class DefaultClearCacheUseCase @Inject constructor(
@ApplicationContext private val context: Context,
private val matrixClient: MatrixClient,
private val coroutineDispatchers: CoroutineDispatchers,
private val authenticationService: MatrixAuthenticationService,
private val defaultCacheIndexProvider: DefaultCacheService,
private val okHttpClient: Provider<OkHttpClient>,
) : ClearCacheUseCase {
override suspend fun invoke() = withContext(coroutineDispatchers.io) {
@ -57,6 +57,6 @@ class DefaultClearCacheUseCase @Inject constructor( @@ -57,6 +57,6 @@ class DefaultClearCacheUseCase @Inject constructor(
// Clear app cache
context.cacheDir.deleteRecursively()
// Ensure the app is restarted
authenticationService.incrementCacheIndex()
defaultCacheIndexProvider.incrementCacheIndex()
}
}

7
libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/auth/MatrixAuthenticationService.kt

@ -29,13 +29,6 @@ interface MatrixAuthenticationService { @@ -29,13 +29,6 @@ interface MatrixAuthenticationService {
suspend fun setHomeserver(homeserver: String): Result<Unit>
suspend fun login(username: String, password: String): Result<SessionId>
/*
* Cache index
*/
fun cacheIndex(): Flow<Int>
fun incrementCacheIndex()
/*
* OIDC part.
*/

10
libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/auth/RustMatrixAuthenticationService.kt

@ -58,8 +58,6 @@ class RustMatrixAuthenticationService @Inject constructor( @@ -58,8 +58,6 @@ class RustMatrixAuthenticationService @Inject constructor(
private val clock: SystemClock,
) : MatrixAuthenticationService {
private val cacheIndexState = MutableStateFlow(0)
private val authService: RustAuthenticationService = RustAuthenticationService(
basePath = baseDirectory.absolutePath,
passphrase = null,
@ -73,14 +71,6 @@ class RustMatrixAuthenticationService @Inject constructor( @@ -73,14 +71,6 @@ class RustMatrixAuthenticationService @Inject constructor(
return sessionStore.isLoggedIn()
}
override fun incrementCacheIndex() {
cacheIndexState.value++
}
override fun cacheIndex(): Flow<Int> {
return cacheIndexState
}
override suspend fun getLatestSessionId(): SessionId? = withContext(coroutineDispatchers.io) {
sessionStore.getLatestSession()?.userId?.let { SessionId(it) }
}

10
libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/auth/FakeAuthenticationService.kt

@ -65,16 +65,6 @@ class FakeAuthenticationService : MatrixAuthenticationService { @@ -65,16 +65,6 @@ class FakeAuthenticationService : MatrixAuthenticationService {
loginError?.let { Result.failure(it) } ?: Result.success(A_USER_ID)
}
private val cacheIndexFlow = MutableStateFlow(0)
override fun cacheIndex(): Flow<Int> {
return cacheIndexFlow
}
override fun incrementCacheIndex() {
cacheIndexFlow.value++
}
override suspend fun getOidcUrl(): Result<OidcDetails> = simulateLongTask {
oidcError?.let { Result.failure(it) } ?: Result.success(A_OIDC_DATA)
}

Loading…
Cancel
Save