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,7 +110,9 @@ class NiaAppState(
initialValue = false, initialValue = false,
) )
val snackbarMessage = errorMessage.stateIn( val snackbarMessage = errorMessages
.map { it.firstOrNull() }
.stateIn(
scope = coroutineScope, scope = coroutineScope,
started = SharingStarted.WhileSubscribed(5_000), started = SharingStarted.WhileSubscribed(5_000),
initialValue = null, initialValue = null,

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

@ -24,5 +24,5 @@ import kotlinx.coroutines.flow.Flow
interface ErrorMonitor { interface ErrorMonitor {
fun addErrorMessage(error: String): String? fun addErrorMessage(error: String): String?
fun clearErrorMessage(id: 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.Flow
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
import java.util.UUID import java.util.UUID
import javax.inject.Inject import javax.inject.Inject
@ -31,12 +30,8 @@ class SnackbarErrorMonitor @Inject constructor() : ErrorMonitor {
/** /**
* List of [ErrorMessage] to be shown to the user, via Snackbar. * List of [ErrorMessage] to be shown to the user, via Snackbar.
*/ */
private val errorMessages = MutableStateFlow<List<ErrorMessage>>(emptyList()) private val _errorMessages = MutableStateFlow<List<ErrorMessage>>(emptyList())
override val errorMessages: Flow<List<ErrorMessage>> = _errorMessages
/**
* Current [ErrorMessage] or null if there are none.
*/
override val errorMessage: Flow<ErrorMessage?> = errorMessages.map { it.firstOrNull() }
/** /**
* Creates an [ErrorMessage] from String value and adds it to the list. * 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? { override fun addErrorMessage(error: String): String? {
if (error.isNotBlank()) { if (error.isNotBlank()) {
val newError = ErrorMessage(error) val newError = ErrorMessage(error)
errorMessages.update { it + newError } _errorMessages.update { it + newError }
return newError.id return newError.id
} }
return null return null
@ -59,7 +54,7 @@ class SnackbarErrorMonitor @Inject constructor() : ErrorMonitor {
* Removes the [ErrorMessage] with the specified [id] from the list. * Removes the [ErrorMessage] with the specified [id] from the list.
*/ */
override fun clearErrorMessage(id: String) { 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 @Test
fun whenErrorIsNotAdded_NullIsPresent() = runTest(UnconfinedTestDispatcher()) { fun whenErrorIsNotAdded_NullIsPresent() = runTest(UnconfinedTestDispatcher()) {
backgroundScope.launch { state.errorMessage.collect() } backgroundScope.launch { state.errorMessages.collect() }
assertEquals( assertEquals(
null, null,
message, message,
@ -46,8 +46,8 @@ class SnackbarErrorMonitorTest {
@Test @Test
fun whenErrorIsAdded_ErrorMessageIsPresent() = runTest(UnconfinedTestDispatcher()) { fun whenErrorIsAdded_ErrorMessageIsPresent() = runTest(UnconfinedTestDispatcher()) {
backgroundScope.launch { backgroundScope.launch {
state.errorMessage.collect { state.errorMessages.collect {
message = it message = it.firstOrNull()
} }
} }
@ -66,8 +66,8 @@ class SnackbarErrorMonitorTest {
state.addErrorMessage("Test Error Message 2") state.addErrorMessage("Test Error Message 2")
backgroundScope.launch { backgroundScope.launch {
state.errorMessage.collect { state.errorMessages.collect {
message = it message = it.firstOrNull()
} }
} }
@ -81,8 +81,8 @@ class SnackbarErrorMonitorTest {
fun whenErrorIsCleared_ErrorMessageIsNotPresent() = fun whenErrorIsCleared_ErrorMessageIsNotPresent() =
runTest(UnconfinedTestDispatcher()) { runTest(UnconfinedTestDispatcher()) {
backgroundScope.launch { backgroundScope.launch {
state.errorMessage.collect { state.errorMessages.collect {
message = it message = it.firstOrNull()
} }
} }
val id = state.addErrorMessage("Test Error Message 1") val id = state.addErrorMessage("Test Error Message 1")
@ -99,8 +99,8 @@ class SnackbarErrorMonitorTest {
fun whenErrorsAreCleared_NextErrorMessageIsPresent() = fun whenErrorsAreCleared_NextErrorMessageIsPresent() =
runTest(UnconfinedTestDispatcher()) { runTest(UnconfinedTestDispatcher()) {
backgroundScope.launch { backgroundScope.launch {
state.errorMessage.collect { state.errorMessages.collect {
message = it message = it.firstOrNull()
} }
} }
val id1 = state.addErrorMessage("Test Error Message 1") val id1 = state.addErrorMessage("Test Error Message 1")

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

Loading…
Cancel
Save