android - 使用 Hilt 提供首选项数据存储
问题描述
我试图提供一个通用DataStore<Preferences>
的,以便可以在多个地方使用相同的首选项文件,但我得到了有用的错误消息:
找不到符号:DaggerMyApplication_HiltComponents_SingletonC.builder()
@Module
@InstallIn(ApplicationComponent::class)
object DataStoreModule {
@Provides
fun provideDataStore(@ApplicationContext context: Context): DataStore<Preferences> = context.createDataStore("settings")
}
但是,我可以执行以下操作并在@Inject
构造函数中使用它。
@Singleton
class DataStoreProvider @Inject constructor(@ApplicationContext context: Context) {
val dataStore: DataStore<Preferences> = context.createDataStore("settings")
}
我假设该扩展createDataStore
正在做一些 Hilt 不喜欢的事情,但即使问题无法解决,我也希望能解释正在发生的事情。
解决方案
这对我有用:
@Provides
@Singleton
fun dataStore(@ApplicationContext appContext: Context): DataStore<Preferences> =
appContext.createDataStore("settings")
这个想法@Singleton
落后于提供者方法。
2021 年 2 月 9 日更新:
最好创建一个经理并提供:
class DataStoreManager(appContext: Context) {
private val settingsDataStore = appContext.createDataStore("settings")
suspend fun setThemeMode(mode: Int) {
settingsDataStore.edit { settings ->
settings[Settings.NIGHT_MODE] = mode
}
}
val themeMode: Flow<Int> = settingsDataStore.data.map { preferences ->
preferences[Settings.NIGHT_MODE] ?: AppCompatDelegate.MODE_NIGHT_UNSPECIFIED
}
}
应用模块:
@InstallIn(SingletonComponent::class)
@Module
class AppModule {
@Provides
@Singleton
fun dataStoreManager(@ApplicationContext appContext: Context): DataStoreManager =
DataStoreManager(appContext)
2021 年 3 月 20 日更新:
版本 1.0.0-alpha07
private val Context.dataStore by preferencesDataStore("settings")
class DataStoreManager(appContext: Context) {
private val settingsDataStore = appContext.dataStore
suspend fun setThemeMode(mode: Int) {
settingsDataStore.edit { settings ->
settings[Settings.NIGHT_MODE] = mode
}
}
val themeMode: Flow<Int> = settingsDataStore.data.map { preferences ->
preferences[Settings.NIGHT_MODE] ?: AppCompatDelegate.MODE_NIGHT_UNSPECIFIED
}
}
2021 年 5 月 1 日更新: @Florian 完全正确,我忘记了。
删除dataStoreManager
提供者。然后,
private val Context.dataStore by preferencesDataStore("settings")
@Singleton //You can ignore this annotation as return `datastore` from `preferencesDataStore` is singletone
class DataStoreManager @Inject constructor(@ApplicationContext appContext: Context) {
private val settingsDataStore = appContext.dataStore
suspend fun setThemeMode(mode: Int) {
settingsDataStore.edit { settings ->
settings[Settings.NIGHT_MODE] = mode
}
}
val themeMode: Flow<Int> = settingsDataStore.data.map { preferences ->
preferences[Settings.NIGHT_MODE] ?: AppCompatDelegate.MODE_NIGHT_UNSPECIFIED
}
}
推荐阅读
- php - 成功登录身份验证和重定向后,会话在 Laravel 5.7 中不起作用
- python - 从views.py调用命令函数
- random - 如何生成随机时间?
- android - 尼泊尔语字符串不支持 Android
- c# - EF 6.4 DbCommandInterceptor 相关 C#
- javascript - React - 测试 useContext() 调度值
- flutter - dart/flutter中根据key值查询map对象数据
- kubernetes - Google Cloud Build 部署特定/当前图像标签
- google-bigquery - 在 BigQuery 上复制表,而不在目标表上添加新字段
- java - Java:从队列中读取数据并转换为所需的输出