Save User State

Sep 10 2024 · Kotlin 1.9, Android 14, Android Studio Koala | 2024.1.1

Lesson 01: Save Simple Data

Demo: Write Data to DataStore

Episode complete

Play next episode

Next
Transcript

In this section, you’ll work on logic to save data to DataStore.

Continuing work in the DataStoreManager class, create a new suspend function called saveSelectedFilter(). saveSelectedFilter() has one parameter:

suspend fun saveSelectedFilter(selectedFilter: String) {
  val dataStoreKey = stringPreferencesKey("filters")
  context.dataStore.edit { preferences ->
    preferences[dataStoreKey] = selectedFilter
  }
}

The function above takes a String parameter called selectedFilter. You then create a dataStoreKey using the stringPreferencesKey() function. This function creates a Preference.Key<String> instance. You then call dataStore.edit(), which takes in a lambda that provides access to the preferences. Inside the lambda, you set the selectedFilter to the dataStoreKey. This will save the selected filter to DataStore.

And that’s it! You’ve successfully created a method to save data to DataStore.

Now, you’re going to call this method in the MainViewModel class to save the selected filter. The UI logic has already been handled for you, you just need to call the saveSelectedFilter() method in the MainViewModel class.

Start by providing the DataStoreManager class to the MainViewModel class. You’ll use Dependency Injection to provide the DataStoreManager class to the MainViewModel class. Navigate to ui/viewmodels/MainViewModel. Create a new constructor in the MainViewModel class that takes a DataStoreManager class as a parameter:

private val dataStoreManager: DataStoreManager

This provides an instance of the DataStoreManager class to the MainViewModel class. Now, you need to update your Koin modules to add the new dependency. Head over to the di package and open the Modules.kt file. This file has all the Koin modules that provide dependencies to your app. You can see that the IDE already highlights an error. The DataStoreManager class isn’t provided in the MainViewModel class. Before fixing it, you’ll add the DataStoreManager dependency to your Koin modules. Add the following code to the dataStoreModule:

single { DataStoreManager(androidContext()) }

This code provides a single instance of the DataStoreManager class to the app. Now, you need to provide the DataStoreManager class to the MainViewModel class. Modify the MainViewModel in the viewModelModule to provide the DataStoreManager class:

viewModel { MainViewModel(get()) }

You have added a get() call that will provide the DataStoreManager class to the MainViewModel class.

Now, head over to MainViewModel again. Locate // TODO: Save selected filter and replace it with the following code:

viewModelScope.launch {
  dataStoreManager.saveSelectedFilter(selectedFilter)
}

Follow the context action and import kotlinx.coroutines.launch and androidx.lifecycle.viewModelScope.

In the code above, you call the saveSelectedFilter() method from the DataStoreManager class. You then pass the selectedFilter as a parameter. This will save the selected filter to DataStore. Since the saveSelectedFilter() method is a suspend function, you need to call it inside a coroutine.  This is why you use the viewModelScope.launch. It launches a coroutine in the MainViewModel class. With that, your app is ready to save the selected filter to DataStore.

Run the app and tap the filter icon in the top-right corner of the screen. Select a filter and tap it. The selected filter will be saved to DataStore. But, when you close the app, open it again, and tap the icon again, the selected filter isn’t retained. This is because you haven’t implemented the logic to read the selected filter from DataStore.

In the next sections, you’ll learn how to read data from DataStore and implement the logic to read the selected filter from DataStore.

See forum comments
Cinema mode Download course materials from Github
Previous: Write Data to DataStore Next: Read Data from DataStore