Add ErrorMessages state to app state

pull/1461/head
TM 1 year ago
parent b85cf7d476
commit 6a31a4d9bc

@ -46,12 +46,18 @@ import com.google.samples.apps.nowinandroid.navigation.TopLevelDestination.BOOKM
import com.google.samples.apps.nowinandroid.navigation.TopLevelDestination.FOR_YOU import com.google.samples.apps.nowinandroid.navigation.TopLevelDestination.FOR_YOU
import com.google.samples.apps.nowinandroid.navigation.TopLevelDestination.INTERESTS import com.google.samples.apps.nowinandroid.navigation.TopLevelDestination.INTERESTS
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.update
import kotlinx.datetime.TimeZone import kotlinx.datetime.TimeZone
import java.util.UUID
@Composable @Composable
fun rememberNiaAppState( fun rememberNiaAppState(
@ -117,6 +123,20 @@ class NiaAppState(
initialValue = false, initialValue = false,
) )
private val _errorMessages = MutableStateFlow<List<ErrorMessage>>(emptyList())
val errorMessage: StateFlow<String?> = _errorMessages.map{ it.firstOrNull()?.message }.stateIn(
scope = coroutineScope,
started = SharingStarted.WhileSubscribed(5_000),
initialValue = null,
)
fun addErrorMessage(error: String) {
_errorMessages.update { it + ErrorMessage(error) }
}
fun clearErrorMessage() {
_errorMessages.update { it.drop(1) }
}
/** /**
* Map of top level destinations to be used in the TopBar, BottomBar and NavRail. The key is the * Map of top level destinations to be used in the TopBar, BottomBar and NavRail. The key is the
* route. * route.
@ -198,3 +218,8 @@ private fun NavigationTrackingSideEffect(navController: NavHostController) {
} }
} }
} }
data class ErrorMessage(
val message: String,
val id: String = UUID.randomUUID().toString(),
)

Loading…
Cancel
Save