Closed asknask closed 2 years ago
Not sure if this is technically the right way to do it but converting the injected context into a local variable in the PreferencesManager
class solves the issue.
class PreferencesManager @Inject constructor(@ApplicationContext private val context: Context)
So I'm trying to follow this tutorial with the latest libraries and have hit a snag.
DataStore
is now created withprivate val Context.dataStore: DataStore<Preferences> by preferencesDataStore("user_preferences")
. There are 2 functions in thePreferencesManager.kt
file that require access to the dataStore, which in turn requires a context. I've updated them to this (added a context input):suspend fun updateSortOrder(context: Context, sortOrder: SortOrder) { context.dataStore.edit { preferences -> preferences[PreferencesKeys.SORT_ORDER] = sortOrder.name } }
However when this function is accessed from the
TasksViewModel
, the following code gives an error:fun onSortOrderSelected(sortOrder: SortOrder) = viewModelScope.launch { preferencesManager.updateSortOrder(this, sortOrder) }
It refuses to accept the context from the coroutine scope. What do I do?