VIPER 在 Presenter 中注入交互器
VIPER Inject Interactor in Presenter
我有一个采用 VIPER 架构的应用程序。我将 DI 与 dagger-2 一起使用,但无法将交互器注入演示器。
我得到下一个错误:
lateinit property interactor has not been initialized
我有下一个代码:
主视图(Kotlin)
interface MainView {
interface View : BaseView {
showMessage(message : String)
}
interface InteractorOutput{
...
}
}
MainActivity (Kotlin)
class MainActivity : BaseActivity(), MainView.View {
@Inject lateinit var presenter: MainPresenter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main)
}
override fun onCreatePresenter() {
presenter.setView(this)
presenter.onCreate()
}
override fun initializeDagger() {
activityComponent.inject(this)
}
override fun showMessage(message : String){
Toast(...)
}
}
MainPresenter (Kotlin)
class MainPresenter @Inject constructor(): BasePresenter<MainView.View, MainInteractor>(), MainView.InteractorOutput {
@Inject lateinit var interactor : MainInteractor
override fun onCreate() {
interactor.doSomething()
}
}
MainInteractor (Kotlin)
class MainInteractor: BaseInteractor() {
fun doSomething() {
Log.e("Hello ", "World!")
}
}
ActivityComponent (Java)
@PerActivity
@Component(dependencies = ApplicationComponent.class, modules = ActivityModule.class})
public interface ActivityComponent {
void inject(MainActivity mainActivity);
}
ActivityModule (Java)
@Module
public class ActivityModule {
private final BaseActivity mActivity;
public ActivityModule(BaseActivity activity) {
mActivity = activity;
}
@Provides
@PerActivity
BaseActivity activity() {
return mActivity;
}
@Provides
@PerActivity
MainPresenter provideMainPresenter() {
return new MainPresenter();
}
}
ApplicationComponent (Java)
@Singleton
@Component(modules = {ApplicationModule.class})
public interface ApplicationComponent {
void inject(BaseActivity baseActivity);
}
ApplicationModule (Java)
@Module
public class ApplicationModule {
private final BaseApplication mApplication;
public ApplicationModule(BaseApplication application) {
mApplication = application;
}
@Provides
@Singleton
Context provideApplicationContext() {
return mApplication;
}
}
目前您已将交互器设置为要注入的字段。然而,没有注入这个字段的机制,实际上没有必要这样做,因为你应该使用构造函数注入来代替。
从您的演示者中删除以下内容:
@Inject lateinit var interactor: MainInteractor
而是在构造函数中注入它:
class MainPresenter @Inject constructor(private val interactor: MainInteractor)
此外,目前 Dagger 无法提供 MainInteractor
,因此您需要使用以下方式注释其构造函数:
class MainInteractor: BaseInteractor @Inject constructor () {
我不清楚 MainPresenter
和 MainActivityPresenter
之间的区别,您可能还有其他问题。不过,这些变化应该会让你们更接近。
我有一个采用 VIPER 架构的应用程序。我将 DI 与 dagger-2 一起使用,但无法将交互器注入演示器。
我得到下一个错误:
lateinit property interactor has not been initialized
我有下一个代码:
主视图(Kotlin)
interface MainView {
interface View : BaseView {
showMessage(message : String)
}
interface InteractorOutput{
...
}
}
MainActivity (Kotlin)
class MainActivity : BaseActivity(), MainView.View {
@Inject lateinit var presenter: MainPresenter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main)
}
override fun onCreatePresenter() {
presenter.setView(this)
presenter.onCreate()
}
override fun initializeDagger() {
activityComponent.inject(this)
}
override fun showMessage(message : String){
Toast(...)
}
}
MainPresenter (Kotlin)
class MainPresenter @Inject constructor(): BasePresenter<MainView.View, MainInteractor>(), MainView.InteractorOutput {
@Inject lateinit var interactor : MainInteractor
override fun onCreate() {
interactor.doSomething()
}
}
MainInteractor (Kotlin)
class MainInteractor: BaseInteractor() {
fun doSomething() {
Log.e("Hello ", "World!")
}
}
ActivityComponent (Java)
@PerActivity
@Component(dependencies = ApplicationComponent.class, modules = ActivityModule.class})
public interface ActivityComponent {
void inject(MainActivity mainActivity);
}
ActivityModule (Java)
@Module
public class ActivityModule {
private final BaseActivity mActivity;
public ActivityModule(BaseActivity activity) {
mActivity = activity;
}
@Provides
@PerActivity
BaseActivity activity() {
return mActivity;
}
@Provides
@PerActivity
MainPresenter provideMainPresenter() {
return new MainPresenter();
}
}
ApplicationComponent (Java)
@Singleton
@Component(modules = {ApplicationModule.class})
public interface ApplicationComponent {
void inject(BaseActivity baseActivity);
}
ApplicationModule (Java)
@Module
public class ApplicationModule {
private final BaseApplication mApplication;
public ApplicationModule(BaseApplication application) {
mApplication = application;
}
@Provides
@Singleton
Context provideApplicationContext() {
return mApplication;
}
}
目前您已将交互器设置为要注入的字段。然而,没有注入这个字段的机制,实际上没有必要这样做,因为你应该使用构造函数注入来代替。
从您的演示者中删除以下内容:
@Inject lateinit var interactor: MainInteractor
而是在构造函数中注入它:
class MainPresenter @Inject constructor(private val interactor: MainInteractor)
此外,目前 Dagger 无法提供 MainInteractor
,因此您需要使用以下方式注释其构造函数:
class MainInteractor: BaseInteractor @Inject constructor () {
我不清楚 MainPresenter
和 MainActivityPresenter
之间的区别,您可能还有其他问题。不过,这些变化应该会让你们更接近。