最近閑來有空寫了個kotlin練手小項(xiàng)目擎值,正好鞏固一下剛學(xué)的kotlin知識影兽,由于是本人的第一個Kotlin項(xiàng)目蹂空,或許部分地方寫法有待完善,歡迎指出一起交流進(jìn)步第焰。
本項(xiàng)目用的是網(wǎng)上找的開眼api买优,由某個大神爬來的數(shù)據(jù),所以只適合用于學(xué)習(xí)挺举,項(xiàng)目采用了MVVM模式杀赢,主要用到了 ViewModel、 LiveData 兩個組件湘纵,如果未接觸過的話可以先到官網(wǎng)或網(wǎng)上的一些相關(guān)博客先了解一下葵陵,如果說對于 MVVM 模式還不是很了解的話也有必要去學(xué)習(xí)一下,這里稍微說一下該項(xiàng)目 MVVM 模式的結(jié)構(gòu)圖整個流程如上圖所示瞻佛,十分簡單脱篙,由 View 層發(fā)起數(shù)據(jù)請求,來到 ViewModel 交給 Model 去進(jìn)行網(wǎng)絡(luò)請求獲取數(shù)據(jù)伤柄,這里并沒涉及本地?cái)?shù)據(jù)绊困,所以 Model 層會相對簡單一些,獲取到網(wǎng)絡(luò)數(shù)據(jù)之后回到給 ViewModel 處理相應(yīng)業(yè)務(wù)邏輯(沒有就不處理)适刀,最后通過 LiveData 將數(shù)據(jù)或事件 Post 到 View 層進(jìn)行界面展示秤朗,View 用到了DataBinding 框架,如果沒有使用過的話需要先進(jìn)行學(xué)習(xí)一番笔喉。
附上三層基類代碼取视, Model:
abstract class BaseModel {
abstract fun getObservable(): Observable<out BaseBean>
protected fun getRestService(): RestService {
return RestCreator.getInstance().getRestService()
}
fun loadData() {
getObservable()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(object : Observer<BaseBean> {
override fun onComplete() {
}
override fun onSubscribe(d: Disposable) {
}
override fun onNext(baseBean: BaseBean) {
baseBean.responCode = 1
baseBean.responType = ""
iDatasListener?.getSuccess(baseBean)
}
override fun onError(e: Throwable) {
val baseBean = BaseBean()
baseBean.responCode = -1
baseBean.responType = ""
baseBean.errorMessage = e.message.toString()
iDatasListener?.getFaild(baseBean)
}
})
}
private var iDatasListener: IDatasListener? = null
fun setIDatasListener(iDatasListener: IDatasListener) {
this.iDatasListener = iDatasListener
}
}
該類邏輯主要是通過 Retrofit 獲取到網(wǎng)絡(luò)數(shù)據(jù)通過接口回調(diào)到 ViewModel 層。接著看 ViewModel 的基類代碼:
open class BaseViewModel : ViewModel(), IDatasListener {
var baseModel: BaseModel? = null
private val successLiveData = MutableLiveData<Any>()
private val errorLiveData = MutableLiveData<Any>()
fun getSuccessLiveData(): MutableLiveData<Any> = successLiveData
fun getErrorLiveData(): MutableLiveData<Any> = errorLiveData
open fun onSuccess(any: Any) {
successLiveData.value = any
}
open fun onFail(any: Any) {
errorLiveData.value = any
}
open fun loadData() {
baseModel?.let {
it.setIDatasListener(this)
it.loadData()
}
}
override fun getSuccess(baseBean: BaseBean) {
onSuccess(baseBean)
}
override fun getFaild(baseBean: BaseBean) {
onFail(baseBean)
}
}
該類主要做兩件事常挚,1.讓 Model 獲取數(shù)據(jù)作谭, 2.獲取到 Model 返回的數(shù)據(jù)之后通過 LiveData Post給 View 層。最后看一下 View 的基類實(shí)現(xiàn):
open class BaseFragment : Fragment(), IBaseView {
override fun <T : BaseViewModel> createViewModel(viewModelClass: Class<T>): T {
return ViewModelProviders.of(this).get(viewModelClass)
}
override fun registerViewModelObserver(baseViewModel: BaseViewModel) {
baseViewModel.getSuccessLiveData().observe(this, Observer<Any> {
if (it != null) {
onApiSuccessCallBack(it)
}
})
baseViewModel.getErrorLiveData().observe(this, Observer<Any> {
if (it != null) {
onApiErrorCallBack(it)
}
})
}
override fun onApiSuccessCallBack(any: Any) {
}
override fun onApiErrorCallBack(any: Any) {
}
}
該類也主要做兩件事奄毡,1.創(chuàng)建 ViewModel 類折欠,2.觀察 LiveData 的數(shù)據(jù)變化進(jìn)而用于顯示界面。
嗶嗶了一會是時候給點(diǎn)該項(xiàng)目的幾個截圖了
界面比較簡單,畢竟是第一個練手項(xiàng)目锐秦,也沒想著做太復(fù)雜咪奖,最后附上項(xiàng)目地址:https://github.com/amazingokc/Eye-kotlin,歡迎下載一起學(xué)習(xí)酱床,如發(fā)現(xiàn)需要改進(jìn)的地方歡迎指正羊赵,晚安。