Expose list of error messages instead of single current

Updated tests to match
pull/1461/head
TM 1 year ago
parent a72bb98f1a
commit e776036f7d

@ -110,11 +110,13 @@ class NiaAppState(
initialValue = false,
)
val snackbarMessage = errorMessage.stateIn(
scope = coroutineScope,
started = SharingStarted.WhileSubscribed(5_000),
initialValue = null,
)
val snackbarMessage = errorMessages
.map { it.firstOrNull() }
.stateIn(
scope = coroutineScope,
started = SharingStarted.WhileSubscribed(5_000),
initialValue = null,
)
/**
* Map of top level destinations to be used in the TopBar, BottomBar and NavRail. The key is the

@ -31,6 +31,6 @@ class FakeErrorMonitor @Inject constructor() : ErrorMonitor {
// Do nothing
}
override val errorMessage: Flow<ErrorMessage?>
get() = flowOf(null)
override val errorMessages: Flow<List<ErrorMessage?>>
get() = flowOf(emptyList())
}

@ -24,5 +24,5 @@ import kotlinx.coroutines.flow.Flow
interface ErrorMonitor {
fun addErrorMessage(error: String): String?
fun clearErrorMessage(id: String)
val errorMessage: Flow<ErrorMessage?>
val errorMessages: Flow<List<ErrorMessage?>>
}

@ -18,7 +18,6 @@ package com.google.samples.apps.nowinandroid.core.data.util
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.update
import java.util.UUID
import javax.inject.Inject
@ -31,12 +30,8 @@ class SnackbarErrorMonitor @Inject constructor() : ErrorMonitor {
/**
* List of [ErrorMessage] to be shown to the user, via Snackbar.
*/
private val errorMessages = MutableStateFlow<List<ErrorMessage>>(emptyList())
/**
* Current [ErrorMessage] or null if there are none.
*/
override val errorMessage: Flow<ErrorMessage?> = errorMessages.map { it.firstOrNull() }
private val _errorMessages = MutableStateFlow<List<ErrorMessage>>(emptyList())
override val errorMessages: Flow<List<ErrorMessage>> = _errorMessages
/**
* Creates an [ErrorMessage] from String value and adds it to the list.
@ -49,7 +44,7 @@ class SnackbarErrorMonitor @Inject constructor() : ErrorMonitor {
override fun addErrorMessage(error: String): String? {
if (error.isNotBlank()) {
val newError = ErrorMessage(error)
errorMessages.update { it + newError }
_errorMessages.update { it + newError }
return newError.id
}
return null
@ -59,7 +54,7 @@ class SnackbarErrorMonitor @Inject constructor() : ErrorMonitor {
* Removes the [ErrorMessage] with the specified [id] from the list.
*/
override fun clearErrorMessage(id: String) {
errorMessages.update { it.filter { item -> item.id != id } }
_errorMessages.update { it.filter { item -> item.id != id } }
}
}

@ -36,7 +36,7 @@ class SnackbarErrorMonitorTest {
@Test
fun whenErrorIsNotAdded_NullIsPresent() = runTest(UnconfinedTestDispatcher()) {
backgroundScope.launch { state.errorMessage.collect() }
backgroundScope.launch { state.errorMessages.collect() }
assertEquals(
null,
message,
@ -46,8 +46,8 @@ class SnackbarErrorMonitorTest {
@Test
fun whenErrorIsAdded_ErrorMessageIsPresent() = runTest(UnconfinedTestDispatcher()) {
backgroundScope.launch {
state.errorMessage.collect {
message = it
state.errorMessages.collect {
message = it.firstOrNull()
}
}
@ -66,8 +66,8 @@ class SnackbarErrorMonitorTest {
state.addErrorMessage("Test Error Message 2")
backgroundScope.launch {
state.errorMessage.collect {
message = it
state.errorMessages.collect {
message = it.firstOrNull()
}
}
@ -81,8 +81,8 @@ class SnackbarErrorMonitorTest {
fun whenErrorIsCleared_ErrorMessageIsNotPresent() =
runTest(UnconfinedTestDispatcher()) {
backgroundScope.launch {
state.errorMessage.collect {
message = it
state.errorMessages.collect {
message = it.firstOrNull()
}
}
val id = state.addErrorMessage("Test Error Message 1")
@ -99,8 +99,8 @@ class SnackbarErrorMonitorTest {
fun whenErrorsAreCleared_NextErrorMessageIsPresent() =
runTest(UnconfinedTestDispatcher()) {
backgroundScope.launch {
state.errorMessage.collect {
message = it
state.errorMessages.collect {
message = it.firstOrNull()
}
}
val id1 = state.addErrorMessage("Test Error Message 1")

@ -30,6 +30,6 @@ class TestErrorMonitor : ErrorMonitor {
// Do nothing
}
override val errorMessage: Flow<ErrorMessage?>
get() = flowOf(ErrorMessage("Error Message", "1"))
override val errorMessages: Flow<List<ErrorMessage?>>
get() = flowOf(listOf(ErrorMessage("Error Message", "1")))
}

Loading…
Cancel
Save