desc:提供loader的抽象实现

author:wangyimiao
master
yimiao 3 years ago
parent 128e879620
commit 49dc1dd810

@ -10,15 +10,10 @@ import retrofit2.http.Headers
import retrofit2.http.Path import retrofit2.http.Path
class CollectionLoader(interceptors: List<Interceptor>) : class CollectionLoader(interceptors: List<Interceptor>) :
BaseObserve<CollectionLoader.CollectionApi>() { BaseObserve<CollectionLoader.CollectionApi>(CollectionApi::class.java, interceptors) {
private val api: CollectionApi
init {
api = initService(CollectionApi::class.java, interceptors)
}
fun getCollections(pageName: Int, callBack: RequestCallBack<CollectionResponse>) { fun getCollections(pageName: Int, callBack: RequestCallBack<CollectionResponse>) {
observe(api.getCollections(pageName), callBack) observe(api?.getCollections(pageName), callBack)
} }
interface CollectionApi { interface CollectionApi {

@ -11,15 +11,10 @@ import retrofit2.http.Path
/** /**
* 网络请求样例 * 网络请求样例
*/ */
class InternetLoader : BaseObserve<InternetLoader.HomeApi>() { class InternetLoader : BaseObserve<InternetLoader.HomeApi>(HomeApi::class.java) {
var homeApi: HomeApi
init {
homeApi = initService(HomeApi::class.java)
}
fun getHomeArticles(pageName: Int, callBack: RequestCallBack<ArticlesResponse>) { fun getHomeArticles(pageName: Int, callBack: RequestCallBack<ArticlesResponse>) {
observe(homeApi.getHomeArticles(pageName), callBack) observe(api?.getHomeArticles(pageName), callBack)
} }
interface HomeApi { interface HomeApi {

@ -8,15 +8,10 @@ import okhttp3.Interceptor
import retrofit2.http.* import retrofit2.http.*
class LoginLoader(interceptors: List<Interceptor>) : class LoginLoader(interceptors: List<Interceptor>) :
BaseObserve<LoginLoader.LoginApi>() { BaseObserve<LoginLoader.LoginApi>(LoginApi::class.java, interceptors) {
private val api: LoginApi
init {
api = initService(LoginApi::class.java, interceptors)
}
fun login(userName: String, pwd: String, callBack: RequestCallBack<LoginResponse>) { fun login(userName: String, pwd: String, callBack: RequestCallBack<LoginResponse>) {
observe(api.login(userName, pwd), callBack) observe(api?.login(userName, pwd), callBack)
} }
interface LoginApi { interface LoginApi {

@ -2,6 +2,7 @@ package com.common.commonlib.net
import android.net.ParseException import android.net.ParseException
import android.util.Log import android.util.Log
import com.common.commonlib.log.Logger
import com.common.commonlib.net.callback.RequestCallBack import com.common.commonlib.net.callback.RequestCallBack
import com.google.gson.JsonParseException import com.google.gson.JsonParseException
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
@ -24,6 +25,16 @@ import javax.net.ssl.SSLHandshakeException
* @since 2021/7/28 * @since 2021/7/28
*/ */
open class BaseObserve<T> { open class BaseObserve<T> {
var api: T? = null
constructor(clazz: Class<T>) {
api = RequestManager.create(clazz)
}
constructor(clazz: Class<T>, interceptors: List<Interceptor>) {
api = RequestManager.create(clazz, interceptors)
}
/** /**
* 初始化服务类 * 初始化服务类
*/ */
@ -31,6 +42,9 @@ open class BaseObserve<T> {
return RequestManager.create(clazz) return RequestManager.create(clazz)
} }
/**
* 初始化服务类
*/
fun initService(clazz: Class<T>, interceptors: List<Interceptor>): T { fun initService(clazz: Class<T>, interceptors: List<Interceptor>): T {
return RequestManager.create(clazz, interceptors) return RequestManager.create(clazz, interceptors)
} }
@ -40,7 +54,11 @@ open class BaseObserve<T> {
* @param observable 可订阅 * @param observable 可订阅
* @param callBack 请求回调 * @param callBack 请求回调
*/ */
fun <R> observe(observable: Observable<R>, callBack: RequestCallBack<R>?) { fun <R> observe(observable: Observable<R>?, callBack: RequestCallBack<R>?) {
if (observable == null) {
Logger.e("BaseObserve", "observe observable is null")
return
}
observable observable
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io()) .unsubscribeOn(Schedulers.io())

Loading…
Cancel
Save