我想将我的Room Repository和Rest Repository注入到ViewModel类中。但是,我可以注入剩余存储库,但是当我尝试注入空间存储库时,它会出错。
class RestRepository @Inject constructor(private val restService: RestService) {
fun register(username: String, password: String, email: String): Single<NormalResponse> {
return restService.register(username, password, email)
}
fun createAuthToken(username: String, password: String): Single<TokenResponse> {
return restService.createAuthToken(username, password)
}
}
class MessageRepository @Inject constructor(private val messageDao: MessageDao) {
val all: List<Message> get() = messageDao.getAll
fun insert(vararg messages: Message) {
messageDao.insert(*messages)
}
fun update(message: Message) {
messageDao.update(message)
}
fun delete(message: Message) {
messageDao.delete(message)
}
}
@Module
class RestModule {
@Provides
internal fun provideRetrofit(): Retrofit {
return Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build()
}
@Provides
internal fun provideService(retrofit: Retrofit): RestService {
return retrofit.create<RestService>(RestService::class.java)
}
@Provides
internal fun provideRepository(restService: RestService): RestRepository {
return RestRepository(restService)
}
}
@Module(includes = [ApplicationModule::class])
class RoomModule {
@Provides
internal fun provideRoomDatabase(context: Context): AppDatabase {
return AppDatabase.getAppDatabase(context)
}
@Provides
internal fun provideMessageDao(appDatabase: AppDatabase): MessageDao {
return appDatabase.messageDao()
}
@Provides
internal fun provideMessageRepository(messageDao: MessageDao): MessageRepository {
return MessageRepository(messageDao)
}
}
@Module
abstract class MainFragmentBindingModule {
@ContributesAndroidInjector(modules = [ApplicationModule::class, UtilsModule::class, RestModule::class, RoomModule::class])
internal abstract fun provideChatFragmentFactory(): ChatFragment
}
class ChatViewModel @Inject constructor(val restRepository: RestRepository, val messageRepository: MessageRepository) : ViewModel() {
}
错误是这样的:
错误:[dagger.android.AndroidInjector.inject(T)]如果没有@Provides注释的方法,则无法提供me.ibrahimsn.capsules.data.local.message.MessageDao。
答案 0 :(得分:0)
我已解决问题,谢谢。
@Module(includes = [(ViewModelModule::class)])
class ApplicationModule {
@Provides
@Singleton
internal fun provideRetrofit(): Retrofit {
return Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build()
}
@Provides
@Singleton
internal fun provideRetrofitService(retrofit: Retrofit): RestService {
return retrofit.create<RestService>(RestService::class.java)
}
@Provides
@Singleton
internal fun provideRoomDatabase(app: Application): AppDatabase {
return AppDatabase.getAppDatabase(app)
}
@Provides
@Singleton
internal fun provideMessageDao(appDatabase: AppDatabase): MessageDao {
return appDatabase.messageDao()
}
}
@Singleton
class MessageRepository @Inject constructor(private val messageDao: MessageDao) {
val all: List<Message> get() = messageDao.getAll
fun insert(vararg messages: Message) {
messageDao.insert(*messages)
}
fun update(message: Message) {
messageDao.update(message)
}
fun delete(message: Message) {
messageDao.delete(message)
}
}