Challenge 3: A Settings ViewModel — Solution @HiltViewModel class SettingsViewModel @Inject constructor( private val repository: SettingsRepository ) : ViewModel() { val notificationsEnabled: StateFlow = repository.notificationsEnabled .stateIn( scope = viewModelScope, started = SharingStarted.WhileSubscribed(5000), initialValue = true ) fun toggleNotifications() { viewModelScope.launch { val current = notificationsEnabled.value repository.setNotificationsEnabled(!current) } } } // In a composable @Composable fun SettingsScreen(viewModel: SettingsViewModel = hiltViewModel()) { val notificationsEnabled by viewModel.notificationsEnabled.collectAsStateWithLifecycle() Row { Text("Notifications") Switch( checked = notificationsEnabled, onCheckedChange = { viewModel.toggleNotifications() } ) } } Notes: - notificationsEnabled.value inside toggleNotifications() reads the StateFlow's CURRENT value directly (StateFlow always has one, per Kotlin Intermediate Chapter 2) — no need to collect it separately just to read the latest value for the toggle logic. - !current inverts the boolean, and repository.setNotificationsEnabled(!current) writes it back through the repository, which itself writes it into DataStore — the UI never touches DataStore directly at any point in this chain. - This ViewModel follows the exact same shape as every other @HiltViewModel in this course: a repository injected via constructor, a Flow converted to StateFlow via stateIn, and viewModelScope.launch wrapping any suspend write — DataStore is simply the newest data source slotted into an already-established pattern.