Как добавить репозиторий в модуль комнаты кинжала? Мой репозиторий использует приложение, когда я пытаюсь добавить этот класс в модуль, я получаю сообщение об ошибке. Как правильно встроить этот репозиторий в Dagger, чтобы позже я смог сделать инъекцию в своем классе ViewModel? Почему первые две функции обычно реализуются в комнатном модуле, а последняя - нет. Любая помощь Мой репозиторий:
class ContactRepository (application: Application) {
private var contactDao: ContactDao
private var allContacts: MutableLiveData<ArrayList<Contact>>
companion object {
@Volatile
private var INSTANCE: ContactRepository? = null
fun getInstance(application: Application): ContactRepository {
return INSTANCE ?: getInstance(application)
}
}
init {
val database: ContactDatabase? = ContactDatabase.getInstance(application.applicationContext)
contactDao = database!!.contactDao()
allContacts = contactDao.getAllContact()
}
fun insert(contact: Contact) {
InsertContactAsyncTask().execute(contact)
}
fun updateAll(contactsModel: List<ContactsModel>) {
}
fun update(contact: Contact) {
}
fun delete(contact: Contact) {
}
fun getAllContact(): MutableLiveData<ArrayList<Contact>> {
return allContacts
}
class InsertContactAsyncTask : AsyncTask<Contact, Unit, Unit>() {
private val contactDao: ContactDao? = null
override fun doInBackground(vararg param: Contact) {
contactDao?.insert(param[0])
}
}
}
RoomModule:
@Module
class RoomModule {
private lateinit var contactDatabase: ContactDatabase
fun RoomModule(application: Application) {
contactDatabase = Room.databaseBuilder<ContactDatabase>(application, ContactDatabase::class.java, "contact_database")
.build()
}
@Singleton
@Provides
fun providesRoomDatabase(): ContactDatabase {
return contactDatabase
}
@Singleton
@Provides
fun providesContactDao(contactDatabase: ContactDatabase): ContactDao {
return contactDatabase.contactDao()
}
@Provides
fun providesContactRepository(application: Application): ContactRepository {
return ContactRepository(application)
}
}
Исключение:
public abstract interface ApplicationComponent {
^
android.app.Application is injected at com.infernal93.phonebookappmvvmanddagger.di.modules.RoomModule.providesContactRepository(application)
com.infernal93.phonebookappmvvmanddagger.room.ContactRepository is injected at
com.infernal93.phonebookappmvvmanddagger.viewmodels.ContactsViewModel(contactRepository)
com.infernal93.phonebookappmvvmanddagger.viewmodels.ContactsViewModel is injected at
com.infernal93.phonebookappmvvmanddagger.di.modules.ViewModelModule.bindViewModel(viewModel)
java.util.Map<java.lang.Class<? extends androidx.lifecycle.ViewModel>,javax.inject.Provider<androidx.lifecycle.ViewModel>> is injected at
com.infernal93.phonebookappmvvmanddagger.viewmodels.ViewModelFactory(viewModelsMap)
AppComponent:
@Singleton
@Component(modules = [ContextModule::class, NetworkModule::class, RoomModule::class])
interface ApplicationComponent {
fun inject(activity: ContactListActivity)
fun inject(activity: AddContactActivity)
fun inject(app: Application)
}
ViewNodel:
class ContactsViewModel @Inject constructor(private val contactRepository: ContactRepository) : ViewModel() {
fun getAllContacts(): MutableLiveData<ArrayList<Contact>>{
val contacts = contactRepository.getAllContact()
return contacts
}