Delegate and Inject ErrorMonitor

pull/1461/head
TM 1 year ago
parent 18c57eb937
commit dd996ff7f7

@ -96,7 +96,7 @@ fun NiaApp(
val snackbarHostState = remember { SnackbarHostState() } val snackbarHostState = remember { SnackbarHostState() }
val isOffline by appState.isOffline.collectAsStateWithLifecycle() val isOffline by appState.isOffline.collectAsStateWithLifecycle()
val errorMessage by appState.errorMessage.collectAsStateWithLifecycle() val snackbarMessage by appState.snackbarMessage.collectAsStateWithLifecycle()
// If user is not connected to the internet show a snack bar to inform them. // If user is not connected to the internet show a snack bar to inform them.
val notConnectedMessage = stringResource(R.string.not_connected) val notConnectedMessage = stringResource(R.string.not_connected)
@ -108,8 +108,8 @@ fun NiaApp(
) )
} }
} }
LaunchedEffect(errorMessage) { LaunchedEffect(snackbarMessage) {
errorMessage?.let { snackbarMessage?.let {
val snackBarResult = snackbarHostState.showSnackbar( val snackBarResult = snackbarHostState.showSnackbar(
message = it.message, message = it.message,
actionLabel = "Continue", actionLabel = "Continue",

@ -89,7 +89,7 @@ class NiaAppState(
errorMonitor: ErrorMonitor, errorMonitor: ErrorMonitor,
userNewsResourceRepository: UserNewsResourceRepository, userNewsResourceRepository: UserNewsResourceRepository,
timeZoneMonitor: TimeZoneMonitor, timeZoneMonitor: TimeZoneMonitor,
) { ) : ErrorMonitor by errorMonitor {
val currentDestination: NavDestination? val currentDestination: NavDestination?
@Composable get() = navController @Composable get() = navController
.currentBackStackEntryAsState().value?.destination .currentBackStackEntryAsState().value?.destination
@ -110,7 +110,7 @@ class NiaAppState(
initialValue = false, initialValue = false,
) )
val errorMessage = errorMonitor.errorMessage.stateIn( val snackbarMessage = errorMessage.stateIn(
scope = coroutineScope, scope = coroutineScope,
started = SharingStarted.WhileSubscribed(5_000), started = SharingStarted.WhileSubscribed(5_000),
initialValue = null, initialValue = null,

@ -25,4 +25,4 @@ 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 errorMessage: Flow<ErrorMessage?>
} }

@ -21,12 +21,13 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.map 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
/** /**
* Interface implementation for handling general errors. * Interface implementation for handling general errors.
*/ */
class SnackbarErrorMonitor : ErrorMonitor { 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.
*/ */
@ -68,4 +69,4 @@ class SnackbarErrorMonitor : ErrorMonitor {
data class ErrorMessage( data class ErrorMessage(
val message: String, val message: String,
val id: String = UUID.randomUUID().toString(), val id: String = UUID.randomUUID().toString(),
) )

Loading…
Cancel
Save