package com.xw.yunwei.ui.work.checkInMission.viewmodel import android.app.Application import androidx.lifecycle.MutableLiveData import androidx.lifecycle.viewModelScope import com.amap.api.maps.model.LatLng import com.amap.api.navi.model.AMapNaviLocation import com.amap.api.navi.model.NaviPoi import com.xw.common.base.BaseViewModel import com.xw.yunwei.model.CheckInMissionPointInfo import com.xw.yunwei.model.CruiseCheckInOperationInfo import com.xw.yunwei.net.WorkRepository import com.xw.yunwei.ui.work.checkInMission.CruiseCheckInOperationActivity import kotlinx.coroutines.delay import kotlinx.coroutines.launch import org.koin.core.component.inject class CruiseCheckInViewModel(application: Application) : BaseViewModel(application) { val workRepository: WorkRepository by inject() var id: String = "" var points = MutableLiveData(listOf()) var startPoint: NaviPoi? = null var type: Int = 0 //是否需要算路 var needCalculateRoute = MutableLiveData(false) //是否在播放语音 var hadPlayTTS = false; //是否全览 val isOverview = MutableLiveData(false) //是否展开 val hadExpand = MutableLiveData(false) //是否能够打卡 val couldCheckIn = MutableLiveData(false) //高德导航定位 var aMapNaviLocation: AMapNaviLocation? = null //操作打卡信息 val checkInOperationInfo = MutableLiveData() //记录是否弹窗显示打卡 val showCheckInNoticeHistory = mutableListOf() //选中点 val choicePoint = MutableLiveData() //获取路径规划终点 fun getEndPoint(): NaviPoi? { return points.value?.lastOrNull { !it.hadCheckIn() }?.let { NaviPoi( "终点", LatLng(it.lat?.toDoubleOrNull() ?: 0.0, it.lon?.toDoubleOrNull() ?: 0.0), "" ) }?.takeIf { it.coordinate.latitude != 0.0 && it.coordinate.longitude != 0.0 } } //获取路径规划途经点 fun getPassingPoints(): List { val endPoint = getEndPoint() return points.value?.filter { !it.hadCheckIn() }?.mapIndexed { index, info -> NaviPoi( "途经点${index + 1}", LatLng(info.lat?.toDoubleOrNull() ?: 0.0, info.lon?.toDoubleOrNull() ?: 0.0), "" ) }?.filter { it.coordinate.latitude != 0.0 && it.coordinate.longitude != 0.0 && it.coordinate != endPoint?.coordinate } ?: emptyList() } //获取下一个点 fun getNextPoint(): CheckInMissionPointInfo? { return points.value?.find { if (checkInOperationInfo.value?.needShowCheckInNotice() == true) { if (it.hadCheckIn()) { return@find false } it.locationId == checkInOperationInfo.value?.checkLocationId } else !it.hadCheckIn() } } //检测超时 fun checkOutTimeOut() { viewModelScope.launch { while (true) { delay(5000) aMapNaviLocation?.also { it -> launchRequest(showLoading = false, error = { _, _ -> }) { workRepository.uploadRealTimePosition( id, it.coord.latitude, it.coord.longitude ) ?.also { it -> points.value?.filter { point -> point.locationId == it.checkLocationId && !point.hadCheckIn() } ?.takeIf { it.isNotEmpty() } .also { _ -> checkInOperationInfo.postValue(it) } } } } } } } //跳转打卡页 fun jumpToCheckInPage(info: CheckInMissionPointInfo) { startActivity( CruiseCheckInOperationActivity::class.java, CruiseCheckInOperationActivity.getParams(id, info, info == points.value?.lastOrNull()) ) } //刷新点 fun refreshPoints(locationId: String?, checkIn: String?, checkInResult: String?) { points.value?.find { it.locationId == locationId }?.apply { this.checkIn = checkIn this.checkInResult = checkInResult } if (checkInOperationInfo.value?.checkLocationId == locationId) { checkInOperationInfo.postValue(CruiseCheckInOperationInfo()) } val endPoint = getEndPoint() if (endPoint == null) { finish() } else { points.postValue((points.value ?: mutableListOf()).toMutableList()) aMapNaviLocation?.also { startPoint = NaviPoi( "起点", LatLng(it.coord.latitude, it.coord.longitude), "" ) } needCalculateRoute.postValue(true) } } }