指标
- 简略调用、少写反复代码
- 不依赖第三方库(只含
Retrofit+Okhttp+协程
) - 齐全不懂协程也能立马上手(模板代码)
用Kotlin的形式写Kotlin代码,什么意思呢?比照一下上面2个代码就晓得了:
mViewModel.wxArticleLiveData.observe(this, object : IStateObserver<List<WxArticleBean>>() {
override fun onSuccess(data: List<WxArticleBean>?) {
}
override fun onError() {
}
})
mViewModel.wxArticleLiveData.observeState(this) {
onSuccess { data: List<WxArticleBean>? ->
}
onError {
}
}
既然是用Kotlin了,就不要用Java的形式写接口回掉了,DSL
表达式不香么?
提供两种形式实现:
- 形式一代码量更少,网络申请自带Loading,不须要手动调用Loading
- 形式二解耦更彻底
两种形式设计思路在解耦这一块存在差别,看具体需要,没有谁好谁差,按照本人的我的项目,哪个更不便用哪个。
基于官网架构的封装:
一、封装一
核心思想是:通过一个LiveData 贯通整个流程,借用网上一张图:
Activity中的代码示例
点击申请网络
mViewModel.getArticleData()
设置监听,只监听胜利的后果,应用默认异样解决
mViewModel.wxArticleLiveData.observeState(this) {
onSuccess { data ->
Log.i("wutao","网络申请的后果是:$data")
}
}
如果须要独自解决每一个回调
这些回调都是可选的,不须要可不实现
mViewModel.wxArticleLiveData.observeState(this) {
onSuccess { data ->
Log.i("wutao","网络申请的后果是:$data")
}
onEmpty{
Log.i("wutao", "返回的数据是空,展现空布局")
}
onFailed {
Log.i("wutao", "后盾返回的errorCode: $it")
}
onException { e ->
Log.i("wutao","这是非后盾返回的异样回调")
}
onShowLoading {
Log.i("wutao","自定义单个申请的Loading")
}
onComplete {
Log.i("wutao","网络申请完结")
}
}
申请自带Loading
很多网络申请都须要Loading
,不想每次都写onShowLoading{}
办法,也so easy。
mViewModel.wxArticleLoadingLiveData.observeState(this, this) {
onSuccess { data ->
Log.i("wutao","网络申请的后果是:$data")
}
}
observeState()
第二个办法传入ui的援用就可,这样单个网络申请之前会主动加载Loading,胜利或者失败主动勾销Loading。
下面代码都是Activity
中,咱们来看下ViewModel
中。
ViewModel中代码示例
class MainViewModel{
private val repository by lazy { WxArticleRepository() }
val wxArticleLiveData = StateLiveData<List<WxArticleBean>>()
fun requestNet() {
viewModelScope.launch {
repository.fetchWxArticle(wxArticleLiveData)
}
}
}
很简略,引入对应的数据仓库Repo,而后应用协程执行网络申请办法。来看下Repo
中的代码。
Repository
中代码示例
class WxArticleRepository : BaseRepository() {
private val mService by lazy { RetrofitClient.service }
suspend fun fetchWxArticle(stateLiveData: StateLiveData<List<WxArticleBean>>) {
executeResp(stateLiveData, mService::getWxArticle)
}
}
interface ApiService {
@GET("wxarticle/chapters/json")
suspend fun getWxArticle(): BaseResponse<List<WxArticleBean>>
}
获取一个Retrofit
实例,而后调用ApiService
接口办法。
封装一的劣势
- 代码很简洁,不须要手写线程切换代码,没有很多的接口回调。
- 自带Loading状态,不须要手动启用Loading和敞开Loading。
- 数据驱动ui,以LiveData为载体,将页面状态和网络后果通过在LiveData返回给ui。
我的项目地址见:
https://github.com/ldlywt/Fas… (分支名字是:withLoading)
封装一的有余
*封装一的核心思想是:一个LiveData贯通整个网络申请链。这是它的劣势,也是它的劣势。
- 解耦不彻底,违反了”在利用的各个模块之间设定明确定义的职责界线”的思维
LiveData
监听时,如果须要Loading
,BaseActivity
都须要实现带有Loading办法接口。obserState()
办法第二个参数中传入了UI援用。- 不能达到”看办法如其意”,如果是刚接触,会有很多疑难:为什么须要一个livedata作为办法的参数。网络申请的返回值去哪了?
- 封装一还有一个最大的缺点:对于是多数据源,封装一就展现了很不敌对的一面。
Repository是做一个数据仓库,我的项目中获取数据的形式都在这里批准治理,网络获取数据只是其中一个形式而已。
如果想加一个从数据库或者缓存中获取数据,封装一想改都不好改,如果强制改就毁坏了封装,侵入性很大。
针对封装一的有余,优化出了封装二。
二、封装二
思路
- 想要解决下面的有余,不能以LiveData为载体贯通整个网络申请。
Observe()
办法中去掉ui援用,不要小看一个ui援用,这个援用代表着具体的Activity跟Observe耦合起来了,并且Activity还要实现IUiView接口。- 网络申请跟Loading状态离开了,须要手动管制Loading。
Repository
中的办法都有返回值,会返回后果,也不须要用livedata作为办法参数。LiveData
只存在于ViewModel
中,LiveData
不会贯通整个申请链。Repository中也不须要LiveData的援用,Repository
的代码就是单纯的获取数据。- 针对多数据源,也十分好解决。
- 跟ui没任何关系,能够齐全作为一个独立的Lib应用。
Activity中代码
// 申请网络
mViewModel.login("username", "password")
// 注册监听
mViewModel.userLiveData.observeState(this) {
onSuccess {data ->
mBinding.tvContent.text = data.toString()
}
onComplete {
dismissLoading()
}
}
observeState()
中不再须要一个ui援用了。
ViewModel
中
class MainViewModel {
val userLiveData = StateLiveData<User?>()
fun login(username: String, password: String) {
viewModelScope.launch {
userLiveData.value = repository.login(username, password)
}
}
}
通过livedata的setValue
或者postValue
办法将数据发送进来。
Repository中
suspend fun login(username: String, password: String): ApiResponse<User?> {
return executeHttp {
mService.login(username, password)
}
}
Repository
中的办法都返回申请后果,并且办法参数不须要livedata。Repository
齐全能够独立进去了。
针对多数据源
// WxArticleRepository
class WxArticleRepository : BaseRepository() {
private val mService by lazy {
RetrofitClient.service
}
suspend fun fetchWxArticleFromNet(): ApiResponse<List<WxArticleBean>> {
return executeHttp {
mService.getWxArticle()
}
}
suspend fun fetchWxArticleFromDb(): ApiResponse<List<WxArticleBean>> {
return getWxArticleFromDatabase()
}
}
// MainViewModel.kt
private val dbLiveData = StateLiveData<List<WxArticleBean>>()
private val apiLiveData = StateLiveData<List<WxArticleBean>>()
val mediatorLiveDataLiveData = MediatorLiveData<ApiResponse<List<WxArticleBean>>>().apply {
this.addSource(apiLiveData) {
this.value = it
}
this.addSource(dbLiveData) {
this.value = it
}
}
能够看到,封装二更合乎职责繁多准则,Repository
单纯的获取数据,ViewModel
对数据进行解决和发送。
三、实现原理
数据来源于鸿洋大神的玩Android 凋谢API
回数据结构定义:
{
"data": ...,
"errorCode": 0,
"errorMsg": ""
}
封装一和封装二的代码差距很小,次要看封装二。
定义数据返回类
open class ApiResponse<T>(
open val data: T? = null,
open val errorCode: Int? = null,
open val errorMsg: String? = null,
open val error: Throwable? = null,
) : Serializable {
val isSuccess: Boolean
get() = errorCode == 0
}
data class ApiSuccessResponse<T>(val response: T) : ApiResponse<T>(data = response)
class ApiEmptyResponse<T> : ApiResponse<T>()
data class ApiFailedResponse<T>(override val errorCode: Int?, override val errorMsg: String?) : ApiResponse<T>(errorCode = errorCode, errorMsg = errorMsg)
data class ApiErrorResponse<T>(val throwable: Throwable) : ApiResponse<T>(error = throwable)
基于后盾返回的基类,依据不同的后果,定义不同的状态数据类。
网络申请对立解决:BaseRepository
open class BaseRepository {
suspend fun <T> executeHttp(block: suspend () -> ApiResponse<T>): ApiResponse<T> {
runCatching {
block.invoke()
}.onSuccess { data: ApiResponse<T> ->
return handleHttpOk(data)
}.onFailure { e ->
return handleHttpError(e)
}
return ApiEmptyResponse()
}
/**
* 非后盾返回谬误,捕捉到的异样
*/
private fun <T> handleHttpError(e: Throwable): ApiErrorResponse<T> {
if (BuildConfig.DEBUG) e.printStackTrace()
handlingExceptions(e)
return ApiErrorResponse(e)
}
/**
* 返回200,然而还要判断isSuccess
*/
private fun <T> handleHttpOk(data: ApiResponse<T>): ApiResponse<T> {
return if (data.isSuccess) {
getHttpSuccessResponse(data)
} else {
handlingApiExceptions(data.errorCode, data.errorMsg)
ApiFailedResponse(data.errorCode, data.errorMsg)
}
}
/**
* 胜利和数据为空的解决
*/
private fun <T> getHttpSuccessResponse(response: ApiResponse<T>): ApiResponse<T> {
return if (response.data == null || response.data is List<*> && (response.data as List<*>).isEmpty()) {
ApiEmptyResponse()
} else {
ApiSuccessResponse(response.data!!)
}
}
}
Retrofit协程的错误码解决是通过异样抛出来的,所以通过try…catch来捕获非200的错误码。包装成不同的数据类对象返回。
扩大LiveData和Observer
在LiveData的Observer()
来判断是哪种数据类,进行相应的回调解决:
abstract class IStateObserver<T> : Observer<ApiResponse<T>> {
override fun onChanged(apiResponse: ApiResponse<T>) {
when (apiResponse) {
is ApiSuccessResponse -> onSuccess(apiResponse.response)
is ApiEmptyResponse -> onDataEmpty()
is ApiFailedResponse -> onFailed(apiResponse.errorCode, apiResponse.errorMsg)
is ApiErrorResponse -> onError(apiResponse.throwable)
}
onComplete()
}
再扩大LiveData
,通过kotlin的DSL表达式替换java的callback
回调,简写代码。
class StateLiveData<T> : MutableLiveData<ApiResponse<T>>() {
fun observeState(owner: LifecycleOwner, listenerBuilder: ListenerBuilder.() -> Unit) {
val listener = ListenerBuilder().also(listenerBuilder)
val value = object : IStateObserver<T>() {
override fun onSuccess(data: T) {
listener.mSuccessListenerAction?.invoke(data)
}
override fun onError(e: Throwable) {
listener.mErrorListenerAction?.invoke(e) ?: toast("Http Error")
}
override fun onDataEmpty() {
listener.mEmptyListenerAction?.invoke()
}
override fun onComplete() {
listener.mCompleteListenerAction?.invoke()
}
override fun onFailed(errorCode: Int?, errorMsg: String?) {
listener.mFailedListenerAction?.invoke(errorCode, errorMsg)
}
}
super.observe(owner, value)
}
}
四、总结
封装一
:代码量更少,能够依据我的项目须要封装一些具体的ui相干,开发起来更疾速,用起来更爽。
封装二
:解耦更彻底,能够独立于ui模块运行。
集体认为,框架设计次要还是服务于本人的我的项目需要(开源我的项目除外),合乎设计模式和设计准则更好,然而不满足也没关系,适宜本人我的项目需要,能节俭本人的工夫,就是好的。
咱们本人我的项目中应用,怎么轻便,怎么疾速,怎么写的爽就怎么来。
原文链接:https://juejin.cn/post/699329…
文末
您的点赞珍藏就是对我最大的激励!
欢送关注我,分享Android干货,交换Android技术。
对文章有何见解,或者有何技术问题,欢送在评论区一起留言探讨!
发表回复