android5-2 Exercise 1: ViewModel, Route and Screen =============================================== NotesUiState: data class NotesUiState( val notes: List = emptyList(), val isLoading: Boolean = false, val userMessage: String? = null ) NotesViewModel: import androidx.lifecycle.ViewModel import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.update class NotesViewModel : ViewModel() { // Private and writable inside the ViewModel only... private val _uiState = MutableStateFlow(NotesUiState(isLoading = true)) // ...exposed to the UI as read-only val uiState: StateFlow = _uiState.asStateFlow() init { loadNotes() } private fun loadNotes() { // Placeholder data: Chapters 3 to 7 replace this with real loading _uiState.update { it.copy(notes = listOf("Buy milk", "Call Ada"), isLoading = false) } } fun addNote(text: String) { if (text.isBlank()) { _uiState.update { it.copy(userMessage = "A note cannot be empty") } return } _uiState.update { it.copy(notes = it.notes + text) } } fun messageShown() { _uiState.update { it.copy(userMessage = null) } } } NotesRoute: import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.lifecycle.viewmodel.compose.viewModel @Composable fun NotesRoute(viewModel: NotesViewModel = viewModel()) { val uiState by viewModel.uiState.collectAsStateWithLifecycle() NotesScreen( state = uiState, onAddNote = viewModel::addNote, onMessageShown = viewModel::messageShown ) } NotesScreen imports and code as in the chapter. Expected: add a note, rotate the emulator, and the notes are still there, because the ViewModel survives rotation (docs). The Chapter 1 version, with the list held by plain remember, would have lost the notes on rotation (Android Development Fundamentals with Compose 4, general knowledge). Report what you actually saw. Code unrun.