Мои настройки:
Код: Выделить всё
com.example.app
|-configurations
|-AppComponent.kt
|-AppDB.kt
|-AppModule.kt
|-MyApp.kt
@Singleton
@Component(modules = [AppModule::class])
interface AppComponent {
fun inject(baseViewModel: BaseViewModel)
}
@Database(entities = [
User::class
], version = 1, exportSchema = true)
abstract class AppDB : RoomDatabase() {
abstract fun userRepository(): IUserRepository
companion object{
@Volatile
private var db_instance: AppDB? = null
@Synchronized
fun getAppDBInstance(context : Context): AppDB {
return db_instance ?: synchronized(this) {
val instance = Room.databaseBuilder(
context.applicationContext,
AppDB::class.java,
"TransitPayDB.db3"
).allowMainThreadQueries()
.build()
db_instance = instance
instance
}
}
}
}
class MyApp: Application() {
private lateinit var appComponent: AppComponent
override fun onCreate() {
super.onCreate()
appComponent = DaggerAppComponent.builder().appModule(AppModule(this)).build()
}
fun getAppComponent(): AppComponent {
return appComponent
}
}
@Module
class AppModule(private val application: Application) {
@Singleton
@Provides
fun getUserRepo(appDB: AppDB): IUserRepository {
return appDB.userRepository()
}
@Singleton
@Provides
fun getRoomDBInstance(context: Context): AppDB {
return AppDB.getAppDBInstance(context)
}
@Singleton
@Provides
fun provideAppContext(): Context {
return application.applicationContext
}
}
Код: Выделить всё
android:name=".configurations.MyApp"
что-то не так с моими конфигурациями?кстати, именно так я внедряю свои репозитории:
Код: Выделить всё
open class BaseViewModel(application: Application) : AndroidViewModel(application) {
@Inject lateinit var userRepository: IUserRepository
init {
(application as MyApp).getAppComponent().inject(this)
sharedUserId = sharedPrefs.getInt("userId",0)
sharedCompanyId = sharedPrefs.getInt("companyId",0)
sharedRoleId = sharedPrefs.getInt("roleId",0)
sharedFullName = sharedPrefs.getString("fullName","")
sharedApiToken = sharedPrefs.getString("apiToken","")
}
}
Подробнее здесь: https://stackoverflow.com/questions/787 ... persistent
Мобильная версия