在kotlin-MVP使用dagger2(v2.11+)

这个一个简单的例子;介绍如何在Kotlin-MVP中使用dagger2(v2.11+),基本使用就不介绍了,还是跟之前版本还是一样,只是设计到activity/Fragment的时候有点不一样
dagger2官方介绍与android使用的文档在这里

导入

  • 导入dagger
  //dagger2
    compile 'com.google.dagger:dagger-android:2.11'
    compile 'com.google.dagger:dagger-android-support:2.11'
    kapt  'com.google.dagger:dagger-android-processor:2.11'
    kapt 'com.google.dagger:dagger-compiler:2.11'

在build.gradle最外层结果添加:

kapt {
    generateStubs = true
}

1. 创建mvp模板

  • bean
data class UserInfo(var name: String, var age: Int)
  • model
class MainModel {
    interface CallBack {
        fun data(info: UserInfo)
    }

    fun getInfo(callBack: CallBack) {
        callBack.data(UserInfo("Kotlin", 3))
    }
}
  • presenter
class MainPresenter @Inject constructor(var model: MainModel?, var view: MainView?) { fun getInfo() { model?.getInfo(object : MainModel.CallBack { override fun data(info: UserInfo) { view?.showInfo(info) } }) } fun destroy() { model = null view = null } }

2.使用dagger2

  • 创建presenter的module(记得让MainActivity继承MainView)
@Module class MainPresenterModule {

    @Provides fun modelProvider(): MainModel {
        return MainModel()
    }

    @Provides fun viewProvider(activity: MainActivity): MainView {
        return activity
    }
}
  • 创建Activity的module(v2.11后新加)
@Module
abstract class MainActivityModule {
    @Binds
    @IntoMap
    @ActivityKey(MainActivity::class)
    internal abstract fun bind(builder: MainSubComponent.Builder): AndroidInjector.Factory<out Activity>
}
  • 创建Activity的SubComponent(v2.11后新加)
@Subcomponent(modules = arrayOf(MainPresenterModule::class))
interface MainSubComponent : AndroidInjector<MainActivity> {

    @Subcomponent.Builder
    abstract class Builder : AndroidInjector.Builder<MainActivity>()
}
  • 创建application的module
@Module(subcomponents = arrayOf(MainSubComponent::class))
class AppModule {
    @Provides
    fun contextProvider(application: MyApplication): Context {
        return application.applicationContext
    }
}
  • 创建application的Component(v2.11后新加)
@Component(modules = arrayOf(
        /* Use AndroidInjectionModule.class if you're not using support library */
        AndroidSupportInjectionModule::class,
        AppModule::class,
        MainActivityModule::class
))
interface AppComponent {
    fun inject(application: MyApplication)
}
  • 最后就是改造Application类了
class MyApplication : Application(), HasActivityInjector {

    @Inject
    lateinit var dispatchingActivityInjector: DispatchingAndroidInjector<Activity>

    override fun activityInjector(): AndroidInjector<Activity> {
        return dispatchingActivityInjector
    }
}

到这里点击AS的build,完成没有报错之后再application的onCreate方法下:

    override fun onCreate() {
        super.onCreate()
        DaggerAppComponent.create()
                .inject(this)
    }

使用presenter

  • MainActivity 创建MainPresenter的变量
    @Inject
    lateinit var presenter: MainPresenter
  • 然后在onCreate里super语句之前加上(这句代码一样要加在super之前
  AndroidInjection.inject(this)
 override fun onCreate(savedInstanceState: Bundle?) {
        AndroidInjection.inject(this)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        presenter.getInfo()
    }

build一下就全部搞定完成了;

本文demo源码 ;

若你感兴趣欢迎start,也可以看看我用kotlin与dagger2写的项目: https://github.com/a1018875550/ZhihuDaily

你可能感兴趣的:(android,mvp,Kotlin,Dagger2)