package com.xw.yunwei.ui.work.checkInMission.viewmodel
|
|
import android.app.Application
|
import android.content.Context
|
import android.view.View
|
import androidx.lifecycle.MutableLiveData
|
import com.xw.common.base.BasePagingViewModel
|
import com.xw.common.utils.ToastUtilCommon
|
import com.xw.yunwei.R
|
import com.xw.yunwei.model.CheckInMissionPointInfo
|
import com.xw.yunwei.net.WorkRepository
|
import com.xw.yunwei.ui.dialog.CruiseCheckInSuccessDialog
|
import com.xw.yunwei.ui.work.checkInMission.RoutePlanningActivity
|
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.withContext
|
import org.koin.core.component.inject
|
|
class CheckInMissionPointsListViewModel(application: Application) :
|
BasePagingViewModel(application) {
|
val workRepository: WorkRepository by inject()
|
var id: String = ""
|
|
//打卡点列表
|
val pointsList = MutableLiveData<List<CheckInMissionPointInfo>>()
|
|
var onClick = View.OnClickListener {
|
when (it.id) {
|
R.id.btn_plan -> routePlanning()
|
}
|
}
|
|
override fun getDataList(index: Int) {
|
launchRequest(showLoading = false, error = { code, msg ->
|
ToastUtilCommon.showError(msg)
|
refreshLiveData.done()
|
}) {
|
workRepository.getCheckInMissionPointsList(id)?.also {
|
pointsList.postValue(it)
|
refreshLiveData.first()
|
}
|
}
|
}
|
|
//路线规划
|
fun routePlanning(position: Int = -1) {
|
if (pointsList.value.isNullOrEmpty()) {
|
return
|
}
|
if (position in 0..<(pointsList.value?.size ?: 0)) {
|
if (pointsList.value!![position].hadCheckIn()) {
|
return
|
}
|
val list =
|
pointsList.value!!.filterIndexed { index, info -> index <= position && !info.hadCheckIn() }
|
startActivity(
|
RoutePlanningActivity::class.java,
|
RoutePlanningActivity.getParams(id, list)
|
)
|
} else {
|
val list = pointsList.value!!.filter { !it.hadCheckIn() }
|
startActivity(
|
RoutePlanningActivity::class.java,
|
RoutePlanningActivity.getParams(id, list)
|
)
|
}
|
}
|
|
//显示打卡成功弹窗
|
fun showCheckInSuccessDialog(context: Context, info: CheckInMissionPointInfo) {
|
launchRequest {
|
workRepository.getCruiseCheckInRecordInfo(id, info.locationId!!)?.also {
|
withContext(Dispatchers.Main) {
|
CruiseCheckInSuccessDialog.show(context, it, needNext = false)
|
}
|
}
|
}
|
}
|
}
|