MVVMLin
一个基于 MVVM 用 Kotlin+Retrofit+协程+Databinding(ViewBinding)+LiveData 来封装的快速开发框架: 项目地址:MVVMLin
框架简介
- 使用技术 基于 MVVM 模式用了 kotlin+协程+retrofit+livedata+DataBinding
- 基本封装 封装了 BaseActivity、BaseFragment、BaseViewModel 基于协和的网络请方式更加方便,考虑到有些小伙伴不太喜欢用 DataBinding 在 xml 中绑定数据的方式,也提供了相应的适配,两种方式自行选择。Retrofit2.6 及以上版本提供了对协程的支持,使用起来更加方便. 增加了 Flow 的转换
- 引入第三方库
AndroidUtilCode:包含了大量的常用工具类,简直是必备神器啊。
coil:图片加载(更适合 KT 的图片加载)
Retrofit:网络请求
使用方式
启用 dataBinding
在主工程 app 的 build.gradle 的 android {}中加入:
buildFeatures {
//dataBinding = true
viewBinding = true
}
依赖
在主项目 app 的 build.gradle 中依赖
dependencies {
...
implementation 'com.github.AleynP:MVVMLin:2.0.0'
}
或者 下载到本地导入 Module
配置 buildSrc (1.0.6 版本改为 butkdSrc 方式构建)
复制 Demo 的 buildSrc 到根目录,用 Gradle 同步。AS 会自动识别 buildSrc 目录 (远程依赖可以忽略)
添加 FlowAdapter
Retrofit.Builder()
.addCallAdapterFactory(FlowAdapterFactory.create(true))
//...
.build()
如上添加 FlowAdapter
后, Retrofit 可直接返回 Flow 类型。
快速开始
Activity
继承 BaseVMActivity
class DetailActivity : 继承 BaseVMActivity<NoViewModel, ViewBinding>() {
override val layoutId() = R.layout.activity_detail
override fun initView(savedInstanceState: Bundle?) {
....
}
override fun initData() {
....
}
}
第一个泛型是 ViewModel,如果页面很简单不需要 ViewModel,可以继承 BaseActivity。 第二个泛型是 Databinding 或者 ViewBinding 的生成类,如果页面使用 Databinding 或者 ViewBinding 的话,就要传对应的 Binding 生成类,如果不使用 DataBinding 或者 ViewBinding,传 ViewBinding 。基类不用初始化 mBinding 而会使用常规方式。 layoutId 方法返回对应布局。当使用 ViewBinding 时 不用重写此方法,其余都要重写 返回布局 Id initView() 和 initData() 为默认实现,做初始化 UI 等操作
Fragment
继承 BaseVMFragment
class HomeFragment : 继承 BaseVMFragment<HomeViewModel, ViewBinding>() {
override fun layoutId() = R.layout.home_fragment
override fun initView(savedInstanceState: Bundle?) { }
override fun lazyLoadData() {
....
}
}
实现方法同 Activity 一样,Fragment 多了懒加载方法lazyLoadData() 可选择性重写。
ViewModel
继承 BaseViewModel
class HomeViewModel : BaseViewModel() {
.........
}
在 Application 中设置全局网络异常处理(可选)
MVVMLin.setNetException(CoroutineExceptionHandler { context, e ->
//...
})
ViewModel 中所有网络请求都写要 BaseViewModel 的 launch
作用域中,如下:
Flow 方式(同 RxJava 相似)
// Service 用 Flow 接收
@GET("xxx/xxx")
fun getBannerData(): Flow<BaseResult<XXX>>>
class HomeViewModel : BaseViewModel() {
private val homeRepository by lazy { InjectorUtil.getHomeRepository() }
private val _banners = MutableSharedFlow<List<BannerBean>>()
val mBanners: SharedFlow<List<BannerBean>> = _banners
fun getBanner(refresh: Boolean = false) {
//只返回结果,其他全抛自定义异常
launch {
homeRepository.getBannerData(refresh)
.asResponse()
.bindLoading(this@HomeViewModel)//绑定 Lodaing
.collect(_banners)
}
}
}
操作符
以下几个是用 Flow 时用到的操作符:
- asResponse: 只取成功结果其他全抛异常
- asSuccess:只做成功处理,不关心结果
- bindLoading: 绑定 Loading
- netCache: 自定义错误处理,返回的是 ResponseThrowable
普通方式(不使用 Flow)
注意不使用 Flow 时, Service 返回值直接用基类,并添加上 suspend
@GET("xxx/xxx")
suspend fun getBannerData(): BaseResult<XXX>
class ProjectViewModel : BaseViewModel() {
fun getProjectList(cid: Int) {
launch {
homeRepository.getProjectList(page, cid).getOrThrow()
.let {
items.clear()
items.addAll(it.datas)
}
}
}
}
转换符
以下几个是不使用 Flow 时用到的转换符:
- getOrThrow: 只取成功结果其他全抛异常
- check:检测是否成功
IBaseResponse
由于请求中依赖了数据基类,我们每个项目的基类字段都不相同,所以我们根据后台的字段定义完基类以后,要实现 IBaseResponde 接口,如下:
data class BaseResult<T>(
val errorMsg: String,
val errorCode: Int,
val data: T
) : IBaseResponse<T> {
override fun code() = errorCode
override fun msg() = errorMsg
override fun data() = data
override fun isSuccess() = errorCode == 0
}
来保证过滤请求结果的正常使用
例子
Demo 中只展示了三种列表使用方式
不使用 Databinging,结合BRVAH
详见 Demo 的 HomeFragment
使用 Databinging,结合bindingcollectionadapter
结合 bindingcollectionadapter 不用写 Adapter 适配器了,详见 Demo 的 ProjectFragment
使用 Databinging,结合BRVAH
BRVAH 对 DataBinding 也做了支持,详见 Demo 的 MeFragment
最后
QQ 群:(791382057)