Преглед изворни кода

退出编辑模式后,舍弃修改内容,恢复原数据。

Ulric пре 5 дана
родитељ
комит
b508b0be4a

+ 2 - 2
app/src/main/java/com/ys/imageProcess/ui/work/WorkScreen.kt

@@ -97,7 +97,7 @@ fun WorkScreen(
                     }
 
                     LaunchedEffect(canvasSize, processedData) {
-                        viewModel.transformPoints()
+                        viewModel.useDefaultPaths()
                     }
 
                     val aniOffset by animateOffsetAsState(
@@ -141,7 +141,7 @@ fun WorkScreen(
                 ) { action ->
                     if (action == ViewAction.Back) {
                         if (imageState.editMode) {
-                            viewModel.handleActions(ViewAction.SwitchEdit)
+                            viewModel.exitEditing()
                         } else {
                             if (back != null) {
                                 back()

+ 25 - 13
app/src/main/java/com/ys/imageProcess/viewModel/WorkViewModel.kt

@@ -206,7 +206,7 @@ class WorkViewModel : ViewModel() {
         mapData.value = map
     }
 
-    fun transformPoints() {
+    fun useDefaultPaths() {
         val sizeValid = canvasSize.width * canvasSize.height > 1
         val hasData = processedData.value.bitmap != null
         Log.i(TAG, "size valid:$sizeValid, has data: $hasData")
@@ -306,6 +306,11 @@ class WorkViewModel : ViewModel() {
         updateVisibleArea()
     }
 
+    fun exitEditing() {
+        imageState.value = imageState.value.copy(editMode = false)
+        useDefaultPaths()
+    }
+
     fun onDrag(position: Offset) {
         // 开始拖动
         if (dragStartPos == null) {
@@ -334,18 +339,25 @@ class WorkViewModel : ViewModel() {
                 (position - offset.value - canvasSize.center) / scale
                     .value + canvasSize.center
 
-            if (editingPath == 0) {
-                val newPath = contourPath.value.toMutableList()
-                newPath[editingPoint] = newVertex
-                contourPath.value = newPath
-            } else if (editingPath == 1) {
-                val newPath = lengthPath.value.toMutableList()
-                newPath[editingPoint] = newVertex
-                lengthPath.value = newPath
-            } else if (editingPath == 2) {
-                val newPath = widthPath.value.toMutableList()
-                newPath[editingPoint] = newVertex
-                widthPath.value = newPath
+
+            when (editingPath) {
+                0 -> {
+                    val newPath = contourPath.value.toMutableList()
+                    newPath[editingPoint] = newVertex
+                    contourPath.value = newPath
+                }
+
+                1 -> {
+                    val newPath = lengthPath.value.toMutableList()
+                    newPath[editingPoint] = newVertex
+                    lengthPath.value = newPath
+                }
+
+                2 -> {
+                    val newPath = widthPath.value.toMutableList()
+                    newPath[editingPoint] = newVertex
+                    widthPath.value = newPath
+                }
             }
         } else {
             // 拖动图像