In this section, you’ll work on logic to save data to DataStore.
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
}
}
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.
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.
MainViewModel
class. You’ll use Dependency Injection to provide the DataStoreManager class to the MainViewMode
l 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
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()) }
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()) }
get()
call that will provide the DataStoreManager
class to the MainViewModel
class.
MainViewModel
again. Locate // TODO: Save selected filter and replace it with the following code:
viewModelScope.launch {
dataStoreManager.saveSelectedFilter(selectedFilter)
}
kotlinx.coroutines.launch
and androidx.lifecycle.viewModelScope
.
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.