我正在学习如何使用MVP模式和Dagger2。我已根据教程将演示者注入到视图中,并将模型注入了演示者中,但是我无法弄清楚如何将“外部”内容(例如应用程序/上下文)注入到我的模型中我已经阅读了有关创建ContextModule的内容,但我仍然不知道如何使其与我的课程一起使用。 所以我有:
SettingsActivity -查看课程
class SettingsActivity : AppCompatActivity() {
@Inject
lateinit var settingsPresenter: SettingsPresenter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_settings)
var component: SettingsComponent = DaggerSettingsComponent.create()
component.inject(this)
}
}
SettingsPresenter -具有MVP接口的演示者类
class SettingsPresenter : BasePresenter<SettingsContract.View>, SettingsContract.Presenter{
var settings: SettingsModel
@Inject
constructor(settings: SettingsModel) : super() {
this.settings = settings
}
}
interface SettingsContract {
interface View
interface Presenter
}
@Component
interface SettingsComponent {
fun inject(settingsActivity: SettingsActivity)
}
SettingsModel -模型类
class SettingsModel {
@Inject
constructor() {
}
//I'd like to have my context field here
@Inject
lateinit var context: Context
}
AppModule -我想用来提供上下文的类
@Module
class AppModule {
var context: Context
constructor(context: Context) {
this.context = context
}
@Provides
fun getAppContext() : Context {
return context.applicationContext
}
}
我应该如何将上下文注入SettingsModel以及从何处启动?