|
@@ -1,35 +1,30 @@
|
|
|
package com.framework.mvvm.mv
|
|
|
|
|
|
-import androidx.lifecycle.*
|
|
|
+import android.util.Log
|
|
|
+import androidx.lifecycle.ViewModel
|
|
|
+import androidx.lifecycle.viewModelScope
|
|
|
import cn.sagacloud.cadengine.OkhttpUtil
|
|
|
import com.framework.app.pathDownloadMap
|
|
|
-import com.framework.app.testIP
|
|
|
-import com.framework.mvvm.model.data.AbsentLiveData
|
|
|
-import com.framework.mvvm.model.data.Data
|
|
|
-import com.framework.mvvm.model.db.entity.ProjectEntity
|
|
|
+import com.framework.app.tencentIP
|
|
|
+import com.framework.mvvm.model.data.EmptyResponse
|
|
|
+import com.framework.mvvm.model.data.FailureResponse
|
|
|
+import com.framework.mvvm.model.data.SuccessResponse
|
|
|
+import com.framework.mvvm.model.db.entity.ObjectEntity
|
|
|
import com.framework.mvvm.model.repository.AdmRepository
|
|
|
import com.framework.mvvm.model.vo.AdmRequest
|
|
|
import com.framework.mvvm.model.vo.FloorInfo
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
+import kotlinx.coroutines.flow.SharingStarted.Companion.WhileSubscribed
|
|
|
+import kotlinx.coroutines.flow.StateFlow
|
|
|
+import kotlinx.coroutines.flow.flow
|
|
|
+import kotlinx.coroutines.flow.stateIn
|
|
|
import kotlinx.coroutines.launch
|
|
|
import kotlinx.serialization.decodeFromString
|
|
|
import kotlinx.serialization.json.Json
|
|
|
|
|
|
|
|
|
class AdmViewModel(private val repository: AdmRepository) : ViewModel() {
|
|
|
- private val _frameRequest = MutableLiveData<AdmRequest>()
|
|
|
-
|
|
|
- private var _projects = MutableLiveData<Data<List<ProjectEntity>>>()
|
|
|
-
|
|
|
- val projects: LiveData<Data<List<ProjectEntity>>> =
|
|
|
- Transformations.switchMap(_frameRequest) { request ->
|
|
|
- if (request == null) {
|
|
|
- AbsentLiveData.create()
|
|
|
- } else {
|
|
|
- repository.getProjects(request)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 请求架构数据
|
|
|
*/
|
|
@@ -39,9 +34,8 @@ class AdmViewModel(private val repository: AdmRepository) : ViewModel() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
- * 请求字典
|
|
|
+ * 请求字典数据
|
|
|
*/
|
|
|
fun requestDict(request: AdmRequest) {
|
|
|
viewModelScope.launch {
|
|
@@ -50,7 +44,7 @@ class AdmViewModel(private val repository: AdmRepository) : ViewModel() {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 请求建筑
|
|
|
+ * 请求建筑数据
|
|
|
*/
|
|
|
fun requestBuilding(request: AdmRequest) {
|
|
|
viewModelScope.launch(Dispatchers.IO) {
|
|
@@ -58,19 +52,51 @@ class AdmViewModel(private val repository: AdmRepository) : ViewModel() {
|
|
|
val floors = repository.getFloors()
|
|
|
floors.forEach {
|
|
|
val strInfo = it.strInfos
|
|
|
- if (!strInfo.isNullOrEmpty()) {
|
|
|
+ if (!strInfo.isNullOrEmpty() && strInfo.contains("floorMap")) {
|
|
|
try {
|
|
|
val info = Json.decodeFromString<FloorInfo>(strInfo)
|
|
|
val map = info.floorMap
|
|
|
val floorMap =
|
|
|
- OkhttpUtil.getGzipMap("$testIP$pathDownloadMap${map!!}")
|
|
|
+ OkhttpUtil.getGzipMap("$tencentIP$pathDownloadMap${map!!}")
|
|
|
it.floorMap = floorMap
|
|
|
- repository.upObject(it)
|
|
|
+ repository.updateObject(it)
|
|
|
} catch (e: Exception) {
|
|
|
println("floor map Exception: $e")
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ println("floor map over !!!, floors: ${floors.size}")
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传建筑数据
|
|
|
+ */
|
|
|
+ fun uploadBuilding(buildingId: String) {
|
|
|
+ viewModelScope.launch(Dispatchers.IO) {
|
|
|
+ when (val response = repository.uploadBuilding(buildingId)) {
|
|
|
+ is EmptyResponse -> {
|
|
|
+ Log.e("AdmViewModel", "uploadBuilding empty")
|
|
|
+ }
|
|
|
+ is FailureResponse -> {
|
|
|
+ Log.e("AdmViewModel", "uploadBuilding fail:${response.msg}")
|
|
|
+ }
|
|
|
+ is SuccessResponse -> {
|
|
|
+ Log.e("AdmViewModel", "uploadBuilding success")
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取建筑数量
|
|
|
+ */
|
|
|
+ val buildings: StateFlow<List<ObjectEntity>> = flow {
|
|
|
+ emit(repository.getBuildings())
|
|
|
+ }.stateIn(
|
|
|
+ scope = viewModelScope,
|
|
|
+ started = WhileSubscribed(5000),
|
|
|
+ initialValue = arrayListOf()
|
|
|
+ )
|
|
|
}
|