package com.xw.yunwei.ui.task.create
|
|
import android.app.Application
|
import android.view.View
|
import androidx.core.os.bundleOf
|
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.map
|
import com.amap.api.location.AMapLocation
|
import com.blankj.utilcode.util.GsonUtils
|
import com.xw.common.base.BaseViewModel
|
import com.xw.common.base.FileSelectBean
|
import com.xw.common.base.ImageEventListener
|
import com.xw.common.base.ImageSelectAdapter
|
import com.xw.common.utils.MMKVManager
|
import com.xw.common.utils.ToastUtilCommon
|
import com.xw.yunwei.R
|
import com.xw.yunwei.model.CacheAccidentModel
|
import com.xw.yunwei.model.CarModel
|
import com.xw.yunwei.model.Category
|
import com.xw.yunwei.model.FinishPage
|
import com.xw.yunwei.model.Item
|
import com.xw.yunwei.model.SwitchTabMain
|
import com.xw.yunwei.model.SwitchTabTask
|
import com.xw.yunwei.net.CommonRepository
|
import com.xw.yunwei.net.TaskRepository
|
import com.xw.yunwei.net.WorkRepository
|
import com.xw.yunwei.ui.MainActivity
|
import com.xw.yunwei.ui.task.selectCar.SelectCarActivity
|
import kotlinx.coroutines.delay
|
import okhttp3.MediaType.Companion.toMediaType
|
import okhttp3.MultipartBody
|
import okhttp3.RequestBody.Companion.asRequestBody
|
import org.greenrobot.eventbus.EventBus
|
import org.koin.core.component.inject
|
import java.io.File
|
import kotlin.collections.joinToString
|
import kotlin.collections.map
|
import kotlin.getValue
|
|
class CreateTaskVM(myApplication: Application) : BaseViewModel(myApplication) {
|
|
val workRepository: WorkRepository by inject()
|
val taskRepository: TaskRepository by inject()
|
val commonRepository: CommonRepository by inject()
|
|
var categories = mutableListOf<Category>()
|
|
var selectCar: MutableLiveData<CarModel> = MutableLiveData()
|
var location: MutableLiveData<AMapLocation?> = MutableLiveData(null)
|
val address = location.map {
|
it?.address ?: "定位中..."
|
}
|
// 0 = P0(最紧急), 1 = P1 , 2 = P2 , 3 = P3(最不紧急)
|
val taskLevel: MutableLiveData<Int> = MutableLiveData(3)
|
val taskDesc = MutableLiveData("")
|
|
var onClick = View.OnClickListener {
|
when (it.id) {
|
R.id.imgSelectCar -> {
|
// startActivityForResult(
|
// SelectCarActivity::class.java,
|
// 110,
|
// bundleOf("car" to selectCar.value)
|
// )
|
onBackPressed()
|
}
|
|
R.id.level0 -> taskLevel.value = 0
|
R.id.level1 -> taskLevel.value = 1
|
R.id.level2 -> taskLevel.value = 2
|
R.id.level3 -> taskLevel.value = 3
|
R.id.submit -> submit()
|
}
|
}
|
|
lateinit var adapter: ImageSelectAdapter
|
val maxSize = 6
|
|
fun submit() {
|
if (selectCar.value == null) {
|
ToastUtilCommon.showWarn("请选择车辆")
|
return
|
}
|
if (location.value == null) {
|
ToastUtilCommon.showWarn("未获取到定位")
|
return
|
}
|
if (getSelectType().isEmpty()) {
|
ToastUtilCommon.showWarn("请选择事故类型")
|
return
|
}
|
if (taskLevel.value !in arrayOf(0, 1, 2, 3)) {
|
ToastUtilCommon.showWarn("请选择任务等级")
|
return
|
}
|
if (taskDesc.value.isNullOrEmpty()) {
|
ToastUtilCommon.showWarn("请填写任务描述")
|
return
|
}
|
if (adapter.data.isEmpty()) {
|
ToastUtilCommon.showWarn("请上传相关照片")
|
return
|
}
|
|
showDialog("上传中...")
|
uploadFile(adapter.data) {
|
dismissDialog()
|
launchRequest("提交中") {
|
val map = mutableMapOf<String, Any?>()
|
map["id"] = selectCar.value?.id
|
map["taskDescription"] = taskDesc.value
|
map["taskImage"] = "${adapter.data.map { it.url }}"
|
map["taskTypeId"] = getSelectType()
|
map["taskUrgencyLevel"] = taskLevel.value
|
taskRepository.saveVehicleAccident(map)
|
ToastUtilCommon.showSuc("提交成功")
|
EventBus.getDefault().post(SwitchTabMain(1))
|
EventBus.getDefault().post(SwitchTabTask(0))
|
// delay(1000)
|
EventBus.getDefault().post(FinishPage())
|
finish()
|
}
|
}
|
|
}
|
|
private fun uploadFile(files: List<FileSelectBean>, succ: () -> Unit) {
|
val result = files.find { it.url.isEmpty() }
|
if (result == null) {
|
succ()
|
} else {
|
launchRequest(showLoading = false, error = { _, errMsg ->
|
ToastUtilCommon.showError(errMsg)
|
dismissDialog()
|
}) {
|
val file = File(result.localPath)
|
val fileRequestBody = file.asRequestBody("image/jpg".toMediaType())
|
val mBuilder = MultipartBody.Builder()
|
mBuilder.addFormDataPart("file", file.name, fileRequestBody)
|
mBuilder.setType(MultipartBody.FORM)
|
val data = commonRepository.uploadFile(mBuilder.build())
|
result.url = GsonUtils.toJson(data)
|
uploadFile(files, succ)
|
}
|
}
|
}
|
|
fun getTypeList(block: () -> Unit) {
|
if (categories.isNotEmpty()) {
|
block()
|
} else {
|
launchRequest {
|
taskRepository.getTypeList()?.forEachIndexed { index, type ->
|
val items = mutableListOf<Item>()
|
type.child?.forEach {
|
items.add(Item(it.id ?: 0L, it.errorName ?: ""))
|
}
|
categories.add(Category(index, type.errorName ?: "", items))
|
runOnMainThread { block() }
|
}
|
}
|
}
|
}
|
|
fun getSelectType(): String {
|
val items = mutableListOf<Item>()
|
categories.forEach { items.addAll(it.items) }
|
return items.filter { it.isSelected }.map { it.id }.joinToString()
|
}
|
|
/*获取数据填写情况*/
|
fun getDataWrite(): CacheAccidentModel? {
|
// 事故类型/任务等级/任务描述/相关照片 暂时定义为这几个填了就算填了事故
|
return if (getSelectType().isEmpty() && taskLevel.value !in arrayOf(0, 1, 2, 3)
|
&& taskDesc.value.isNullOrEmpty() && adapter.data.isEmpty()
|
) {
|
null
|
} else {
|
CacheAccidentModel(
|
selectCar.value,
|
location.value,
|
categories,
|
taskLevel.value!!,
|
taskDesc.value!!,
|
adapter.data
|
)
|
}
|
}
|
}
|