puzhibing
2026-08-21 5fa94b3718b4a78a4fcea4c97652b0ef20b7de3a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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<CheckInMissionPointInfo>())
    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<CruiseCheckInOperationInfo>()
 
    //记录是否弹窗显示打卡
    val showCheckInNoticeHistory = mutableListOf<String>()
 
    //选中点
    val choicePoint = MutableLiveData<CheckInMissionPointInfo>()
 
    //获取路径规划终点
    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<NaviPoi> {
        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)
        }
    }
}