android5-8 Exercise 1: A persistent sort switch ============================================ // Module build.gradle.kts (version is a placeholder) dependencies { implementation("androidx.datastore:datastore-preferences:") } import android.content.Context import androidx.datastore.preferences.preferencesDataStore // Top level, once per file name: one DataStore instance per file val Context.settingsDataStore by preferencesDataStore(name = "settings") import androidx.datastore.core.DataStore import androidx.datastore.preferences.core.Preferences import androidx.datastore.preferences.core.booleanPreferencesKey import androidx.datastore.preferences.core.edit import androidx.datastore.preferences.core.emptyPreferences import java.io.IOException import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.map class SettingsRepository( private val dataStore: DataStore ) { private val newestFirstKey = booleanPreferencesKey("newest_first") // Reading: a Flow, so the UI follows changes automatically val newestFirst: Flow = dataStore.data .catch { e -> if (e is IOException) emit(emptyPreferences()) else throw e } .map { prefs -> prefs[newestFirstKey] ?: true } // default when never set // Writing: a suspend function, no apply() or commit() suspend fun setNewestFirst(value: Boolean) { dataStore.edit { prefs -> prefs[newestFirstKey] = value } } } class NotesApp : Application() { // ...database and notesRepository as before... val settingsRepository: SettingsRepository by lazy { SettingsRepository(settingsDataStore) // the Context extension from above } } data class NotesUiState( val notes: List = emptyList(), val newestFirst: Boolean = true, // new val isLoading: Boolean = false, val isRefreshing: Boolean = false, val userMessage: String? = null ) class NotesViewModel( private val repository: NotesRepository, private val settings: SettingsRepository ) : ViewModel() { private val message = MutableStateFlow(null) private val isRefreshing = MutableStateFlow(false) val uiState: StateFlow = combine(repository.getNotesStream(), settings.newestFirst, message, isRefreshing) { notes, newestFirst, userMessage, refreshing -> NotesUiState( notes = if (newestFirst) notes.sortedByDescending { it.createdAt } else notes.sortedBy { it.createdAt }, newestFirst = newestFirst, isRefreshing = refreshing, userMessage = userMessage ) }.stateIn( scope = viewModelScope, started = SharingStarted.WhileSubscribed(5_000), initialValue = NotesUiState(isLoading = true) ) fun setNewestFirst(value: Boolean) { viewModelScope.launch { settings.setNewestFirst(value) } } // addNote, deleteNote, refresh and messageShown are as in the earlier chapters } // In the Factory: NotesViewModel(app.notesRepository, app.settingsRepository) // In NotesScreen Row(verticalAlignment = Alignment.CenterVertically) { Text("Newest first") Switch(checked = state.newestFirst, onCheckedChange = onNewestFirstChange) } Also: onNewestFirstChange: (Boolean) -> Unit in NotesScreen and onNewestFirstChange = viewModel::setNewestFirst in NotesRoute. Expected: the switch re-sorts the list immediately; after closing and reopening the app the switch is in the same position. Report what you saw and the DataStore version used. Code unrun.