package com.xw.yunwei.ui.work.notice
|
|
import android.app.Application
|
import androidx.lifecycle.MutableLiveData
|
import com.xw.common.base.BasePagingViewModel
|
import com.xw.common.utils.ToastUtilCommon
|
import com.xw.yunwei.model.NoticeModel
|
import com.xw.yunwei.net.UserRepository
|
import org.koin.core.component.inject
|
|
class NoticeVM(application: Application) : BasePagingViewModel(application) {
|
|
val userRepository: UserRepository by inject()
|
|
val dataList: MutableLiveData<List<NoticeModel>> = MutableLiveData()
|
|
override fun getDataList(index: Int) {
|
if (index == 1) {
|
refreshLiveData.refresh()
|
} else {
|
refreshLiveData.loading()
|
}
|
launchRequest(showLoading = false, error = { code, msg ->
|
ToastUtilCommon.showError(msg)
|
refreshLiveData.done()
|
}) {
|
val data = userRepository.getNoticeList(mapOf("pageNum" to index, "pageSize" to pageSize))
|
val list: MutableList<NoticeModel> = ArrayList()
|
if (index == 1) {
|
refreshLiveData.first()
|
} else {
|
refreshLiveData.other()
|
list.addAll(dataList.value ?: ArrayList())
|
}
|
for (item in data?.records ?: ArrayList()) {
|
list.add(item)
|
}
|
if ((data?.total ?: 0) <= list.size) {
|
refreshLiveData.done()
|
}
|
dataList.postValue(list)
|
pageNo = index
|
}
|
}
|
|
fun setRead(id: Long, block: () -> Unit) {
|
launchRequest {
|
userRepository.read(id)
|
runOnMainThread { block() }
|
}
|
}
|
|
}
|