您当前的位置: 首页 > 
  • 0浏览

    0关注

    674博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

依赖注入框架Koin(二)快速上手

沙漠一只雕得儿得儿 发布时间:2021-01-03 17:13:58 ,浏览量:0

一、集成Koin

依赖包的添加

//1、在project的build.gradle下,buildScript的dependencies添加 classpath
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath "org.koin:koin-gradle-plugin:$koin_version"
    }
}
//2、应用koin插件
apply plugin: 'koin'
//3、在各自module的build.gralde 的dependencies添加依赖包
// Koin for Kotlin
implementation "org.koin:koin-core:$koin_version"
implementation "org.koin:koin-core-ext:$koin_version"

// Koin for Android
implementation "org.koin:koin-android:$koin_version"
implementation "org.koin:koin-android-scope:$koin_version"
implementation "org.koin:koin-android-viewmodel:$koin_version"
implementation "org.koin:koin-android-ext:$koin_version"
//koin for Androidx 基本二选一即可
implementation "org.koin:koin-androidx-scope:$koin_version"
implementation "org.koin:koin-androidx-viewmodel:$koin_version"
implementation "org.koin:koin-androidx-fragment:$koin_version"
implementation "org.koin:koin-androidx-ext:$koin_version"

testImplementation "org.koin:koin-test:$koin_version"
二、基本使用

1、创建被注入组件,来提供并使用相关数据

class HomeViewModel : ViewModel() {

    private val _text = MutableLiveData().apply {
        value = "This is home Fragment"
    }
    val text: LiveData = _text
}

2、创建Koin module,使用该module函数声明模块

val cnModules = module {
    //viewModel
    viewModel { DashboardViewModel() }
    viewModel { HomeViewModel() }
}
  • module 类似dagger2中的 @Module
  • factory每次Activity需要一个实例时都会创建一个新实例。
  • single 区别在于其提供的实例是单例的
  • get()这里的功能是直接检索实例(非延迟)

代码分析:

  • module中声明了DashboardViewModel、HomeViewModel,它将自动搜索到对应的实例去创建实例。
  • single标明创建的是单例

3、启动koin 现在有了一个模块,只需要在Application里调用startKoin()函数:

class CnApplication : Application() {
    override fun onCreate() {
        super.onCreate()
        //koin的注解使用
        startKoin {
            androidLogger(Level.ERROR)//目前已知bug,除了level.error外,使用androidlogger会导致崩溃
            //context
            androidContext(this@CnApplication)
            //assets 资源数据
            androidFileProperties("ass.file")//默认取值assets下koin.properties文件内的属性配置,可自定义
            //加载需要的module
            modules(cnModules)
        }
    }
}

4、依赖注入,也就是获取Koin为我们创建好的实例对象

class DashboardFragment : Fragment() {

    private val dashboardViewModel: DashboardViewModel by viewModel()
//    val dashboardViewModel by viewModels { defaultViewModelProviderFactory }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
//        dashboardViewModel = ViewModelProviders.of(this).get(DashboardViewModel::class.java)
        val root = inflater.inflate(R.layout.fragment_dashboard, container, false)
        val textView: TextView = root.findViewById(R.id.text_dashboard)
        dashboardViewModel.text.observe(viewLifecycleOwner, Observer {
            textView.text = it
        })
        return root
    }

}

可以看到,原来的dashboardViewModel的实例获取改为了如下方式:

val dashboardViewModel: DashboardViewModel by viewModel()

或者:

val homeViewModel = getViewModel()

上面的是懒加载,下面的是同步直接获取到ViewModel对象。

关注
打赏
1657159701
查看更多评论
立即登录/注册

微信扫码登录

0.0440s