Inject a StringProvider instead of the context, and update tests.

Also remove the other StringProvider we had, it was not used anymore
This commit is contained in:
Benoit Marty
2025-08-13 10:49:05 +02:00
committed by Benoit Marty
parent 5bbfaad03b
commit 9c32dbba58
7 changed files with 36 additions and 29 deletions

View File

@@ -16,5 +16,4 @@ interface AppErrorStateService {
fun showError(title: String, body: String)
fun showError(@StringRes titleRes: Int, @StringRes bodyRes: Int)
}

View File

@@ -23,6 +23,7 @@ dependencies {
implementation(projects.libraries.di)
implementation(projects.libraries.designsystem)
implementation(projects.libraries.uiStrings)
implementation(projects.services.toolbox.api)
implementation(projects.anvilannotations)
implementation(libs.coroutines.core)
@@ -34,4 +35,5 @@ dependencies {
testImplementation(libs.coroutines.test)
testImplementation(libs.test.turbine)
testImplementation(libs.test.truth)
testImplementation(projects.services.toolbox.test)
}

View File

@@ -7,13 +7,12 @@
package io.element.android.services.apperror.impl
import android.content.Context
import com.squareup.anvil.annotations.ContributesBinding
import io.element.android.libraries.di.AppScope
import io.element.android.libraries.di.ApplicationContext
import io.element.android.libraries.di.SingleIn
import io.element.android.services.apperror.api.AppErrorState
import io.element.android.services.apperror.api.AppErrorStateService
import io.element.android.services.toolbox.api.strings.StringProvider
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import javax.inject.Inject
@@ -21,7 +20,7 @@ import javax.inject.Inject
@ContributesBinding(AppScope::class)
@SingleIn(AppScope::class)
class DefaultAppErrorStateService @Inject constructor(
@ApplicationContext private val context: Context,
private val stringProvider: StringProvider,
) : AppErrorStateService {
private val currentAppErrorState = MutableStateFlow<AppErrorState>(AppErrorState.NoError)
override val appErrorStateFlow: StateFlow<AppErrorState> = currentAppErrorState
@@ -37,8 +36,8 @@ class DefaultAppErrorStateService @Inject constructor(
}
override fun showError(titleRes: Int, bodyRes: Int) {
val title = context.getString(titleRes)
val body = context.getString(bodyRes)
val title = stringProvider.getString(titleRes)
val body = stringProvider.getString(bodyRes)
showError(title, body)
}
}

View File

@@ -10,14 +10,14 @@ package io.element.android.services.apperror.impl
import app.cash.turbine.test
import com.google.common.truth.Truth.assertThat
import io.element.android.services.apperror.api.AppErrorState
import io.element.android.services.toolbox.test.strings.FakeStringProvider
import kotlinx.coroutines.test.runTest
import org.junit.Test
internal class DefaultAppErrorStateServiceTest {
@Test
fun `initial value is no error`() = runTest {
val service = DefaultAppErrorStateService()
val service = createDefaultAppErrorStateService()
service.appErrorStateFlow.test {
val state = awaitItem()
assertThat(state).isInstanceOf(AppErrorState.NoError::class.java)
@@ -26,8 +26,7 @@ internal class DefaultAppErrorStateServiceTest {
@Test
fun `showError - emits value`() = runTest {
val service = DefaultAppErrorStateService()
val service = createDefaultAppErrorStateService()
service.appErrorStateFlow.test {
skipItems(1)
@@ -42,9 +41,22 @@ internal class DefaultAppErrorStateServiceTest {
}
@Test
fun `dismiss - clears value`() = runTest {
val service = DefaultAppErrorStateService()
fun `showError - emits value from ids`() = runTest {
val service = createDefaultAppErrorStateService()
service.appErrorStateFlow.test {
skipItems(1)
service.showError(1, 2)
val state = awaitItem()
assertThat(state).isInstanceOf(AppErrorState.Error::class.java)
val errorState = state as AppErrorState.Error
assertThat(errorState.title).isEqualTo("A string")
assertThat(errorState.body).isEqualTo("A string")
}
}
@Test
fun `dismiss - clears value`() = runTest {
val service = createDefaultAppErrorStateService()
service.appErrorStateFlow.test {
skipItems(1)
@@ -58,4 +70,8 @@ internal class DefaultAppErrorStateServiceTest {
assertThat(awaitItem()).isInstanceOf(AppErrorState.NoError::class.java)
}
}
private fun createDefaultAppErrorStateService() = DefaultAppErrorStateService(
stringProvider = FakeStringProvider(),
)
}