Check errorMessages by id

pull/1461/head
TM 1 year ago
parent 2bf6689108
commit 6b5eb714a6

@ -108,13 +108,13 @@ fun NiaApp(appState: NiaAppState, modifier: Modifier = Modifier) {
LaunchedEffect(errorMessage) { LaunchedEffect(errorMessage) {
errorMessage?.let { errorMessage?.let {
val snackBarResult = snackbarHostState.showSnackbar( val snackBarResult = snackbarHostState.showSnackbar(
message = it, message = it.message,
actionLabel = "Continue", actionLabel = "Continue",
duration = Indefinite, duration = Indefinite,
) == ActionPerformed ) == ActionPerformed
if (snackBarResult) { if (snackBarResult) {
appState.clearErrorMessage() appState.clearErrorMessage(it.id)
} }
} }
} }

@ -121,17 +121,22 @@ class NiaAppState(
) )
private val errorMessages = MutableStateFlow<List<ErrorMessage>>(emptyList()) private val errorMessages = MutableStateFlow<List<ErrorMessage>>(emptyList())
val errorMessage: StateFlow<String?> = errorMessages.map { it.filter { it.message.isNotEmpty() && it.message.isNotBlank() }.firstOrNull()?.message }.stateIn( val errorMessage: StateFlow<ErrorMessage?> = errorMessages.map { it.firstOrNull() }.stateIn(
scope = coroutineScope, scope = coroutineScope,
started = SharingStarted.WhileSubscribed(5_000), started = SharingStarted.WhileSubscribed(5_000),
initialValue = null, initialValue = null,
) )
fun addErrorMessage(error: String) { fun addErrorMessage(error: String): String? {
if(error.isNotEmpty() && error.isNotBlank()) errorMessages.update { it + ErrorMessage(error) } if(error.isNotBlank()) {
val newError = ErrorMessage(error)
errorMessages.update { it + newError }
return newError.id
}
return null
} }
fun clearErrorMessage() { fun clearErrorMessage(id: String) {
errorMessages.update { it.drop(1) } errorMessages.update { it.filter { item -> item.id != id } }
} }
/** /**

Loading…
Cancel
Save