【发布时间】:2019-05-08 06:47:18
【问题描述】:
最近我开始将我们的应用程序划分为更小的 Android 模块,但我很难让 Dagger 按我想要的方式工作。
我目前的匕首设置包括:
- ApplicationComponent 标有 @Singleton。该组件是在应用启动时创建的。
- UserSubComponent 标有@UserScope。该子组件在用户登录时创建。
这两个组件与负责创建这两个组件的 App 类一起放在我的 app 模块中。
在我的login 模块中(它是我的应用程序模块的父级,因此它无法访问应用程序模块中的任何内容)我有我的AuthenticationManager。
当用户登录时,我使用 RxJava 从我的AuthenticationManager 向App 发送信号,因此可以创建UserSubComponent。
我的问题是我需要从我的UserSubComponent 访问一些依赖项,在它创建之后,在我的AuthenticationManager 中,所以我可以在继续之前预加载用户的数据。
模块结构:
app (AppComponent & UserSubComponent)
^
|
login (AuthenticationManager) - feature 2 - feature 3
我的应用类:
class App : DaggerApplication() {
@Inject
lateinit var authenticationManager: AuthenticationManager
override fun onCreate() {
super.onCreate()
authenticationManager
.authenticationStateStream
.subscribe { state ->
if (state == AuthenticationState.AUTHENTICATED) {
AppInjector.userComponent.inject(this)
}
}
}
身份验证管理器:
class AuthenticationManager @Inject constructor(loginApi: LoginApi) {
@Inject
lateinit var preLoader : PreLoader // This won't work because of different scope
val authenticationStateStream = Observable<AuthenticationState>()
fun login() {
if (success) {
authenticationStateStream.emit(AuthenticationState.AUTHENTICATED)
// UserSubComponent is now created
preLoader.preload()
}
}
}
应用组件
@Singleton
@Component(modules = [AppModule::class, AndroidSupportInjectionModule::class])
interface AppComponent : AndroidInjector<App> {
fun userComponentBuilder(): UserComponent.Builder
}
应用模块
@Module
class AppModule {
@Provides
@Singleton
fun provideLoginApi() = LoginApi()
}
用户子组件
@UserScope
@Subcomponent(modules = [UserModule::class, AndroidSupportInjectionModule::class])
interface UserComponent : AndroidInjector<App> {
@Subcomponent.Builder
interface Builder {
fun build(): UserComponent
}
}
用户模块
@Module
class UserModule {
@Provides
@UserScope
fun providesPreLoader() = PreLoader()
}
我能以某种方式让这个结构工作吗?或者当涉及到模块 + 匕首时,我有什么选择?
【问题讨论】:
标签: android module dagger-2 dagger