android_catalogue1-9 Exercise 3: A Delete That Can Fail ======================================================== State: add a flag to the detail view model, outside DetailUiState (it is a one-off event, not a screen state): private val _deleteFailed = MutableStateFlow(false) val deleteFailed: StateFlow = _deleteFailed.asStateFlow() fun delete(onDeleted: () -> Unit) { val current = (uiState.value as? DetailUiState.Found)?.details ?: return viewModelScope.launch { try { repository.deleteItem(current.item) onDeleted() } catch (e: CancellationException) { throw e } catch (e: Exception) { _deleteFailed.value = true } } } fun errorShown() { _deleteFailed.value = false } Message: in DetailContent add a SnackbarHostState, show it in LaunchedEffect(deleteFailed) { if (deleteFailed) { host.showSnackbar("Couldn't delete. Please try again."); onErrorShown() } } and put snackbarHost = { SnackbarHost(host) } in the Scaffold. DetailScreen collects deleteFailed with collectAsStateWithLifecycle() and passes it down. Where things live: the flag lives in the view model (it is caused by a data operation and must survive rotation); showing the snackbar is UI work and lives in the composable. The user stays on the detail screen because onDeleted() (the navigation) is only called on success. Why not put the flag in DetailUiState? Found/NotFound/Loading describe what the item is. A failed delete is an event about an action; mixing them would force every screen state to carry an error field.