在我的项目中,我有两个DataStore
(更好的SharedPref API )。我有AuthDataStore
和UserDataStore
,有单独的包。我想把这两个DataStores分别注入repositories
中。我用了匕首希尔特
implementation 'com.google.dagger:hilt-android:2.40.5'
kapt 'com.google.dagger:hilt-android-compiler:2.40.5'
kapt 'androidx.hilt:hilt-compiler:1.0.0'
implementation 'androidx.hilt:hilt-navigation-compose:1.0.0'
这是我的DataStoreModule
@Module
@InstallIn(SingletonComponent::class)
object DsModule {
@Provides
@Singleton
fun provideUserDataStoreSource(
ds: DataStore<Preferences>
): UserDataStoreSource {
return UserDataStoreSourceImpl(ds = ds)
}
@Provides
@Named(value = Constants.USER_DS_NAME)
@Singleton
fun provideUserDataStore(
@ApplicationContext context: Context
): DataStore<Preferences> {
return PreferenceDataStoreFactory.create(
produceFile = {
context.preferencesDataStoreFile(Constants.USER_DS_NAME)
}
)
}
@Provides
@Singleton
fun provideAuthDataStoreSource(
ds: DataStore<Preferences>
): AuthDataStore {
return AuthDataStoreSourceImpl(ds = ds)
}
@Provides
@Named(value = Constants.AUTH_DS_NAME)
@Singleton
fun provideAuthDataStore(
@ApplicationContext context: Context
): DataStore<Preferences> {
return PreferenceDataStoreFactory.create(
produceFile = {
context.preferencesDataStoreFile(Constants.AUTH_DS_NAME)
}
)
}
}
这是我的宝库。
open class AuthDataStoreSourceImpl @Inject constructor(
@Named(value = Constants.AUTH_DS_NAME) private val ds: DataStore<Preferences>
) : AuthDataStore {
companion object {
val AUTH_STATE = booleanPreferencesKey("com.galaxytechno.auth_state")
}
override suspend fun putAuthFlag(isLoggedIn: Boolean) {
ds.edit {
it[AUTH_STATE] = isLoggedIn
}
}
override suspend fun pullAuthFlag(): Flow<Boolean> {
return ds.data
.catch { exception ->
if (exception is IOException) emit(emptyPreferences()) else throw exception
}.map {
it[AUTH_STATE] ?: false
}
}
}
open class UserDataStoreSourceImpl @Inject constructor(
@Named(value = Constants.USER_DS_NAME) private val ds: DataStore<Preferences>
) : UserDataStoreSource {
companion object {
val ACCESS_TOKEN = stringPreferencesKey("com.galaxytechno.user.access_token")
val REFRESH_TOKEN = stringPreferencesKey("com.galaxytechno.user.refresh_token")
}
override suspend fun putAccessToken(token: String) {
ds.edit {
it[ACCESS_TOKEN] = token
}
}
override suspend fun pullAccessToken(): Flow<String> {
return ds.data
.catch { exception ->
if (exception is IOException) emit(emptyPreferences()) else throw exception
}
.map {
it[ACCESS_TOKEN] ?: "empty_access_token"
}
} }
这些是我的主要回复:
class AuthRepositoryImpl @Inject constructor(
private val api: AuthApiService,
@Named(value = Constants.AUTH_DS_NAME) private val ds: AuthDataStore,
@Qualifier.Io private val io: CoroutineDispatcher
) : AuthRepository {
override suspend fun login(
mobileNumber: String,
password: String
): Flow<RemoteResource<LoginDTO>> {
return flow {
emit(
safeApiCall {
api.login(
mobileNumber = mobileNumber,
password = password
)
}
)
}.flowOn(io)
}
override suspend fun putAuthFlag(isLoggedIn: Boolean) {
withContext(io){
ds.putAuthFlag(isLoggedIn = isLoggedIn)
}
}
override suspend fun pullAuthFlag(): Flow<Boolean> {
return ds.pullAuthFlag()
}
}
class UserRepositoryImpl @Inject constructor(
private val api: UserApiService,
private val db: UserDatabase,
@Named(value = Constants.USER_DS_NAME) private val ds : UserDataStoreSource,
@Qualifier.Io private val io: CoroutineDispatcher
) : UserRepository {
}
这是我的回购模块;
@Module
@InstallIn(SingletonComponent::class)
object RepositoryModule {
@Provides
@Singleton
fun provideUserRepository(
api: UserApiService,
db: UserDatabase,
@Named(value = Constants.USER_DS_NAME) ds : UserDataStoreSource,
@Qualifier.Io io: CoroutineDispatcher
): UserRepository {
return UserRepositoryImpl(
api = api,
db = db,
ds = ds,
io = io
)
}
@Provides
@Singleton
fun provideAuthRepository(
api: AuthApiService,
@Named(value = Constants.AUTH_DS_NAME) ds: AuthDataStore,
@Qualifier.Io io: CoroutineDispatcher
): AuthRepository {
return AuthRepositoryImpl(
api = api,
ds = ds,
io = io
)
}
}
我对此一无所知。我经常读刀柄文件。但我对匕首缺乏了解。请帮帮我。
我的错误:
D:\galaxy_techno\Chat\app\build\generated\source\kapt\debug\com\galaxytechno\chat\app\ChatApp_HiltComponents.java:140: error: [Dagger/MissingBinding] @javax.inject.Named("auth.chat.ds") com.galaxytechno.chat.auth.data.ds.AuthDataStore cannot be provided without an @Provides-annotated method.
public abstract static class SingletonC implements ChatApp_GeneratedInjector,
^
@javax.inject.Named("auth.chat.ds") com.galaxytechno.chat.auth.data.ds.AuthDataStore is injected at
com.galaxytechno.chat.app.di.RepositoryModule.provideAuthRepository(�, ds, �)
com.galaxytechno.chat.auth.domain.repository.AuthRepository is injected at
com.galaxytechno.chat.auth.domain.usecase.GetAuthStateUseCase(repo)
com.galaxytechno.chat.auth.domain.usecase.GetAuthStateUseCase is injected at
com.galaxytechno.chat.core.presentation.MainViewModel(getAuthStateUseCase)
com.galaxytechno.chat.core.presentation.MainViewModel is injected at
com.galaxytechno.chat.core.presentation.MainViewModel_HiltModules.BindsModule.binds(vm)
@dagger.hilt.android.internal.lifecycle.HiltViewModelMap java.util.Map<java.lang.String,javax.inject.Provider<androidx.lifecycle.ViewModel>> is requested at
dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.ViewModelFactoriesEntryPoint.getHiltViewModelMap() [com.galaxytechno.chat.app.ChatApp_HiltComponents.SingletonC ? com.galaxytechno.chat.app.ChatApp_HiltComponents.ActivityRetainedC ? com.galaxytechno.chat.app.ChatApp_HiltComponents.ViewModelC]
发布于 2022-02-15 17:12:01
根据[Dagger/MissingBinding]
的说法,Dagger正在寻找一个@Named("auth.chat.ds") AuthDataStore
。这意味着您需要一个精确的绑定匹配,通常使用@Binds
或@Provides
提供,包括精确的类名、精确的泛型和具有完全相同注释值的精确@Qualifier
注释(如@Named
)。
但是,您的DataStoreModule列出了:
@Provides
@Singleton // No @Named annotation
fun provideAuthDataStoreSource(
ds: DataStore<Preferences>
): AuthDataStore { // AuthDataStore
return /* ... */
}
@Provides
@Named(value = Constants.AUTH_DS_NAME) // @Named("auth.chat.ds")
@Singleton
fun provideAuthDataStore(
@ApplicationContext context: Context
): DataStore<Preferences> { // DataStore<Preferences>
return /* ... */
}
因此,您在provideAuthModule
中的参数@Named(value = Constants.AUTH_DS_NAME) ds: AuthDataStore
不匹配。它既不是@Named(AUTH_DS_NAME) DataStore<Preferences>
,也不是未命名/不合格的AuthDataStore
。
删除您的@Named
,您应该会没事的:
@Provides
@Singleton
fun provideAuthRepository(
api: AuthApiService,
/* @Named(value = Constants.AUTH_DS_NAME) */ ds: AuthDataStore,
/* ^-------------remove-----------------^ */
@Qualifier.Io io: CoroutineDispatcher
): AuthRepository
https://stackoverflow.com/questions/71121237
复制相似问题