Files
assistant-android/app/src/main/java/com/gh/gamecenter/energy/TaskViewModel.kt

53 lines
1.8 KiB
Kotlin

package com.gh.gamecenter.energy
import android.app.Application
import com.gh.gamecenter.baselist.ListViewModel
import com.gh.gamecenter.entity.TaskEntity
import com.gh.gamecenter.retrofit.BiResponse
import com.gh.gamecenter.retrofit.RetrofitManager
import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
class TaskViewModel(application: Application)
: ListViewModel<TaskEntity, TaskItemData>(application) {
init {
setOverLimitSize(1000) // 该页面不需要分页
}
private val mApi = RetrofitManager.getInstance(getApplication()).api
override fun provideDataObservable(page: Int) = null
override fun provideDataSingle(page: Int): Single<MutableList<TaskEntity>> {
return mApi.getDailyTasks(page)
}
override fun mergeResultLiveData() {
mResultLiveData.addSource(mListLiveData) { list ->
val itemDataList = arrayListOf<TaskItemData>()
mApi.noviceTasks
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(object : BiResponse<List<TaskEntity>>() {
override fun onSuccess(data: List<TaskEntity>) {
if (data.isNotEmpty()) {
itemDataList.add(TaskItemData(title = "新手任务"))
itemDataList.add(TaskItemData(noviceTasks = data))
}
itemDataList.add(TaskItemData(title = "日常任务"))
list.forEach {
itemDataList.add(TaskItemData(dailyTask = it))
}
mResultLiveData.postValue(itemDataList)
}
})
}
}
}