Files
assistant-android/app/src/main/java/com/gh/gamecenter/toolbox/ToolBoxViewModel.kt

116 lines
4.9 KiB
Kotlin

package com.gh.gamecenter.toolbox
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import com.gh.common.constant.Constants
import com.gh.common.util.*
import com.gh.gamecenter.baselist.LoadStatus
import com.gh.gamecenter.entity.ToolBoxBlockEntity
import com.gh.gamecenter.entity.ToolBoxEntity
import com.gh.gamecenter.retrofit.Response
import com.gh.gamecenter.retrofit.RetrofitManager
import com.google.gson.Gson
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import retrofit2.HttpException
class ToolBoxViewModel(application: Application) : AndroidViewModel(application) {
private var mPage = 1
var mSearchKey = "" //搜索关键字
var mReset = false
val toolBoxBlockList = MutableLiveData<List<ToolBoxBlockEntity>>()
val searchResultList = MutableLiveData<List<ToolBoxEntity>>()
val loadStatus = MutableLiveData<LoadStatus>()
val searchStatus = MutableLiveData<LoadStatus>()
private val resultList = mutableListOf<ToolBoxEntity>()
fun getToolBoxList() {
RetrofitManager.getInstance().newApi.categoryToolKits
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(object : Response<List<ToolBoxBlockEntity>>() {
override fun onResponse(response: List<ToolBoxBlockEntity>?) {
super.onResponse(response)
response?.let {
if (SPUtils.getString(Constants.TOOLBOX_HISTORY).isNotEmpty()) {
toolBoxBlockList.postValue(getNewList(it))
} else {
toolBoxBlockList.postValue(it)
}
}
loadStatus.postValue(if (response.isNullOrEmpty()) LoadStatus.INIT_EMPTY else LoadStatus.INIT_LOADED)
}
override fun onFailure(e: HttpException?) {
super.onFailure(e)
loadStatus.postValue(LoadStatus.INIT_FAILED)
}
})
}
// 添加最近使用列表
fun getNewList(list: List<ToolBoxBlockEntity>): List<ToolBoxBlockEntity> {
val historyList = listOf(*Gson().fromJson(SPUtils.getString(Constants.TOOLBOX_HISTORY), Array<ToolBoxEntity>::class.java))
val validList = mutableListOf<ToolBoxEntity>().apply {
// 最多展示30天以内的打开记录
for (item in historyList) {
val offsetDay = TimeUtils.getBeforeDays(item.lastOpenTime / 1000)
if (offsetDay <= 30) {
add(item)
}
}
}
SPUtils.setString(Constants.TOOLBOX_HISTORY, validList.take(4).toJson())
return if (validList.isNotEmpty()) arrayListOf(ToolBoxBlockEntity(categoryName = "最近使用", toolboxList = validList)).apply {
addAll(list)
} else list
}
fun getSearchResultList(reset: Boolean = false) {
mReset = reset
if (reset) {
mPage = 1
resultList.clear()
}
RetrofitManager.getInstance().api.getToolKitData(mPage, UrlFilterUtils.getFilterQuery("keyword", mSearchKey))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(object : Response<List<ToolBoxEntity>>() {
override fun onResponse(response: List<ToolBoxEntity>?) {
super.onResponse(response)
response?.let {
if (mPage != 1 && (response.isEmpty() || response.size < PAGE_SIZE)) {
searchStatus.postValue(LoadStatus.LIST_OVER)
resultList.addAll(response)
} else if (mPage == 1 && response.isEmpty()) {
searchStatus.postValue(LoadStatus.INIT_EMPTY)
resultList.clear()
} else if (mPage == 1 && response.size < PAGE_SIZE) {
searchStatus.postValue(LoadStatus.INIT_OVER)
resultList.clear()
resultList.addAll(response)
} else {
mPage++
searchStatus.postValue(if (mPage == 1) LoadStatus.INIT_LOADED else LoadStatus.LIST_LOADED)
if (mPage == 1) resultList.clear()
resultList.addAll(response)
}
searchResultList.postValue(resultList)
}
}
override fun onFailure(e: HttpException?) {
super.onFailure(e)
searchStatus.postValue(if (mPage == 1) LoadStatus.INIT_FAILED else LoadStatus.LIST_FAILED)
}
})
}
companion object {
const val PAGE_SIZE = 20
}
}