From b7bd461a2308e198eb04517a2ee8954c896fe6ee Mon Sep 17 00:00:00 2001
From: Denab905 <denab905@student.liu.se>
Date: Tue, 25 Feb 2025 10:27:08 +0100
Subject: [PATCH] Actiondata to clone

---
 .../com/example/aida/pages/SequenceTab.kt     | 21 ++++-----
 .../SequenceTabComposables/ActionsData.kt     | 44 +++++++++++++++++--
 .../SequenceTabComposables/SequenceBar.kt     |  3 +-
 3 files changed, 50 insertions(+), 18 deletions(-)

diff --git a/android/app/src/main/java/com/example/aida/pages/SequenceTab.kt b/android/app/src/main/java/com/example/aida/pages/SequenceTab.kt
index 0eb73a03..edd4f6b8 100644
--- a/android/app/src/main/java/com/example/aida/pages/SequenceTab.kt
+++ b/android/app/src/main/java/com/example/aida/pages/SequenceTab.kt
@@ -817,8 +817,8 @@ private fun addActionToSequenceBar(action: BaseAction, actionsInSequenceBar: Mut
         val loopStart = SpecialAction(id = 2, name = "LOOP START", isActive = mutableStateOf(true), isReorderable = true, data = mutableStateOf("1"), icon = mutableStateOf(Icons.Outlined.Repeat), cardColor = mutableStateOf(specialActionColor), duration = 0f)
         val loopEnd = SpecialAction(id = 2, name = "LOOP END", isActive = mutableStateOf(true), isReorderable = true, data = mutableStateOf(""), icon = mutableStateOf(Icons.Outlined.Repeat), cardColor = mutableStateOf(specialActionColor), duration = 0f)
 
-        val copiedLoopStart = loopStart.copy(data = mutableStateOf(loopStart.data.value),height = mutableStateOf(150.dp), width = mutableStateOf(160.dp), actionScale = mutableDoubleStateOf(1.0), icon = mutableStateOf(loopStart.icon.value))
-        val copiedLoopEnd = loopEnd.copy(data = mutableStateOf(loopEnd.data.value),  height = mutableStateOf(150.dp), width = mutableStateOf(160.dp), actionScale = mutableDoubleStateOf(1.0), icon = mutableStateOf(loopEnd.icon.value))
+        val copiedLoopStart = loopStart.clone()
+        val copiedLoopEnd = loopEnd.clone()
 
         actionsInSequenceBar.add(copiedLoopStart)
         actionsInSequenceBar.add(copiedLoopEnd)
@@ -827,14 +827,8 @@ private fun addActionToSequenceBar(action: BaseAction, actionsInSequenceBar: Mut
     {
         // For normal actions, create a copy with fresh mutable states so each action is independent
         val copiedAction = when (action) {
-            is MoveAction -> action.copy(data = mutableStateOf(action.data.value), height = mutableStateOf(150.dp), width = mutableStateOf(160.dp), actionScale = mutableDoubleStateOf(1.0), remainingDuration = mutableFloatStateOf(action.remainingDuration.value,))
-            is SpecialAction -> action.copy(
-                data = mutableStateOf(action.data.value),
-                cardColor = mutableStateOf(action.cardColor.value),
-                icon = mutableStateOf(action.icon.value),
-                height = mutableStateOf(150.dp),
-                width = mutableStateOf(160.dp)
-            )
+            is MoveAction -> action.clone()
+            is SpecialAction -> action.clone()
             else -> action
         }
 
@@ -890,10 +884,10 @@ private suspend fun executeActionWithCountdown(
     action.remainingDuration.value = action.duration
     action.width.value = 210.dp
     action.height.value = 200.dp
-    action.actionScale.value = 1.5
+    action.actionScale.value = 1.6
 
     // Start countdown in parallel with action execution
-    scope.launch {
+    val thred = scope.launch {
         while (action.remainingDuration.value > 0) {
             delay(100) // Update every 100ms
             action.remainingDuration.value = (action.remainingDuration.value - 0.1f)
@@ -908,7 +902,8 @@ private suspend fun executeActionWithCountdown(
     }
 
     // Wait for the full duration
-    delay((action.duration * 1000).toLong())
+    //delay((action.duration * 1000).toLong())
+    thred.join()
     action.width.value = 160.dp
     action.height.value = 150.dp
     action.actionScale.value = 1.0
diff --git a/android/app/src/main/java/com/example/aida/ui/composables/SequenceTabComposables/ActionsData.kt b/android/app/src/main/java/com/example/aida/ui/composables/SequenceTabComposables/ActionsData.kt
index 721e788d..a1bfb81c 100644
--- a/android/app/src/main/java/com/example/aida/ui/composables/SequenceTabComposables/ActionsData.kt
+++ b/android/app/src/main/java/com/example/aida/ui/composables/SequenceTabComposables/ActionsData.kt
@@ -28,7 +28,7 @@ val inActiveColor = Color(0xff8A8A8A)      // Color indicating an inactive state
  *      - `MoveAction` instances are active by default.
  *      - `SpecialAction` instances are inactive by default, except for the "Loop" action, which starts as active.
  */
-interface BaseAction {
+interface BaseAction: Cloneable {
     val id: Int                         // A unique identifier for each action
     val name: String                    // The name of the action
     var duration: Float // Expected duration of action
@@ -42,6 +42,7 @@ interface BaseAction {
     var width: MutableState<Dp>
     var actionScale: MutableState<Double>
 
+    public override fun clone(): BaseAction
     }
 /**
  * These data classes implement `BaseAction` and can define their own specific attributes.
@@ -59,9 +60,25 @@ data class MoveAction(
     override var height: MutableState<Dp> = mutableStateOf(150.dp),
     override var width: MutableState<Dp> = mutableStateOf(160.dp),
     override var actionScale: MutableState<Double> = mutableDoubleStateOf(1.0)
+) : BaseAction {
 
-
-) : BaseAction
+    override fun clone(): MoveAction {
+        return MoveAction(
+            id = this.id,
+            name = this.name,
+            isActive = mutableStateOf(this.isActive.value),
+            isReorderable = this.isReorderable,
+            duration = this.duration,
+            remainingDuration = mutableFloatStateOf(this.remainingDuration.value),
+            data = mutableStateOf(this.data.value),
+            icon = mutableStateOf(this.icon.value),
+            cardColor = mutableStateOf(this.cardColor.value),
+            height = mutableStateOf(this.height.value),
+            width = mutableStateOf(this.width.value),
+            actionScale = mutableDoubleStateOf(this.actionScale.value)
+        )
+    }
+}
 
 data class SpecialAction(
     override val id: Int,
@@ -77,7 +94,26 @@ data class SpecialAction(
     override var width: MutableState<Dp> = mutableStateOf(160.dp),
     override var actionScale: MutableState<Double> = mutableDoubleStateOf(1.0),
     val iconRotation: MutableState<Float> = mutableStateOf(0f)
-) : BaseAction
+) : BaseAction {
+
+    override fun clone(): SpecialAction {
+        return SpecialAction(
+            id = this.id,
+            name = this.name,
+            duration = this.duration,
+            remainingDuration = mutableFloatStateOf(this.remainingDuration.value),
+            isActive = mutableStateOf(this.isActive.value), 
+            isReorderable = this.isReorderable,
+            data = mutableStateOf(this.data.value), 
+            icon = mutableStateOf(this.icon.value), 
+            cardColor = mutableStateOf(this.cardColor.value), 
+            height = mutableStateOf(this.height.value), 
+            width = mutableStateOf(this.width.value),  
+            actionScale = mutableDoubleStateOf(this.actionScale.value),  
+            iconRotation = mutableStateOf(this.iconRotation.value) 
+        )
+    }
+}
 
 
 /**
diff --git a/android/app/src/main/java/com/example/aida/ui/composables/SequenceTabComposables/SequenceBar.kt b/android/app/src/main/java/com/example/aida/ui/composables/SequenceTabComposables/SequenceBar.kt
index a9e6fd4d..5c956b3e 100644
--- a/android/app/src/main/java/com/example/aida/ui/composables/SequenceTabComposables/SequenceBar.kt
+++ b/android/app/src/main/java/com/example/aida/ui/composables/SequenceTabComposables/SequenceBar.kt
@@ -6,6 +6,7 @@ import kotlinx.coroutines.launch
 import sh.calvin.reorderable.ReorderableRow
 import androidx.compose.animation.*
 import androidx.compose.animation.core.FastOutSlowInEasing
+import androidx.compose.animation.core.spring
 import androidx.compose.animation.core.tween
 import androidx.compose.foundation.Canvas
 import androidx.compose.foundation.ScrollState
@@ -269,7 +270,7 @@ fun SequenceBar(
                 // Each action is represented as a card:
                 Card(
                     modifier = Modifier
-                        .animateContentSize()
+                        .animateContentSize(animationSpec = spring(dampingRatio = 50f, stiffness = 0f))
                         .width(item.width.value)
                         .height(item.height.value)
                         .clip(RoundedCornerShape(12.dp))
-- 
GitLab