android 将类提供给视图模型的Hilt问题

rryofs0p  于 2023-08-01  发布在  Android
关注(0)|答案(1)|浏览(107)

我无法启动我的应用程序,因为这两个新模块DataStoreModuleUserModule到目前为止我所做的:

interface DataStoresProvider {
    fun userDataStore(): DataStore<Preferences>
}
class UserDataStore(
    dataStoresProvider: DataStoresProvider
) : ModelDataStore<UserModel>(
    dataStoresProvider.userDataStore(),
    USER_KEY
) {
interface UserRepository {

    suspend fun saveUser(userModel: UserModel)

    suspend fun getUser(): Flow<UserModel?>
}
class UserRepositoryImpl @Inject constructor(val userDataStore: UserDataStore): UserRepository {
    override suspend fun saveUser(userModel: UserModel) {
        userDataStore.putModel(userModel)
    }

    override suspend fun getUser(): Flow<UserModel?> {
        return userDataStore.getModel<UserModel>()
    }
}
@HiltViewModel
class HomeViewModel @Inject constructor(val userRepository: UserRepository) : ViewModel() {
@Composable
fun HomeScreen(outputDirectory: File?) {
   val viewModel = hiltViewModel<HomeViewModel>()

或版本,其中包含MainActivity中的ViewModel

@Composable
fun HomeScreen(outputDirectory: File?, viewModel: HomeViewModel) {
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    private val viewModel: HomeViewModel by viewModels()

模块:

@Module
@InstallIn(SingletonComponent::class)
object DataStoresModule {

    @Provides
    fun provideUserDataStore(dataStoresProvider: DataStoresProvider): UserDataStore {
        return UserDataStore(dataStoresProvider)
    }

}
@Module
@InstallIn(SingletonComponent::class)
class UserModule {

    @Provides
    fun provideUserRepository(userDataStore: UserDataStore): UserRepository {
        return UserRepositoryImpl(userDataStore)
    }
}

我的错误:

error: [Dagger/MissingBinding] com.example.insta.local.DataStoresProvider cannot be provided without an @Provides-annotated method.


我不明白为什么它不起作用。每个组件提供另一个直到viewModel

public abstract static class SingletonC implements TransApplication_GeneratedInjector,
                         ^
      com.example.instatrans.local.DataStoresProvider is injected at
          com.example.instatrans.module.DataStoresModule.provideUserDataStore(dataStoresProvider)
      com.example.instatrans.models.UserDataStore is injected at
          com.example.instatrans.module.UserModule.provideUserRepository(userDataStore)
      com.example.instatrans.repository.UserRepository is injected at
          com.example.instatrans.ui.home.HomeViewModel(userRepository)
      com.example.instatrans.ui.home.HomeViewModel is injected at
          com.example.instatrans.ui.home.HomeViewModel_HiltModules.BindsModule.binds(arg0)
      @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.example.instatrans.TransApplication_HiltComponents.SingletonC → com.example.instatrans.TransApplication_HiltComponents.ActivityRetainedC → com.example.instatrans.TransApplication_HiltComponents.ViewModelC]

UserDataStore

class UserDataStore @Inject constructor(
    val dataStoresProvider: DataStoresProvider
) : ModelDataStore<UserModel>(
    dataStoresProvider.userDataStore(),
    USER_KEY
) {

    companion object {
        val USER_KEY = baseStringPreferencesKey("USER_KEY")
    }
}

加入:

@Provides
    fun provideUserDataStoreProvider(@ApplicationContext app: Context): DataStoresProvider {
        return DataStoreProviderImpl(app)
    }

class DataStoreProviderImpl @Inject constructor(
    private val context: Context
) : DataStoresProvider {

    private val Context.userDataStore: DataStore<Preferences> by appPreferencesDataStore(
        name = "userData"
    )

    override fun userDataStore(): DataStore<Preferences> {
        return context.userDataStore
    }

}

fun appPreferencesDataStore(name: String) = preferencesDataStore(name = name + DATA_STORE_SUFFIX)

private const val DATA_STORE_SUFFIX = "_v3"```
hfyxw5xn

hfyxw5xn1#

您需要将DataStoresModule包含到UserModule中,因为它使用了该模块的依赖项(UserDataStore):

@Module(
    includes = [
        DataStoresModule::class
    ]
)
@InstallIn(SingletonComponent::class)
class UserModule {

    @Provides
    fun provideUserRepository(userDataStore: UserDataStore): UserRepository {
        return UserRepositoryImpl(userDataStore)
    }
}

字符串

相关问题