Forráskód Böngészése

@task: record upload result log

bai 3 éve
szülő
commit
4e04b64574

+ 5 - 1
demo/src/main/java/com/framework/mvvm/model/repository/AdmRepository.kt

@@ -425,7 +425,8 @@ class AdmRepository(
         userId: String,
         groupCode: String,
         projectId: String,
-        buildingId: String
+        buildingId: String,
+        log: ((String, Building?) -> Unit)? = null
     ) {
         try {
             val pipeDao = db.pipeDao()
@@ -507,6 +508,7 @@ class AdmRepository(
                 }
                 is FailureResponse -> {
                     Log.e("AdmViewModel", "uploadBuilding fail:${response.msg}")
+                    log?.invoke("uploadBuilding FailureResponse: ${response.msg}", null)
                 }
                 is SuccessResponse -> {
                     val result = response.data
@@ -547,10 +549,12 @@ class AdmRepository(
                         it.state = 2
                         serveAreaDao.insertArea(it)
                     }
+                    log?.invoke("uploadBuilding SuccessResponse", building)
                 }
             }
         } catch (e: Exception) {
             println("uploadBuilding Exception: $e")
+            log?.invoke("uploadBuilding Exception: ${e.message}", null)
         }
     }
 

+ 8 - 2
demo/src/main/java/com/framework/mvvm/mv/AdmViewModel.kt

@@ -20,6 +20,7 @@ import com.framework.mvvm.model.db.entity.UserEntity
 import com.framework.mvvm.model.db.entity.task.ObjectEntity
 import com.framework.mvvm.model.repository.AdmRepository
 import com.framework.mvvm.model.vo.AdmRequest
+import com.framework.mvvm.model.vo.Building
 import com.framework.mvvm.model.vo.Client
 import com.framework.mvvm.model.vo.Login
 import com.sybotan.android.demo.DemoApp
@@ -70,7 +71,11 @@ class AdmViewModel(private val repository: AdmRepository) : ViewModel() {
     /**
      * 上传建筑数据
      */
-    fun uploadBuilding(request: AdmRequest, block: (() -> Unit)? = null) {
+    fun uploadBuilding(
+        request: AdmRequest,
+        block: (() -> Unit)? = null,
+        log: ((String, Building?) -> Unit)? = null
+    ) {
         viewModelScope.launch(Dispatchers.IO) {
             //val buildings = repository.getBuildings()
             val buildings = repository.getBuildingByProjectId(request.projectId)
@@ -80,7 +85,8 @@ class AdmViewModel(private val repository: AdmRepository) : ViewModel() {
                     userId = request.userId,
                     groupCode = building.groupCode!!,
                     projectId = building.projectId!!,
-                    buildingId = building.id
+                    buildingId = building.id,
+                    log = log
                 )
             }
             withContext(Dispatchers.Main) {

+ 49 - 19
demo/src/main/java/com/sybotan/android/demo/activities/PocActivity.kt

@@ -15,7 +15,9 @@ import androidx.constraintlayout.widget.ConstraintLayout
 import androidx.core.app.ActivityCompat
 import androidx.lifecycle.lifecycleScope
 import com.framework.mvvm.model.vo.AdmRequest
+import com.framework.mvvm.model.vo.Building
 import com.framework.mvvm.mv.AdmViewModel
+import com.google.gson.Gson
 import com.sybotan.android.demo.R
 import com.sybotan.android.demo.activities.poc.SceneActivity
 import com.sybotan.android.demo.activities.poc.SpaceActivity
@@ -32,6 +34,9 @@ import org.kodein.di.DIAware
 import org.kodein.di.android.closestDI
 import org.kodein.di.android.x.viewmodel.viewModel
 import org.kodein.di.instance
+import java.io.File
+import java.io.FileWriter
+import java.text.SimpleDateFormat
 
 class PocActivity : BaseActivity(), DIAware {
 
@@ -122,28 +127,53 @@ class PocActivity : BaseActivity(), DIAware {
             lifecycleScope.launch(Dispatchers.IO) {
                 LocalDataOperation.getInstance().offlineSendRequest.uploadImageNew(this@PocActivity)
             }
-            mVm.uploadBuilding(request) {
-                // ToastUtils.showMyToast("建筑数据上传成功")
-                val builder = AlertDialog.Builder(this)
-                val dialog = builder.setMessage("建筑数据上传成功")
-                    .setPositiveButton("确定") { dialog, id ->
-                        dialog.dismiss()
-                    }.create()
-                dialog.show()
+            mVm.uploadBuilding(
+                request, block = {
+                    // ToastUtils.showMyToast("建筑数据上传成功")
+                    val builder = AlertDialog.Builder(this)
+                    val dialog = builder.setMessage("建筑数据上传成功")
+                        .setPositiveButton("确定") { dialog, id ->
+                            dialog.dismiss()
+                        }.create()
+                    dialog.show()
+
+                    lifecycleScope.launch {
+                        val unUploadNum = mVm.getNoDoneTaskByProjectId(projectId)
+                        unUploadTv.text = "$unUploadNum"
+                        if (unUploadNum > 0) {
+                            unUploadLabelTv.setTextColor(Color.RED)
+                            unUploadTv.setTextColor(Color.RED)
+                        } else {
+                            unUploadLabelTv.setTextColor(resources.getColor(R.color.black_1F2429))
+                            unUploadTv.setTextColor(resources.getColor(R.color.black_1F2429))
+                        }
+                    }
 
-                lifecycleScope.launch {
-                    val unUploadNum = mVm.getNoDoneTaskByProjectId(projectId)
-                    unUploadTv.text = "$unUploadNum"
-                    if (unUploadNum > 0) {
-                        unUploadLabelTv.setTextColor(Color.RED)
-                        unUploadTv.setTextColor(Color.RED)
-                    } else {
-                        unUploadLabelTv.setTextColor(resources.getColor(R.color.black_1F2429))
-                        unUploadTv.setTextColor(resources.getColor(R.color.black_1F2429))
+                },
+                log = { msg: String, building: Building? ->
+                    val dir = getExternalFilesDir("log")
+                    if (dir == null || !dir.exists()) {
+                        dir?.mkdir()
                     }
-                }
 
-            }
+                    val writer = FileWriter(
+                        File(
+                            dir,
+                            "${
+                                SimpleDateFormat("YYYY年MM月dd日 HH:mm:ss").format(
+                                    System.currentTimeMillis()
+                                )
+                            }-${request.projectId}.${building?.buildingId ?: ""}.log"
+                        )
+                    )
+                    writer.use {
+                        it.append(msg)
+                        it.append("\n")
+                        val json = Gson().toJson(building)
+                        it.append(json)
+                    }
+                }
+            )
         }
 
         dictBtn.setOnClickListener {