Retrofit完美适配LiveData,使用Kotlin和Android JetPack开发一款App

项目简介

电影资讯App是一个纯练手项目,使用了系统API28,所有是基于Androidx包下的。使用Kotlin语言开发,使用了Android JetPack中的LiveData、ViewModel以及Room。为了学习Kotlin和巩固自定义View和ViewGroup,这个项目是尽可能少应用依赖第三方库。Api来自豆瓣电影,豆瓣电影Api目前是不公开的,小编是在简书上看到有人公开了访问豆瓣电影的Api的API_KEY,若是有侵犯,请联系删除!java

项目截图

截图1 截图2 截图 3 截图 4 截图5
5.jpg
3.jpg
1.jpg
4.jpg
6.jpg

app主要设计到知识点:

  • Material Design UI设计风格;
  • 使用Kotlin开发,你们想学习和巩固Kotlin,能够参考这个App;
  • Retrofit适配LiveData手写一个LiveDataCallAdapter适配器;
  • RecyclerView支持上拉刷新和下拉加载,而且能够添加不一样类型头部和底部的View以及动画;
  • 支持视频播放

看几段代码,访问网络的核心代码,设计到了蛮多的知识点,这是初始化Retrofitgit

class RetrofitClient {
    companion object {
        val serviceApi: ServiceApi by lazy {
            val retrofitClient = Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .client(OkHttpClient.Builder()
                            .addInterceptor(HttpLoggingInterceptor(HttpLoggingInterceptor.Logger { message ->
                                Log.i(TAG, message)
                            }).setLevel(HttpLoggingInterceptor.Level.BODY)
                            ).build())
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(LiveDataCallAdapterFactory())
                    .build()
            retrofitClient.create(ServiceApi::class.java)
        }
    }
}
复制代码

注意这段代码,经过工厂模式添加一个LiveData数据源适配器github

.addCallAdapterFactory(LiveDataCallAdapterFactory())
复制代码

经过方法工厂返回LiveDataCallAdapter的实例,而且获取泛型的参数类型网络

class LiveDataCallAdapterFactory : Factory() {
    override fun get(returnType: Type, annotations: Array<Annotation>, retrofit: Retrofit): CallAdapter<*, *>? {
        val responseType: Type

        if (Factory.getRawType(returnType) != LiveData::class.java) {
            throw IllegalStateException("return type must be parameterized")
        }
        val observableType = Factory.getParameterUpperBound(0, returnType as ParameterizedType)
        val rawObservableType = Factory.getRawType(observableType)
        responseType = if (rawObservableType == Response::class.java) {
            if (observableType !is ParameterizedType) {
                throw IllegalArgumentException("Response must be parameterized")
            }
            Factory.getParameterUpperBound(0, observableType)
        } else {
            observableType
        }
        return LiveDataCallAdapter<Any>(responseType)
    }
}
复制代码

注意adapt()方法中的代码,经过适配器模式将Call转为LiveDataapp

class LiveDataCallAdapter<R>(private val responseType: Type) :
        CallAdapter<R, LiveData<R>> {
​
    override fun responseType() = responseType
​
    override fun adapt(call: Call<R>): LiveData<R> {
        return object : LiveData<R>() {
            private var started = AtomicBoolean(false)
            override fun onActive() {
                super.onActive()
                if (started.compareAndSet(false, true)) {
                    call.enqueue(object : Callback<R> {
                        override fun onResponse(call: Call<R>, response: Response<R>) {
                            postValue(response.body())
                        }
​
                        override fun onFailure(call: Call<R>, throwable: Throwable) {
                            postValue(null)
                        }
                    })
                }
            }
        }
    }
}
复制代码

你们可能看这些代码会比较懵,我是看了RxJava2中的RxJava2CallAdapterFactory中的代码,之前只是会写,可是点击去看源码,在分析就不同了。Observable在这至关于LiveData(被观察者)。经过调用retrofitClient.create(ServiceApi::class.java),最终会调用到adapt()方法中。ide

github传送门post

相关文章
相关标签/搜索