浏览代码

@task: upload toast

bai 3 年之前
父节点
当前提交
ddf463e779

+ 10 - 3
demo/src/main/java/com/framework/mvvm/mv/AdmViewModel.kt

@@ -3,8 +3,8 @@ package com.framework.mvvm.mv
 import androidx.lifecycle.ViewModel
 import androidx.lifecycle.viewModelScope
 import cn.sagacloud.cadengine.OkhttpUtil
-import com.framework.app.pathDownloadMap
 import com.framework.app.IP
+import com.framework.app.pathDownloadMap
 import com.framework.mvvm.model.db.entity.ObjectEntity
 import com.framework.mvvm.model.repository.AdmRepository
 import com.framework.mvvm.model.vo.AdmRequest
@@ -16,6 +16,7 @@ import kotlinx.coroutines.flow.StateFlow
 import kotlinx.coroutines.flow.flow
 import kotlinx.coroutines.flow.stateIn
 import kotlinx.coroutines.launch
+import kotlinx.coroutines.withContext
 import kotlinx.serialization.decodeFromString
 import kotlinx.serialization.json.Json
 
@@ -70,9 +71,15 @@ class AdmViewModel(private val repository: AdmRepository) : ViewModel() {
     /**
      * 上传建筑数据
      */
-    fun uploadBuilding(projectId: String, buildingId: String) {
+    fun uploadBuilding(block: (() -> Unit)? = null) {
         viewModelScope.launch(Dispatchers.IO) {
-            repository.uploadBuilding(projectId, buildingId)
+            val buildings = repository.getBuildings()
+            for (building in buildings) {
+                repository.uploadBuilding(building.projectId, building.id)
+            }
+            withContext(Dispatchers.Main) {
+                block?.invoke()
+            }
         }
     }
 

+ 2 - 7
demo/src/main/java/com/sybotan/android/demo/activities/PocActivity.kt

@@ -69,13 +69,8 @@ class PocActivity : AppCompatActivity(), DIAware {
         }
 
         uploadBtn.setOnClickListener {
-            lifecycleScope.launch {
-                val buildings = mVm.getBuildings()
-                println("upload buildings: $buildings")
-                for (building in buildings) {
-                    mVm.uploadBuilding(building.projectId, building.id)
-                }
-                ToastUtils.showMyToast("建筑数据上传完成")
+            mVm.uploadBuilding {
+                ToastUtils.showMyToast("建筑数据上传成功")
             }
         }